forked from Azure/azure-sdk-for-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support nested & fake additional properties (Azure#9624)
* Support nested & fake additional properties * Use @JsonAnySetter & @JsonAnyGetter * Remove special handling in deserializer
- Loading branch information
1 parent
de84f8b
commit aebc0b0
Showing
6 changed files
with
334 additions
and
21 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
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
136 changes: 136 additions & 0 deletions
136
.../azure/core/util/serializer/AdditionalPropertiesSerializerWithJacksonAnnotationTests.java
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 |
---|---|---|
@@ -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")); | ||
} | ||
} |
108 changes: 108 additions & 0 deletions
108
sdk/core/azure-core/src/test/java/com/azure/core/util/serializer/NewFoo.java
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 |
---|---|---|
@@ -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; | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
sdk/core/azure-core/src/test/java/com/azure/core/util/serializer/NewFooChild.java
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 |
---|---|---|
@@ -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 { | ||
} |