question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

nulls silently dropped even when using "protostuff.runtime.allow_null_array_element"

See original GitHub issue

I don´t understand why these two produce different results.

static final class Foo
{
	Object o;
	Foo(Object o)
	{
		this.o = o;
	}
}

public static void main(String[] args)
{
	System.setProperty("protostuff.runtime.allow_null_array_element", "true");

	System.out.println("Serializing an ArrayList<String>:");
	roundTrip(true);
	System.out.println("\nSerializing a String[]:");
	roundTrip(false);
}

static void roundTrip(boolean bug)
{
	LinkedBuffer buffer = LinkedBuffer.allocate(512);

	Foo bef = bug ? new Foo(new ArrayList(Arrays.asList("1", null, "2"))) : new Foo(new String[]{"1", null, "2"});

	Schema<Foo> schema = RuntimeSchema.getSchema(Foo.class);

	byte[] bytes = ProtostuffIOUtil.toByteArray(bef, schema, buffer);
	buffer.clear();

	Foo aft = schema.newMessage();
	ProtostuffIOUtil.mergeFrom(bytes, aft, schema);

	if (bug) {
		System.out.println("Before: " + bef.o);
		System.out.println("After : " + aft.o);
		System.out.println("Set   : " + RuntimeEnv.ALLOW_NULL_ARRAY_ELEMENT);
	} else {
		System.out.println("Before: " + Arrays.toString((String[]) bef.o));
		System.out.println("After : " + Arrays.toString((String[]) aft.o));
		System.out.println("Set   : " + RuntimeEnv.ALLOW_NULL_ARRAY_ELEMENT);
	}
}

The output is:

Serializing an ArrayList<String>: Before: [1, null, 2] After : [1, 2] Set : true

Serializing a String[]: Before: [1, null, 2] After : [1, null, 2] Set : true

Notice how the null is silently dropped in the first output.

I have tried this with 1.6.2 and 1.7.2

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
mikaelgrevcommented, May 24, 2020

Tried both cases i posted above with 1.8.0-SNAPSHOT with the new flag and they both now work just as I expect them would. Thanks, and good job!

1reaction
dyucommented, May 24, 2020

Fixed in master. You can currently test with 1.8.0-SNAPSHOT Use: -Dprotostuff.runtime.preserve_null_elements=true It applies to both collections and arrays

Read more comments on GitHub >

github_iconTop Results From Across the Web

No results found

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found