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

Deserialization of record fails on constructor parameter ordering #167

Closed
Giovds opened this issue Oct 5, 2024 · 5 comments
Closed

Deserialization of record fails on constructor parameter ordering #167

Giovds opened this issue Oct 5, 2024 · 5 comments
Labels
2.18 has-failing-test Indicates that there exists a test case (under `failing/`) to reproduce the issue

Comments

@Giovds
Copy link

Giovds commented Oct 5, 2024

I was testing if I could use record deserialization in my project and I found that it fails to match the correct parameter in the record constructor. I seems to follow alphabetic order. I'm not sure if this can differ in other JDK implementations. I'm using azul-21.

The ValueReaderLocator#_resolveBeanForDeser() gets the parameters from the record, which seem to be returned in alphabetic order. This ordering will then be used when iterating over that list and thus using the incorrect constructor index when creating the BeanPropertyReader here. If all types match it will continue to parse and will return incorrect values. Those are eventually read in the BeanReader based on this indexing as shown in the test provided. If they don't match it results in a type mismatch exception.

I have added a test below to reproduce the issue, but you can recreate it with the Cow examples as well by renaming the fields.

record FoundDependency(String id, String g, String a, String v, String timestamp) {}

public void testOrderOfParamsFails() throws Exception {
    final var input = """
        {
          "id": "org.apache.maven:maven-core:3.9.8",
          "g": "org.apache.maven",
          "a": "maven-core",
          "v": "3.9.8",
          "p": "jar",
          "timestamp": 1718267050000,
          "ec": [
            "-cyclonedx.json",
            "-sources.jar",
            "-cyclonedx.xml",
            ".pom",
            "-javadoc.jar",
            ".jar"
          ],
          "tags": [
            "core",
            "maven",
            "classes"
          ]
        }
        """;
    final var expected = new FoundDependency("org.apache.maven:maven-core:3.9.8", "org.apache.maven", "maven-core", "3.9.8", "1718267050000");
    final var actual = jsonHandler.beanFrom(FoundDependency.class, input);
    assertEquals(expected, actual);
}
@cowtowncoder
Copy link
Member

Ok sounds like a flaw -- thank you for reporting this.

cowtowncoder added a commit that referenced this issue Oct 9, 2024
@cowtowncoder cowtowncoder added the has-failing-test Indicates that there exists a test case (under `failing/`) to reproduce the issue label Oct 9, 2024
@cowtowncoder
Copy link
Member

cc @TomaszGaweda looks like there's some work left to do with Record deserialization.

@Giovds
Copy link
Author

Giovds commented Oct 13, 2024

I think I know how to fix this. I'd be happy to provide a PR later this week, if that is ok.

@cowtowncoder
Copy link
Member

cowtowncoder commented Oct 13, 2024 via email

@Giovds
Copy link
Author

Giovds commented Oct 18, 2024

@cowtowncoder @TomaszGaweda I think the approach I propose in the PR should solve the issue. If there are any concerns please let me know.

I'm a bit hesitant on whether or not BeanPropertyReader should be using a index if it isn't a record, so perhaps that should be a different model to keep the concerns separate. I think it is only used for reading the index when reading to a Record.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
2.18 has-failing-test Indicates that there exists a test case (under `failing/`) to reproduce the issue
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants