You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm not sure if this has come up before, but the use of a HashSet to implement a list of unique items does not guarantee the order is preserved. Unfortunately I believe JS (and in turn JSON) assume the list order should be preserved.
I've run into a case where something I've defined as a unique list of strings has unpredictable order when I try to use the POJOs generated by jsonschema2pojo.
Can lead to unpredictable ordering in the street_address list. This is not ideal for cases where you might want to print a shipping label, or something else where the order of the street address lines matter.
Obviously, the schema could be modified to have "street_address_1", "street_address_2", etc., but that seems like a hack rather than an ideal solution.
Is there a reason to use a HashSet over a LinkedHashSet? I believe a LinkedHashSet will preserve insertion order and does not incur the same performance hit as a TreeSet.
Am I correct in thinking that a change to jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/rules/DefaultRule.java is enough to make this happen?
Forgive me if this has been asked a million times before.
Thanks!
The text was updated successfully, but these errors were encountered:
Thanks for raising this Chad, this is a very good find! I don't think a lot of people use arrays with uniqueItems as I'm surprised this hasn't been reported before now.
I'll definitely look to fix this in the next version (or feel free to modify DefaultRule.java yourself and submit a PR). I don't see any problem with LinkedHashSet and goal here is always (as far as possible) that it should be possible to roundtrip your json through these types and it shouldn't change. Losing the ordering of arrays is definitely a bug in that respect.
Hey there!
I'm not sure if this has come up before, but the use of a HashSet to implement a list of unique items does not guarantee the order is preserved. Unfortunately I believe JS (and in turn JSON) assume the list order should be preserved.
I've run into a case where something I've defined as a unique list of strings has unpredictable order when I try to use the POJOs generated by jsonschema2pojo.
For example, this schema:
Can lead to unpredictable ordering in the street_address list. This is not ideal for cases where you might want to print a shipping label, or something else where the order of the street address lines matter.
Obviously, the schema could be modified to have "street_address_1", "street_address_2", etc., but that seems like a hack rather than an ideal solution.
Is there a reason to use a HashSet over a LinkedHashSet? I believe a LinkedHashSet will preserve insertion order and does not incur the same performance hit as a TreeSet.
Am I correct in thinking that a change to jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/rules/DefaultRule.java is enough to make this happen?
Forgive me if this has been asked a million times before.
Thanks!
The text was updated successfully, but these errors were encountered: