FasterXML / jackson-future-ideas

Repository for SOLE PURPOSE of issue tracker and Wiki for NEW IDEAS. Please: NO BUG REPORTS.
18 stars 6 forks source link

Support default values in Java 14 records (and POJOs too?) #67

Open calii23 opened 1 year ago

calii23 commented 1 year ago

It would be very useful in some situations to define default values when deserializing into a record. It is only possible in the moment to define default values using the @JsonSetter annotation and then define custom logic in the setter. But as this is not possible when using java records, as in this case all properties must be passed using the constructor and then the object becomes immutable.

It might be possible to use this:

public record PageRequest(
        int page,

        int pageSize
) {
    @JsonCreator
    public PageRequest(
            @JsonProperty("page") int page,
            @JsonProperty("pageSize") Integer pageSize
    ) {
        this(page, pageSize == null ? 20 : pageSize);
    }
}

But the readability (especially when we have more than 2 properties) is not very good, and there is a lot of boilerplate code here. This variant might be much more readable:

public record PageRequest(
        int page,

        @JsonProperty(defaultValue = "20") // this parameter already exists, just doesn't do anything in the moment
        int pageSize
) {
}

or:

public record PageRequest(
        int page,

        @JsonDefaultInt(20)
        int pageSize
) {
}

This may also allow to handle default value in case the property is missing, so that it can still be set explicitly to null.

I know this ticket is kinda duplicated from #39 but I thought I may open a new one specifically related to java records and not only to default from null more to default when the property is missing.

JooHyukKim commented 8 months ago

Idk how I ran into this issue, but this issue about defaultValue and required and may help coming up with a solution.

cowtowncoder commented 8 months ago

Yes, the annotation mechanism would be

@JsonProperty(default = "10")

but the real challenges are:

  1. How to deserialize values from JSON Strings (cannot use JsonDeserializer interface)
  2. How to apply defaults

For (1) we might be able to use KeyDeserializer, possibly extending support for types provided. For (2) we would probably need to limit this to @JsonCreator provided values, same as @JsonProperty.required

Also note that this is not really Record-specific; if this was to be supported, could and should work for POJOs too, I think.