Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow collections to be null by default #156

Closed
akushe opened this issue Mar 4, 2014 · 8 comments
Closed

Allow collections to be null by default #156

akushe opened this issue Mar 4, 2014 · 8 comments
Milestone

Comments

@akushe
Copy link

akushe commented Mar 4, 2014

Hi ,
I wanted to enforce this constraint. There is a list of Experiences which is not required but if present then there should be at least one object .I have configured it in the following fashion

"experiences" : {
            "description": "This list represents Experiences
            "type": "array",
            "minItems": 1,
            "items": {
                    { "$ref": "Experiences.json" }
            }

This adds a @SiZe validation constraint which fails even if you dont provide any experiences object.I want it to fail only if the experiences is empty.Let me know how to configure this

@joelittlejohn
Copy link
Owner

I think the problem here is that fields are initialized with an empty collections, so your generated field would look like:

@JsonProperty("experiences")
private List<Experience> experiences = new ArrayList<Experience>();

The default values are very useful when interacting with these types as there is no need to initialize every collection. There's no way to disable this as present but I guess we could add a configuration option that would allow you to leave these field uninitialized. If the field value was null @SiZe would not cause validation to fail (as nullvalues are considered valid).

Before doing this though I think you should consider carefully whether this validation rule is really worth adding. By leaving these field uninitialized you will force anyone using these types to a) initialize the collection manually before adding values b) do null checks every time they use any collection in the generated types.

Would it not be better to simply treat an empty "experiences" array the same as if none had been provided?

@akushe
Copy link
Author

akushe commented Mar 4, 2014

If we treat the empty arrays as the same as none then our Validator (JSR 303) complains that the array should at least have one Experience object which is right
So if we treat empty "experiences" array the same as None then how can we ensure that experiences is not printed when the object is serialized .JsonInclude.Include.NON_NULL only handles null values .
This is a genuine problem because I cannot control who uses the serialized version of the object

@joelittlejohn
Copy link
Owner

I'm suggesting that if you treat empty arrays the same as none, then you don't need "minItems": 1, and the validator will not complain.

I'm not sure why an empty experiences array should be seen as a semantic problem for anyone who uses the serialized version. Can you think of an example in which an empty array could/would be treated differently than an omitted array? I don't see a printed empty array as a problem.

If you really need to avoid printing these you could implement your own annotator that extends one of the existing annotators but adds Include.NON_EMPTY instead of Include.NON_NULL.

@akushe
Copy link
Author

akushe commented Mar 4, 2014

Well removing "minItems" : 1 looks to be required at the moment. In the schema if I add "default" : null will it declare List experiences = null or by default all objects are created as Empty Lists ?

@joelittlejohn
Copy link
Owner

Did you try it? Unfortunately I think that no, the logic is such that "default":null wont work for array properties. This is definitely something we should fix in the next version.

@akushe
Copy link
Author

akushe commented Mar 4, 2014

Yeah I tried it but does not work

@joelittlejohn joelittlejohn added this to the 0.4.1 milestone Mar 5, 2014
@akushe
Copy link
Author

akushe commented Mar 5, 2014

So you added the enhancement. Great!. When would 0.4.1 be pushed to remote?

@joelittlejohn
Copy link
Owner

I'll hopefully get this released sometime over the next few days. Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants