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

[BUG] Support @JsonBackReference and JsonManagedReference annotations #2644

Closed
jorgerod opened this issue May 29, 2024 · 3 comments
Closed
Labels
bug Something isn't working fixed
Milestone

Comments

@jorgerod
Copy link

Hi

We are testing FastJson2 in a spring-boot based application and we have a DTO with the @JsonManagedReference and @JsonBackReference annotation and it is being supported and throws the following exception:

com.alibaba.fastjson2.JSONException: level too large : 2048

	at com.alibaba.fastjson2.JSONWriterUTF16.startObject(JSONWriterUTF16.java:122)
	at com.alibaba.fastjson2.writer.OWG_1_2_Item.write(Unknown Source)
	at com.alibaba.fastjson2.writer.FieldWriterList.writeList(FieldWriterList.java:353)
	at com.alibaba.fastjson2.writer.FieldWriterListFunc.write(FieldWriterListFunc.java:76)
	at com.alibaba.fastjson2.writer.ObjectWriter2.write(ObjectWriter2.java:71)
	at com.alibaba.fastjson2.writer.OWG_1_2_Item.write(Unknown Source)
	at com.alibaba.fastjson2.writer.FieldWriterList.writeList(FieldWriterList.java:353)
	at com.alibaba.fastjson2.writer.FieldWriterListFunc.write(FieldWriterListFunc.java:76)
	at com.alibaba.fastjson2.writer.ObjectWriter2.write(ObjectWriter2.java:71)
	at com.alibaba.fastjson2.writer.OWG_1_2_Item.write(Unknown Source)
	at com.alibaba.fastjson2.writer.FieldWriterList.writeList(FieldWriterList.java:353)
	at com.alibaba.fastjson2.writer.FieldWriterListFunc.write(FieldWriterListFunc.java:76)
	at com.alibaba.fastjson2.writer.ObjectWriter2.write(ObjectWriter2.java:71)
	at com.alibaba.fastjson2.writer.OWG_1_2_Item.write(Unknown Source)
	at com.alibaba.fastjson2.writer.FieldWriterList.writeList(FieldWriterList.java:353)
	at com.alibaba.fastjson2.writer.FieldWriterListFunc.write(FieldWriterListFunc.java:76)
	at com.alibaba.fastjson2.writer.ObjectWriter2.write(ObjectWriter2.java:71)
	at com.alibaba.fastjson2.writer.OWG_1_2_Item.write(Unknown Source)
	at com.alibaba.fastjson2.writer.FieldWriterList.writeList(FieldWriterList.java:353)
	at com.alibaba.fastjson2.writer.FieldWriterListFunc.write(FieldWriterListFunc.java:76)
	at com.alibaba.fastjson2.writer.ObjectWriter2.write(ObjectWriter2.java:71)
	at com.alibaba.fastjson2.writer.OWG_1_2_Item.write(Unknown Source)
	at com.alibaba.fastjson2.writer.FieldWriterList.writeList(FieldWriterList.java:353)
	at com.alibaba.fastjson2.writer.FieldWriterListFunc.write(FieldWriterListFunc.java:76)
	at com.alibaba.fastjson2.writer.ObjectWriter2.write(ObjectWriter2.java:71)
	at com.alibaba.fastjson2.writer.OWG_1_2_Item.write(Unknown Source)
	at com.alibaba.fastjson2.writer.FieldWriterList.writeList(FieldWriterList.java:353)
	at com.alibaba.fastjson2.writer.FieldWriterListFunc.write(FieldWriterListFunc.java:76)
	at com.alibaba.fastjson2.writer.ObjectWriter2.write(ObjectWriter2.java:71)
	at com.alibaba.fastjson2.writer.OWG_1_2_Item.write(Unknown Source)
	at com.alibaba.fastjson2.writer.FieldWriterList.writeList(FieldWriterList.java:353)
	at com.alibaba.fastjson2.writer.FieldWriterListFunc.write(FieldWriterListFunc.java:76)
	at com.alibaba.fastjson2.writer.ObjectWriter2.write(ObjectWriter2.java:71)
	at com.alibaba.fastjson2.writer.OWG_1_2_Item.write(Unknown Source)
	at com.alibaba.fastjson2.writer.FieldWriterList.writeList(FieldWriterList.java:353)
	at com.alibaba.fastjson2.writer.FieldWriterListFunc.write(FieldWriterListFunc.java:76)
	at com.alibaba.fastjson2.writer.ObjectWriter2.write(ObjectWriter2.java:71)
	at com.alibaba.fastjson2.writer.OWG_1_2_Item.write(Unknown Source)
	at com.alibaba.fastjson2.writer.FieldWriterList.writeList(FieldWriterList.java:353)
	at com.alibaba.fastjson2.writer.FieldWriterListFunc.write(FieldWriterListFunc.java:76)
	at com.alibaba.fastjson2.writer.ObjectWriter2.write(ObjectWriter2.java:71)
	at com.alibaba.fastjson2.writer.OWG_1_2_Item.write(Unknown Source)
	at com.alibaba.fastjson2.writer.FieldWriterList.writeList(FieldWriterList.java:353)
	at com.alibaba.fastjson2.writer.FieldWriterListFunc.write(FieldWriterListFunc.java:76)
	at com.alibaba.fastjson2.writer.ObjectWriter2.write(ObjectWriter2.java:71)
	at com.alibaba.fastjson2.writer.OWG_1_2_Item.write(Unknown Source)
	at com.alibaba.fastjson2.writer.FieldWriterList.writeList(FieldWriterList.java:353)
	at com.alibaba.fastjson2.writer.FieldWriterListFunc.write(FieldWriterListFunc.java:76)
	at com.alibaba.fastjson2.writer.ObjectWriter2.write(ObjectWriter2.java:71)
	at com.alibaba.fastjson2.writer.OWG_1_2_Item.write(Unknown Source)
	at com.alibaba.fastjson2.writer.FieldWriterList.writeList(FieldWriterList.java:353)
	at com.alibaba.fastjson2.writer.FieldWriterListFunc.write(FieldWriterListFunc.java:76)
	at com.alibaba.fastjson2.writer.ObjectWriter2.write(ObjectWriter2.java:71)
	at com.alibaba.fastjson2.writer.OWG_1_2_Item.write(Unknown Source)
	at com.alibaba.fastjson2.writer.FieldWriterList.writeList(FieldWriterList.java:353)
	at com.alibaba.fastjson2.writer.FieldWriterListFunc.write(FieldWriterListFunc.java:76)
	at com.alibaba.fastjson2.writer.ObjectWriter2.write(ObjectWriter2.java:71)
	at com.alibaba.fastjson2.writer.OWG_1_2_Item.write(Unknown Source)
	at com.alibaba.fastjson2.writer.FieldWriterList.writeList(FieldWriterList.java:353)
	at com.alibaba.fastjson2.writer.FieldWriterListFunc.write(FieldWriterListFunc.java:76)
	at com.alibaba.fastjson2.writer.ObjectWriter2.write(ObjectWriter2.java:71)
	at com.alibaba.fastjson2.writer.OWG_1_2_Item.write(Unknown Source)
	at com.alibaba.fastjson2.writer.FieldWriterList.writeList(FieldWriterList.java:353)
	at com.alibaba.fastjson2.writer.FieldWriterListFunc.write(FieldWriterListFunc.java:76)
	at com.alibaba.fastjson2.writer.ObjectWriter2.write(ObjectWriter2.java:71)
	at com.alibaba.fastjson2.writer.OWG_1_2_Item.write(Unknown Source)
	at com.alibaba.fastjson2.writer.FieldWriterList.writeList(FieldWriterList.java:353)
	at com.alibaba.fastjson2.writer.FieldWriterListFunc.write(FieldWriterListFunc.java:76)
	at com.alibaba.fastjson2.writer.ObjectWriter2.write(ObjectWriter2.java:71)
	at com.alibaba.fastjson2.writer.OWG_1_2_Item.write(Unknown Source)
	at com.alibaba.fastjson2.writer.FieldWriterList.writeList(FieldWriterList.java:353)
	at com.alibaba.fastjson2.writer.FieldWriterListFunc.write(FieldWriterListFunc.java:76)
	at com.alibaba.fastjson2.writer.ObjectWriter2.write(ObjectWriter2.java:71)
	at com.alibaba.fastjson2.writer.OWG_1_2_Item.write(Unknown Source)
	at com.alibaba.fastjson2.writer.FieldWriterList.writeList(FieldWriterList.java:353)
	at com.alibaba.fastjson2.writer.FieldWriterListFunc.write(FieldWriterListFunc.java:76)
	at com.alibaba.fastjson2.writer.ObjectWriter2.write(ObjectWriter2.java:71)
	at com.alibaba.fastjson2.writer.OWG_1_2_Item.write(Unknown Source)
	at com.alibaba.fastjson2.writer.FieldWriterList.writeList(FieldWriterList.java:353)
	at com.alibaba.fastjson2.writer.FieldWriterListFunc.write(FieldWriterListFunc.java:76)
	at com.alibaba.fastjson2.writer.ObjectWriter2.write(ObjectWriter2.java:71)
	at com.alibaba.fastjson2.writer.OWG_1_2_Item.write(Unknown Source)
	at com.alibaba.fastjson2.writer.FieldWriterList.writeList(FieldWriterList.java:353)
	at com.alibaba.fastjson2.writer.FieldWriterListFunc.write(FieldWriterListFunc.java:76)
	at com.alibaba.fastjson2.writer.ObjectWriter2.write(ObjectWriter2.java:71)
	at com.alibaba.fastjson2.writer.OWG_1_2_Item.write(Unknown Source)
	at com.alibaba.fastjson2.writer.FieldWriterList.writeList(FieldWriterList.java:353)
	at com.alibaba.fastjson2.writer.FieldWriterListFunc.write(FieldWriterListFunc.java:76)
	at com.alibaba.fastjson2.writer.ObjectWriter2.write(ObjectWriter2.java:71)
	at com.alibaba.fastjson2.writer.OWG_1_2_Item.write(Unknown Source)
	at com.alibaba.fastjson2.writer.FieldWriterList.writeList(FieldWriterList.java:353)
	at com.alibaba.fastjson2.writer.FieldWriterListFunc.write(FieldWriterListFunc.java:76)
	at com.alibaba.fastjson2.writer.ObjectWriter2.write(ObjectWriter2.java:71)
	at com.alibaba.fastjson2.writer.OWG_1_2_Item.write(Unknown Source)
	at com.alibaba.fastjson2.writer.FieldWriterList.writeList(FieldWriterList.java:353)
	at com.alibaba.fastjson2.writer.FieldWriterListFunc.write(FieldWriterListFunc.java:76)
	at com.alibaba.fastjson2.writer.ObjectWriter2.write(ObjectWriter2.java:71)
	at com.alibaba.fastjson2.writer.OWG_1_2_Item.write(Unknown Source)
	at com.alibaba.fastjson2.writer.FieldWriterList.writeList(FieldWriterList.java:353)
	at com.alibaba.fastjson2.writer.FieldWriterListFunc.write(FieldWriterListFunc.java:76)
	at com.alibaba.fastjson2.writer.ObjectWriter2.write(ObjectWriter2.java:71)
	at com.alibaba.fastjson2.writer.OWG_1_2_Item.write(Unknown Source)
	at com.alibaba.fastjson2.writer.FieldWriterList.writeList(FieldWriterList.java:353)
	at com.alibaba.fastjson2.writer.FieldWriterListFunc.write(FieldWriterListFunc.java:76)
	at com.alibaba.fastjson2.writer.ObjectWriter2.write(ObjectWriter2.java:71)
	at com.alibaba.fastjson2.writer.OWG_1_2_Item.write(Unknown Source)
	at com.alibaba.fastjson2.writer.FieldWriterList.writeList(FieldWriterList.java:353)
	at com.alibaba.fastjson2.writer.FieldWriterListFunc.write(FieldWriterListFunc.java:76)
	at com.alibaba.fastjson2.writer.ObjectWriter2.write(ObjectWriter2.java:71)
	at com.alibaba.fastjson2.writer.OWG_1_2_Item.write(Unknown Source)
	at com.alibaba.fastjson2.writer.FieldWriterList.writeList(FieldWriterList.java:353)
	at com.alibaba.fastjson2.writer.FieldWriterListFunc.write(FieldWriterListFunc.java:76)
	at com.alibaba.fastjson2.writer.ObjectWriter2.write(ObjectWriter2.java:71)
	at com.alibaba.fastjson2.writer.OWG_1_2_Item.write(Unknown Source)
	at com.alibaba.fastjson2.writer.FieldWriterList.writeList(FieldWriterList.java:353)
	at com.alibaba.fastjson2.writer.FieldWriterListFunc.write(FieldWriterListFunc.java:76)
	at com.alibaba.fastjson2.writer.ObjectWriter2.write(ObjectWriter2.java:71)
	at com.alibaba.fastjson2.writer.OWG_1_2_Item.write(Unknown Source)
	at com.alibaba.fastjson2.writer.FieldWriterList.writeList(FieldWriterList.java:353)
	at com.alibaba.fastjson2.writer.FieldWriterListFunc.write(FieldWriterListFunc.java:76)
	at com.alibaba.fastjson2.writer.ObjectWriter2.write(ObjectWriter2.java:71)
	at com.alibaba.fastjson2.writer.OWG_1_2_Item.write(Unknown Source)
	at com.alibaba.fastjson2.writer.FieldWriterList.writeList(FieldWriterList.java:353)
	at com.alibaba.fastjson2.writer.FieldWriterListFunc.write(FieldWriterListFunc.java:76)
	at com.alibaba.fastjson2.writer.ObjectWriter2.write(ObjectWriter2.java:71)
	at com.alibaba.fastjson2.writer.OWG_1_2_Item.write(Unknown Source)
	at com.alibaba.fastjson2.writer.FieldWriterList.writeList(FieldWriterList.java:353)
	at com.alibaba.fastjson2.writer.FieldWriterListFunc.write(FieldWriterListFunc.java:76)
	at com.alibaba.fastjson2.writer.ObjectWriter2.write(ObjectWriter2.java:71)
	at com.alibaba.fastjson2.writer.OWG_1_2_Item.write(Unknown Source)
	at com.alibaba.fastjson2.writer.FieldWriterList.writeList(FieldWriterList.java:353)
	at com.alibaba.fastjson2.writer.FieldWriterListFunc.write(FieldWriterListFunc.java:76)
	at com.alibaba.fastjson2.writer.ObjectWriter2.write(ObjectWriter2.java:71)
	at com.alibaba.fastjson2.writer.OWG_1_2_Item.write(Unknown Source)
	at com.alibaba.fastjson2.writer.FieldWriterList.writeList(FieldWriterList.java:353)
	at com.alibaba.fastjson2.writer.FieldWriterListFunc.write(FieldWriterListFunc.java:76)
	at com.alibaba.fastjson2.writer.ObjectWriter2.write(ObjectWriter2.java:71)
	at com.alibaba.fastjson2.writer.OWG_1_2_Item.write(Unknown Source)
	at com.alibaba.fastjson2.writer.FieldWriterList.writeList(FieldWriterList.java:353)
	at com.alibaba.fastjson2.writer.FieldWriterListFunc.write(FieldWriterListFunc.java:76)
	at com.alibaba.fastjson2.writer.ObjectWriter2.write(ObjectWriter2.java:71)
	at com.alibaba.fastjson2.writer.OWG_1_2_Item.write(Unknown Source)
	at com.alibaba.fastjson2.writer.FieldWriterList.writeList(FieldWriterList.java:353)
	at com.alibaba.fastjson2.writer.FieldWriterListFunc.write(FieldWriterListFunc.java:76)
	at com.alibaba.fastjson2.writer.ObjectWriter2.write(ObjectWriter2.java:71)
	at com.alibaba.fastjson2.writer.OWG_1_2_Item.write(Unknown Source)
	at com.alibaba.fastjson2.writer.FieldWriterList.writeList(FieldWriterList.java:353)
	at com.alibaba.fastjson2.writer.FieldWriterListFunc.write(FieldWriterListFunc.java:76)
	at com.alibaba.fastjson2.writer.ObjectWriter2.write(ObjectWriter2.java:71)
	at com.alibaba.fastjson2.writer.OWG_1_2_Item.write(Unknown Source)
	at com.alibaba.fastjson2.writer.FieldWriterList.writeList(FieldWriterList.java:353)
	at com.alibaba.fastjson2.writer.FieldWriterListFunc.write(FieldWriterListFunc.java:76)
	at com.alibaba.fastjson2.writer.ObjectWriter2.write(ObjectWriter2.java:71)
	at com.alibaba.fastjson2.writer.OWG_1_2_Item.write(Unknown Source)
	at com.alibaba.fastjson2.writer.FieldWriterList.writeList(FieldWriterList.java:353)
	at com.alibaba.fastjson2.writer.FieldWriterListFunc.write(FieldWriterListFunc.java:76)
	at com.alibaba.fastjson2.writer.ObjectWriter2.write(ObjectWriter2.java:71)
	at com.alibaba.fastjson2.writer.OWG_1_2_Item.write(Unknown Source)
	at com.alibaba.fastjson2.writer.FieldWriterList.writeList(FieldWriterList.java:353)
	at com.alibaba.fastjson2.writer.FieldWriterListFunc.write(FieldWriterListFunc.java:76)
	at com.alibaba.fastjson2.writer.ObjectWriter2.write(ObjectWriter2.java:71)
	at com.alibaba.fastjson2.writer.OWG_1_2_Item.write(Unknown Source)
	at com.alibaba.fastjson2.writer.FieldWriterList.writeList(FieldWriterList.java:353)
	at com.alibaba.fastjson2.writer.FieldWriterListFunc.write(FieldWriterListFunc.java:76)
	at com.alibaba.fastjson2.writer.ObjectWriter2.write(ObjectWriter2.java:71)
	at com.alibaba.fastjson2.writer.OWG_1_2_Item.write(Unknown Source)
	at com.alibaba.fastjson2.writer.FieldWriterList.writeList(FieldWriterList.java:353)
	at com.alibaba.fastjson2.writer.FieldWriterListFunc.write(FieldWriterListFunc.java:76)
	at com.alibaba.fastjson2.writer.ObjectWriter2.write(ObjectWriter2.java:71)
	at com.alibaba.fastjson2.writer.OWG_1_2_Item.write(Unknown Source)
	at com.alibaba.fastjson2.writer.FieldWriterList.writeList(FieldWriterList.java:353)
	at com.alibaba.fastjson2.writer.FieldWriterListFunc.write(FieldWriterListFunc.java:76)
	at com.alibaba.fastjson2.writer.ObjectWriter2.write(ObjectWriter2.java:71)
	at com.alibaba.fastjson2.writer.OWG_1_2_Item.write(Unknown Source)
	at com.alibaba.fastjson2.writer.FieldWriterList.writeList(FieldWriterList.java:353)
	at com.alibaba.fastjson2.writer.FieldWriterListFunc.write(FieldWriterListFunc.java:76)
	at com.alibaba.fastjson2.writer.ObjectWriter2.write(ObjectWriter2.java:71)
	at com.alibaba.fastjson2.writer.OWG_1_2_Item.write(Unknown Source)
	at com.alibaba.fastjson2.writer.FieldWriterList.writeList(FieldWriterList.java:353)
	at com.alibaba.fastjson2.writer.FieldWriterListFunc.write(FieldWriterListFunc.java:76)
	at com.alibaba.fastjson2.writer.ObjectWriter2.write(ObjectWriter2.java:71)
	at com.alibaba.fastjson2.writer.OWG_1_2_Item.write(Unknown Source)
	at com.alibaba.fastjson2.writer.FieldWriterList.writeList(FieldWriterList.java:353)
	at com.alibaba.fastjson2.writer.FieldWriterListFunc.write(FieldWriterListFunc.java:76)
	at com.alibaba.fastjson2.writer.ObjectWriter2.write(ObjectWriter2.java:71)
	at com.alibaba.fastjson2.writer.OWG_1_2_Item.write(Unknown Source)
	at com.alibaba.fastjson2.writer.FieldWriterList.writeList(FieldWriterList.java:353)
	at com.alibaba.fastjson2.writer.FieldWriterListFunc.write(FieldWriterListFunc.java:76)
	at com.alibaba.fastjson2.writer.ObjectWriter2.write(ObjectWriter2.java:71)
	at com.alibaba.fastjson2.writer.OWG_1_2_Item.write(Unknown Source)
	at com.alibaba.fastjson2.writer.FieldWriterList.writeList(FieldWriterList.java:353)
	at com.alibaba.fastjson2.writer.FieldWriterListFunc.write(FieldWriterListFunc.java:76)

