Skip to content

SORT_PROPERTIES_ALPHABETICALLY doesn't work for partial constructors #2604

Closed
@l0rinc

Description

@l0rinc

Given the following class:

class Child2 {
    public Object a1, b, a2;
    @JsonCreator public Child2(/*Object a1,*/ Object b /*, Object a2*/) {}
}

The following Spock test fails:

def "fields are sorted"() {
    def objectMapper = new ObjectMapper()
        .registerModule(new ParameterNamesModule())
        .enable(SerializationFeature.CLOSE_CLOSEABLE)
        .enable(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY)

    def jsonBuffer = new ByteArrayOutputStream()
    objectMapper.writeValue(jsonBuffer, new Child2(null))
    def serialized = jsonBuffer.toString()

    expect:
    serialized == '{"a1":null,"a2":null,"b":null}'
}

fails, since it's serialized to the keys b,a1,a2 (uncommenting the first param, i.e. @JsonCreator public Child2(Object a1, Object b /*, Object a2*/) {} would return a1, b,a2).

While this may be a relatively stable solution (not even sure about that, since apparently constructor changes can affect the order), it's definitely not alphabetic.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions