FasterXML / jackson-databind

General data-binding package for Jackson (2.x): works on streaming API (core) implementation(s)
Apache License 2.0
3.51k stars 1.37k forks source link

Record property serialization order not preserved #4617

Closed GeorgiPetkov closed 1 month ago

GeorgiPetkov commented 1 month ago

Search before asking

Describe the bug

Since 2.16.0 the order of record's properties is not preserved when @JsonProperty is present. This is not the case for version 2.15.X.

The impact:

Version Information

1.16.0+, works on 1.15.X

Reproduction

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;

class RecordPropertiesOrderTest {

    @Test
    void assertRecordPropertiesOrder() throws JsonProcessingException {
        assertEquals("{\"aa\":1,\"b\":2}", new ObjectMapper().writeValueAsString(new TestRecord(1, 2)));
    }

    private record TestRecord(@JsonProperty("aa") int a, int b) {
    }
}

Expected behavior

The properties in the JSON should be in the same order as defined in the record just like it behaves without the @JsonProperty annotation.

Additional context

No response

pjfanning commented 1 month ago

Could you try testing with 2.18.0-SNAPSHOT? This version contains more changes in the code handling property order.

GeorgiPetkov commented 1 month ago

I don't know where to find this version. Is it released somewhere? You can test it yourself pretty easily given the provided test. It only depends on JUnit5 and Jackson.

pjfanning commented 1 month ago

Snapshots are a commonly used way of testing pre-release changes. Jackson uses Sonatype for publishing jars.

Maybe reading reading the docs for your preferred build tool could e an option.

For Maven, this has some answers: https://stackoverflow.com/questions/16286055/how-to-get-snapshot-from-sonatype

GeorgiPetkov commented 1 month ago

The test passes with the current 2.18.0-SNAPSHOT (jackson-databind-2.18.0-20240706.005235-157.jar).

Do you have any rough estimate regarding when we can expect release of 2.18.0?

cowtowncoder commented 1 month ago

@GeorgiPetkov Most likely end of August -- need at least one Release Candidate and we are not yet ready for that. Plus I'll be on vacation last week of July, first of August.

cowtowncoder commented 1 month ago

Marked as resolved for 2.18.0.