From 7412384fe635441dbf5fbfff458b8d2c5b847ba7 Mon Sep 17 00:00:00 2001 From: RanVaknin <50976344+RanVaknin@users.noreply.github.com> Date: Fri, 20 Nov 2026 11:43:41 -0800 Subject: [PATCH 1/3] Restore DynamoDBTypeConvertedJson verbatim Restore @DynamoDBTypeConvertedJson and its tests exactly as they were at f8d9c8c3c4a, before #7502 removed them, so the next commit shows only the changes needed to drop the jackson-databind dependency. This commit does not compile on its own: the annotation still imports jackson-databind, which the mapper no longer depends on. --- .../dynamodb/DynamoDBTypeConvertedJson.java | 103 ++++++++ .../LegacyMarshallerMigrationTest.java | 154 +++++++++++- .../StandardModelFactoriesEdgeCasesTest.java | 57 ++++- ...rtedJsonMultiAttributeIntegrationTest.java | 221 ++++++++++++++++++ .../TypeConvertedJsonIntegrationTest.java | 96 ++++++++ .../mapper/TypeConvertedJsonTest.java | 101 ++++++++ .../pojos/AllSupportedAnnotationsClass.java | 14 +- 7 files changed, 743 insertions(+), 3 deletions(-) create mode 100644 services-custom/dynamodb-mapper/src/main/java/software/amazon/awssdk/mapper/dynamodb/DynamoDBTypeConvertedJson.java create mode 100644 services-custom/dynamodb-mapper/src/test/java/software/amazon/awssdk/mapper/dynamodb/TypeConvertedJsonMultiAttributeIntegrationTest.java create mode 100644 services-custom/dynamodb-mapper/src/test/java/software/amazon/awssdk/mapper/dynamodb/mapper/TypeConvertedJsonIntegrationTest.java create mode 100644 services-custom/dynamodb-mapper/src/test/java/software/amazon/awssdk/mapper/dynamodb/mapper/TypeConvertedJsonTest.java diff --git a/services-custom/dynamodb-mapper/src/main/java/software/amazon/awssdk/mapper/dynamodb/DynamoDBTypeConvertedJson.java b/services-custom/dynamodb-mapper/src/main/java/software/amazon/awssdk/mapper/dynamodb/DynamoDBTypeConvertedJson.java new file mode 100644 index 000000000000..3a398e96bf2f --- /dev/null +++ b/services-custom/dynamodb-mapper/src/main/java/software/amazon/awssdk/mapper/dynamodb/DynamoDBTypeConvertedJson.java @@ -0,0 +1,103 @@ +/* + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"). + * You may not use this file except in compliance with the License. + * A copy of the License is located at + * + * http://aws.amazon.com/apache2.0 + * + * or in the "license" file accompanying this file. This file is distributed + * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either + * express or implied. See the License for the specific language governing + * permissions and limitations under the License. + */ + +package software.amazon.awssdk.mapper.dynamodb; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +import com.fasterxml.jackson.databind.DeserializationFeature; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.ObjectWriter; +import software.amazon.awssdk.annotations.SdkPreviewApi; + +/** + * A simple JSON converter that uses the Jackson JSON processor. + * + *

It shares all limitations of that library. For more information about + * Jackson, see: http://wiki.fasterxml.com/JacksonHome

+ * + * {@snippet : + * @DynamoDBTypeConvertedJson + * public Currency getCurrency() { return currency; } + * } + * + *

Where,

+ * {@snippet : + * public class Currency { + * private Double amount; + * private String unit; + * + * public Double getAmount() { return amount; } + * public void setAmount(Double amount) { this.amount = amount; } + * + * public String getUnit() { return unit; } + * public void setUnit(String unit) { this.unit = unit; } + * } + * } + * + *

Would write the following value to DynamoDB given,

