Skip to content
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

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

Open
calii23 opened this issue Jun 12, 2023 · 2 comments
Open

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

calii23 opened this issue Jun 12, 2023 · 2 comments

Comments

@calii23
Copy link

calii23 commented Jun 12, 2023

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.

@cowtowncoder cowtowncoder changed the title Default values in Java 14 records Support default values in Java 14 records Jun 12, 2023
@JooHyukKim
Copy link
Member

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

@cowtowncoder
Copy link
Member

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.

@cowtowncoder cowtowncoder changed the title Support default values in Java 14 records Support default values in Java 14 records (and POJOs too?) Jan 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants