superfell/json2apex

Multiple Empty Arrays Data Type Issue

sidm1983 opened this issue · 2 comments

Hi,

My team and I were investigating a JSON deserialisation issue when we realised that this JSON2Apex tool seemed to be generating incorrect data types for arrays.

Basically all properties that are empty arrays end up having the same data type as the first empty array in the JSON. Json2Apex did not always behave this way. This behaviour seems to have been introduced recently.

Here is a screenshot showcasing the issue:
image

Input:

{
    "foo": [],
    "bar": []
}

Actual Output:

//
// Generated by JSON2Apex http://json2apex.herokuapp.com/
//

public class Temp {

	public List<Foo> foo;
	public List<Foo> bar;

	public class Foo {
	}

	
	public static Temp parse(String json) {
		return (Temp) System.JSON.deserialize(json, Temp.class);
	}
}

Expected Output:

//
// Generated by JSON2Apex http://json2apex.herokuapp.com/
//

public class Temp {

	public List<Foo> foo;
	public List<Bar> bar;

	public class Foo {
	}
	
	public class Bar {
	}
	
	public static Temp parse(String json) {
		return (Temp) System.JSON.deserialize(json, Temp.class);
	}
}

I am not sure if this is specifically a Json2Apex issue, because we are seeing this issue when Salesforce deserializes JSON responses from any APIs we are hitting that have empty arrays in them. It looks like there may be an issue in the serialization / deserialization library being used?

Any thoughts or help on this would be greatly appreciated. Thank you!

As you can see from the commit history, there have been no recent changes in JSON2Apex. So i don't think this is a change in behavior. From a cursory glance at TypeFactory.java I'd say the behavior you see is the what I'd expect. Values with the same structure resolve to the same apex type. It has no idea if foo's [] is the same or different to bar's []. The better sample JSON you feed it, the better it can do generating code.

Yup totally understand. And I believe this maybe an issue with SF's deserialisation library as it used to deserialise that input to the expected output and then changed at some point to deserialising differently as per the actual output above.

All good. Thank you for your thoughts. Was just trying to get a bit more context if there was any from your side.

Thank you for your time and thoughts. Much appreciated! 👍🏽