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

@JsonProperty required = true does not work for constructors #174

Closed
christophsturm opened this issue Feb 23, 2013 · 6 comments
Closed

@JsonProperty required = true does not work for constructors #174

christophsturm opened this issue Feb 23, 2013 · 6 comments

Comments

@christophsturm
Copy link

for example with this constructor

    public Strategy(@JsonProperty(value = "hehe") int hehe,@JsonProperty(value = "blah", required = true) int blah)

I can still deserialize a json that does not contain blah.

I did not see this documented anywhere, just in the changelog for 2.1.0:

* [Issue#9]: Implement 'required' JSON Schema attribute for bean properties

so i guess constructor support for this feature is just missing. is that correct?

@cowtowncoder
Copy link
Member

Correct. More generally, 'required' attribute is not supported for deserialization at all -- supporting it will require surprisingly significant changes in deserializer. So if/when this is done, it should also be made to work with creator methods.

@robertkuhar
Copy link

I would really like to see this implemented. I expected that "required" worked both ways and was surprised to learn that it didn't.

@cowtowncoder
Copy link
Member

Yup, we are always open for good code contributions!

@lukens
Copy link

lukens commented Sep 24, 2014

I came across this issue after making a similar assumption about what the behaviour would be.

I would, however, also comment that if you expect a value passed to a constructor to be non-null, then it should be non-null whether deserialised from JSON or constructed otherwise. As such, even if this was enforced through the @JsonProperty annotation when deserialising, you could still construct an invalid instance of the class through other means. I therefore feel it is better to check for non-null values in the constructor code itself.

Of course, if you only ever construct your objects through JSON deserialisation you probably less concerned about this.

@cowtowncoder
Copy link
Member

I think this is dup of #230, so closing as such -- still hoping to add support for checks in future, just want to reduce duplication in issue tracker.

@cowtowncoder
Copy link
Member

Correction: this was not exact dup of #230, being a subset.

But we do have #781 -- just implemented for 2.6.0! -- which was re-filed for the specific case of supporting required-ness for Creator methods. This because that subset is relatively easy to support, and is needed to support some Scala functionality as well.

christophercurrie pushed a commit to christophercurrie/jackson-databind that referenced this issue Jul 19, 2015
resolves FasterXML#172 by adding last operation in JsonPointer.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants