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

Default ordering for record with @JsonProperties fields mixed with fields without annotations #4712

Closed
1 task done
mumukiller opened this issue Sep 24, 2024 · 8 comments
Closed
1 task done
Labels
2.17 Issues planned at earliest for 2.17 duplicate Duplicate of an existing (usually earlier) issue
Milestone

Comments

@mumukiller
Copy link

mumukiller commented Sep 24, 2024

Search before asking

  • I searched in the issues and found nothing similar.

Describe the bug

Expected order of fields in result of writeValueAsString method should have the same order as order fields which are declared in record, but in fact it's not true.

Version Information

2.17.2

Reproduction

Below there is a simple test

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 java.math.BigDecimal;

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

public class WhereIsMyOrderTest {

  private final ObjectMapper mapper = new ObjectMapper();

  @Test
  public void test() throws JsonProcessingException {
    var dto = new TestDto("1", BigDecimal.ONE,"3", "4");
    var result = mapper.writeValueAsString(dto);

    assertEquals("{\"a\":\"1\",\"b\":\"1\",\"c\":\"3\",\"d\":\"4\"}", result);
  }

  public record TestDto(
          @JsonProperty("a") String a,
          BigDecimal b,
          @JsonProperty("c") String c,
          String d) {}
}

Expected :{"a":"1","b":"1","c":"3","d":"4"}
Actual :{"b":1,"d":"4","a":"1","c":"3"}

So i expect that fields in the result will be int he same order as declared in record

Probably that my understanding of how it should work is wrong.

Expected behavior

{"a":"1","b":"1","c":"3","d":"4"}

Additional context

No response

@mumukiller mumukiller added the to-evaluate Issue that has been received but not yet evaluated label Sep 24, 2024
@mumukiller mumukiller changed the title Default ordering for record with @JsonProperties Default ordering for record with @JsonProperties fields mixed with fields without annotations Sep 24, 2024
@yihtserns
Copy link
Contributor

yihtserns commented Sep 24, 2024

Version Information
2.15.4

Are you sure that's the correct version? From my local testing:

  • 2.14.2: {"a":"1","c":"3","b":1,"d":"4"}
  • 2.15.0: {"a":"1","c":"3","b":1,"d":"4"}
  • 2.15.4: {"a":"1","c":"3","b":1,"d":"4"}

Seems like it has never been according to "expected" nor "actual".

UPDATE
The upcoming 2.18.0 will produce {"a":"1","b":1,"c":"3","d":"4"} (tested with 2.18.0-SNAPSHOT), likely thanks to #4515.

@JooHyukKim
Copy link
Member

Great! Thank you for testing @yihtserns 👍🏼

2.18 version is in release candidate state now. Please wait and use 2.18 release @mumukiller 👌🏼👌🏼

@mumukiller
Copy link
Author

mumukiller commented Sep 24, 2024

Sorry i found this issue due to spring boot update, there were some issues with gradle cache, the real current version is 2.17.2

Do i need to change the description?

PS Did it)

@cowtowncoder
Copy link
Member

If this is fixed in 2.18.0, let's close?

@cowtowncoder cowtowncoder added this to the 2.18.0 milestone Sep 24, 2024
@cowtowncoder cowtowncoder removed the to-evaluate Issue that has been received but not yet evaluated label Sep 24, 2024
@yihtserns
Copy link
Contributor

@cowtowncoder do you remember if you added a test with the scenario above, when you did #4515?

@cowtowncoder
Copy link
Member

@yihtserns I think I did, and that there'd be actually specific issue for this very thing... let me see if I find in release notes.

@cowtowncoder
Copy link
Member

Ok: I think #4617 ?

@cowtowncoder cowtowncoder added duplicate Duplicate of an existing (usually earlier) issue 2.17 Issues planned at earliest for 2.17 labels Sep 24, 2024
@cowtowncoder
Copy link
Member

Closing as duplicate of #4617.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
2.17 Issues planned at earliest for 2.17 duplicate Duplicate of an existing (usually earlier) issue
Projects
None yet
Development

No branches or pull requests

4 participants