+ * + * + * @see software.amazon.awssdk.mapper.dynamodb.DynamoDBTypeConverted + */ +@SdkPreviewApi +@DynamoDBTypeConverted(converter=DynamoDBTypeConvertedJson.Converter.class) +@DynamoDBTyped(DynamoDBMapperFieldModel.DynamoDBAttributeType.S) +@Retention(RetentionPolicy.RUNTIME) +@Target({ElementType.TYPE, ElementType.FIELD, ElementType.METHOD}) +public @interface DynamoDBTypeConvertedJson { + + /** + * The value type to use when calling the JSON mapper's {@code readValue}; + * a value of {@code Void.class} indicates to use the getter's type. + */ + Class targetType() default void.class; + + /** + * JSON type converter. + */ + final class Converter implements DynamoDBTypeConverter { + private static final ObjectMapper mapper = new ObjectMapper().disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES); + private final Class targetType; + + public Converter(Class targetType, DynamoDBTypeConvertedJson annotation) { + this.targetType = annotation.targetType() == void.class ? targetType : (Class)annotation.targetType(); + } + + @Override + public final String convert(final T object) { + try { + return mapper.writeValueAsString(object); + } catch (final Exception e) { + throw new DynamoDBMappingException("Unable to write object to JSON", e); + } + } + + @Override + public final T unconvert(final String object) { + try { + return mapper.readValue(object, targetType); + } catch (final Exception e) { + throw new DynamoDBMappingException("Unable to read JSON string", e); + } + } + } + +} diff --git a/services-custom/dynamodb-mapper/src/test/java/software/amazon/awssdk/mapper/dynamodb/LegacyMarshallerMigrationTest.java b/services-custom/dynamodb-mapper/src/test/java/software/amazon/awssdk/mapper/dynamodb/LegacyMarshallerMigrationTest.java index d88a434d23b5..70269e6995dc 100644 --- a/services-custom/dynamodb-mapper/src/test/java/software/amazon/awssdk/mapper/dynamodb/LegacyMarshallerMigrationTest.java +++ b/services-custom/dynamodb-mapper/src/test/java/software/amazon/awssdk/mapper/dynamodb/LegacyMarshallerMigrationTest.java @@ -18,7 +18,11 @@ import static org.junit.Assert.assertEquals; import com.amazonaws.services.dynamodbv2.datamodeling.AbstractEnumMarshaller; +import com.amazonaws.services.dynamodbv2.datamodeling.JsonMarshaller; +import java.util.ArrayList; +import java.util.Date; import java.util.HashMap; +import java.util.List; import java.util.Map; import java.util.UUID; import org.junit.After; @@ -34,7 +38,7 @@ /** * Verifies the migration path from the removed v1 marshaller layer ({@code DynamoDBMarshaller}, - * {@code @DynamoDBMarshalling}, {@code AbstractEnumMarshaller}) to the converter layer. + * {@code @DynamoDBMarshalling}, {@code AbstractEnumMarshaller}, {@code JsonMarshaller}) to the converter layer. *

* Fixtures are produced by the real v1 marshallers ({@code com.amazonaws.services.dynamodbv2.datamodeling}), so the * tests check that items written by v1 load unchanged through the replacement annotations, and that items written @@ -47,7 +51,12 @@ public class LegacyMarshallerMigrationTest extends LocalDynamoDBTestBase { private static final String TABLE_NAME = LegacyMarshallerMigrationTest.class.getSimpleName() + "-" + System.currentTimeMillis(); + // A fixed instant with non-zero milliseconds, to catch any precision loss in the JSON date encoding. + private static final Date UPDATED = new Date(1_700_000_000_123L); + private static final AbstractEnumMarshaller V1_ENUM = new AbstractEnumMarshaller() { }; + private static final JsonMarshaller V1_PART_JSON = new JsonMarshaller(Part.class); + private static final JsonMarshaller V1_PART_LIST_JSON = new JsonMarshaller(PartList.class); private DynamoDbClient ddb; private DynamoDBMapper mapper; @@ -109,6 +118,83 @@ public void genericConverterWithClassConstructor_readsV1EnumMarshallerOutput() { assertEquals(Status.X, mapper.load(Item.class, key).getGenericStatus()); } + @Test + public void typeConvertedJson_readsV1JsonMarshallerOutput() { + PartList parts = new PartList(); + parts.add(new Part("b", 1, UPDATED)); + parts.add(new Part("c", 2, null)); + Map item = new HashMap(); + item.put("part", s(V1_PART_JSON.marshall(new Part("a", 3, UPDATED)))); + item.put("parts", s(V1_PART_LIST_JSON.marshall(parts))); + String key = putRaw(item); + + Item loaded = mapper.load(Item.class, key); + + assertPart(loaded.getPart(), "a", 3, UPDATED); + assertEquals(PartList.class, loaded.getParts().getClass()); + assertEquals(2, loaded.getParts().size()); + assertPart(loaded.getParts().get(0), "b", 1, UPDATED); + assertPart(loaded.getParts().get(1), "c", 2, null); + } + + @Test + public void typeConvertedJson_writesFormatV1JsonMarshallerReads() { + Item item = newItem(); + item.setPart(new Part("a", 3, UPDATED)); + List parts = new ArrayList(); + parts.add(new Part("b", 1, UPDATED)); + item.setParts(parts); + mapper.save(item); + + Map stored = getRaw(item.getKey()); + + // Byte-for-byte the same JSON the legacy JsonMarshaller writes, not just readable by it. + assertEquals(V1_PART_JSON.marshall(item.getPart()), stored.get("part").s()); + PartList expectedParts = new PartList(); + expectedParts.addAll(parts); + assertEquals(V1_PART_LIST_JSON.marshall(expectedParts), stored.get("parts").s()); + + assertPart(V1_PART_JSON.unmarshall(Part.class, stored.get("part").s()), "a", 3, UPDATED); + PartList v1Parts = V1_PART_LIST_JSON.unmarshall(PartList.class, stored.get("parts").s()); + assertEquals(1, v1Parts.size()); + assertPart(v1Parts.get(0), "b", 1, UPDATED); + } + + /** + * Unlike the legacy {@code JsonMarshaller}, the replacement ignores JSON properties the class does not declare, + * for example ones added by a newer writer. + */ + @Test + public void typeConvertedJson_withUnknownProperty_ignoresIt() { + String legacy = V1_PART_JSON.marshall(new Part("a", 3, UPDATED)); + String withExtra = legacy.substring(0, legacy.length() - 1) + ",\"addedLater\":\"x\"}"; + String key = putRaw("part", s(withExtra)); + + assertPart(mapper.load(Item.class, key).getPart(), "a", 3, UPDATED); + } + + @Test + public void typeConvertedJson_roundTrips() { + Item item = newItem(); + item.setPart(new Part("a", 3, UPDATED)); + List parts = new ArrayList(); + parts.add(new Part("b", 1, null)); + item.setParts(parts); + + mapper.save(item); + Item loaded = mapper.load(Item.class, item.getKey()); + + assertPart(loaded.getPart(), "a", 3, UPDATED); + assertEquals(1, loaded.getParts().size()); + assertPart(loaded.getParts().get(0), "b", 1, null); + } + + private static void assertPart(Part part, String id, int quantity, Date updated) { + assertEquals(id, part.getId()); + assertEquals(Integer.valueOf(quantity), part.getQuantity()); + assertEquals(updated, part.getUpdated()); + } + private static AttributeValue s(String value) { return AttributeValue.builder().s(value).build(); } @@ -163,11 +249,59 @@ public E unconvert(String object) { } } + /** + * Concrete list type that keeps the element type through erasure, as a legacy {@code JsonMarshaller} subclass + * would have with {@code super(Type.class)}. + */ + public static final class PartList extends ArrayList { + } + + public static class Part { + private String id; + private Integer quantity; + private Date updated; + + public Part() { + } + + public Part(String id, Integer quantity, Date updated) { + this.id = id; + this.quantity = quantity; + this.updated = updated; + } + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public Integer getQuantity() { + return quantity; + } + + public void setQuantity(Integer quantity) { + this.quantity = quantity; + } + + public Date getUpdated() { + return updated; + } + + public void setUpdated(Date updated) { + this.updated = updated; + } + } + @DynamoDBTable(tableName = "overridden") public static class Item { private String key; private Status status; private Status genericStatus; + private Part part; + private List parts; @DynamoDBHashKey public String getKey() { @@ -195,5 +329,23 @@ public Status getGenericStatus() { public void setGenericStatus(Status genericStatus) { this.genericStatus = genericStatus; } + + @DynamoDBTypeConvertedJson + public Part getPart() { + return part; + } + + public void setPart(Part part) { + this.part = part; + } + + @DynamoDBTypeConvertedJson(targetType = PartList.class) + public List getParts() { + return parts; + } + + public void setParts(List parts) { + this.parts = parts; + } } } diff --git a/services-custom/dynamodb-mapper/src/test/java/software/amazon/awssdk/mapper/dynamodb/StandardModelFactoriesEdgeCasesTest.java b/services-custom/dynamodb-mapper/src/test/java/software/amazon/awssdk/mapper/dynamodb/StandardModelFactoriesEdgeCasesTest.java index 3312e9b15859..77358daaba3c 100644 --- a/services-custom/dynamodb-mapper/src/test/java/software/amazon/awssdk/mapper/dynamodb/StandardModelFactoriesEdgeCasesTest.java +++ b/services-custom/dynamodb-mapper/src/test/java/software/amazon/awssdk/mapper/dynamodb/StandardModelFactoriesEdgeCasesTest.java @@ -48,7 +48,7 @@ public class StandardModelFactoriesEdgeCasesTest { .withConversionSchema(ConversionSchemas.V2) .build(); - private static final DynamoDBMapperModelFactory factory = StandardModelFactories.of(); + private static final DynamoDBMapperModelFactory factory = StandardModelFactories.of(S3Link.Factory.of(null)); private static final DynamoDBMapperModelFactory.TableFactory v1Models = factory.getTableFactory(V1_CONFIG); private static final DynamoDBMapperModelFactory.TableFactory v2Models = factory.getTableFactory(V2_CONFIG); @@ -169,6 +169,61 @@ public void unconvert_withUnknownEnumValue_throwsException() { } } + public static class JsonPayload { + private String name; + private int count; + public String getName() { return name; } + public void setName(String name) { this.name = name; } + public int getCount() { return count; } + public void setCount(int count) { this.count = count; } + @Override + public boolean equals(Object o) { + if (!(o instanceof JsonPayload)) { + return false; + } + JsonPayload that = (JsonPayload) o; + return count == that.count + && (name == null ? that.name == null : name.equals(that.name)); + } + @Override + public int hashCode() { + return (name == null ? 0 : name.hashCode()) * 31 + count; + } + } + + public static class JsonPojo extends AutoKeyAndVal { + @DynamoDBTypeConvertedJson + public JsonPayload getVal() { return super.getVal(); } + public void setVal(JsonPayload val) { super.setVal(val); } + } + + @Test + public void convert_withJsonPayload_producesString() { + JsonPayload payload = new JsonPayload(); + payload.setName("widget"); + payload.setCount(7); + + DynamoDBMapperTableModel model = table(new JsonPojo()); + AttributeValue av = model.field("val").convert(payload); + + Assert.assertNotNull(av.s()); + Assert.assertTrue(av.s().contains("widget")); + Assert.assertTrue(av.s().contains("7")); + } + + @Test + public void convertAndUnconvert_withJsonPayload_roundTrips() { + JsonPayload payload = new JsonPayload(); + payload.setName("widget"); + payload.setCount(7); + + DynamoDBMapperTableModel model = table(new JsonPojo()); + AttributeValue av = model.field("val").convert(payload); + Object back = model.field("val").unconvert(av); + + assertEquals(payload, back); + } + @Test public void unconvert_withByteBuffer_returnsWritableBuffer() { // v1 parity: v1 getB() returned a writable buffer. v2 SdkBytes.asByteBuffer() is diff --git a/services-custom/dynamodb-mapper/src/test/java/software/amazon/awssdk/mapper/dynamodb/TypeConvertedJsonMultiAttributeIntegrationTest.java b/services-custom/dynamodb-mapper/src/test/java/software/amazon/awssdk/mapper/dynamodb/TypeConvertedJsonMultiAttributeIntegrationTest.java new file mode 100644 index 000000000000..48275e2da636 --- /dev/null +++ b/services-custom/dynamodb-mapper/src/test/java/software/amazon/awssdk/mapper/dynamodb/TypeConvertedJsonMultiAttributeIntegrationTest.java @@ -0,0 +1,221 @@ +/* + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"). + * You may not use this file except in compliance with the License. + * A copy of the License is located at + * + * http://aws.amazon.com/apache2.0 + * + * or in the "license" file accompanying this file. This file is distributed + * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either + * express or implied. See the License for the specific language governing + * permissions and limitations under the License. + */ + +package software.amazon.awssdk.mapper.dynamodb; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.fail; + +import java.util.ArrayList; +import java.util.Date; +import java.util.List; +import java.util.UUID; + +import org.junit.Test; + +import software.amazon.awssdk.mapper.dynamodb.DynamoDBMapperIntegrationTestBase; +import software.amazon.awssdk.mapper.dynamodb.DynamoDBAutoGeneratedKey; +import software.amazon.awssdk.mapper.dynamodb.DynamoDBHashKey; +import software.amazon.awssdk.mapper.dynamodb.DynamoDBTable; +import software.amazon.awssdk.mapper.dynamodb.DynamoDBTypeConvertedJson; + +/** + * Integration tests for an item with several {@code @DynamoDBTypeConvertedJson} attributes, including lists + * bound to a concrete element type through {@code targetType}. This is the replacement for the removed v1 + * {@code JsonMarshaller} / {@code @DynamoDBMarshalling} pair. + */ +public class TypeConvertedJsonMultiAttributeIntegrationTest extends DynamoDBMapperIntegrationTestBase { + + @Test + public void testMarshalling() { + final DynamoDBMapper mapper = new DynamoDBMapper(dynamo); + + final TestObject object1 = new TestObject(); + object1.setOneItem(new TestObject.OneItem()); + object1.addOneItem(new TestObject.OneItem(UUID.randomUUID().toString(), 1)); + object1.addOneItem(new TestObject.OneItem(UUID.randomUUID().toString(), 2)); + object1.addOneItem(new TestObject.OneItem(UUID.randomUUID().toString(), 3)); + object1.setTwoItem(new TestObject.TwoItem()); + object1.addTwoItem(new TestObject.TwoItem(UUID.randomUUID().toString(), new Date())); + + mapper.save(object1); + + assertNotNull(object1.getKey()); + + assertNotNull(object1.getOneItem()); + assertNotNull(object1.getOneItems()); + assertEquals(3, object1.getOneItems().size()); + + assertNotNull(object1.getTwoItem()); + assertNotNull(object1.getTwoItems()); + assertEquals(1, object1.getTwoItems().size()); + + final TestObject object2 = mapper.load(TestObject.class, object1.getKey()); + + assertEquals(object1.getKey(), object2.getKey()); + + assertEquals(object1.getOneItem().getId(), object2.getOneItem().getId()); + assertEquals(object1.getOneItem().getQuantity(), object2.getOneItem().getQuantity()); + assertEquals(object1.getOneItems().size(), object2.getOneItems().size()); + + for (int i = 0, its = object1.getOneItems().size(); i < its; i++) { + assertEquals(object1.getOneItems().get(i).getId(), object2.getOneItems().get(i).getId()); + assertEquals(object1.getOneItems().get(i).getQuantity(), object2.getOneItems().get(i).getQuantity()); + } + + assertEquals(object1.getTwoItem().getId(), object2.getTwoItem().getId()); + assertEquals(object1.getTwoItem().getDate(), object2.getTwoItem().getDate()); + assertEquals(object1.getTwoItems().size(), object2.getTwoItems().size()); + + for (int i = 0, its = object1.getTwoItems().size(); i < its; i++) { + assertEquals(object1.getTwoItems().get(i).getId(), object2.getTwoItems().get(i).getId()); + assertEquals(object1.getTwoItems().get(i).getDate(), object2.getTwoItems().get(i).getDate()); + } + } + + @DynamoDBTable(tableName="aws-java-sdk-util") + public static class TestObject { + // Concrete list types keep the element type through erasure, as the legacy JsonMarshaller subclasses did. + public static final class OneItemList extends ArrayList { } + public static final class TwoItemList extends ArrayList { } + + private String key; + private OneItem aitem; + private List oneItems; + private TwoItem bitem; + private List twoItems; + + @DynamoDBHashKey + @DynamoDBAutoGeneratedKey + public String getKey() { + return this.key; + } + + public void setKey(String key) { + this.key = key; + } + + @DynamoDBTypeConvertedJson + public OneItem getOneItem() { + return this.aitem; + } + + public void setOneItem(OneItem aitem) { + this.aitem = aitem; + } + + @DynamoDBTypeConvertedJson(targetType = OneItemList.class) + public List getOneItems() { + return this.oneItems; + } + + public void setOneItems(List oneItems) { + this.oneItems = oneItems; + } + + public void addOneItem(OneItem aitem) { + if (this.oneItems == null) { + this.oneItems = new ArrayList(); + } + this.oneItems.add(aitem); + } + + @DynamoDBTypeConvertedJson + public TwoItem getTwoItem() { + return this.bitem; + } + + public void setTwoItem(TwoItem bitem) { + this.bitem = bitem; + } + + @DynamoDBTypeConvertedJson(targetType = TwoItemList.class) + public List getTwoItems() { + return this.twoItems; + } + + public void setTwoItems(List twoItems) { + this.twoItems = twoItems; + } + + public void addTwoItem(TwoItem bitem) { + if (this.twoItems == null) { + this.twoItems = new ArrayList(); + } + this.twoItems.add(bitem); + } + + public static class OneItem { + private String id; + private Integer quantity; + + public OneItem(String id, Integer quantity) { + this.id = id; + this.quantity = quantity; + } + + public OneItem() { + this(null, null); + } + + public String getId() { + return this.id; + } + + public void setId(String id) { + this.id = id; + } + + public Integer getQuantity() { + return this.quantity; + } + + public void setQuantity(Integer quantity) { + this.quantity = quantity; + } + } + + public static class TwoItem { + private String id; + private Date date; + + public TwoItem(String id, Date date) { + this.id = id; + this.date = date; + } + + public TwoItem() { + this(null, null); + } + + public String getId() { + return this.id; + } + + public void setId(String id) { + this.id = id; + } + + public Date getDate() { + return this.date; + } + + public void setDate(Date date) { + this.date = date; + } + } + } +} diff --git a/services-custom/dynamodb-mapper/src/test/java/software/amazon/awssdk/mapper/dynamodb/mapper/TypeConvertedJsonIntegrationTest.java b/services-custom/dynamodb-mapper/src/test/java/software/amazon/awssdk/mapper/dynamodb/mapper/TypeConvertedJsonIntegrationTest.java new file mode 100644 index 000000000000..2c10edc8eebf --- /dev/null +++ b/services-custom/dynamodb-mapper/src/test/java/software/amazon/awssdk/mapper/dynamodb/mapper/TypeConvertedJsonIntegrationTest.java @@ -0,0 +1,96 @@ +/* + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"). + * You may not use this file except in compliance with the License. + * A copy of the License is located at + * + * http://aws.amazon.com/apache2.0 + * + * or in the "license" file accompanying this file. This file is distributed + * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either + * express or implied. See the License for the specific language governing + * permissions and limitations under the License. + */ + +package software.amazon.awssdk.mapper.dynamodb.mapper; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; + +import software.amazon.awssdk.mapper.dynamodb.DynamoDBAutoGeneratedKey; +import software.amazon.awssdk.mapper.dynamodb.DynamoDBHashKey; +import software.amazon.awssdk.mapper.dynamodb.DynamoDBTable; +import software.amazon.awssdk.mapper.dynamodb.DynamoDBTypeConvertedJson; +import software.amazon.awssdk.mapper.dynamodb.pojos.AutoKeyAndVal; +import software.amazon.awssdk.mapper.dynamodb.pojos.Currency; + +import java.util.ArrayList; +import java.util.List; + +import org.junit.Test; + +/** + * Integration tests for {@code DynamoDBTypeConvertedJson}. + */ +public class TypeConvertedJsonIntegrationTest extends AbstractKeyAndValIntegrationTestCase { + + /** + * Test marshalling. + */ + @Test + public void testMarshalling() { + final KeyAndCurrency object = new KeyAndCurrency(); + object.setVal(new Currency(12.95D, "USD")); + assertBeforeAndAfterChange(false, object); + } + + /** + * Test marshalling a list. + */ + @Test + public void testListMarshalling() { + final KeyAndCurrencyList object = new KeyAndCurrencyList(); + object.setVal(new ArrayList()); + object.getVal().add(new Currency(1.99D, "CAD")); + object.getVal().add(new Currency(2.99D, "CAD")); + + final List after = assertBeforeAndAfterChange(false, object); + for (final Currency currency : after) { + assertNotNull(currency.getAmount()); + assertNotNull(currency.getUnit()); + } + } + + /** + * An object with a complex type. + */ + @DynamoDBTable(tableName="aws-java-sdk-util") + public static class KeyAndCurrency extends AutoKeyAndVal { + @DynamoDBTypeConvertedJson + public Currency getVal() { + return super.getVal(); + } + @Override + public void setVal(final Currency val) { + super.setVal(val); + } + } + + /** + * An object with a complex type. + */ + @DynamoDBTable(tableName="aws-java-sdk-util") + public static class KeyAndCurrencyList extends AutoKeyAndVal> { + public static final class CurrencyListType extends ArrayList {} + @DynamoDBTypeConvertedJson(targetType=CurrencyListType.class) + public List getVal() { + return super.getVal(); + } + @Override + public void setVal(final List val) { + super.setVal(val); + } + } + +} diff --git a/services-custom/dynamodb-mapper/src/test/java/software/amazon/awssdk/mapper/dynamodb/mapper/TypeConvertedJsonTest.java b/services-custom/dynamodb-mapper/src/test/java/software/amazon/awssdk/mapper/dynamodb/mapper/TypeConvertedJsonTest.java new file mode 100644 index 000000000000..4ab5df46f7ae --- /dev/null +++ b/services-custom/dynamodb-mapper/src/test/java/software/amazon/awssdk/mapper/dynamodb/mapper/TypeConvertedJsonTest.java @@ -0,0 +1,101 @@ +/* + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"). + * You may not use this file except in compliance with the License. + * A copy of the License is located at + * + * http://aws.amazon.com/apache2.0 + * + * or in the "license" file accompanying this file. This file is distributed + * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either + * express or implied. See the License for the specific language governing + * permissions and limitations under the License. + */ + +package software.amazon.awssdk.mapper.dynamodb.mapper; + +import java.util.HashMap; +import java.util.Map; + +import software.amazon.awssdk.services.dynamodb.DynamoDbClient; +import software.amazon.awssdk.mapper.dynamodb.DynamoDBHashKey; +import software.amazon.awssdk.mapper.dynamodb.DynamoDBMapper; +import software.amazon.awssdk.mapper.dynamodb.DynamoDBTable; +import software.amazon.awssdk.mapper.dynamodb.DynamoDBTypeConvertedJson; +import software.amazon.awssdk.services.dynamodb.model.AttributeValue; +import software.amazon.awssdk.services.dynamodb.model.GetItemRequest; +import software.amazon.awssdk.services.dynamodb.model.GetItemResponse; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.junit.MockitoJUnitRunner; + +import static org.junit.Assert.assertEquals; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.when; + +@RunWith(MockitoJUnitRunner.class) +public class TypeConvertedJsonTest { + + private static final String HASH_KEY = "1234"; + + @Mock + private DynamoDbClient ddb; + + @Test + public void responseWithUnmappedField_IgnoresUnknownFieldAndUnmarshallsCorrectly() { + final DynamoDBMapper mapper = new DynamoDBMapper(ddb); + Map item = new HashMap<>(); + item.put("hashKey", AttributeValue.builder().s(HASH_KEY).build()); + item.put("jsonMappedPojo", AttributeValue.builder().s( + "{\"knownField\": \"knownValue\", \"unknownField\": \"unknownValue\"}").build()); + when(ddb.getItem(any(GetItemRequest.class))) + .thenReturn(GetItemResponse.builder().item(item).build()); + + final TopLevelPojo pojo = mapper.load(new TopLevelPojo().setHashKey(HASH_KEY)); + assertEquals("knownValue", pojo.getJsonMappedPojo().getKnownField()); + } + + @DynamoDBTable(tableName = "TestTable") + public static class TopLevelPojo { + + @DynamoDBHashKey + private String hashKey; + + @DynamoDBTypeConvertedJson + private JsonMappedPojo jsonMappedPojo; + + public String getHashKey() { + return hashKey; + } + + public TopLevelPojo setHashKey(String hashKey) { + this.hashKey = hashKey; + return this; + } + + public JsonMappedPojo getJsonMappedPojo() { + return jsonMappedPojo; + } + + public void setJsonMappedPojo(JsonMappedPojo jsonMappedPojo) { + this.jsonMappedPojo = jsonMappedPojo; + } + } + + public static class JsonMappedPojo { + + private String knownField; + + public String getKnownField() { + return knownField; + } + + public void setKnownField(String knownField) { + this.knownField = knownField; + } + } + +} diff --git a/services-custom/dynamodb-mapper/src/test/java/software/amazon/awssdk/mapper/dynamodb/pojos/AllSupportedAnnotationsClass.java b/services-custom/dynamodb-mapper/src/test/java/software/amazon/awssdk/mapper/dynamodb/pojos/AllSupportedAnnotationsClass.java index eb28beb369c3..1b9c36712d33 100644 --- a/services-custom/dynamodb-mapper/src/test/java/software/amazon/awssdk/mapper/dynamodb/pojos/AllSupportedAnnotationsClass.java +++ b/services-custom/dynamodb-mapper/src/test/java/software/amazon/awssdk/mapper/dynamodb/pojos/AllSupportedAnnotationsClass.java @@ -29,6 +29,7 @@ import software.amazon.awssdk.mapper.dynamodb.DynamoDBKeyed; import software.amazon.awssdk.mapper.dynamodb.DynamoDBNamed; import software.amazon.awssdk.mapper.dynamodb.DynamoDBTable; +import software.amazon.awssdk.mapper.dynamodb.DynamoDBTypeConvertedJson; import software.amazon.awssdk.mapper.dynamodb.DynamoDBTypeConvertedEpochDate; import software.amazon.awssdk.services.dynamodb.model.KeyType; @@ -52,6 +53,7 @@ public class AllSupportedAnnotationsClass extends TestDocClass { private String gsiHashKey; private String gsiRangeKey; private String namedAttribute; + private Currency currencyAttribute; private Date epochDate; private DateRange dateRange; @@ -154,6 +156,15 @@ public void setNamedAttribute(String namedAttribute) { this.namedAttribute = namedAttribute; } + @DynamoDBTypeConvertedJson + public Currency getCurrencyAttribute() { + return currencyAttribute; + } + + public void setCurrencyAttribute(Currency currencyAttribute) { + this.currencyAttribute = currencyAttribute; + } + @DynamoDBTypeConvertedEpochDate public Date getEpochDate() { return epochDate; @@ -189,6 +200,7 @@ && eq(ignoredAttribute, other.ignoredAttribute) && eq(gsiHashKey, other.gsiHashKey) && eq(gsiRangeKey, other.gsiRangeKey) && eq(namedAttribute, other.namedAttribute) + && eq(currencyAttribute, other.currencyAttribute) && eq(epochDate, other.epochDate) && eq(dateRange, other.dateRange) && super.equals(other) @@ -201,7 +213,7 @@ public String toString() { + ", mapOfLists=" + super.getMapOfLists() + ", integerAttribute=" + integerAttribute + ", uuid=" + uuid + ", date=" + date + ", defaultAttribute=" + defaultAttribute + ", booleanAttribute=" + booleanAttribute + ", phoneNumber=" + phoneNumber + ", ignoredAttribute=" + ignoredAttribute + ", gsiHashKey=" + gsiHashKey + ", gsiRangeKey=" + gsiRangeKey - + ", namedAttribute=" + namedAttribute + ", epochDate=" + epochDate +", dateRange=" + dateRange + + ", namedAttribute=" + namedAttribute + ", currencyAttribute=" + currencyAttribute + ", epochDate=" + epochDate +", dateRange=" + dateRange + "}"; } } From 354df240904fe1d60d5ed4c2c3c048c627044b95 Mon Sep 17 00:00:00 2001 From: RanVaknin <50976344+RanVaknin@users.noreply.github.com> Date: Fri, 20 Nov 2026 14:04:03 -0800 Subject: [PATCH 2/3] Use the application's jackson-databind Call jackson-databind through reflection instead of depending on it, so the mapper ships without databind and @DynamoDBTypeConvertedJson uses the application's copy. The calls and configuration match the v1 converter exactly, so stored JSON stays the same. When databind is missing, the mapper throws a DynamoDBMappingException naming the dependency to add, when it builds the model for an annotated class. Add tests against v1's own converter, a test run with databind removed from the classpath, and databind as a test-only dependency. --- services-custom/dynamodb-mapper/pom.xml | 30 +++ .../dynamodb/DynamoDBTypeConvertedJson.java | 13 +- .../dynamodb/JacksonDatabindSupport.java | 161 +++++++++++++++ ...amoDBMapperWithoutJacksonDatabindTest.java | 146 ++++++++++++++ .../DynamoDBTypeConvertedJsonTest.java | 187 ++++++++++++++++++ .../StandardModelFactoriesEdgeCasesTest.java | 2 +- 6 files changed, 532 insertions(+), 7 deletions(-) create mode 100644 services-custom/dynamodb-mapper/src/main/java/software/amazon/awssdk/mapper/dynamodb/JacksonDatabindSupport.java create mode 100644 services-custom/dynamodb-mapper/src/test/java/software/amazon/awssdk/mapper/dynamodb/DynamoDBMapperWithoutJacksonDatabindTest.java create mode 100644 services-custom/dynamodb-mapper/src/test/java/software/amazon/awssdk/mapper/dynamodb/DynamoDBTypeConvertedJsonTest.java diff --git a/services-custom/dynamodb-mapper/pom.xml b/services-custom/dynamodb-mapper/pom.xml index 40e82fdaca84..73f465cd6a5d 100644 --- a/services-custom/dynamodb-mapper/pom.xml +++ b/services-custom/dynamodb-mapper/pom.xml @@ -91,6 +91,7 @@ **/*WithoutJodaTimeTest.java + **/*WithoutJacksonDatabindTest.java @@ -116,6 +117,28 @@ + + + without-jackson-databind + + test + + + + **/*WithoutJacksonDatabindTest.java + + + *WithoutJacksonDatabindTest + + com.fasterxml.jackson.core:jackson-databind + + + true + + + @@ -179,6 +202,13 @@ + + + com.fasterxml.jackson.core + jackson-databind + test + junit junit diff --git a/services-custom/dynamodb-mapper/src/main/java/software/amazon/awssdk/mapper/dynamodb/DynamoDBTypeConvertedJson.java b/services-custom/dynamodb-mapper/src/main/java/software/amazon/awssdk/mapper/dynamodb/DynamoDBTypeConvertedJson.java index 3a398e96bf2f..42fe345d56be 100644 --- a/services-custom/dynamodb-mapper/src/main/java/software/amazon/awssdk/mapper/dynamodb/DynamoDBTypeConvertedJson.java +++ b/services-custom/dynamodb-mapper/src/main/java/software/amazon/awssdk/mapper/dynamodb/DynamoDBTypeConvertedJson.java @@ -20,9 +20,6 @@ import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; -import com.fasterxml.jackson.databind.DeserializationFeature; -import com.fasterxml.jackson.databind.ObjectMapper; -import com.fasterxml.jackson.databind.ObjectWriter; import software.amazon.awssdk.annotations.SdkPreviewApi; /** @@ -31,6 +28,10 @@ *

It shares all limitations of that library. For more information about * Jackson, see: http://wiki.fasterxml.com/JacksonHome

* + *

jackson-databind is not a dependency of the mapper. Applications that use this annotation must add + * {@code com.fasterxml.jackson.core:jackson-databind} 2.x to their classpath. Without it, the mapper throws a + * {@link DynamoDBMappingException} when it builds the model for a class that uses this annotation.

+ * * {@snippet : * @DynamoDBTypeConvertedJson * public Currency getCurrency() { return currency; } @@ -74,17 +75,17 @@ * JSON type converter. */ final class Converter implements DynamoDBTypeConverter { - private static final ObjectMapper mapper = new ObjectMapper().disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES); private final Class targetType; public Converter(Class targetType, DynamoDBTypeConvertedJson annotation) { this.targetType = annotation.targetType() == void.class ? targetType : (Class)annotation.targetType(); + JacksonDatabindSupport.initialize(); } @Override public final String convert(final T object) { try { - return mapper.writeValueAsString(object); + return JacksonDatabindSupport.writeValueAsString(object); } catch (final Exception e) { throw new DynamoDBMappingException("Unable to write object to JSON", e); } @@ -93,7 +94,7 @@ public final String convert(final T object) { @Override public final T unconvert(final String object) { try { - return mapper.readValue(object, targetType); + return JacksonDatabindSupport.readValue(object, targetType); } catch (final Exception e) { throw new DynamoDBMappingException("Unable to read JSON string", e); } diff --git a/services-custom/dynamodb-mapper/src/main/java/software/amazon/awssdk/mapper/dynamodb/JacksonDatabindSupport.java b/services-custom/dynamodb-mapper/src/main/java/software/amazon/awssdk/mapper/dynamodb/JacksonDatabindSupport.java new file mode 100644 index 000000000000..43eb1ff39ad0 --- /dev/null +++ b/services-custom/dynamodb-mapper/src/main/java/software/amazon/awssdk/mapper/dynamodb/JacksonDatabindSupport.java @@ -0,0 +1,161 @@ +/* + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"). + * You may not use this file except in compliance with the License. + * A copy of the License is located at + * + * http://aws.amazon.com/apache2.0 + * + * or in the "license" file accompanying this file. This file is distributed + * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either + * express or implied. See the License for the specific language governing + * permissions and limitations under the License. + */ + +package software.amazon.awssdk.mapper.dynamodb; + +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import software.amazon.awssdk.annotations.SdkInternalApi; +import software.amazon.awssdk.utils.Lazy; + +/** + * Calls the application's jackson-databind for {@link DynamoDBTypeConvertedJson}. The mapper doesn't depend on + * jackson-databind, so this class finds it on the classpath by name and calls it through reflection. + * + *

The calls and configuration match the v1 mapper's converter exactly, so the stored JSON is the same: one shared + * {@code ObjectMapper} with {@code FAIL_ON_UNKNOWN_PROPERTIES} disabled, {@code writeValueAsString(Object)} to write + * and {@code readValue(String, Class)} to read. + * + *

This class must not reference any jackson-databind type, so that it can be loaded without jackson-databind. + */ +@SdkInternalApi +final class JacksonDatabindSupport { + + private static final String OBJECT_MAPPER_CLASS = "com.fasterxml.jackson.databind.ObjectMapper"; + private static final String DESERIALIZATION_FEATURE_CLASS = "com.fasterxml.jackson.databind.DeserializationFeature"; + + /** + * Checked with the mapper's own class loader, for the same reason as {@link JodaTimeSupport}. + */ + private static final boolean AVAILABLE = isLoadable(OBJECT_MAPPER_CLASS); + + private static final Lazy BRIDGE = new Lazy<>(ObjectMapperBridge::create); + + private JacksonDatabindSupport() { + } + + /** + * Returns whether jackson-databind is on the mapper's classpath. + */ + static boolean isAvailable() { + return AVAILABLE; + } + + /** + * Returns the exception to throw when a feature that needs jackson-databind is used without it. + * + * @param feature the feature that needs jackson-databind, for example {@code "@DynamoDBTypeConvertedJson"} + */ + static DynamoDBMappingException missing(String feature) { + return new DynamoDBMappingException( + feature + " requires jackson-databind, which the mapper doesn't include. Add " + + "com.fasterxml.jackson.core:jackson-databind (Brazil package Jackson-databind) to the classpath, where " + + "the mapper's class loader can load it."); + } + + /** + * Prepares the shared {@code ObjectMapper}, so that a missing or unusable jackson-databind fails when the mapper + * builds the model rather than on the first save or load. + */ + static void initialize() { + BRIDGE.getValue(); + } + + /** + * Writes {@code value} as JSON, as {@code ObjectMapper.writeValueAsString} does. + * + * @throws Exception the exception jackson-databind threw + */ + static String writeValueAsString(Object value) throws Exception { + ObjectMapperBridge bridge = BRIDGE.getValue(); + return (String) invoke(bridge.writeValueAsString, bridge.objectMapper, value); + } + + /** + * Reads {@code json} as {@code type}, as {@code ObjectMapper.readValue(String, Class)} does. + * + * @throws Exception the exception jackson-databind threw + */ + @SuppressWarnings("unchecked") + static T readValue(String json, Class type) throws Exception { + ObjectMapperBridge bridge = BRIDGE.getValue(); + return (T) invoke(bridge.readValue, bridge.objectMapper, json, type); + } + + /** + * Calls {@code method} and rethrows what it threw, rather than an {@link InvocationTargetException}, so callers see + * the same exceptions as a direct call. + */ + private static Object invoke(Method method, Object target, Object... args) throws Exception { + try { + return method.invoke(target, args); + } catch (InvocationTargetException e) { + Throwable cause = e.getCause(); + if (cause instanceof Exception) { + throw (Exception) cause; + } + if (cause instanceof Error) { + throw (Error) cause; + } + throw e; + } + } + + private static boolean isLoadable(String className) { + try { + Class.forName(className, false, JacksonDatabindSupport.class.getClassLoader()); + return true; + } catch (ClassNotFoundException | LinkageError e) { + return false; + } + } + + private static final class ObjectMapperBridge { + private final Object objectMapper; + private final Method writeValueAsString; + private final Method readValue; + + private ObjectMapperBridge(Object objectMapper, Method writeValueAsString, Method readValue) { + this.objectMapper = objectMapper; + this.writeValueAsString = writeValueAsString; + this.readValue = readValue; + } + + @SuppressWarnings({"unchecked", "rawtypes"}) + private static ObjectMapperBridge create() { + if (!AVAILABLE) { + throw missing("@DynamoDBTypeConvertedJson"); + } + try { + ClassLoader loader = JacksonDatabindSupport.class.getClassLoader(); + Class mapperClass = Class.forName(OBJECT_MAPPER_CLASS, true, loader); + Class featureClass = + (Class) Class.forName(DESERIALIZATION_FEATURE_CLASS, true, loader); + + Object objectMapper = mapperClass.getConstructor().newInstance(); + mapperClass.getMethod("disable", featureClass) + .invoke(objectMapper, Enum.valueOf(featureClass, "FAIL_ON_UNKNOWN_PROPERTIES")); + + return new ObjectMapperBridge(objectMapper, + mapperClass.getMethod("writeValueAsString", Object.class), + mapperClass.getMethod("readValue", String.class, Class.class)); + } catch (ReflectiveOperationException | LinkageError | IllegalArgumentException e) { + throw new DynamoDBMappingException( + "@DynamoDBTypeConvertedJson found jackson-databind on the classpath but couldn't use it. It " + + "requires jackson-databind 2.x.", e); + } + } + } +} diff --git a/services-custom/dynamodb-mapper/src/test/java/software/amazon/awssdk/mapper/dynamodb/DynamoDBMapperWithoutJacksonDatabindTest.java b/services-custom/dynamodb-mapper/src/test/java/software/amazon/awssdk/mapper/dynamodb/DynamoDBMapperWithoutJacksonDatabindTest.java new file mode 100644 index 000000000000..1702b6318b36 --- /dev/null +++ b/services-custom/dynamodb-mapper/src/test/java/software/amazon/awssdk/mapper/dynamodb/DynamoDBMapperWithoutJacksonDatabindTest.java @@ -0,0 +1,146 @@ +/* + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"). + * You may not use this file except in compliance with the License. + * A copy of the License is located at + * + * http://aws.amazon.com/apache2.0 + * + * or in the "license" file accompanying this file. This file is distributed + * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either + * express or implied. See the License for the specific language governing + * permissions and limitations under the License. + */ + +package software.amazon.awssdk.mapper.dynamodb; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.assertj.core.api.Assertions.catchThrowable; +import static org.junit.jupiter.api.Assumptions.assumeTrue; + +import java.util.Map; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; +import software.amazon.awssdk.auth.credentials.AnonymousCredentialsProvider; +import software.amazon.awssdk.regions.Region; +import software.amazon.awssdk.services.dynamodb.DynamoDbClient; +import software.amazon.awssdk.services.dynamodb.model.AttributeValue; + +/** + * Checks that the mapper works without jackson-databind, which it doesn't depend on, and that + * {@link DynamoDBTypeConvertedJson} fails with a clear message when the application doesn't provide it. + * + *

The {@code without-jackson-databind} surefire execution runs this class with jackson-databind removed from the + * classpath. Elsewhere, for example in an IDE, it is skipped. + */ +class DynamoDBMapperWithoutJacksonDatabindTest { + + private final DynamoDBMapper mapper = DynamoDBMapper.builder() + .dynamoDbClient(DynamoDbClient.builder() + .region(Region.US_EAST_1) + .credentialsProvider(AnonymousCredentialsProvider.create()) + .build()) + .build(); + + @BeforeAll + static void requireJacksonDatabindExcluded() { + assumeTrue(Boolean.getBoolean("mapper.test.jacksonDatabindExcluded"), + "runs only in the without-jackson-databind surefire execution"); + assertThatThrownBy(() -> Class.forName("com.fasterxml.jackson.databind.ObjectMapper")) + .as("jackson-databind must not be on the classpath of this test") + .isInstanceOf(ClassNotFoundException.class); + } + + @Test + void isAvailable_withoutJacksonDatabind_returnsFalse() { + assertThat(JacksonDatabindSupport.isAvailable()).isFalse(); + } + + @Test + void getTableModel_withoutJsonAttribute_convertsWithoutJacksonDatabind() { + PlainItem item = new PlainItem(); + item.setId("1"); + item.setName("plain string"); + + DynamoDBMapperTableModel model = mapper.getTableModel(PlainItem.class); + Map stored = model.convert(item); + + assertThat(stored.get("name").s()).isEqualTo("plain string"); + assertThat(model.unconvert(stored).getName()).isEqualTo("plain string"); + } + + @Test + void getTableModel_withJsonAttribute_throwsMissingJacksonDatabindMessage() { + Throwable thrown = catchThrowable(() -> mapper.getTableModel(JsonItem.class)); + + assertThat(thrown).isInstanceOf(DynamoDBMappingException.class); + Throwable root = thrown; + while (root.getCause() != null) { + root = root.getCause(); + } + assertThat(root) + .isInstanceOf(DynamoDBMappingException.class) + .hasMessageContaining("@DynamoDBTypeConvertedJson requires jackson-databind") + .hasMessageContaining("com.fasterxml.jackson.core:jackson-databind") + .hasMessageContaining("Jackson-databind"); + } + + public static class PlainItem { + private String id; + private String name; + + @DynamoDBHashKey + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + } + + public static class JsonItem { + private String id; + private Payload payload; + + @DynamoDBHashKey + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + @DynamoDBTypeConvertedJson + public Payload getPayload() { + return payload; + } + + public void setPayload(Payload payload) { + this.payload = payload; + } + } + + public static class Payload { + private String value; + + public String getValue() { + return value; + } + + public void setValue(String value) { + this.value = value; + } + } +} diff --git a/services-custom/dynamodb-mapper/src/test/java/software/amazon/awssdk/mapper/dynamodb/DynamoDBTypeConvertedJsonTest.java b/services-custom/dynamodb-mapper/src/test/java/software/amazon/awssdk/mapper/dynamodb/DynamoDBTypeConvertedJsonTest.java new file mode 100644 index 000000000000..fea560964a73 --- /dev/null +++ b/services-custom/dynamodb-mapper/src/test/java/software/amazon/awssdk/mapper/dynamodb/DynamoDBTypeConvertedJsonTest.java @@ -0,0 +1,187 @@ +/* + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"). + * You may not use this file except in compliance with the License. + * A copy of the License is located at + * + * http://aws.amazon.com/apache2.0 + * + * or in the "license" file accompanying this file. This file is distributed + * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either + * express or implied. See the License for the specific language governing + * permissions and limitations under the License. + */ + +package software.amazon.awssdk.mapper.dynamodb; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.JsonMappingException; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Map; +import org.junit.jupiter.api.Test; +import software.amazon.awssdk.auth.credentials.AnonymousCredentialsProvider; +import software.amazon.awssdk.regions.Region; +import software.amazon.awssdk.services.dynamodb.DynamoDbClient; +import software.amazon.awssdk.services.dynamodb.model.AttributeValue; + +class DynamoDBTypeConvertedJsonTest { + + private final DynamoDBMapper mapper = DynamoDBMapper.builder() + .dynamoDbClient(DynamoDbClient.builder() + .region(Region.US_EAST_1) + .credentialsProvider(AnonymousCredentialsProvider.create()) + .build()) + .build(); + + @Test + void isAvailable_withJacksonDatabind_returnsTrue() { + assertThat(JacksonDatabindSupport.isAvailable()).isTrue(); + } + + @Test + void getTableModel_jsonAttribute_storesJsonStringAndReadsItBack() { + JsonItem item = new JsonItem(); + item.setId("1"); + item.setCurrency(new Currency(79.99, "USD")); + + DynamoDBMapperTableModel model = mapper.getTableModel(JsonItem.class); + Map stored = model.convert(item); + + assertThat(stored.get("currency").s()).isEqualTo("{\"amount\":79.99,\"unit\":\"USD\"}"); + Currency loaded = model.unconvert(stored).getCurrency(); + assertThat(loaded.getAmount()).isEqualTo(79.99); + assertThat(loaded.getUnit()).isEqualTo("USD"); + } + + @Test + void getTableModel_targetType_readsIntoTargetType() { + ListItem item = new ListItem(); + item.setId("1"); + CurrencyList currencies = new CurrencyList(); + currencies.addAll(Arrays.asList(new Currency(1.0, "EUR"), new Currency(2.0, "GBP"))); + item.setCurrencies(currencies); + + DynamoDBMapperTableModel model = mapper.getTableModel(ListItem.class); + ListItem loaded = model.unconvert(model.convert(item)); + + assertThat(loaded.getCurrencies()).isInstanceOf(CurrencyList.class); + assertThat(loaded.getCurrencies()).extracting(Currency::getUnit).containsExactly("EUR", "GBP"); + } + + @Test + void unconvert_unknownProperty_isIgnored() { + Currency loaded = converter(Currency.class).unconvert("{\"amount\":1.0,\"unit\":\"EUR\",\"unknown\":true}"); + + assertThat(loaded.getUnit()).isEqualTo("EUR"); + } + + @Test + void unconvert_invalidJson_throwsMappingExceptionWithDatabindCause() { + assertThatThrownBy(() -> converter(Currency.class).unconvert("{not json")) + .isInstanceOf(DynamoDBMappingException.class) + .hasMessage("Unable to read JSON string") + .hasCauseInstanceOf(JsonProcessingException.class); + } + + @Test + void convert_unserializableObject_throwsMappingExceptionWithDatabindCause() { + assertThatThrownBy(() -> converter(Object.class).convert(new Object())) + .isInstanceOf(DynamoDBMappingException.class) + .hasMessage("Unable to write object to JSON") + .hasCauseInstanceOf(JsonMappingException.class); + } + + @SuppressWarnings({"unchecked", "rawtypes"}) + private static DynamoDBTypeConvertedJson.Converter converter(Class type) { + try { + DynamoDBTypeConvertedJson annotation = + JsonItem.class.getMethod("getCurrency").getAnnotation(DynamoDBTypeConvertedJson.class); + return new DynamoDBTypeConvertedJson.Converter(type, annotation); + } catch (NoSuchMethodException e) { + throw new AssertionError(e); + } + } + + public static class JsonItem { + private String id; + private Currency currency; + + @DynamoDBHashKey + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + @DynamoDBTypeConvertedJson + public Currency getCurrency() { + return currency; + } + + public void setCurrency(Currency currency) { + this.currency = currency; + } + } + + public static class ListItem { + private String id; + private CurrencyList currencies; + + @DynamoDBHashKey + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + @DynamoDBTypeConvertedJson(targetType = CurrencyList.class) + public CurrencyList getCurrencies() { + return currencies; + } + + public void setCurrencies(CurrencyList currencies) { + this.currencies = currencies; + } + } + + public static class CurrencyList extends ArrayList { + } + + public static class Currency { + private Double amount; + private String unit; + + public Currency() { + } + + Currency(Double amount, String unit) { + this.amount = amount; + this.unit = unit; + } + + public Double getAmount() { + return amount; + } + + public void setAmount(Double amount) { + this.amount = amount; + } + + public String getUnit() { + return unit; + } + + public void setUnit(String unit) { + this.unit = unit; + } + } +} diff --git a/services-custom/dynamodb-mapper/src/test/java/software/amazon/awssdk/mapper/dynamodb/StandardModelFactoriesEdgeCasesTest.java b/services-custom/dynamodb-mapper/src/test/java/software/amazon/awssdk/mapper/dynamodb/StandardModelFactoriesEdgeCasesTest.java index 77358daaba3c..acb3dcc9135d 100644 --- a/services-custom/dynamodb-mapper/src/test/java/software/amazon/awssdk/mapper/dynamodb/StandardModelFactoriesEdgeCasesTest.java +++ b/services-custom/dynamodb-mapper/src/test/java/software/amazon/awssdk/mapper/dynamodb/StandardModelFactoriesEdgeCasesTest.java @@ -48,7 +48,7 @@ public class StandardModelFactoriesEdgeCasesTest { .withConversionSchema(ConversionSchemas.V2) .build(); - private static final DynamoDBMapperModelFactory factory = StandardModelFactories.of(S3Link.Factory.of(null)); + private static final DynamoDBMapperModelFactory factory = StandardModelFactories.of(); private static final DynamoDBMapperModelFactory.TableFactory v1Models = factory.getTableFactory(V1_CONFIG); private static final DynamoDBMapperModelFactory.TableFactory v2Models = factory.getTableFactory(V2_CONFIG); From e7322fee107e1cf4231ed9bb6f586569105f9d36 Mon Sep 17 00:00:00 2001 From: RanVaknin <50976344+RanVaknin@users.noreply.github.com> Date: Fri, 20 Nov 2026 14:05:59 -0800 Subject: [PATCH 3/3] Add v1 fixture compliance tests Check @DynamoDBTypeConvertedJson against JSON recorded from v1's converter (aws-java-sdk-dynamodb 1.12.797 on jackson-databind 2.17.2) over 145 cases. Writes must match byte for byte. Classes with getter-only properties are compared without regard to property order, because jackson-databind orders those by getDeclaredMethods(), which differs between JVMs even in v1. --- ...BTypeConvertedJsonV1CompatibilityTest.java | 144 ++ .../jsonfixtures/JsonModelFixtures.java | 1400 +++++++++++++++++ .../type_converted_json_fixture.json | 147 ++ 3 files changed, 1691 insertions(+) create mode 100644 services-custom/dynamodb-mapper/src/test/java/software/amazon/awssdk/mapper/dynamodb/DynamoDBTypeConvertedJsonV1CompatibilityTest.java create mode 100644 services-custom/dynamodb-mapper/src/test/java/software/amazon/awssdk/mapper/dynamodb/jsonfixtures/JsonModelFixtures.java create mode 100644 services-custom/dynamodb-mapper/src/test/resources/type_converted_json_fixture.json diff --git a/services-custom/dynamodb-mapper/src/test/java/software/amazon/awssdk/mapper/dynamodb/DynamoDBTypeConvertedJsonV1CompatibilityTest.java b/services-custom/dynamodb-mapper/src/test/java/software/amazon/awssdk/mapper/dynamodb/DynamoDBTypeConvertedJsonV1CompatibilityTest.java new file mode 100644 index 000000000000..97453e6e8223 --- /dev/null +++ b/services-custom/dynamodb-mapper/src/test/java/software/amazon/awssdk/mapper/dynamodb/DynamoDBTypeConvertedJsonV1CompatibilityTest.java @@ -0,0 +1,144 @@ +/* + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"). + * You may not use this file except in compliance with the License. + * A copy of the License is located at + * + * http://aws.amazon.com/apache2.0 + * + * or in the "license" file accompanying this file. This file is distributed + * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either + * express or implied. See the License for the specific language governing + * permissions and limitations under the License. + */ + +package software.amazon.awssdk.mapper.dynamodb; + +import static org.assertj.core.api.Assertions.assertThat; + +import com.fasterxml.jackson.core.type.TypeReference; +import com.fasterxml.jackson.databind.ObjectMapper; +import java.io.IOException; +import java.io.InputStream; +import java.io.UncheckedIOException; +import java.util.Arrays; +import java.util.HashSet; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.stream.Stream; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; +import software.amazon.awssdk.mapper.dynamodb.jsonfixtures.JsonModelFixtures; +import software.amazon.awssdk.mapper.dynamodb.jsonfixtures.JsonModelFixtures.ReadCase; + +/** + * Checks {@link DynamoDBTypeConvertedJson.Converter} against JSON recorded from the v1 mapper. + * + *

{@code type_converted_json_fixture.json} holds the output of v1's + * {@code com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBTypeConvertedJson.Converter} from + * {@code aws-java-sdk-dynamodb} 1.12.797, running on jackson-databind 2.17.2, the version that release depends on, + * for every case in {@link JsonModelFixtures}. Write cases hold the JSON v1 wrote; read cases hold the object v1 read, + * serialized again with a default {@code ObjectMapper}. + */ +class DynamoDBTypeConvertedJsonV1CompatibilityTest { + + private static final String FIXTURE = "type_converted_json_fixture.json"; + + /** + * Classes with properties that have a getter but no field. jackson-databind orders those properties by + * {@code Class.getDeclaredMethods()}, which the JVM doesn't specify, so their JSON can differ between JVMs even in + * v1. Cases that include them are compared without regard to property order. + */ + private static final Set GETTER_ONLY_CLASSES = new HashSet<>(Arrays.asList( + "SubclassWithFieldsAndGetters", "OverridesGetter", "GetterOnly", "ManglesGetterNames", "IsGetters")); + + private static final ObjectMapper JSON = new ObjectMapper(); + + private static final Map RECORDED = load(); + + private static final DynamoDBTypeConvertedJson ANNOTATION = annotation(); + + @DynamoDBTypeConvertedJson + private Object holder; + + static Stream writeCases() { + List writes = JsonModelFixtures.writeCorpus(); + return Stream.iterate(0, i -> i + 1).limit(writes.size()) + .map(i -> Arguments.of(writeName(i, writes.get(i)), writes.get(i))); + } + + static Stream readCases() { + List reads = JsonModelFixtures.readCorpus(); + return Stream.iterate(0, i -> i + 1).limit(reads.size()) + .map(i -> Arguments.of(readName(i, reads.get(i)), reads.get(i))); + } + + @ParameterizedTest(name = "{0}") + @MethodSource("writeCases") + void convert_matchesV1(String name, Object value) throws IOException { + assertMatchesRecorded(name, converter(value == null ? Object.class : value.getClass()).convert(value)); + } + + @ParameterizedTest(name = "{0}") + @MethodSource("readCases") + void unconvert_matchesV1(String name, ReadCase readCase) throws IOException { + Object read = converter(readCase.targetType()).unconvert(readCase.json()); + + assertMatchesRecorded(name, JSON.writeValueAsString(read)); + } + + @Test + void fixture_hasExactlyOneEntryPerCase() { + Map expected = new LinkedHashMap<>(); + writeCases().forEach(a -> expected.put((String) a.get()[0], "")); + readCases().forEach(a -> expected.put((String) a.get()[0], "")); + + assertThat(RECORDED.keySet()).containsExactlyInAnyOrderElementsOf(expected.keySet()); + } + + private static void assertMatchesRecorded(String name, String actual) throws IOException { + String recorded = RECORDED.get(name); + assertThat(recorded).as("recorded v1 output for %s", name).isNotNull(); + if (GETTER_ONLY_CLASSES.contains(name.substring(name.indexOf('-') + 1))) { + assertThat(JSON.readTree(actual)).as(name).isEqualTo(JSON.readTree(recorded)); + } else { + assertThat(actual).as(name).isEqualTo(recorded); + } + } + + private static String writeName(int index, Object value) { + return String.format("write/%03d-%s", index, value == null ? "null" : value.getClass().getSimpleName()); + } + + private static String readName(int index, ReadCase readCase) { + return String.format("read/%03d-%s", index, readCase.targetType().getSimpleName()); + } + + @SuppressWarnings({"unchecked", "rawtypes"}) + private static DynamoDBTypeConvertedJson.Converter converter(Class type) { + return new DynamoDBTypeConvertedJson.Converter(type, ANNOTATION); + } + + private static Map load() { + try (InputStream in = DynamoDBTypeConvertedJsonV1CompatibilityTest.class.getClassLoader() + .getResourceAsStream(FIXTURE)) { + return JSON.readValue(in, new TypeReference>() { }); + } catch (IOException e) { + throw new UncheckedIOException(e); + } + } + + private static DynamoDBTypeConvertedJson annotation() { + try { + return DynamoDBTypeConvertedJsonV1CompatibilityTest.class.getDeclaredField("holder") + .getAnnotation(DynamoDBTypeConvertedJson.class); + } catch (NoSuchFieldException e) { + throw new AssertionError(e); + } + } +} diff --git a/services-custom/dynamodb-mapper/src/test/java/software/amazon/awssdk/mapper/dynamodb/jsonfixtures/JsonModelFixtures.java b/services-custom/dynamodb-mapper/src/test/java/software/amazon/awssdk/mapper/dynamodb/jsonfixtures/JsonModelFixtures.java new file mode 100644 index 000000000000..8d6a348c2ef5 --- /dev/null +++ b/services-custom/dynamodb-mapper/src/test/java/software/amazon/awssdk/mapper/dynamodb/jsonfixtures/JsonModelFixtures.java @@ -0,0 +1,1400 @@ +/* + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"). + * You may not use this file except in compliance with the License. + * A copy of the License is located at + * + * http://aws.amazon.com/apache2.0 + * + * or in the "license" file accompanying this file. This file is distributed + * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either + * express or implied. See the License for the specific language governing + * permissions and limitations under the License. + */ + +package software.amazon.awssdk.mapper.dynamodb.jsonfixtures; + +import com.fasterxml.jackson.annotation.JsonIgnore; +import com.fasterxml.jackson.annotation.JsonProperty; +import com.fasterxml.jackson.annotation.JsonPropertyOrder; +import java.math.BigDecimal; +import java.math.BigInteger; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Calendar; +import java.util.Collections; +import java.util.Date; +import java.util.GregorianCalendar; +import java.util.LinkedHashMap; +import java.util.LinkedHashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.TimeZone; +import java.util.TreeMap; +import java.util.UUID; + +/** + * Object shapes and JSON inputs for checking that {@link software.amazon.awssdk.mapper.dynamodb.DynamoDBTypeConvertedJson} + * writes and reads the same JSON as the v1 mapper. + * + *

The shapes follow how customers used the v1 annotation: Lombok-style POJOs with private fields and generated + * accessors, nested beans and collections, named {@code ArrayList} subclasses passed as {@code targetType()}, numbers, + * dates and strings that need escaping, and classes with {@code @JsonProperty}, {@code @JsonIgnore} and + * {@code @JsonPropertyOrder}. + */ +public final class JsonModelFixtures { + + private JsonModelFixtures() { + } + + /** + * Every value whose serialized form must match jackson-databind exactly. + */ + public static List writeCorpus() { + List corpus = new ArrayList<>(); + + // --- The example from the annotation's own javadoc. If nothing else matches, this must. --- + corpus.add(new Currency(79.99, "USD")); + corpus.add(new Currency(null, null)); + + // --- Lombok-shaped beans: the dominant real-world shape. --- + corpus.add(LombokShaped.populated()); + corpus.add(new LombokShaped()); + + // --- Nesting and collections. --- + corpus.add(new Nested(new Currency(1.0, "EUR"), Arrays.asList(new Currency(2.0, "GBP"), null))); + corpus.add(new Nested(null, Collections.emptyList())); + corpus.add(currencyList()); + corpus.add(new CurrencyList()); + corpus.add(Arrays.asList(new Currency(3.0, "JPY"), new Currency(4.0, "CHF"))); + corpus.add(new HasCollections()); + corpus.add(HasCollections.populated()); + + // --- Maps: key stringification and iteration order. --- + corpus.add(stringKeyedMap()); + corpus.add(integerKeyedMap()); + corpus.add(enumKeyedMap()); + corpus.add(sortedMap()); + corpus.add(new HasMaps(stringKeyedMap())); + + // --- Numbers, where a hand-written encoder is most likely to drift. --- + corpus.add(Numbers.populated()); + corpus.add(new Numbers()); + corpus.add(new Doubles(0.0, -0.0, 1.0)); + corpus.add(new Doubles(1.0E20, 1.0E-20, 1.0 / 3.0)); + corpus.add(new Doubles(Double.NaN, Double.POSITIVE_INFINITY, Double.NEGATIVE_INFINITY)); + corpus.add(new Doubles(Double.MIN_VALUE, Double.MAX_VALUE, Double.MIN_NORMAL)); + corpus.add(new Floats(0.0f, 1.0f / 3.0f, Float.MAX_VALUE)); + corpus.add(new Bigs(new BigDecimal("1.2300"), BigInteger.valueOf(42))); + corpus.add(new Bigs(new BigDecimal("1E+10"), new BigInteger("123456789012345678901234567890"))); + corpus.add(new Bigs(new BigDecimal("0.000000000000000000001"), BigInteger.ZERO)); + corpus.add(new Bigs(BigDecimal.ZERO.setScale(5), BigInteger.valueOf(-1))); + + // --- Strings and escaping, which is jackson-core's job and so should be free. --- + for (String string : STRINGS) { + corpus.add(new HasString(string)); + } + corpus.add(new HasChar('a')); + corpus.add(new HasChar('"')); + corpus.add(new HasChar('\n')); + + // --- Other scalars with dedicated databind serializers. --- + corpus.add(new HasDate(new Date(0L), new Date(1_700_000_000_123L))); + corpus.add(new HasDate(null, null)); + corpus.add(new HasCalendar(calendar(1_700_000_000_123L))); + corpus.add(new HasUuid(UUID.fromString("c2a4fcd3-0c3b-4cfb-9ff7-c1cb03e2e9f6"))); + corpus.add(new HasEnum(Color.RED)); + corpus.add(new HasEnum(null)); + corpus.add(new HasBytes(new byte[] {0, 1, 2, -1})); + corpus.add(new HasBytes(new byte[0])); + corpus.add(new HasBytes(null)); + corpus.add(new HasChars(new char[] {'h', 'i'})); + corpus.add(new HasArrays()); + corpus.add(HasArrays.populated()); + + // --- Property discovery and ordering. --- + corpus.add(new PublicFields()); + corpus.add(new MixedVisibility()); + corpus.add(new SkipsStaticAndTransient()); + corpus.add(new SubclassWithFieldsAndGetters()); + corpus.add(new OverridesGetter()); + corpus.add(new GetterOnly()); + corpus.add(new ManglesGetterNames()); + corpus.add(new IsGetters()); + corpus.add(new PrivateNoArgConstructor()); + + // --- @JsonProperty, @JsonIgnore and @JsonPropertyOrder, each case measured against databind. --- + corpus.add(new RenameOnField()); + corpus.add(new RenameOnGetter()); + corpus.add(new RenameMiddle()); + corpus.add(new TwoRenamed()); + corpus.add(new SameNameAsDefault()); + corpus.add(new RenameOnSetterOnly()); + corpus.add(new PrivateFieldNoGetter()); + corpus.add(new PrivateFieldRenamedNoGetter()); + corpus.add(new PrivateGetter()); + corpus.add(new NonBeanMethod()); + corpus.add(new SubOverridesRenamed()); + corpus.add(new IgnoreField()); + corpus.add(new IgnoreGetter()); + corpus.add(new IgnoreSetterOnly()); + corpus.add(new IgnoreFalse()); + corpus.add(new IgnoreFieldButPropertyOnGetter()); + corpus.add(new SubOverridesIgnored()); + corpus.add(new OrderFull()); + corpus.add(new OrderPartial()); + corpus.add(new OrderUnknownName()); + corpus.add(new OrderAlpha()); + corpus.add(new OrderPartialAlpha()); + corpus.add(new OrderByRenamed()); + corpus.add(new OrderByImplicitOfRenamed()); + corpus.add(new OrderInherited()); + corpus.add(new OrderOverridden()); + corpus.add(new AlphaWithRename()); + corpus.add(new Nested(null, Arrays.asList(new Currency(1.0, "EUR")))); // unannotated still unaffected + + // --- Top-level values that are not beans at all. --- + corpus.add("a plain string"); + corpus.add(Integer.valueOf(42)); + corpus.add(Boolean.TRUE); + corpus.add(null); + corpus.add(Color.GREEN); + corpus.add(new BigDecimal("1.2300")); + corpus.add(new Date(1_700_000_000_123L)); + corpus.add(new byte[] {1, 2, 3}); + corpus.add(Arrays.asList(1, "two", null, true)); + corpus.add(stringKeyedMap()); + + return corpus; + } + + /** + * JSON documents that must deserialize to the same object databind produces, paired with the target type. + */ + public static List readCorpus() { + List corpus = new ArrayList<>(); + + corpus.add(new ReadCase("{\"amount\":79.99,\"unit\":\"USD\"}", Currency.class)); + corpus.add(new ReadCase("{\"unit\":\"USD\",\"amount\":79.99}", Currency.class)); + + // The one behaviour the deleted v1 TypeConvertedJsonTest actually asserted: an item carrying a property the + // model does not have must still load, because the v1 converter disabled FAIL_ON_UNKNOWN_PROPERTIES. + corpus.add(new ReadCase("{\"amount\":1.0,\"unit\":\"USD\",\"unknownField\":\"unknownValue\"}", Currency.class)); + corpus.add(new ReadCase("{\"unknownOnly\":1}", Currency.class)); + corpus.add(new ReadCase("{}", Currency.class)); + corpus.add(new ReadCase("{\"amount\":null,\"unit\":null}", Currency.class)); + corpus.add(new ReadCase("null", Currency.class)); + + // Numeric coercion. + corpus.add(new ReadCase("{\"i\":1.0,\"l\":2.0,\"s\":3,\"b\":4,\"d\":5,\"f\":6}", Numbers.class)); + corpus.add(new ReadCase("{\"i\":1.5,\"l\":-2.5}", Numbers.class)); + corpus.add(new ReadCase("{\"i\":\"5\",\"l\":\"6\"}", Numbers.class)); + corpus.add(new ReadCase("{\"i\":null,\"l\":null,\"d\":null}", Numbers.class)); + corpus.add(new ReadCase("{\"decimal\":1.2300,\"integer\":123456789012345678901234567890}", Bigs.class)); + + // Both forms databind can write for a Date. + corpus.add(new ReadCase("{\"epoch\":0,\"later\":1700000000123}", HasDate.class)); + corpus.add(new ReadCase("{\"epoch\":\"1970-01-01T00:00:00.000+0000\"}", HasDate.class)); + + corpus.add(new ReadCase("{\"bytes\":\"AAEC/w==\"}", HasBytes.class)); + corpus.add(new ReadCase("{\"color\":\"RED\"}", HasEnum.class)); + corpus.add(new ReadCase("{\"color\":null}", HasEnum.class)); + corpus.add(new ReadCase("{\"id\":\"c2a4fcd3-0c3b-4cfb-9ff7-c1cb03e2e9f6\"}", HasUuid.class)); + corpus.add(new ReadCase("{\"text\":\"line1\\nline2\\u0000\\\"quoted\\\" \\u00e9 \\u4e2d\"}", HasString.class)); + corpus.add(new ReadCase("{\"text\":42}", HasString.class)); + + corpus.add(new ReadCase("{\"one\":{\"amount\":1.0,\"unit\":\"EUR\"}," + + "\"many\":[{\"amount\":2.0,\"unit\":\"GBP\"},null]}", Nested.class)); + corpus.add(new ReadCase("{\"one\":null,\"many\":[]}", Nested.class)); + + // The named-ArrayList-subclass trick v1 documented for generic element types. + corpus.add(new ReadCase("[{\"amount\":1.0,\"unit\":\"EUR\"},{\"amount\":2.0,\"unit\":\"GBP\"}]", + CurrencyList.class)); + corpus.add(new ReadCase("[]", CurrencyList.class)); + + corpus.add(new ReadCase("{\"list\":[\"a\",\"b\"],\"set\":[\"a\",\"a\",\"b\"],\"nested\":[[1,2],[3]]}", + HasCollections.class)); + corpus.add(new ReadCase("{\"byName\":{\"a\":{\"amount\":1.0,\"unit\":\"EUR\"}}}", HasMaps.class)); + + corpus.add(new ReadCase("{\"ints\":[1,2,3],\"strings\":[\"a\",null]," + + "\"currencies\":[{\"amount\":1.0,\"unit\":\"EUR\"}]}", HasArrays.class)); + + // Untyped targets: databind's own choice of Integer / Long / BigInteger / Double / LinkedHashMap / ArrayList. + corpus.add(new ReadCase("{\"any\":{\"i\":1,\"big\":123456789012345678901234567890,\"l\":4294967296," + + "\"d\":1.5,\"s\":\"x\",\"b\":true,\"n\":null,\"a\":[1,[2]],\"o\":{\"k\":\"v\"}}}", + HasObject.class)); + corpus.add(new ReadCase("{\"any\":[1,2,3]}", HasObject.class)); + + corpus.add(new ReadCase("{\"flag\":true,\"boxed\":false,\"ready\":true}", IsGetters.class)); + corpus.add(new ReadCase("{\"id\":7,\"name\":\"n\"}", PrivateNoArgConstructor.class)); + corpus.add(new ReadCase("{\"baseField\":\"b\",\"subField\":\"s\"}", SubclassWithFieldsAndGetters.class)); + + // Read-only properties: a getter with no setter, and a private field with no setter. With + // FAIL_ON_UNKNOWN_PROPERTIES disabled both are simply skipped. + corpus.add(new ReadCase("{\"readOnly\":\"ignored\",\"writable\":\"kept\"}", PartlyWritable.class)); + + corpus.add(new ReadCase("{\"x_a\":\"1\",\"b\":\"2\"}", ReadRenamed.class)); + corpus.add(new ReadCase("{\"a\":\"1\",\"b\":\"2\"}", ReadRenamed.class)); + corpus.add(new ReadCase("{\"h\":\"1\"}", ReadPrivateField.class)); + corpus.add(new ReadCase("{\"a\":\"1\",\"b\":\"2\"}", ReadIgnored.class)); + corpus.add(new ReadCase("{\"f\":\"fromjson\"}", ReadFinalField.class)); + corpus.add(new ReadCase("\"top level string\"", String.class)); + corpus.add(new ReadCase("42", Integer.class)); + corpus.add(new ReadCase("true", Boolean.class)); + + return corpus; + } + + /** + * Values whose serialized form a round trip must preserve exactly. Same objects as {@link #writeCorpus()} minus + * the ones that cannot round trip through a declared type (top-level scalars and raw collections). + */ + public static List roundTripCorpus() { + List corpus = new ArrayList<>(); + for (Object value : writeCorpus()) { + if (value != null && isRoundTrippable(value.getClass())) { + corpus.add(value); + } + } + return corpus; + } + + private static boolean isRoundTrippable(Class type) { + return type.getName().startsWith(JsonModelFixtures.class.getName()) + || type == CurrencyList.class + || type == Currency.class; + } + + /** + * Strings chosen for escaping behaviour: quoting, the characters jackson-core does and does not escape, C0 + * controls, non-ASCII, and a surrogate pair. + */ + private static final String[] STRINGS = { + "", + "plain", + "with \"quotes\"", + "back\\slash", + "forward/slash", + "new\nline", + "tab\there", + "carriage\rreturn", + "\u0000\u0001\u001f", + "\u007f", + "caf\u00e9", + "\u4e2d\u6587", + "emoji \ud83d\ude00", + " leading and trailing ", + "a\u2028b\u2029c", + }; + + private static CurrencyList currencyList() { + CurrencyList list = new CurrencyList(); + list.add(new Currency(1.0, "EUR")); + list.add(new Currency(2.0, "GBP")); + return list; + } + + private static Map stringKeyedMap() { + Map map = new LinkedHashMap<>(); + map.put("zulu", new Currency(1.0, "EUR")); + map.put("alpha", new Currency(2.0, "GBP")); + map.put("", null); + return map; + } + + private static Map integerKeyedMap() { + Map map = new LinkedHashMap<>(); + map.put(2, "two"); + map.put(1, "one"); + return map; + } + + private static Map enumKeyedMap() { + Map map = new LinkedHashMap<>(); + map.put(Color.RED, "r"); + map.put(Color.GREEN, "g"); + return map; + } + + private static Map sortedMap() { + Map map = new TreeMap<>(); + map.put("zulu", 1); + map.put("alpha", 2); + return map; + } + + private static Calendar calendar(long millis) { + Calendar calendar = new GregorianCalendar(TimeZone.getTimeZone("UTC")); + calendar.setTimeInMillis(millis); + return calendar; + } + + /** + * A JSON document plus the type to read it as. + */ + public static final class ReadCase { + private final String json; + private final Class targetType; + + public ReadCase(String json, Class targetType) { + this.json = json; + this.targetType = targetType; + } + + public String json() { + return json; + } + + public Class targetType() { + return targetType; + } + + @Override + public String toString() { + return targetType.getSimpleName() + " <- " + json; + } + } + + // ------------------------------------------------------------------------------------------------------------- + // Models + // ------------------------------------------------------------------------------------------------------------- + + /** The annotation javadoc's own example, character for character. */ + public static class Currency { + private Double amount; + private String unit; + + public Currency() { + } + + public Currency(Double amount, String unit) { + this.amount = amount; + this.unit = unit; + } + + public Double getAmount() { + return amount; + } + + public void setAmount(Double amount) { + this.amount = amount; + } + + public String getUnit() { + return unit; + } + + public void setUnit(String unit) { + this.unit = unit; + } + } + + /** v1 documented this shape - a named ArrayList subclass - as the way to get a generic element type. */ + public static class CurrencyList extends ArrayList { + private static final long serialVersionUID = 1L; + } + + /** + * What Lombok's {@code @Data} generates: private fields, public getters and setters, declaration-ordered. + * The survey puts this shape at ~83% of call sites, which is also why it is the shape with stable ordering. + */ + public static class LombokShaped { + private String id; + private int count; + private boolean active; + private Double ratio; + private List tags; + + static LombokShaped populated() { + LombokShaped value = new LombokShaped(); + value.setId("abc"); + value.setCount(7); + value.setActive(true); + value.setRatio(0.5); + value.setTags(Arrays.asList("x", "y")); + return value; + } + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public int getCount() { + return count; + } + + public void setCount(int count) { + this.count = count; + } + + public boolean isActive() { + return active; + } + + public void setActive(boolean active) { + this.active = active; + } + + public Double getRatio() { + return ratio; + } + + public void setRatio(Double ratio) { + this.ratio = ratio; + } + + public List getTags() { + return tags; + } + + public void setTags(List tags) { + this.tags = tags; + } + } + + public static class Nested { + private Currency one; + private List many; + + public Nested() { + } + + public Nested(Currency one, List many) { + this.one = one; + this.many = many; + } + + public Currency getOne() { + return one; + } + + public void setOne(Currency one) { + this.one = one; + } + + public List getMany() { + return many; + } + + public void setMany(List many) { + this.many = many; + } + } + + public static class HasCollections { + private List list; + private Set set; + private List> nested; + + static HasCollections populated() { + HasCollections value = new HasCollections(); + value.setList(Arrays.asList("a", null, "b")); + value.setSet(new LinkedHashSet<>(Arrays.asList("a", "b"))); + value.setNested(Arrays.asList(Arrays.asList(1, 2), Collections.singletonList(3))); + return value; + } + + public List getList() { + return list; + } + + public void setList(List list) { + this.list = list; + } + + public Set getSet() { + return set; + } + + public void setSet(Set set) { + this.set = set; + } + + public List> getNested() { + return nested; + } + + public void setNested(List> nested) { + this.nested = nested; + } + } + + public static class HasMaps { + private Map byName; + + public HasMaps() { + } + + public HasMaps(Map byName) { + this.byName = byName; + } + + public Map getByName() { + return byName; + } + + public void setByName(Map byName) { + this.byName = byName; + } + } + + public static class Numbers { + private int i; + private long l; + private short s; + private byte b; + private double d; + private float f; + private Integer boxedInt; + private Long boxedLong; + + static Numbers populated() { + Numbers value = new Numbers(); + value.setI(Integer.MIN_VALUE); + value.setL(Long.MAX_VALUE); + value.setS((short) -1); + value.setB((byte) -128); + value.setD(1.5); + value.setF(2.5f); + value.setBoxedInt(0); + value.setBoxedLong(-1L); + return value; + } + + public int getI() { + return i; + } + + public void setI(int i) { + this.i = i; + } + + public long getL() { + return l; + } + + public void setL(long l) { + this.l = l; + } + + public short getS() { + return s; + } + + public void setS(short s) { + this.s = s; + } + + public byte getB() { + return b; + } + + public void setB(byte b) { + this.b = b; + } + + public double getD() { + return d; + } + + public void setD(double d) { + this.d = d; + } + + public float getF() { + return f; + } + + public void setF(float f) { + this.f = f; + } + + public Integer getBoxedInt() { + return boxedInt; + } + + public void setBoxedInt(Integer boxedInt) { + this.boxedInt = boxedInt; + } + + public Long getBoxedLong() { + return boxedLong; + } + + public void setBoxedLong(Long boxedLong) { + this.boxedLong = boxedLong; + } + } + + public static class Doubles { + private double a; + private double b; + private Double c; + + public Doubles() { + } + + public Doubles(double a, double b, Double c) { + this.a = a; + this.b = b; + this.c = c; + } + + public double getA() { + return a; + } + + public void setA(double a) { + this.a = a; + } + + public double getB() { + return b; + } + + public void setB(double b) { + this.b = b; + } + + public Double getC() { + return c; + } + + public void setC(Double c) { + this.c = c; + } + } + + public static class Floats { + private float a; + private float b; + private Float c; + + public Floats() { + } + + public Floats(float a, float b, Float c) { + this.a = a; + this.b = b; + this.c = c; + } + + public float getA() { + return a; + } + + public void setA(float a) { + this.a = a; + } + + public float getB() { + return b; + } + + public void setB(float b) { + this.b = b; + } + + public Float getC() { + return c; + } + + public void setC(Float c) { + this.c = c; + } + } + + public static class Bigs { + private BigDecimal decimal; + private BigInteger integer; + + public Bigs() { + } + + public Bigs(BigDecimal decimal, BigInteger integer) { + this.decimal = decimal; + this.integer = integer; + } + + public BigDecimal getDecimal() { + return decimal; + } + + public void setDecimal(BigDecimal decimal) { + this.decimal = decimal; + } + + public BigInteger getInteger() { + return integer; + } + + public void setInteger(BigInteger integer) { + this.integer = integer; + } + } + + public static class HasString { + private String text; + + public HasString() { + } + + public HasString(String text) { + this.text = text; + } + + public String getText() { + return text; + } + + public void setText(String text) { + this.text = text; + } + } + + public static class HasChar { + private char c; + private Character boxed; + + public HasChar() { + } + + public HasChar(char c) { + this.c = c; + this.boxed = c; + } + + public char getC() { + return c; + } + + public void setC(char c) { + this.c = c; + } + + public Character getBoxed() { + return boxed; + } + + public void setBoxed(Character boxed) { + this.boxed = boxed; + } + } + + public static class HasDate { + private Date epoch; + private Date later; + + public HasDate() { + } + + public HasDate(Date epoch, Date later) { + this.epoch = epoch; + this.later = later; + } + + public Date getEpoch() { + return epoch; + } + + public void setEpoch(Date epoch) { + this.epoch = epoch; + } + + public Date getLater() { + return later; + } + + public void setLater(Date later) { + this.later = later; + } + } + + public static class HasCalendar { + private Calendar when; + + public HasCalendar() { + } + + public HasCalendar(Calendar when) { + this.when = when; + } + + public Calendar getWhen() { + return when; + } + + public void setWhen(Calendar when) { + this.when = when; + } + } + + public static class HasUuid { + private UUID id; + + public HasUuid() { + } + + public HasUuid(UUID id) { + this.id = id; + } + + public UUID getId() { + return id; + } + + public void setId(UUID id) { + this.id = id; + } + } + + public enum Color { + RED, GREEN + } + + public static class HasEnum { + private Color color; + + public HasEnum() { + } + + public HasEnum(Color color) { + this.color = color; + } + + public Color getColor() { + return color; + } + + public void setColor(Color color) { + this.color = color; + } + } + + public static class HasBytes { + private byte[] bytes; + + public HasBytes() { + } + + public HasBytes(byte[] bytes) { + this.bytes = bytes; + } + + public byte[] getBytes() { + return bytes; + } + + public void setBytes(byte[] bytes) { + this.bytes = bytes; + } + } + + public static class HasChars { + private char[] chars; + + public HasChars() { + } + + public HasChars(char[] chars) { + this.chars = chars; + } + + public char[] getChars() { + return chars; + } + + public void setChars(char[] chars) { + this.chars = chars; + } + } + + public static class HasArrays { + private int[] ints; + private String[] strings; + private Currency[] currencies; + + static HasArrays populated() { + HasArrays value = new HasArrays(); + value.setInts(new int[] {1, 2, 3}); + value.setStrings(new String[] {"a", null}); + value.setCurrencies(new Currency[] {new Currency(1.0, "EUR")}); + return value; + } + + public int[] getInts() { + return ints; + } + + public void setInts(int[] ints) { + this.ints = ints; + } + + public String[] getStrings() { + return strings; + } + + public void setStrings(String[] strings) { + this.strings = strings; + } + + public Currency[] getCurrencies() { + return currencies; + } + + public void setCurrencies(Currency[] currencies) { + this.currencies = currencies; + } + } + + public static class HasObject { + private Object any; + + public Object getAny() { + return any; + } + + public void setAny(Object any) { + this.any = any; + } + } + + /** Public fields with no getters are still properties, since the default field visibility is PUBLIC_ONLY. */ + public static class PublicFields { + public String first = "1"; + public int second = 2; + public final String third = "3"; + } + + /** A private field with no getter is invisible, but it still occupies an ordering slot. */ + public static class MixedVisibility { + private String invisible = "hidden"; + public String visibleField = "shown"; + private String withGetter = "also shown"; + + public String getWithGetter() { + return withGetter; + } + } + + public static class SkipsStaticAndTransient { + public static String staticField = "skipped"; + public transient String transientField = "skipped"; + public String kept = "kept"; + } + + /** Pins the measured ordering rule: field slots first (superclass first), then getter-only slots. */ + public static class BaseWithFieldAndGetter { + public String baseField = "bf"; + + public String getBaseGetter() { + return "bg"; + } + } + + public static class SubclassWithFieldsAndGetters extends BaseWithFieldAndGetter { + public String subField = "sf"; + + public String getSubGetter() { + return "sg"; + } + } + + public static class BaseWithGetter { + public String getValue() { + return "base"; + } + } + + /** An overridden getter is one property, not two. */ + public static class OverridesGetter extends BaseWithGetter { + @Override + public String getValue() { + return "sub"; + } + } + + /** + * No backing fields at all, so every property's position comes from {@code getDeclaredMethods()}. Included on + * purpose: whatever order the JVM hands back, both encoders must agree on it, since they ask the same JVM. + */ + public static class GetterOnly { + public String getZulu() { + return "z"; + } + + public String getAlpha() { + return "a"; + } + + public int getMike() { + return 13; + } + } + + /** Pins databind's legacy name mangling, which lowercases the whole leading uppercase run. */ + public static class ManglesGetterNames { + public String getURL() { + return "u"; + } + + public String getPTLevelRequestIds() { + return "p"; + } + + public String getXMLHttpRequest() { + return "x"; + } + + public String getA() { + return "a"; + } + + public String getAB() { + return "ab"; + } + + public String getABc() { + return "abc"; + } + + public String getaLowerFirst() { + return "l"; + } + } + + public static class IsGetters { + private boolean flag = true; + private Boolean boxed = Boolean.FALSE; + + public boolean isFlag() { + return flag; + } + + public void setFlag(boolean flag) { + this.flag = flag; + } + + public Boolean isBoxed() { + return boxed; + } + + public void setBoxed(Boolean boxed) { + this.boxed = boxed; + } + + public boolean isReady() { + return true; + } + } + + /** databind will use a non-public no-arg constructor, and Lombok's {@code @NoArgsConstructor} often makes one. */ + public static class PrivateNoArgConstructor { + private int id; + private String name; + + private PrivateNoArgConstructor() { + } + + public PrivateNoArgConstructor(int id, String name) { + this.id = id; + this.name = name; + } + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + } + + /** A read-only property alongside a writable one, to pin what happens on the read path. */ + public static class PartlyWritable { + private String writable; + + public String getReadOnly() { + return "constant"; + } + + public String getWritable() { + return writable; + } + + public void setWritable(String writable) { + this.writable = writable; + } + } + + // --------------------------------------------------------------------------------------------------------------- + // Jackson-annotated shapes. The expected bytes are whatever databind writes; see the measured notes in + // JsonBeanModel. Renamed properties move to the end, even when the new name equals the old one. + // --------------------------------------------------------------------------------------------------------------- + + public static class RenameOnField { + @JsonProperty("x_a") private String a = "A"; + private String b = "B"; + public String getA() { return a; } + public String getB() { return b; } + } + + public static class RenameOnGetter { + private String a = "A"; + private String b = "B"; + @JsonProperty("x_a") public String getA() { return a; } + public String getB() { return b; } + } + + public static class RenameMiddle { + private String a = "A"; + @JsonProperty("z") private String b = "B"; + private String c = "C"; + public String getA() { return a; } + public String getB() { return b; } + public String getC() { return c; } + } + + public static class TwoRenamed { + @JsonProperty("q") private String a = "A"; + private String b = "B"; + @JsonProperty("p") private String c = "C"; + public String getA() { return a; } + public String getB() { return b; } + public String getC() { return c; } + } + + public static class SameNameAsDefault { + @JsonProperty("a") private String a = "A"; + private String b = "B"; + public String getA() { return a; } + public String getB() { return b; } + } + + public static class RenameOnSetterOnly { + private String a = "A"; + private String b = "B"; + public String getA() { return a; } + @JsonProperty("x_a") public void setA(String v) { a = v; } + public String getB() { return b; } + } + + public static class PrivateFieldNoGetter { + @JsonProperty private String hidden = "H"; + private String b = "B"; + public String getB() { return b; } + } + + public static class PrivateFieldRenamedNoGetter { + @JsonProperty("h") private String hidden = "H"; + private String b = "B"; + public String getB() { return b; } + } + + public static class PrivateGetter { + private String a = "A"; + private String b = "B"; + @JsonProperty private String getA() { return a; } + public String getB() { return b; } + } + + public static class NonBeanMethod { + private String b = "B"; + public String getB() { return b; } + @JsonProperty public String computed() { return "C"; } + } + + public static class SuperRenamed { + private String a = "A"; + private String b = "B"; + @JsonProperty("x_a") public String getA() { return a; } + public String getB() { return b; } + } + + public static class SubOverridesRenamed extends SuperRenamed { + @Override public String getA() { return "SUB"; } + } + + public static class IgnoreField { + @JsonIgnore private String a = "A"; + private String b = "B"; + public String getA() { return a; } + public String getB() { return b; } + } + + public static class IgnoreGetter { + private String a = "A"; + private String b = "B"; + @JsonIgnore public String getA() { return a; } + public String getB() { return b; } + } + + public static class IgnoreSetterOnly { + private String a = "A"; + private String b = "B"; + public String getA() { return a; } + @JsonIgnore public void setA(String v) { a = v; } + public String getB() { return b; } + } + + public static class IgnoreFalse { + @JsonIgnore(false) private String a = "A"; + private String b = "B"; + public String getA() { return a; } + public String getB() { return b; } + } + + public static class IgnoreFieldButPropertyOnGetter { + @JsonIgnore private String a = "A"; + private String b = "B"; + @JsonProperty public String getA() { return a; } + public String getB() { return b; } + } + + public static class SuperIgnored { + private String a = "A"; + private String b = "B"; + @JsonIgnore public String getA() { return a; } + public String getB() { return b; } + } + + public static class SubOverridesIgnored extends SuperIgnored { + @Override public String getA() { return "SUB"; } + } + + @JsonPropertyOrder({"c", "a", "b"}) + public static class OrderFull { + private String a = "A"; + private String b = "B"; + private String c = "C"; + public String getA() { return a; } + public String getB() { return b; } + public String getC() { return c; } + } + + @JsonPropertyOrder({"c"}) + public static class OrderPartial { + private String a = "A"; + private String b = "B"; + private String c = "C"; + public String getA() { return a; } + public String getB() { return b; } + public String getC() { return c; } + } + + @JsonPropertyOrder({"nope", "b"}) + public static class OrderUnknownName { + private String a = "A"; + private String b = "B"; + public String getA() { return a; } + public String getB() { return b; } + } + + @JsonPropertyOrder(alphabetic = true) + public static class OrderAlpha { + private String c = "C"; + private String a = "A"; + private String b = "B"; + public String getA() { return a; } + public String getB() { return b; } + public String getC() { return c; } + } + + @JsonPropertyOrder(value = {"c"}, alphabetic = true) + public static class OrderPartialAlpha { + private String c2 = "x"; + private String b = "B"; + private String a = "A"; + private String c = "C"; + public String getC2() { return c2; } + public String getA() { return a; } + public String getB() { return b; } + public String getC() { return c; } + } + + @JsonPropertyOrder({"x_a", "b"}) + public static class OrderByRenamed { + private String b = "B"; + @JsonProperty("x_a") private String a = "A"; + public String getA() { return a; } + public String getB() { return b; } + } + + @JsonPropertyOrder({"a", "b"}) + public static class OrderByImplicitOfRenamed { + private String b = "B"; + @JsonProperty("x_a") private String a = "A"; + public String getA() { return a; } + public String getB() { return b; } + } + + @JsonPropertyOrder({"b", "a"}) + public static class OrderBase { + private String a = "A"; + private String b = "B"; + public String getA() { return a; } + public String getB() { return b; } + } + + public static class OrderInherited extends OrderBase { + private String c = "C"; + public String getC() { return c; } + } + + @JsonPropertyOrder({"c"}) + public static class OrderOverridden extends OrderBase { + private String c = "C"; + public String getC() { return c; } + } + + @JsonPropertyOrder(alphabetic = true) + public static class AlphaWithRename { + @JsonProperty("m") private String z = "Z"; + private String b = "B"; + private String a = "A"; + public String getZ() { return z; } + public String getB() { return b; } + public String getA() { return a; } + } + + public static class ReadRenamed { + @JsonProperty("x_a") private String a; + private String b; + public String getA() { return a; } + public void setA(String v) { a = v; } + public String getB() { return b; } + public void setB(String v) { b = v; } + } + + public static class ReadPrivateField { + @JsonProperty("h") private String hidden; + } + + public static class ReadIgnored { + @JsonIgnore private String a; + private String b; + public String getA() { return a; } + public void setA(String v) { a = v; } + public String getB() { return b; } + public void setB(String v) { b = v; } + } + + public static class ReadFinalField { + @JsonProperty("f") private final String f; + private ReadFinalField() { f = "init"; } + } +} diff --git a/services-custom/dynamodb-mapper/src/test/resources/type_converted_json_fixture.json b/services-custom/dynamodb-mapper/src/test/resources/type_converted_json_fixture.json new file mode 100644 index 000000000000..d1945077c2ee --- /dev/null +++ b/services-custom/dynamodb-mapper/src/test/resources/type_converted_json_fixture.json @@ -0,0 +1,147 @@ +{ + "write/000-Currency" : "{\"amount\":79.99,\"unit\":\"USD\"}", + "write/001-Currency" : "{\"amount\":null,\"unit\":null}", + "write/002-LombokShaped" : "{\"id\":\"abc\",\"count\":7,\"active\":true,\"ratio\":0.5,\"tags\":[\"x\",\"y\"]}", + "write/003-LombokShaped" : "{\"id\":null,\"count\":0,\"active\":false,\"ratio\":null,\"tags\":null}", + "write/004-Nested" : "{\"one\":{\"amount\":1.0,\"unit\":\"EUR\"},\"many\":[{\"amount\":2.0,\"unit\":\"GBP\"},null]}", + "write/005-Nested" : "{\"one\":null,\"many\":[]}", + "write/006-CurrencyList" : "[{\"amount\":1.0,\"unit\":\"EUR\"},{\"amount\":2.0,\"unit\":\"GBP\"}]", + "write/007-CurrencyList" : "[]", + "write/008-ArrayList" : "[{\"amount\":3.0,\"unit\":\"JPY\"},{\"amount\":4.0,\"unit\":\"CHF\"}]", + "write/009-HasCollections" : "{\"list\":null,\"set\":null,\"nested\":null}", + "write/010-HasCollections" : "{\"list\":[\"a\",null,\"b\"],\"set\":[\"a\",\"b\"],\"nested\":[[1,2],[3]]}", + "write/011-LinkedHashMap" : "{\"zulu\":{\"amount\":1.0,\"unit\":\"EUR\"},\"alpha\":{\"amount\":2.0,\"unit\":\"GBP\"},\"\":null}", + "write/012-LinkedHashMap" : "{\"2\":\"two\",\"1\":\"one\"}", + "write/013-LinkedHashMap" : "{\"RED\":\"r\",\"GREEN\":\"g\"}", + "write/014-TreeMap" : "{\"alpha\":2,\"zulu\":1}", + "write/015-HasMaps" : "{\"byName\":{\"zulu\":{\"amount\":1.0,\"unit\":\"EUR\"},\"alpha\":{\"amount\":2.0,\"unit\":\"GBP\"},\"\":null}}", + "write/016-Numbers" : "{\"i\":-2147483648,\"l\":9223372036854775807,\"s\":-1,\"b\":-128,\"d\":1.5,\"f\":2.5,\"boxedInt\":0,\"boxedLong\":-1}", + "write/017-Numbers" : "{\"i\":0,\"l\":0,\"s\":0,\"b\":0,\"d\":0.0,\"f\":0.0,\"boxedInt\":null,\"boxedLong\":null}", + "write/018-Doubles" : "{\"a\":0.0,\"b\":-0.0,\"c\":1.0}", + "write/019-Doubles" : "{\"a\":1.0E20,\"b\":1.0E-20,\"c\":0.3333333333333333}", + "write/020-Doubles" : "{\"a\":\"NaN\",\"b\":\"Infinity\",\"c\":\"-Infinity\"}", + "write/021-Doubles" : "{\"a\":4.9E-324,\"b\":1.7976931348623157E308,\"c\":2.2250738585072014E-308}", + "write/022-Floats" : "{\"a\":0.0,\"b\":0.33333334,\"c\":3.4028235E38}", + "write/023-Bigs" : "{\"decimal\":1.2300,\"integer\":42}", + "write/024-Bigs" : "{\"decimal\":1E+10,\"integer\":123456789012345678901234567890}", + "write/025-Bigs" : "{\"decimal\":1E-21,\"integer\":0}", + "write/026-Bigs" : "{\"decimal\":0.00000,\"integer\":-1}", + "write/027-HasString" : "{\"text\":\"\"}", + "write/028-HasString" : "{\"text\":\"plain\"}", + "write/029-HasString" : "{\"text\":\"with \\\"quotes\\\"\"}", + "write/030-HasString" : "{\"text\":\"back\\\\slash\"}", + "write/031-HasString" : "{\"text\":\"forward/slash\"}", + "write/032-HasString" : "{\"text\":\"new\\nline\"}", + "write/033-HasString" : "{\"text\":\"tab\\there\"}", + "write/034-HasString" : "{\"text\":\"carriage\\rreturn\"}", + "write/035-HasString" : "{\"text\":\"\\u0000\\u0001\\u001F\"}", + "write/036-HasString" : "{\"text\":\"\"}", + "write/037-HasString" : "{\"text\":\"café\"}", + "write/038-HasString" : "{\"text\":\"中文\"}", + "write/039-HasString" : "{\"text\":\"emoji \uD83D\uDE00\"}", + "write/040-HasString" : "{\"text\":\" leading and trailing \"}", + "write/041-HasString" : "{\"text\":\"a
b
c\"}", + "write/042-HasChar" : "{\"c\":\"a\",\"boxed\":\"a\"}", + "write/043-HasChar" : "{\"c\":\"\\\"\",\"boxed\":\"\\\"\"}", + "write/044-HasChar" : "{\"c\":\"\\n\",\"boxed\":\"\\n\"}", + "write/045-HasDate" : "{\"epoch\":0,\"later\":1700000000123}", + "write/046-HasDate" : "{\"epoch\":null,\"later\":null}", + "write/047-HasCalendar" : "{\"when\":1700000000123}", + "write/048-HasUuid" : "{\"id\":\"c2a4fcd3-0c3b-4cfb-9ff7-c1cb03e2e9f6\"}", + "write/049-HasEnum" : "{\"color\":\"RED\"}", + "write/050-HasEnum" : "{\"color\":null}", + "write/051-HasBytes" : "{\"bytes\":\"AAEC/w==\"}", + "write/052-HasBytes" : "{\"bytes\":\"\"}", + "write/053-HasBytes" : "{\"bytes\":null}", + "write/054-HasChars" : "{\"chars\":\"hi\"}", + "write/055-HasArrays" : "{\"ints\":null,\"strings\":null,\"currencies\":null}", + "write/056-HasArrays" : "{\"ints\":[1,2,3],\"strings\":[\"a\",null],\"currencies\":[{\"amount\":1.0,\"unit\":\"EUR\"}]}", + "write/057-PublicFields" : "{\"first\":\"1\",\"second\":2,\"third\":\"3\"}", + "write/058-MixedVisibility" : "{\"visibleField\":\"shown\",\"withGetter\":\"also shown\"}", + "write/059-SkipsStaticAndTransient" : "{\"kept\":\"kept\"}", + "write/060-SubclassWithFieldsAndGetters" : "{\"baseField\":\"bf\",\"subField\":\"sf\",\"subGetter\":\"sg\",\"baseGetter\":\"bg\"}", + "write/061-OverridesGetter" : "{\"value\":\"sub\"}", + "write/062-GetterOnly" : "{\"zulu\":\"z\",\"alpha\":\"a\",\"mike\":13}", + "write/063-ManglesGetterNames" : "{\"a\":\"a\",\"ab\":\"ab\",\"abc\":\"abc\",\"ptlevelRequestIds\":\"p\",\"xmlhttpRequest\":\"x\",\"aLowerFirst\":\"l\",\"url\":\"u\"}", + "write/064-IsGetters" : "{\"flag\":true,\"boxed\":false,\"ready\":true}", + "write/065-PrivateNoArgConstructor" : "{\"id\":0,\"name\":null}", + "write/066-RenameOnField" : "{\"b\":\"B\",\"x_a\":\"A\"}", + "write/067-RenameOnGetter" : "{\"b\":\"B\",\"x_a\":\"A\"}", + "write/068-RenameMiddle" : "{\"a\":\"A\",\"c\":\"C\",\"z\":\"B\"}", + "write/069-TwoRenamed" : "{\"b\":\"B\",\"q\":\"A\",\"p\":\"C\"}", + "write/070-SameNameAsDefault" : "{\"b\":\"B\",\"a\":\"A\"}", + "write/071-RenameOnSetterOnly" : "{\"b\":\"B\",\"x_a\":\"A\"}", + "write/072-PrivateFieldNoGetter" : "{\"hidden\":\"H\",\"b\":\"B\"}", + "write/073-PrivateFieldRenamedNoGetter" : "{\"b\":\"B\",\"h\":\"H\"}", + "write/074-PrivateGetter" : "{\"a\":\"A\",\"b\":\"B\"}", + "write/075-NonBeanMethod" : "{\"b\":\"B\",\"computed\":\"C\"}", + "write/076-SubOverridesRenamed" : "{\"b\":\"B\",\"x_a\":\"SUB\"}", + "write/077-IgnoreField" : "{\"b\":\"B\"}", + "write/078-IgnoreGetter" : "{\"b\":\"B\"}", + "write/079-IgnoreSetterOnly" : "{\"b\":\"B\"}", + "write/080-IgnoreFalse" : "{\"a\":\"A\",\"b\":\"B\"}", + "write/081-IgnoreFieldButPropertyOnGetter" : "{\"a\":\"A\",\"b\":\"B\"}", + "write/082-SubOverridesIgnored" : "{\"b\":\"B\"}", + "write/083-OrderFull" : "{\"c\":\"C\",\"a\":\"A\",\"b\":\"B\"}", + "write/084-OrderPartial" : "{\"c\":\"C\",\"a\":\"A\",\"b\":\"B\"}", + "write/085-OrderUnknownName" : "{\"b\":\"B\",\"a\":\"A\"}", + "write/086-OrderAlpha" : "{\"a\":\"A\",\"b\":\"B\",\"c\":\"C\"}", + "write/087-OrderPartialAlpha" : "{\"c\":\"C\",\"a\":\"A\",\"b\":\"B\",\"c2\":\"x\"}", + "write/088-OrderByRenamed" : "{\"x_a\":\"A\",\"b\":\"B\"}", + "write/089-OrderByImplicitOfRenamed" : "{\"x_a\":\"A\",\"b\":\"B\"}", + "write/090-OrderInherited" : "{\"b\":\"B\",\"a\":\"A\",\"c\":\"C\"}", + "write/091-OrderOverridden" : "{\"c\":\"C\",\"a\":\"A\",\"b\":\"B\"}", + "write/092-AlphaWithRename" : "{\"a\":\"A\",\"b\":\"B\",\"m\":\"Z\"}", + "write/093-Nested" : "{\"one\":null,\"many\":[{\"amount\":1.0,\"unit\":\"EUR\"}]}", + "write/094-String" : "\"a plain string\"", + "write/095-Integer" : "42", + "write/096-Boolean" : "true", + "write/097-null" : "null", + "write/098-Color" : "\"GREEN\"", + "write/099-BigDecimal" : "1.2300", + "write/100-Date" : "1700000000123", + "write/101-byte[]" : "\"AQID\"", + "write/102-ArrayList" : "[1,\"two\",null,true]", + "write/103-LinkedHashMap" : "{\"zulu\":{\"amount\":1.0,\"unit\":\"EUR\"},\"alpha\":{\"amount\":2.0,\"unit\":\"GBP\"},\"\":null}", + "read/000-Currency" : "{\"amount\":79.99,\"unit\":\"USD\"}", + "read/001-Currency" : "{\"amount\":79.99,\"unit\":\"USD\"}", + "read/002-Currency" : "{\"amount\":1.0,\"unit\":\"USD\"}", + "read/003-Currency" : "{\"amount\":null,\"unit\":null}", + "read/004-Currency" : "{\"amount\":null,\"unit\":null}", + "read/005-Currency" : "{\"amount\":null,\"unit\":null}", + "read/006-Currency" : "null", + "read/007-Numbers" : "{\"i\":1,\"l\":2,\"s\":3,\"b\":4,\"d\":5.0,\"f\":6.0,\"boxedInt\":null,\"boxedLong\":null}", + "read/008-Numbers" : "{\"i\":1,\"l\":-2,\"s\":0,\"b\":0,\"d\":0.0,\"f\":0.0,\"boxedInt\":null,\"boxedLong\":null}", + "read/009-Numbers" : "{\"i\":5,\"l\":6,\"s\":0,\"b\":0,\"d\":0.0,\"f\":0.0,\"boxedInt\":null,\"boxedLong\":null}", + "read/010-Numbers" : "{\"i\":0,\"l\":0,\"s\":0,\"b\":0,\"d\":0.0,\"f\":0.0,\"boxedInt\":null,\"boxedLong\":null}", + "read/011-Bigs" : "{\"decimal\":1.2300,\"integer\":123456789012345678901234567890}", + "read/012-HasDate" : "{\"epoch\":0,\"later\":1700000000123}", + "read/013-HasDate" : "{\"epoch\":0,\"later\":null}", + "read/014-HasBytes" : "{\"bytes\":\"AAEC/w==\"}", + "read/015-HasEnum" : "{\"color\":\"RED\"}", + "read/016-HasEnum" : "{\"color\":null}", + "read/017-HasUuid" : "{\"id\":\"c2a4fcd3-0c3b-4cfb-9ff7-c1cb03e2e9f6\"}", + "read/018-HasString" : "{\"text\":\"line1\\nline2\\u0000\\\"quoted\\\" é 中\"}", + "read/019-HasString" : "{\"text\":\"42\"}", + "read/020-Nested" : "{\"one\":{\"amount\":1.0,\"unit\":\"EUR\"},\"many\":[{\"amount\":2.0,\"unit\":\"GBP\"},null]}", + "read/021-Nested" : "{\"one\":null,\"many\":[]}", + "read/022-CurrencyList" : "[{\"amount\":1.0,\"unit\":\"EUR\"},{\"amount\":2.0,\"unit\":\"GBP\"}]", + "read/023-CurrencyList" : "[]", + "read/024-HasCollections" : "{\"list\":[\"a\",\"b\"],\"set\":[\"a\",\"b\"],\"nested\":[[1,2],[3]]}", + "read/025-HasMaps" : "{\"byName\":{\"a\":{\"amount\":1.0,\"unit\":\"EUR\"}}}", + "read/026-HasArrays" : "{\"ints\":[1,2,3],\"strings\":[\"a\",null],\"currencies\":[{\"amount\":1.0,\"unit\":\"EUR\"}]}", + "read/027-HasObject" : "{\"any\":{\"i\":1,\"big\":123456789012345678901234567890,\"l\":4294967296,\"d\":1.5,\"s\":\"x\",\"b\":true,\"n\":null,\"a\":[1,[2]],\"o\":{\"k\":\"v\"}}}", + "read/028-HasObject" : "{\"any\":[1,2,3]}", + "read/029-IsGetters" : "{\"flag\":true,\"boxed\":false,\"ready\":true}", + "read/030-PrivateNoArgConstructor" : "{\"id\":7,\"name\":\"n\"}", + "read/031-SubclassWithFieldsAndGetters" : "{\"baseField\":\"b\",\"subField\":\"s\",\"subGetter\":\"sg\",\"baseGetter\":\"bg\"}", + "read/032-PartlyWritable" : "{\"writable\":\"kept\",\"readOnly\":\"constant\"}", + "read/033-ReadRenamed" : "{\"b\":\"2\",\"x_a\":\"1\"}", + "read/034-ReadRenamed" : "{\"b\":\"2\",\"x_a\":null}", + "read/035-ReadPrivateField" : "{\"h\":\"1\"}", + "read/036-ReadIgnored" : "{\"b\":\"2\"}", + "read/037-ReadFinalField" : "{\"f\":\"fromjson\"}", + "read/038-String" : "\"top level string\"", + "read/039-Integer" : "42", + "read/040-Boolean" : "true" +} \ No newline at end of file