Skip to content

@JsonCreator with unknown fallback setter: do not throw error if FAIL_ON_UNKNOWN_PROPERTIES is false #862

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

Closed
fromi opened this issue Jul 14, 2015 · 4 comments

Comments

@fromi
Copy link

fromi commented Jul 14, 2015

Hi, I am currently using @JsonCreator to control object creation, combined with "readerForUpdating" to control object updates.
I have a property that can be created, but not updated. Therefore, it exists as an argument of my constructor, but there is not setter method.

I would like to ignore the property during update. However, I get this error:
No fallback setter/field defined: can not use creator property for ....

The culprit is CreatorProperty L192, with a nice comment:

        /* Hmmmh. Should we return quietly (NOP), or error?
         * Perhaps better to throw an exception, since it's generally an error.
         */

Actually, I would like an exception only if my mapper is configured to FAIL_ON_UNKNOWN_PROPERTIES. Am I correct?

Thanks for the great work by the way!

@fromi fromi changed the title @JsonCreator with unknown fallback setter: do not throw error is FAIL_ON_UNKNOWN_PROPERTIES is false @JsonCreator with unknown fallback setter: do not throw error if FAIL_ON_UNKNOWN_PROPERTIES is false Jul 14, 2015
@cowtowncoder
Copy link
Member

Thank you!

Interesting question here. I think your suggestion is good, to see if FAIL_ON_UNKNOWN_PROPERTIES is set; exception message should then reflect this as well.
So I think this is a reasonable RFE.

@cowtowncoder
Copy link
Member

Finally looking into this, I think I misunderstood this, assuming there was a failure even without attempt to set a value.

However: in case of a known property (due to @JsonCreator), not having a setter, I think an exception seems reasonable: property is not unknown but there's just no way to assign it.

One possible remedy would be to use @JsonAnySetter in such case. I will actually create a separate RFE for this.

@cowtowncoder
Copy link
Member

Re-created as #1401.

@fromi
Copy link
Author

fromi commented Oct 13, 2017

Hi, sorry for this late reply : I would like you to reconsider this issue.

I have an object with an immutable property. Some other properties are mutable. My immutable property is exposed to my front-end. The front-end code contains a form to update de mutable properties, and displays the immutable property. By convenience and code simplicity, I do not want to copy / remove the immutable property when I send my update request, therefore the immutable property is part of the request (but unchanged).
On the back-end side, for security reasons I do not want any setter for the property. However, this property is set, once, when the object is created.

Therefore, I think it a reasonable use case to have a property defined in a JsonCreator, provided inside a readerForUpdating request, but ignored by having no setter.

On the other hand, if you do not throw any exception, the only drawback is that the developer will need to analyse why the property was not updated. I think it is quite easy to find that a setter is missing.

Best regards,
Romain

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

2 participants