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

add serialization order tests (JavaBean style) #3932

Draft
wants to merge 3 commits into
base: 2.16
Choose a base branch
from

Conversation

pjfanning
Copy link
Member

@pjfanning pjfanning commented May 15, 2023

See #3925

There is #3928 for records. This PR focuses on JavaBean style classes.

This highlights 2 problems:

  • When JavaBean only has a JsonProperty on the constructor param, this is ignored
  • Similar JavaBean that duplicates the JsonProperty annotation - putting it on the field and the constructor param - this upsets the order of the fields in the output.

@pjfanning pjfanning marked this pull request as draft May 15, 2023 11:35
@pjfanning
Copy link
Member Author

@yihtserns this test result does not seem to match what you say in #3925 . When the JsonProperty annotation only appears on the constructor param, it appears to be ignored. It does not affect the order and it does not even affect the name of the field in the JSON.

@JooHyukKim
Copy link
Member

@yihtserns this test result does not seem to match what you say in #3925 .

If what @pjfanning is true, can we revise what was said in the mentioned comment? just in case of confusing anybody

@yihtserns
Copy link
Contributor

You're missing @JsonProperty annotation on the field - I said:

public record NestedRecordOne(
    String id,
    String email,
    @JsonProperty(value = "yikes!") NestedRecordTwo nestedRecordTwo) {
}

...is not equivalent to this:

public class NestedClassOne {
    private String id;
    private String name;
    @JsonProperty(value = "all is fine!")  // 👈 not just here
    private NestedClassTwo nestedClassTwo;

    public NestedClassOne(String id,
                          String name,
                          NestedClassTwo nestedClassTwo) {

        this.id = id;
        this.name = name;
        this.nestedClassTwo = nestedClassTwo;
    }

    // getters
}

...but is actually equivalent to this:

public class NestedClassOne {
    private String id;
    private String name;
    @JsonProperty(value = "all is fine!")  // 👈 here
    private NestedClassTwo nestedClassTwo;

    public NestedClassOne(String id,
                          String name,
                          // 👇 and also here
                          @JsonProperty(value = "all is fine!") NestedClassTwo nestedClassTwo) {

        this.id = id;
        this.name = name;
        this.nestedClassTwo = nestedClassTwo;
    }

    ...
    @JsonProperty(value = "all is fine!")  // 👈 and also here, but this part is not important
    public NestedClassTwo getNestedClassTwo() {
        return this.nestedClassTwo;
    }
}

@pjfanning
Copy link
Member Author

pjfanning commented May 15, 2023

@yihtserns why would you want to put the same JsonProperty annotation in 2 places?

@pjfanning pjfanning changed the title add serialization order test add serialization order tests (JavaBean style) May 15, 2023
@yihtserns
Copy link
Contributor

yihtserns commented May 15, 2023

why would you want to put the same JsonProperty annotation in 2 places?

I wouldn't - I'm just describing the resulting Record's compiled .class, when a Record component is annotated with @JsonProperty.

@pjfanning
Copy link
Member Author

Thanks @yihtserns - I updated the PR to have 2 separate classes to highlight the 2 separate issues we are seeing

@cowtowncoder
Copy link
Member

@pjfanning I think I could proceed with this, but it needs merge/rebase from 2.16 I think.

@pjfanning
Copy link
Member Author

@cowtowncoder the tests in this PR just reproduce a problem. I have not attempted a fix.

@cowtowncoder
Copy link
Member

@pjfanning Gotcha. It could be added under ..../failing/ only including failing part, if that would help someone else try solve it. But it can be kept as draft PR too if that makes more sense.

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

Successfully merging this pull request may close these issues.

None yet

4 participants