-
Notifications
You must be signed in to change notification settings - Fork 563
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Reinstate new object addition for collections in patch operations.
The removal of manual collection value appendance for primitives in the context of GH-2261 lead to complex object to be appended onto a collection not being deserialized at all. This deserialization is now reinstantiated with the value to be added looked up by reading the parent node into the collections element type using a JSON Pointer expression of /$propertyName/$index. This is done to make sure that @JsonDeserialize annotations on the property kick in for the deserialization of the individual elements. We now also shortcut the entire merge algorithm for collections that are empty, contain primitives or enums. Fixes GH-2287.
- Loading branch information
Showing
2 changed files
with
76 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -107,6 +107,7 @@ void setUp() { | |
mappingContext.getPersistentEntity(Apple.class); | ||
mappingContext.getPersistentEntity(Pear.class); | ||
mappingContext.getPersistentEntity(WithCustomMappedPrimitiveCollection.class); | ||
mappingContext.getPersistentEntity(BugModel.class); | ||
mappingContext.afterPropertiesSet(); | ||
|
||
this.entities = new PersistentEntities(Collections.singleton(mappingContext)); | ||
|
@@ -678,6 +679,26 @@ void nestedEntitiesAreCreatedWhenMissingForPut() throws Exception { | |
assertThat(result.inner.hidden).isNull(); | ||
} | ||
|
||
@Test | ||
void deserializesNewNestedEntitiesCorrectly() throws Exception { | ||
|
||
var mapper = new ObjectMapper(); | ||
var node = mapper.readTree("{ \"list\" : [ { \"value\" : \"Foo\" }, { \"value\" : \"Bar\" }] }"); | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
beardy247
|
||
|
||
var nested = new BugModel.NestedModel(); | ||
nested.value = "FooBar"; | ||
|
||
var model = new BugModel(); | ||
model.list = new ArrayList<>(); | ||
model.list.add(nested); | ||
|
||
var result = reader.doMerge((ObjectNode) node, model, mapper); | ||
|
||
assertThat(result.list) | ||
.extracting(it -> it.value) | ||
.containsExactly("Foo", "Bar"); | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
private static <T> T as(Object source, Class<T> type) { | ||
|
||
|
@@ -1040,4 +1061,14 @@ public Long deserialize(JsonParser p, DeserializationContext ctxt) throws IOExce | |
} | ||
} | ||
} | ||
|
||
// GH-2287 | ||
static class BugModel { | ||
|
||
public List<NestedModel> list; | ||
|
||
static class NestedModel { | ||
public String value; | ||
} | ||
} | ||
} |
If you make this
"{ \"list\" : [{ \"value\" : \"Foo\" }, { \"value\" : \"Bar\" }, { \"value\" : \"Baz\" }] }"
and expect
.containsExactly("Foo", "Bar", "Baz")
I believe the test will fail.
I think there's still a bug handling more than one new objects in the collection.