-
Notifications
You must be signed in to change notification settings - Fork 1.1k
GeoJson: Improper Deserialization of Document with a GeoJsonPolygon [DATAMONGO-2664] #3517
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
Comments
Bjorn Harvold commented I wanted to follow up on this with another unit test. If I modify the existing GeoJsonPolygon test, in GeoJsonModuleUnitTests, slightly and try to serialize it back to string JSON, it also fails. @Test
public void shouldSerializeGeoJsonPolygonCorrectly() throws IOException {
String json = "{ \"type\": \"Polygon\", \"coordinates\": [ [ [100.0, 0.0], [101.0, 0.0], [101.0, 1.0], [100.0, 1.0], [100.0, 0.0] ] ]}";
List<Point> points = Arrays.asList(new Point(100, 0), new Point(101, 0), new Point(101, 1), new Point(100, 1), new Point(100, 0));
GeoJsonPolygon polygon = new GeoJsonPolygon(points);
assertThat(mapper.readValue(json, GeoJsonPolygon.class)).isEqualTo(polygon);
assertThat(mapper.writeValueAsString(polygon)).isEqualTo(json);
} Results: Expected :"{ "type": "Polygon", "coordinates": [ [ [100.0, 0.0], [101.0, 0.0], [101.0, 1.0], [100.0, 1.0], [100.0, 0.0] ] ]}"
Actual :"{"points":[{"x":100.0,"y":0.0},{"x":101.0,"y":0.0},{"x":101.0,"y":1.0},{"x":100.0,"y":1.0},{"x":100.0,"y":0.0}],"coordinates":[{"type":"LineString","coordinates":[{"x":100.0,"y":0.0},{"x":101.0,"y":0.0},{"x":101.0,"y":1.0},{"x":100.0,"y":1.0},{"x":100.0,"y":0.0}]}],"type":"Polygon"}"
|
Bjorn Harvold commented The issue with this is I need to keep 2 separate data models for Spring Web MVC GET and POST. In other words, it won't let me submit the same payload I received from the server back to the server. The server will require proper GeoJson |
Bjorn Harvold commented More than happy to contribute time to fix this if it will help and if it speeds up the release / patch process |
Christoph Strobl commented Thanks for bringing this up! The You might want to add those to Jackson for now to work around that issue. In case you've got time to open a PR including the required `JsonSerializer`s we'd be happy to review it |
Suggestion:
With accompanying tests:
Notice that I changed the json for MultiPolygon as your original test had set the type to "Polygon" and the 3 polygons had some weirdly nested 2nd and 3rd polygon. I don't know if that was intentional but I found it impossible to serialize with the data I was given from GeoJsonMultiPolygon. |
Thanks @bjornharvold care to create a PR with your changes? |
I'll leave it here for your team to figure out how you want to integrate it. Some projects might not want to use it automatically when the next upgrade from spring data mongodb occurs. So renaming the current module to GeoJsonDeserializersModule() and create a new one based on the code above in a GeoJsonSerializersModule() would probably make sense to most. Anyway, I leave it to you. We are currently using the code above in production without any issues for a two weeks now. Cheers, |
We'd take care of integrating the PR addressing the issues you pointed out. A PR would make sure your contribution is linked to your GH profile and that it follows the CLA. |
Sure thing @christophstrobl. I'll create a PR for you guys. I've already signed your CLA. |
This commit reduces the visibility of the GeoJsonSerializersModule and instead offers static methods on GeoJsonModule allowing to obtain those via a static method. The actual serialization was simpified by moving some of its code to a common base class. Updated reference documentation - relates to: spring-projects/spring-data-commons#2288 We also did, by intent, not change the current GeoJsonModule to add the serializers by default in order to give users time to prepare for that change which will be done with the next major release. Original pull request: #3539. Closes #3517
We've backported the documentation about registering only GeoJson serializers to 3.1.x and 3.0.x. |
Hi @mp911de @christophstrobl @bjornharvold , I'm using the latest version of Spring Data MongoDB Reactive (2.7.5), and I'm getting this exact same serialization issue (where it's returning an array of points with x, y coordinates, instead of the proper coordinates array):
Could you possibly advise me on how to implement this serializer? |
Bjorn Harvold opened DATAMONGO-2664 and commented
I'm trying to attach screenshots but getting "Try again. Invalid token". Will try to attach after I create the ticket.
The existing test that accompanies Spring Data MongoDb is in the class called GeoJsonModuleUnitTests:
And that passes just fine.
However, change those points to GeoJsonPoint objects and it fails:
And that is exactly what happens when I query MongoDb (simple findById()) for a document with Spring Data MongoDb. When I look in the object graph, I see GeoJsonPoint objects as coordinates in my Polygon.
When the Jackson ObjectMapper serializes that data, it looks like this:
Please advise,
Bjorn
Affects: 3.1.1 (2020.0.1)
Attachments:
The text was updated successfully, but these errors were encountered: