spring-projects / spring-data-rest

Simplifies building hypermedia-driven REST web services on top of Spring Data repositories
https://spring.io/projects/spring-data-rest
Apache License 2.0
919 stars 563 forks source link

PersistentEntityJackson2Module throws JsonGenerationException when serializing Entity with ObjectId [DATAREST-853] #1224

Open spring-projects-issues opened 8 years ago

spring-projects-issues commented 8 years ago

Jeff Quandt opened DATAREST-853 and commented

Minimal reproduction project attached.

This was originally submitted as a Jackson bug, but redirected to here. I see DATAREST-697, DATAREST-716 and other similar issues were fixed in 2.4 or 2.5.1. I was able to reproduce my issue with 2.5.2.RELEASE.

See linked Jackson issue for full stack and use case data (or attached StackTrace.txt, Example Json.txt). Summary below.

Issue submission requested by Tatu from jackson-user list question: https://groups.google.com/forum/#!topic/jackson-user/GOj0uGn_nTg

I'm using Jackson 2.8.0, Spring Boot 1.3.6, Spring Data REST 2.5.2.RELEASE to serialize some JPA Entities over a REST API with Spring Data REST. I'm having a problem with serializing an Array of Entities that contain references to the same child object. All objects are using JsonIdentityInfo as below, where the key property is a Long @Id field.

@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "key", scope = MyClass.class)

If I remove the JsonIdentityInfo, I get the results back correctly. However, there are no Object references, and the full child object is serialized every time it is referenced. I'm concerned that this "fix" is not effective, because I originally added the JsonIdentityInfo annotations to get around some recursive relationships (ORM bi-directional stuff). If I have to remove them every time I might refer to the same instance in a result set, what is the point of the JsonIdentityInfo annotation in the first place?

I'm sure I'm just applying something incorrectly, but I'm not able to find documentation on what I may be missing. Do I need a custom serializer to make use of this feature?

Attaching minimal example project. Extract and execute gradlew bootRun. Navigate to localhost:8080/company to see the bug. Full Stack trace is not logged by default and would require debugging or custom exception handler to be applied. Error message is visible on console. Navigating to individual objects (e.g. /company/1) works correctly.

To validate /company works without the duplicate child object, edit rest-api/src/main/resources/data.sql and either set SUPPORT_EMAIL_ADDRESS null or to 3 for company 1 or company 3 and restart the application.

Ex: 2016-07-14T13:50:49,734 WARN [mvc.support.DefaultHandlerExceptionResolver] [http-nio-8080-exec-2] Failed to write HTTP message: org.springframework.http.converter.HttpMessageNotWritableException: Could not write content: Can not write a number, expecting field name; nested exception is com.fasterxml.jackson.core.JsonGenerationException: Can not write a number, expecting field name


Affects: 2.5.2 (Hopper SR2)

Reference URL: https://github.com/FasterXML/jackson-databind/issues/1298

Attachments:

spring-projects-issues commented 8 years ago

Jeff Quandt commented

Attaching additional example data

spring-projects-issues commented 8 years ago

Jeff Quandt commented

I'm not sure how to cancel this. Further research has shown this is a Jackson bug. No change is required in Spring Data Rest other than updating the Jackson dependency whenever the fix is applied to the library

spring-projects-issues commented 8 years ago

Oliver Drotbohm commented

I can close it. Do you have a reference to the bug?

spring-projects-issues commented 8 years ago

Oliver Drotbohm commented

Never mind, I found the link up there 🙃