Skip to content

Enforce @JsonProperty.required when using builder. #2618

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

Open
edinardo opened this issue Feb 7, 2020 · 5 comments
Open

Enforce @JsonProperty.required when using builder. #2618

edinardo opened this issue Feb 7, 2020 · 5 comments
Labels
builder-related Issue related to handling of Builder-style deserialization

Comments

@edinardo
Copy link

edinardo commented Feb 7, 2020

When using @JsonProperty(required = true) with creator/factory (@JsonCreator annotation), an exception is raised if a required field is missing (as expected).

It is possible to have the same feature when using a builder to deserialize?

Sorry if this request is duplicate or already present, but I couldn't find it at issues list, forum, neither as configuration on the ObjectMapper documentation.

Tested with Jackson version 2.10.2, using the following code:

@JsonDeserialize(builder = Person.Builder.class)
public class Person {
    @JsonProperty(required = true)
    private final String name;
    ...
    public static class Builder {
        public Builder withName(String value) {...}
        public Person build() {...}
  }
}
@cowtowncoder
Copy link
Member

There is #230 which could be considered to cover this, but leaving this for builders is ok.
I'll tag this as 3.x since it won't be implemented for 2.x (with 3.x many things will be rewritten opening up new possibilities).

@cowtowncoder cowtowncoder added the 3.x Issues to be only tackled for Jackson 3.x, not 2.x label Feb 7, 2020
@cowtowncoder
Copy link
Member

Actually. Will tag 2.12 to maybe have another look, after 2.11 release.

@cowtowncoder cowtowncoder added 2.12 and removed 3.x Issues to be only tackled for Jackson 3.x, not 2.x labels Apr 4, 2020
@cowtowncoder cowtowncoder changed the title @JsonProperty required validated when using builder. Enforce @JsonProperty.required when using builder. Apr 4, 2020
@cowtowncoder cowtowncoder added 2.13 and removed 2.12 labels Oct 27, 2020
@cowtowncoder
Copy link
Member

Note to self: unfortunately support for builders no easier than that for all POJOs as databind does not have much special handling for builders (they just look like odd POJOs where "Setters" may return this for chaining).

@almson
Copy link

almson commented Dec 14, 2020

Hi, could you clarify your plans on including this feature? I opened a feature request for lombok @Jacksonized to generate @JsonProperty(required=true) (use case is JSON Schema generation) but they retort that it's a liability because Jackson is planning to enable validation of required which will be a backward-incompatible change. Will you really make this breaking change? When? Do you have a mitigation plan?

Also a similar question about defaultValue. (I would like it for documentation of JSON Schema, but the threat that Jackson will start using it creates a dangerous situation.)

@cowtowncoder
Copy link
Member

@almson I guess Lombok folks are way more optimistic about this getting implemented then: there is no on-going work to make this happen. But even if there was, I'd probably consider having a MapperFeature (or perhaps DeserializationFeature) to specify whether action was taken, to keep existing default behavior unchanged (that is: to make code fail user would have to enable a feature).

It is in fact quite unlikely that required detection would be implemented for Jackson 2.x, either for regular POJOs or Builder-style approach, at least if it is up to my implementation.

Same goes for "defaultValue": nothing currently exists to use that, except for JSON Schema generation (it was requested for that use case).

Having said all that: just because I do not have time to work on either feature does not mean other members of Jackson community might not be working on it, or decide to try it. 3 out of 5 "most wanted" features for 2.12 came as full "external" contributions (from new contributors, without much discussion before contribution).
So I cannot really predict future in that sense: if someone contributed implementation of either feature, I would work with contributor, trying to balance backwards-compatibility aspects (I would definitely suggest gating these features, opt-in style).

@cowtowncoder cowtowncoder added 2.14 builder-related Issue related to handling of Builder-style deserialization and removed 2.13 labels Jul 15, 2021
@cowtowncoder cowtowncoder removed the 2.14 label Mar 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
builder-related Issue related to handling of Builder-style deserialization
Projects
None yet
Development

No branches or pull requests

3 participants