Test to reproduce:

public class JsonManagedReferenceTest {

  @Test
  public void testJsonBackReference() throws Exception {
    // given
    final JsonManagedReferenceDTO dto = new JsonManagedReferenceDTO();

    final Item item = new Item();
    item.setItemName("Item1");
    item.setUser(dto);

    dto.getItems().add(item);

    // when
    final ObjectMapper mapper = new ObjectMapper();
    final String jsonJackson = mapper.writeValueAsString(dto);

    final String jsonFastjson2 = JSON.toJSONString(dto);

    // then
    assertThat(jsonJackson).isEqualTo(jsonFastjson2);
  }

  static class JsonManagedReferenceDTO {
    String id;

    @JsonManagedReference
    private List<Item> items = new ArrayList<>();

    public String getId() {
      return this.id;
    }

    public void setId(final String id) {
      this.id = id;
    }

    public List<Item> getItems() {
      return this.items;
    }

    public void setItems(final List<Item> items) {
      this.items = items;
    }

    static class Item {

      private String itemName;

      @JsonBackReference
      private JsonManagedReferenceDTO user;

      public String getItemName() {
        return this.itemName;
      }

      public void setItemName(final String itemName) {
        this.itemName = itemName;
      }

      public JsonManagedReferenceDTO getUser() {
        return this.user;
      }

      public void setUser(final JsonManagedReferenceDTO user) {
        this.user = user;
      }
    }
  }
}

I have tried adding

 String jsonFastjson2 = JSON.toJSONString(dto, Feature.ReferenceDetection); and the result is not correct either:

Result FastJson2

{"items":[{"itemName":"Item1","user":{"$ref":"$"}}]}

Result Jackson

{"id":null,"items":[{"itemName":"Item1"}]}

Thank you very much

@jorgerod jorgerod added the bug Something isn't working label May 29, 2024
@wenshao wenshao added this to the 2.0.51 milestone May 29, 2024
@wenshao
Copy link
Member

wenshao commented May 29, 2024

https://oss.sonatype.org/content/repositories/snapshots/com/alibaba/fastjson2/fastjson2/2.0.51-SNAPSHOT/

The BUG has been fixed. Please help verify it with version 2.0.51-SNAPSHOT

@wenshao wenshao added the fixed label May 29, 2024
@jorgerod
Copy link
Author

Fixed! Thank you very much

@wenshao
Copy link
Member

wenshao commented Jun 1, 2024

https://github.com/alibaba/fastjson2/releases/tag/2.0.51
Please use the new version 2.0.51

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working fixed
Projects
None yet
Development

No branches or pull requests

2 participants