Skip to content

Commit

Permalink
Support nested & fake additional properties (Azure#9624)
Browse files Browse the repository at this point in the history
* Support nested & fake additional properties

* Use @JsonAnySetter & @JsonAnyGetter

* Remove special handling in deserializer
  • Loading branch information
jianghaolu authored Apr 2, 2020
1 parent de84f8b commit aebc0b0
Show file tree
Hide file tree
Showing 6 changed files with 334 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.jsontype.TypeSerializer;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.fasterxml.jackson.databind.ser.BeanSerializerModifier;
import com.fasterxml.jackson.databind.ser.ResolvableSerializer;
Expand All @@ -23,7 +24,10 @@
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Queue;
import java.util.concurrent.LinkedBlockingQueue;

/**
* Custom serializer for serializing complex types with additional properties.
Expand Down Expand Up @@ -91,30 +95,47 @@ public JsonSerializer<?> modifySerializer(SerializationConfig config, BeanDescri

@Override
public void serialize(Object value, JsonGenerator jgen, SerializerProvider provider) throws IOException {
// serialize the original object into JsonNode
ObjectNode root = mapper.valueToTree(value);
// take additional properties node out
Entry<String, JsonNode> additionalPropertiesField = null;
Iterator<Entry<String, JsonNode>> fields = root.fields();
while (fields.hasNext()) {
Entry<String, JsonNode> field = fields.next();
if ("additionalProperties".equalsIgnoreCase(field.getKey())) {
additionalPropertiesField = field;
break;
}
}
if (additionalPropertiesField != null) {
root.remove(additionalPropertiesField.getKey());
// put each item back in
ObjectNode extraProperties = (ObjectNode) additionalPropertiesField.getValue();
fields = extraProperties.fields();
ObjectNode res = root.deepCopy();
Queue<ObjectNode> source = new LinkedBlockingQueue<ObjectNode>();
Queue<ObjectNode> target = new LinkedBlockingQueue<ObjectNode>();
source.add(root);
target.add(res);
while (!source.isEmpty()) {
ObjectNode current = source.poll();
ObjectNode resCurrent = target.poll();
Iterator<Map.Entry<String, JsonNode>> fields = current.fields();
while (fields.hasNext()) {
Entry<String, JsonNode> field = fields.next();
root.put(field.getKey(), field.getValue());
Map.Entry<String, JsonNode> field = fields.next();
String key = field.getKey();
JsonNode outNode = resCurrent.get(key);
if ("additionalProperties".equals(key)) {
// Handle additional properties
resCurrent.remove(key);
// put each item back in
ObjectNode extraProperties = (ObjectNode) outNode;
Iterator<Map.Entry<String, JsonNode>> additionalFields = extraProperties.fields();
while (additionalFields.hasNext()) {
Entry<String, JsonNode> additionalField = additionalFields.next();
resCurrent.put(additionalField.getKey(), additionalField.getValue());
}
}
if (field.getValue() instanceof ObjectNode) {
source.add((ObjectNode) field.getValue());
target.add((ObjectNode) outNode);
} else if (field.getValue() instanceof ArrayNode
&& (field.getValue()).size() > 0
&& (field.getValue()).get(0) instanceof ObjectNode) {
Iterator<JsonNode> sourceIt = field.getValue().elements();
Iterator<JsonNode> targetIt = outNode.elements();
while (sourceIt.hasNext()) {
source.add((ObjectNode) sourceIt.next());
target.add((ObjectNode) targetIt.next());
}
}
}
}

jgen.writeTree(root);
jgen.writeTree(res);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,12 @@ public Object deserialize(JsonParser jp, DeserializationContext ctxt) throws IOE
break;
}
}
((ObjectNode) root).put(value, node);
// If additional properties have a conflicting key, escape the additional property's key
if (root.has(value)) {
String escapedValue = value.replace(".", "\\.");
((ObjectNode) root).set(escapedValue, root.get(value));
}
((ObjectNode) root).set(value, node);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,30 @@ public void canDeserializeAdditionalPropertiesThroughInheritance() throws Except
Assertions.assertEquals("barbar", deserialized.additionalProperties().get("properties.bar"));
Assertions.assertTrue(deserialized instanceof FooChild);
}

@Test
public void canSerializeAdditionalPropertiesWithNestedAdditionalProperties() throws Exception {
Foo foo = new Foo();
foo.bar("hello.world");
foo.baz(new ArrayList<>());
foo.baz().add("hello");
foo.baz().add("hello.world");
foo.qux(new HashMap<>());
foo.qux().put("hello", "world");
foo.qux().put("a.b", "c.d");
foo.qux().put("bar.a", "ttyy");
foo.qux().put("bar.b", "uuzz");
foo.additionalProperties(new HashMap<>());
foo.additionalProperties().put("bar", "baz");
foo.additionalProperties().put("a.b", "c.d");
foo.additionalProperties().put("properties.bar", "barbar");
Foo nestedFoo = new Foo();
nestedFoo.bar("bye.world");
nestedFoo.additionalProperties(new HashMap<>());
nestedFoo.additionalProperties().put("name", "Sushi");
foo.additionalProperties().put("foo", nestedFoo);

String serialized = new JacksonAdapter().serialize(foo, SerializerEncoding.JSON);
Assertions.assertEquals("{\"$type\":\"foo\",\"properties\":{\"bar\":\"hello.world\",\"props\":{\"baz\":[\"hello\",\"hello.world\"],\"q\":{\"qux\":{\"hello\":\"world\",\"a.b\":\"c.d\",\"bar.b\":\"uuzz\",\"bar.a\":\"ttyy\"}}}},\"bar\":\"baz\",\"foo\":{\"properties\":{\"bar\":\"bye.world\"},\"name\":\"Sushi\"},\"a.b\":\"c.d\",\"properties.bar\":\"barbar\"}", serialized);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

package com.azure.core.util.serializer;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import java.util.ArrayList;
import java.util.HashMap;

public class AdditionalPropertiesSerializerWithJacksonAnnotationTests {
@Test
public void canSerializeAdditionalProperties() throws Exception {
NewFoo foo = new NewFoo();
foo.bar("hello.world");
foo.baz(new ArrayList<>());
foo.baz().add("hello");
foo.baz().add("hello.world");
foo.qux(new HashMap<>());
foo.qux().put("hello", "world");
foo.qux().put("a.b", "c.d");
foo.qux().put("bar.a", "ttyy");
foo.qux().put("bar.b", "uuzz");
foo.additionalProperties(new HashMap<>());
foo.additionalProperties().put("bar", "baz");
foo.additionalProperties().put("a.b", "c.d");
foo.additionalProperties().put("properties.bar", "barbar");

String serialized = new JacksonAdapter().serialize(foo, SerializerEncoding.JSON);
Assertions.assertEquals("{\"$type\":\"newfoo\",\"bar\":\"baz\",\"properties\":{\"bar\":\"hello.world\",\"props\":{\"baz\":[\"hello\",\"hello.world\"],\"q\":{\"qux\":{\"hello\":\"world\",\"a.b\":\"c.d\",\"bar.b\":\"uuzz\",\"bar.a\":\"ttyy\"}}}},\"a.b\":\"c.d\",\"properties.bar\":\"barbar\"}", serialized);
}

@Test
public void canDeserializeAdditionalProperties() throws Exception {
String wireValue = "{\"$type\":\"newfoo\",\"properties\":{\"bar\":\"hello.world\",\"props\":{\"baz\":[\"hello\",\"hello.world\"],\"q\":{\"qux\":{\"hello\":\"world\",\"a.b\":\"c.d\",\"bar.b\":\"uuzz\",\"bar.a\":\"ttyy\"}}}},\"bar\":\"baz\",\"a.b\":\"c.d\",\"properties.bar\":\"barbar\"}";
NewFoo deserialized = new JacksonAdapter().deserialize(wireValue, NewFoo.class, SerializerEncoding.JSON);
Assertions.assertNotNull(deserialized.additionalProperties());
Assertions.assertEquals("baz", deserialized.additionalProperties().get("bar"));
Assertions.assertEquals("c.d", deserialized.additionalProperties().get("a.b"));
Assertions.assertEquals("barbar", deserialized.additionalProperties().get("properties.bar"));
}

@Test
public void canSerializeAdditionalPropertiesThroughInheritance() throws Exception {
NewFoo foo = new NewFooChild();
foo.bar("hello.world");
foo.baz(new ArrayList<>());
foo.baz().add("hello");
foo.baz().add("hello.world");
foo.qux(new HashMap<>());
foo.qux().put("hello", "world");
foo.qux().put("a.b", "c.d");
foo.qux().put("bar.a", "ttyy");
foo.qux().put("bar.b", "uuzz");
foo.additionalProperties(new HashMap<>());
foo.additionalProperties().put("bar", "baz");
foo.additionalProperties().put("a.b", "c.d");
foo.additionalProperties().put("properties.bar", "barbar");

String serialized = new JacksonAdapter().serialize(foo, SerializerEncoding.JSON);
Assertions.assertEquals("{\"$type\":\"newfoochild\",\"bar\":\"baz\",\"properties\":{\"bar\":\"hello.world\",\"props\":{\"baz\":[\"hello\",\"hello.world\"],\"q\":{\"qux\":{\"hello\":\"world\",\"a.b\":\"c.d\",\"bar.b\":\"uuzz\",\"bar.a\":\"ttyy\"}}}},\"a.b\":\"c.d\",\"properties.bar\":\"barbar\"}", serialized);
}

@Test
public void canDeserializeAdditionalPropertiesThroughInheritance() throws Exception {
String wireValue = "{\"$type\":\"newfoochild\",\"properties\":{\"bar\":\"hello.world\",\"props\":{\"baz\":[\"hello\",\"hello.world\"],\"q\":{\"qux\":{\"hello\":\"world\",\"a.b\":\"c.d\",\"bar.b\":\"uuzz\",\"bar.a\":\"ttyy\"}}}},\"bar\":\"baz\",\"a.b\":\"c.d\",\"properties.bar\":\"barbar\"}";
NewFoo deserialized = new JacksonAdapter().deserialize(wireValue, NewFoo.class, SerializerEncoding.JSON);
Assertions.assertNotNull(deserialized.additionalProperties());
Assertions.assertEquals("baz", deserialized.additionalProperties().get("bar"));
Assertions.assertEquals("c.d", deserialized.additionalProperties().get("a.b"));
Assertions.assertEquals("barbar", deserialized.additionalProperties().get("properties.bar"));
Assertions.assertTrue(deserialized instanceof NewFooChild);
}

@Test
public void canSerializeAdditionalPropertiesWithNestedAdditionalProperties() throws Exception {
NewFoo foo = new NewFoo();
foo.bar("hello.world");
foo.baz(new ArrayList<>());
foo.baz().add("hello");
foo.baz().add("hello.world");
foo.qux(new HashMap<>());
foo.qux().put("hello", "world");
foo.qux().put("a.b", "c.d");
foo.qux().put("bar.a", "ttyy");
foo.qux().put("bar.b", "uuzz");
foo.additionalProperties(new HashMap<>());
foo.additionalProperties().put("bar", "baz");
foo.additionalProperties().put("a.b", "c.d");
foo.additionalProperties().put("properties.bar", "barbar");
NewFoo nestedNewFoo = new NewFoo();
nestedNewFoo.bar("bye.world");
nestedNewFoo.additionalProperties(new HashMap<>());
nestedNewFoo.additionalProperties().put("name", "Sushi");
foo.additionalProperties().put("foo", nestedNewFoo);

String serialized = new JacksonAdapter().serialize(foo, SerializerEncoding.JSON);
Assertions.assertEquals("{\"$type\":\"newfoo\",\"bar\":\"baz\",\"foo\":{\"name\":\"Sushi\",\"properties\":{\"bar\":\"bye.world\"}},\"properties\":{\"bar\":\"hello.world\",\"props\":{\"baz\":[\"hello\",\"hello.world\"],\"q\":{\"qux\":{\"hello\":\"world\",\"a.b\":\"c.d\",\"bar.b\":\"uuzz\",\"bar.a\":\"ttyy\"}}}},\"a.b\":\"c.d\",\"properties.bar\":\"barbar\"}", serialized);
}

@Test
public void canSerializeAdditionalPropertiesWithConflictProperty() throws Exception {
NewFoo foo = new NewFoo();
foo.bar("hello.world");
foo.baz(new ArrayList<>());
foo.baz().add("hello");
foo.baz().add("hello.world");
foo.qux(new HashMap<>());
foo.qux().put("hello", "world");
foo.qux().put("a.b", "c.d");
foo.qux().put("bar.a", "ttyy");
foo.qux().put("bar.b", "uuzz");
foo.additionalProperties(new HashMap<>());
foo.additionalProperties().put("bar", "baz");
foo.additionalProperties().put("a.b", "c.d");
foo.additionalProperties().put("properties.bar", "barbar");
foo.additionalPropertiesProperty(new HashMap<>());
foo.additionalPropertiesProperty().put("age", 73);

String serialized = new JacksonAdapter().serialize(foo, SerializerEncoding.JSON);
Assertions.assertEquals("{\"$type\":\"newfoo\",\"additionalProperties\":{\"age\":73},\"bar\":\"baz\",\"properties\":{\"bar\":\"hello.world\",\"props\":{\"baz\":[\"hello\",\"hello.world\"],\"q\":{\"qux\":{\"hello\":\"world\",\"a.b\":\"c.d\",\"bar.b\":\"uuzz\",\"bar.a\":\"ttyy\"}}}},\"a.b\":\"c.d\",\"properties.bar\":\"barbar\"}", serialized);
}

@Test
public void canDeserializeAdditionalPropertiesWithConflictProperty() throws Exception {
String wireValue = "{\"$type\":\"newfoo\",\"properties\":{\"bar\":\"hello.world\",\"props\":{\"baz\":[\"hello\",\"hello.world\"],\"q\":{\"qux\":{\"hello\":\"world\",\"a.b\":\"c.d\",\"bar.b\":\"uuzz\",\"bar.a\":\"ttyy\"}}}},\"bar\":\"baz\",\"a.b\":\"c.d\",\"properties.bar\":\"barbar\",\"additionalProperties\":{\"age\":73}}";
NewFoo deserialized = new JacksonAdapter().deserialize(wireValue, NewFoo.class, SerializerEncoding.JSON);
Assertions.assertNotNull(deserialized.additionalProperties());
Assertions.assertEquals("baz", deserialized.additionalProperties().get("bar"));
Assertions.assertEquals("c.d", deserialized.additionalProperties().get("a.b"));
Assertions.assertEquals("barbar", deserialized.additionalProperties().get("properties.bar"));
Assertions.assertEquals(1, deserialized.additionalPropertiesProperty().size());
Assertions.assertEquals(73, deserialized.additionalPropertiesProperty().get("age"));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

package com.azure.core.util.serializer;

import com.azure.core.annotation.JsonFlatten;
import com.fasterxml.jackson.annotation.JsonAnyGetter;
import com.fasterxml.jackson.annotation.JsonAnySetter;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonSubTypes;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.annotation.JsonTypeName;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* Class for testing serialization.
*/
@JsonFlatten
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "$type")
@JsonTypeName("newfoo")
@JsonSubTypes({
@JsonSubTypes.Type(name = "newfoochild", value = NewFooChild.class)
})
public class NewFoo {
@JsonProperty(value = "properties.bar")
private String bar;
@JsonProperty(value = "properties.props.baz")
private List<String> baz;
@JsonProperty(value = "properties.props.q.qux")
private Map<String, String> qux;
@JsonProperty(value = "properties.more\\.props")
private String moreProps;
@JsonProperty(value = "props.empty")
private Integer empty;
@JsonIgnore
private Map<String, Object> additionalProperties;
@JsonProperty(value = "additionalProperties")
private Map<String, Object> additionalPropertiesProperty;

public String bar() {
return bar;
}

public void bar(String bar) {
this.bar = bar;
}

public List<String> baz() {
return baz;
}

public void baz(List<String> baz) {
this.baz = baz;
}

public Map<String, String> qux() {
return qux;
}

public void qux(Map<String, String> qux) {
this.qux = qux;
}

public String moreProps() {
return moreProps;
}

public void moreProps(String moreProps) {
this.moreProps = moreProps;
}

public Integer empty() {
return empty;
}

public void empty(Integer empty) {
this.empty = empty;
}

@JsonAnySetter
private void additionalProperties(String key, Object value) {
if (additionalProperties == null) {
additionalProperties = new HashMap<>();
}
additionalProperties.put(key.replace("\\.", "."), value);
}

@JsonAnyGetter
public Map<String, Object> additionalProperties() {
return additionalProperties;
}

public void additionalProperties(Map<String, Object> additionalProperties) {
this.additionalProperties = additionalProperties;
}

public Map<String, Object> additionalPropertiesProperty() {
return additionalPropertiesProperty;
}

public void additionalPropertiesProperty(Map<String, Object> additionalPropertiesProperty) {
this.additionalPropertiesProperty = additionalPropertiesProperty;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

package com.azure.core.util.serializer;

import com.azure.core.annotation.JsonFlatten;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.annotation.JsonTypeName;

/**
* Class for testing serialization.
*/
@JsonFlatten
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "$type")
@JsonTypeName("newfoochild")
public class NewFooChild extends NewFoo {
}

0 comments on commit aebc0b0

Please sign in to comment.