Skip to content

Add OptBoolean valued @JsonProperty.isRequired to (eventually) replace @JsonProperty.required #284

@cowtowncoder

Description

@cowtowncoder

One nasty issue with @JsonProperty annotation is that its required property is boolean-valued: and as such cannot indicate "use defaults" / "not defined" option -- and default being false, it means that:

@JsonProperty

is same as

@JsonProperty(required = false)

so that it is easy to accidentally specify that a property is NOT required. This is especially true when considering cases where there are more generic defaults (provided by modules, f.ex) at, levels like:

  • For Constructor (require-all)
  • Global defaults (default-unless-overriden)

in which case any use of @JsonProperty either:

  1. Overrides more general default, OR
  2. If only required = true case considered explicit, is impossible to disable "required-ness"

What we need, really, is third state: and use of OptBoolean enum offers that: it has 3 values:

  • TRUE (-> Boolean.TRUE)
  • FALSE (-> Boolean.FALSE)
  • DEFAULT (-> null)

However. We cannot really change type of required property, even with 2.x->3.0 transition -- because we do want to keep jackson-annotations backwards-compatible (in that jackson-annotations 2.x versions will work with 3.x, and (for most purposes) vice-versa).

What we can do is to:

  1. add new property, "isRequired" with OptBoolean value
  2. support combination of both new isRequired (higher precedence) and old required (lower precedence) via databind AnnotationIntrospector
  3. for Jackson 3.0, mark required as Deprecated (... and possibly later 2.x, but not in 2.19)
  4. Profit!

This issue is for addition of the new property: there will be new jackson-databind ticket for reminder of work.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions