Skip to content

Add CoercionConfig[s] mechanism for configuring allowed coercions #2113

@cowtowncoder

Description

@cowtowncoder

One of recurring themes regarding handling of simple scalar values (like numbers, Enums) is that some users want strict handling (only accept values with exactly matching types; JSON boolean for Java boolean), whereas others prefer coercions that may accept "close enough" matches (for example, accepting number 0 to map as false for Java boolean property).

Right now configurability is both limited and global: main mechanism is MapperFeature.ALLOW_COERCION_OF_SCALARS; and there are DeserializationFeatures like FAIL_ON_NULL_FOR_PRIMITIVES, FAIL_ON_NUMBERS_FOR_ENUMS.

But we could and should make use of another existing configuration mechanism:
@JsonFormat has property lenient, which, if set to false, could indicate strict handling.

This setting is actually already used for Date/Time values, but as format feature it is already accessible, and can be configured:

  1. For individual property
  2. For type (class)

thus allowing much more fine-grained setting.

We could and should start with:

  1. Booleans
  2. Numbers (int, long, double, float etc)
  3. Enums

Note, too, that since default for property is OptBoolean.DEFAULT, we can even distinguish between "lenient" / "not lenient" / "default", to allow further coercions, and leaving current set as "default" choice.

Metadata

Metadata

Assignees

No one assigned

    Labels

    most-wantedTag to indicate that there is heavy user +1'ing action

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions