Skip to content
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

Add more tests to azure-core-version-tests #35401

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,22 @@
package com.azure.core.serializer.json.jackson.implementation;

import com.azure.core.util.logging.ClientLogger;
import com.fasterxml.jackson.databind.BeanDescription;
import com.fasterxml.jackson.databind.DeserializationConfig;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.cfg.CoercionAction;
import com.fasterxml.jackson.databind.cfg.CoercionInputShape;
import com.fasterxml.jackson.databind.cfg.MapperConfig;
import com.fasterxml.jackson.databind.deser.BeanDeserializerModifier;
import com.fasterxml.jackson.databind.deser.std.DelegatingDeserializer;
import com.fasterxml.jackson.databind.introspect.AccessorNamingStrategy;
import com.fasterxml.jackson.databind.introspect.AnnotatedClass;
import com.fasterxml.jackson.databind.introspect.AnnotatedMethod;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.util.AccessPattern;

/**
* Utility methods for Jackson Databind types when it's known that the version is 2.12+.
Expand All @@ -27,6 +36,36 @@ final class JacksonDatabind212 {
* @return The updated {@link ObjectMapper}.
*/
static ObjectMapper mutateXmlCoercions(ObjectMapper mapper) {
// https://github.com/FasterXML/jackson-dataformat-xml/pull/585/files fixed array and collection elements
// with coercion to be handled by the coercion config below which is a backwards compatibility breaking
// change for us. Handle empty string items within an array or collection as empty string.
mapper.registerModule(new SimpleModule().setDeserializerModifier(new BeanDeserializerModifier() {
@Override
public JsonDeserializer<?> modifyDeserializer(DeserializationConfig config, BeanDescription beanDesc,
JsonDeserializer<?> deserializer) {
if (String.class.isAssignableFrom(beanDesc.getBeanClass())) {
return new DelegatingDeserializer(deserializer) {
@Override
protected JsonDeserializer<?> newDelegatingInstance(JsonDeserializer<?> newDelegatee) {
return this;
}

@Override
public AccessPattern getNullAccessPattern() {
return AccessPattern.DYNAMIC;
}

@Override
public Object getNullValue(DeserializationContext ctxt) throws JsonMappingException {
return (ctxt.getParser().getParsingContext().inArray()) ? "" : super.getNullValue(ctxt);
}
};
} else {
return deserializer;
}
}
}));

mapper.coercionConfigDefaults().setCoercion(CoercionInputShape.EmptyString, CoercionAction.AsNull);
return mapper;
}
Expand Down
5 changes: 5 additions & 0 deletions sdk/core/azure-core-version-tests/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,11 @@
<scope>provided</scope>
</dependency>

<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-core-test</artifactId>
<version>1.19.0-beta.1</version> <!-- {x-version-update;com.azure:azure-core-test;current} -->
</dependency>
<dependency>
<groupId>io.projectreactor</groupId>
<artifactId>reactor-test</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

package com.azure.core.version.tests;

import com.azure.core.util.serializer.JacksonAdapter;
import com.azure.core.util.serializer.SerializerEncoding;
import com.azure.core.version.tests.models.Foo;
import com.azure.core.version.tests.models.FooChild;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

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

public class AdditionalPropertiesSerializerTests {
@Test
public void canSerializeAdditionalProperties() 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");

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\",\"a.b\":\"c.d\",\"properties.bar\":\"barbar\"}", serialized);
}

@Test
public void canDeserializeAdditionalProperties() throws Exception {
String wireValue = "{\"$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\",\"a.b\":\"c.d\",\"properties.bar\":\"barbar\"}";
Foo deserialized = new JacksonAdapter().deserialize(wireValue, Foo.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 {
Foo foo = new FooChild();
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\":\"foochild\",\"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\"}", serialized);
}

@Test
public void canDeserializeAdditionalPropertiesThroughInheritance() throws Exception {
String wireValue = "{\"$type\":\"foochild\",\"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\"}";
Foo deserialized = new JacksonAdapter().deserialize(wireValue, Foo.class, SerializerEncoding.JSON);
// Check additional properties are populated
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 FooChild);
// Check typed properties are populated
Assertions.assertEquals("hello.world", deserialized.bar());
Assertions.assertNotNull(deserialized.baz());
Assertions.assertEquals(2, deserialized.baz().size());
Assertions.assertTrue(deserialized.baz().contains("hello"));
Assertions.assertTrue(deserialized.baz().contains("hello.world"));
Assertions.assertNotNull(deserialized.qux());
Assertions.assertEquals(4, deserialized.qux().size());
Assertions.assertTrue(deserialized.qux().containsKey("hello"));
Assertions.assertTrue(deserialized.qux().containsKey("a.b"));
Assertions.assertTrue(deserialized.qux().containsKey("bar.a"));
Assertions.assertTrue(deserialized.qux().containsKey("bar.b"));
Assertions.assertEquals("world", deserialized.qux().get("hello"));
Assertions.assertEquals("c.d", deserialized.qux().get("a.b"));
Assertions.assertEquals("ttyy", deserialized.qux().get("bar.a"));
Assertions.assertEquals("uuzz", deserialized.qux().get("bar.b"));
}

@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,140 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

package com.azure.core.version.tests;

import com.azure.core.util.serializer.JacksonAdapter;
import com.azure.core.util.serializer.SerializerEncoding;
import com.azure.core.version.tests.models.NewFoo;
import com.azure.core.version.tests.models.NewFooChild;
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"));
}
}
Loading