Skip to content

Record deserialisation with instance methods fails #172

@Giovds

Description

@Giovds

The deserialisation now works for plain records, as fixed with #167.
However, I've slightly changed my implementation of my record to move some logic for calculating the time into a record instance method. Deserialisation with instance methods fails, as it tries to access a field that does not exist.

The final List<POJODefinition.Prop> rawProps = beanDef.getProperties(); contains a dateTime property which is declared in the record as instance method. When you get the FoundDependency.class declared fields it is not there, as it is a derived value and not a constructor parameter. It does however work (as expected) for static instance methods.

Test case slightly modified to replicate the issue:

public class RecordDeser167Test extends TestCase
{
    record FoundDependency(String id, String g, String a, String v, long timestamp) {
        public LocalDate getDateTime() {
            return Instant.ofEpochMilli(timestamp).atZone(java.time.ZoneId.systemDefault()).toLocalDate();
        }
    }

    // [jackson-jr#167]
    public void testRecordDeserOrder167() throws Exception
    {
        final String 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", 1718267050000L);
        final var actual = JSON.std.beanFrom(FoundDependency.class, input);
        assertEquals(expected, actual);
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    2.18has-failing-testIndicates that there exists a test case (under `failing/`) to reproduce the issue

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions