-
Notifications
You must be signed in to change notification settings - Fork 18
Unable to associate parameter name with single non-annotated constructor argument #21
Comments
One-argument variant is bit problematic since it can ambiguous and could be taken mean either:
By default, assumption is that (1) is used, but explicit name (with But with 2.5 it is possible to explicit define type like so: public class Pojo {
@JsonCreator(mode=JsonCreator.Mode.PROPERTIES)
public Pojo(String name) {
...
} so that it could be mapped from JSON like { "name" : "Bill" } instead of what delegating constructor would expect: "value" |
I take it that (1) as the default is what the stable release does now? If so, or you don't want to revisit that decision for other reasons, is it possible for me to override the default
and later we decide we don't need
it's not immediately obvious that, by removing that second parameter, we've fundamentally changed how JSON deserialization works for that class. |
Yes, the description above was the intended behavior of 2.5. Changing default mode is easiest done by overriding method: public JsonCreator.Mode findCreatorBinding(Annotated a) in |
Gotcha. Closing this since it's intended. Will use my own annotation introspector. |
I've seen more than once questions regarding this issue and it would make sense to provide an API in the ParameterNamesAnnotationIntrospector to override this behavior so users don't have to override a method in JacksonAnnotationIntrospector. The end goal, in the spirit of this module, would be to go even further and make that behavior the default and users that don't want this change can change it with the new API. Opinions? |
@lpandzic Just to make sure I understand the proposal, are you suggesting defaulting the mode for single-argument creator with implicit name to assume |
In case of this module, yes. The module is still new so this might be the best time as ever. |
I am looking at |
Do you have a test so we can reproduce it in parameter names module? |
I don't have a specific test. Addition of an alternate constructor with similar visibility should be fine. I am ok with the change of default behavior for this module. Just needs to be documented on README, explained what the options are. As to value or boolean: I think I now think your idea of specifying value, and allowing null (to mean, just use defaults from core) make sense. The main change, I think, is just to use code from @Override
public JsonCreator.Mode findCreatorBinding(Annotated a) {
JsonCreator ann = a.getAnnotation(JsonCreator.class);
if (ann != null) {
return ann.mode();
}
// otherwise use override we want
return creatorMode;
} which while not ideal is, I think, better than making user change priority of introspectors or such. So: with this check, and some unit testing to rule out possibility of exceptions with multiple non-annotated constructors I can merge the patch. Thank you for helping getting this straightened and merged in 2.6.0. |
I've changed the PR to use null value as default instead of JsonCreator.Mode.DEFAULT. It makes sense to let core decide default behavior instead of reimplementing it here. |
With Jackson 2.6.0-rc2 and the parameter names module, I am running into errors when deserializing JSON into objects with one constructor argument. Here is some sample code demonstrating the issue:
Here is the output of the above.
My real code most closely resembles the
NoAnnotations
example. Is it a bug that that example throws, or is that an expected limitation?The text was updated successfully, but these errors were encountered: