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

SORT_PROPERTIES_ALPHABETICALLY doesn't work for partial constructors #2604

Closed
l0rinc opened this issue Jan 24, 2020 · 2 comments
Closed

SORT_PROPERTIES_ALPHABETICALLY doesn't work for partial constructors #2604

l0rinc opened this issue Jan 24, 2020 · 2 comments

Comments

@l0rinc
Copy link

l0rinc commented Jan 24, 2020

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.

@cowtowncoder
Copy link
Member

One quick note: sort order is a combination of multiple things, with different precedence. Highest precedence is for explicit order information (@JsonPropertyOrder( .... ), @JsonProperty(index = ...)); followed by "alphabetic" and "creator-bound properties first" general ordering info. I think ordering of creator-bound properties has higher precedence than more general alphabetic: but this should be documented better.
And for 3.0 it might be reasonable to reconsider logic, as well; the only reason for trying to sort creator properties first is that this is typically good for deserialization (reduces need for buffering).

I'll tag this as 3.x although if I have time I'd like to verify current processing to make sure I understand how it works first, before considering possible changes.

@cowtowncoder cowtowncoder added 3.x Issues to be only tackled for Jackson 3.x, not 2.x and removed 2.11 labels Mar 13, 2020
@cowtowncoder cowtowncoder removed the 3.x Issues to be only tackled for Jackson 3.x, not 2.x label Dec 1, 2024
@cowtowncoder
Copy link
Member

cowtowncoder commented Dec 1, 2024

Note: there are now 3 relevant MapperFeatures :

  • SORT_PROPERTIES_ALPHABETICALLY (default: false)
  • SORT_CREATOR_PROPERTIES_FIRST (default: true)
  • SORT_CREATOR_PROPERTIES_BY_DECLARATION_ORDER (default: false)

combining which will allow True alphabetic sorting -- by disabling SORT_CREATOR_PROPERTIES_FIRST, enable SORT_PROPERTIES_ALPHABETICALLY).

Will mark as solved.

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

No branches or pull requests

2 participants