Skip to content

Commit

Permalink
Avoid manually handling additional primitive elements in collection m…
Browse files Browse the repository at this point in the history
…erging for PATCHes.

Fixes #2261.
  • Loading branch information
odrotbohm committed May 23, 2023
1 parent e9c43f1 commit d6d8c50
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -352,10 +352,7 @@ private boolean handleArrayNode(ArrayNode array, Collection<Object> collection,
for (JsonNode jsonNode : array) {

if (!value.hasNext()) {

collection.add(mapper.treeToValue(jsonNode, getTypeToMap(null, componentType).getType()));

continue;
break;
}

Object next = value.next();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
import com.fasterxml.jackson.annotation.JsonSubTypes;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.annotation.JsonTypeInfo.As;
import com.fasterxml.jackson.core.JacksonException;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationContext;
Expand All @@ -66,6 +67,8 @@
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.PropertyNamingStrategy;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.node.ObjectNode;

Expand Down Expand Up @@ -109,6 +112,7 @@ void setUp() {
mappingContext.getPersistentEntity(ArrayHolder.class);
mappingContext.getPersistentEntity(Apple.class);
mappingContext.getPersistentEntity(Pear.class);
mappingContext.getPersistentEntity(WithCustomMappedPrimitiveCollection.class);
mappingContext.afterPropertiesSet();

this.entities = new PersistentEntities(Collections.singleton(mappingContext));
Expand Down Expand Up @@ -645,6 +649,21 @@ void nestedEntitiesWithReadonlyFieldAreKeptForPut() throws Exception {
assertThat(result.inner).isSameAs(inner);
}

@Test // GH-2261
void deserializesCustomCollectionOfPrimitives() throws Exception {

var node = new ObjectMapper().readTree("""
{ "longs" : [ "foo:1", "bar:2" ] }
""");

var collection = new WithCustomMappedPrimitiveCollection();
collection.longs = List.of(3L);

var result = reader.doMerge((ObjectNode) node, collection, new ObjectMapper());

assertThat(result.longs).isEqualTo(List.of(1L, 2L));
}

@SuppressWarnings("unchecked")
private static <T> T as(Object source, Class<T> type) {

Expand Down Expand Up @@ -900,4 +919,31 @@ static class Apple extends Fruit {
static class Pear extends Fruit {
String pear;
}

// GH-2261
static class WithCustomMappedPrimitiveCollection {

@JsonDeserialize(contentUsing = CustomDeserializer.class) //
List<Long> longs;

@SuppressWarnings("serial")
static class CustomDeserializer extends StdDeserializer<Long> {

protected CustomDeserializer() {
super(Long.class);
}

/*
* (non-Javadoc)
* @see com.fasterxml.jackson.databind.JsonDeserializer#deserialize(com.fasterxml.jackson.core.JsonParser, com.fasterxml.jackson.databind.DeserializationContext)
*/
@Override
public Long deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JacksonException {

var elements = p.getText().split(":");

return Long.valueOf(elements[elements.length - 1]);
}
}
}
}

0 comments on commit d6d8c50

Please sign in to comment.