From d53677229eb0669592d076af7fc2fd40f9acb829 Mon Sep 17 00:00:00 2001 From: Andrew Omondi Date: Thu, 1 Feb 2024 14:14:23 +0300 Subject: [PATCH 01/14] WIP: Base models TODO. Tests in serialization libs --- .../kiota/serialization/UntypedArray.java | 24 ++++++++++ .../kiota/serialization/UntypedBoolean.java | 24 ++++++++++ .../kiota/serialization/UntypedDecimal.java | 26 +++++++++++ .../kiota/serialization/UntypedDouble.java | 24 ++++++++++ .../kiota/serialization/UntypedFloat.java | 24 ++++++++++ .../kiota/serialization/UntypedInteger.java | 24 ++++++++++ .../kiota/serialization/UntypedLong.java | 24 ++++++++++ .../kiota/serialization/UntypedNode.java | 44 +++++++++++++++++++ .../kiota/serialization/UntypedNull.java | 22 ++++++++++ .../kiota/serialization/UntypedObject.java | 26 +++++++++++ .../kiota/serialization/UntypedString.java | 24 ++++++++++ gradle.properties | 4 +- 12 files changed, 288 insertions(+), 2 deletions(-) create mode 100644 components/abstractions/src/main/java/com/microsoft/kiota/serialization/UntypedArray.java create mode 100644 components/abstractions/src/main/java/com/microsoft/kiota/serialization/UntypedBoolean.java create mode 100644 components/abstractions/src/main/java/com/microsoft/kiota/serialization/UntypedDecimal.java create mode 100644 components/abstractions/src/main/java/com/microsoft/kiota/serialization/UntypedDouble.java create mode 100644 components/abstractions/src/main/java/com/microsoft/kiota/serialization/UntypedFloat.java create mode 100644 components/abstractions/src/main/java/com/microsoft/kiota/serialization/UntypedInteger.java create mode 100644 components/abstractions/src/main/java/com/microsoft/kiota/serialization/UntypedLong.java create mode 100644 components/abstractions/src/main/java/com/microsoft/kiota/serialization/UntypedNode.java create mode 100644 components/abstractions/src/main/java/com/microsoft/kiota/serialization/UntypedNull.java create mode 100644 components/abstractions/src/main/java/com/microsoft/kiota/serialization/UntypedObject.java create mode 100644 components/abstractions/src/main/java/com/microsoft/kiota/serialization/UntypedString.java diff --git a/components/abstractions/src/main/java/com/microsoft/kiota/serialization/UntypedArray.java b/components/abstractions/src/main/java/com/microsoft/kiota/serialization/UntypedArray.java new file mode 100644 index 000000000..250707fd9 --- /dev/null +++ b/components/abstractions/src/main/java/com/microsoft/kiota/serialization/UntypedArray.java @@ -0,0 +1,24 @@ +package com.microsoft.kiota.serialization; + +/** + * Represents an untyped node with a collection of other untyped nodes. + */ +public class UntypedArray extends UntypedNode { + /** + * The constructor for the UntypedArray + */ + public UntypedArray(Iterable collection) { + value = collection; + } + + private final Iterable value; + + /** + * Gets the value assigned to untyped node. + * @return The string value of the node. + */ + @Override + public Iterable getValue() { + return value; + } +} diff --git a/components/abstractions/src/main/java/com/microsoft/kiota/serialization/UntypedBoolean.java b/components/abstractions/src/main/java/com/microsoft/kiota/serialization/UntypedBoolean.java new file mode 100644 index 000000000..69070e531 --- /dev/null +++ b/components/abstractions/src/main/java/com/microsoft/kiota/serialization/UntypedBoolean.java @@ -0,0 +1,24 @@ +package com.microsoft.kiota.serialization; + +/** + * Represents an untyped node with boolean value. + */ +public class UntypedBoolean extends UntypedNode { + /** + * The constructor for the UntypedBoolean + */ + public UntypedBoolean(Boolean boolValue) { + value = boolValue; + } + + private final Boolean value; + + /** + * Gets the value assigned to untyped node. + * @return The bool value of the node. + */ + @Override + public Boolean getValue() { + return value; + } +} diff --git a/components/abstractions/src/main/java/com/microsoft/kiota/serialization/UntypedDecimal.java b/components/abstractions/src/main/java/com/microsoft/kiota/serialization/UntypedDecimal.java new file mode 100644 index 000000000..30b6d89c6 --- /dev/null +++ b/components/abstractions/src/main/java/com/microsoft/kiota/serialization/UntypedDecimal.java @@ -0,0 +1,26 @@ +package com.microsoft.kiota.serialization; + +import java.math.BigDecimal; + +/** + * Represents an untyped node with decimal value. + */ +public class UntypedDecimal extends UntypedNode { + /** + * The constructor for the UntypedDecimal + */ + public UntypedDecimal(BigDecimal decimalValue) { + value = decimalValue; + } + + private final BigDecimal value; + + /** + * Gets the value assigned to untyped node. + * @return The BigDecimal value of the node. + */ + @Override + public BigDecimal getValue() { + return value; + } +} diff --git a/components/abstractions/src/main/java/com/microsoft/kiota/serialization/UntypedDouble.java b/components/abstractions/src/main/java/com/microsoft/kiota/serialization/UntypedDouble.java new file mode 100644 index 000000000..88c48541b --- /dev/null +++ b/components/abstractions/src/main/java/com/microsoft/kiota/serialization/UntypedDouble.java @@ -0,0 +1,24 @@ +package com.microsoft.kiota.serialization; + +/** + * Represents an untyped node with double value. + */ +public class UntypedDouble extends UntypedNode { + /** + * The constructor for the UntypedDouble + */ + public UntypedDouble(Double doubleValue) { + value = doubleValue; + } + + private final Double value; + + /** + * Gets the value assigned to untyped node. + * @return The double value of the node. + */ + @Override + public Double getValue() { + return value; + } +} diff --git a/components/abstractions/src/main/java/com/microsoft/kiota/serialization/UntypedFloat.java b/components/abstractions/src/main/java/com/microsoft/kiota/serialization/UntypedFloat.java new file mode 100644 index 000000000..f22738ff0 --- /dev/null +++ b/components/abstractions/src/main/java/com/microsoft/kiota/serialization/UntypedFloat.java @@ -0,0 +1,24 @@ +package com.microsoft.kiota.serialization; + +/** + * Represents an untyped node with Float value. + */ +public class UntypedFloat extends UntypedNode { + /** + * The constructor for the UntypedFloat + */ + public UntypedFloat(Float floatValue) { + value = floatValue; + } + + private final Float value; + + /** + * Gets the value assigned to untyped node. + * @return The float value of the node. + */ + @Override + public Float getValue() { + return value; + } +} diff --git a/components/abstractions/src/main/java/com/microsoft/kiota/serialization/UntypedInteger.java b/components/abstractions/src/main/java/com/microsoft/kiota/serialization/UntypedInteger.java new file mode 100644 index 000000000..0fa1be066 --- /dev/null +++ b/components/abstractions/src/main/java/com/microsoft/kiota/serialization/UntypedInteger.java @@ -0,0 +1,24 @@ +package com.microsoft.kiota.serialization; + +/** + * Represents an untyped node with integer value. + */ +public class UntypedInteger extends UntypedNode { + /** + * The constructor for the UntypedObject + */ + public UntypedInteger(Integer intValue) { + value = intValue; + } + + private final Integer value; + + /** + * Gets the value assigned to untyped node. + * @return The integer value of the node. + */ + @Override + public Integer getValue() { + return value; + } +} diff --git a/components/abstractions/src/main/java/com/microsoft/kiota/serialization/UntypedLong.java b/components/abstractions/src/main/java/com/microsoft/kiota/serialization/UntypedLong.java new file mode 100644 index 000000000..726b6b3c1 --- /dev/null +++ b/components/abstractions/src/main/java/com/microsoft/kiota/serialization/UntypedLong.java @@ -0,0 +1,24 @@ +package com.microsoft.kiota.serialization; + +/** + * Represents an untyped node with Long value. + */ +public class UntypedLong extends UntypedNode { + /** + * The constructor for the UntypedLong + */ + public UntypedLong(Long longValue) { + value = longValue; + } + + private final Long value; + + /** + * Gets the value assigned to untyped node. + * @return The long value of the node. + */ + @Override + public Long getValue() { + return value; + } +} diff --git a/components/abstractions/src/main/java/com/microsoft/kiota/serialization/UntypedNode.java b/components/abstractions/src/main/java/com/microsoft/kiota/serialization/UntypedNode.java new file mode 100644 index 000000000..80da6767e --- /dev/null +++ b/components/abstractions/src/main/java/com/microsoft/kiota/serialization/UntypedNode.java @@ -0,0 +1,44 @@ +package com.microsoft.kiota.serialization; + +import jakarta.annotation.Nonnull; + +import java.util.HashMap; +import java.util.Map; +import java.util.function.Consumer; + +/** + * Base class for untyped node. + */ +public class UntypedNode implements Parsable { + + /** + * The deserialization information for the current model. + * @return The map of serializer methods for this object. + */ + @Nonnull @Override + public Map> getFieldDeserializers() { + return new HashMap<>(); + } + + /** + * Serializes the current object + */ + @Override + public void serialize(@Nonnull SerializationWriter writer) {} + + /** + * Gets the value assigned to untyped node. + * @return The value assigned to untyped node. + */ + public Object getValue() { + return null; + } + + /** + * Creates a new instance of the appropriate class based on discriminator value. + * @return A new UntypedNode instance. + */ + public static UntypedNode createFromDiscriminatorValue(@Nonnull final ParseNode parseNode) { + return new UntypedNode(); + } +} diff --git a/components/abstractions/src/main/java/com/microsoft/kiota/serialization/UntypedNull.java b/components/abstractions/src/main/java/com/microsoft/kiota/serialization/UntypedNull.java new file mode 100644 index 000000000..24bfa034a --- /dev/null +++ b/components/abstractions/src/main/java/com/microsoft/kiota/serialization/UntypedNull.java @@ -0,0 +1,22 @@ +package com.microsoft.kiota.serialization; + +/** + * Represents an untyped node with null value. + */ +public class UntypedNull extends UntypedNode { + /** + * The default constructor for the UntypedNull + */ + public UntypedNull() { + // empty constructor + } + + /** + * Gets the value assigned to untyped node. + * @return null value. + */ + @Override + public Object getValue() { + return null; + } +} diff --git a/components/abstractions/src/main/java/com/microsoft/kiota/serialization/UntypedObject.java b/components/abstractions/src/main/java/com/microsoft/kiota/serialization/UntypedObject.java new file mode 100644 index 000000000..10888cb86 --- /dev/null +++ b/components/abstractions/src/main/java/com/microsoft/kiota/serialization/UntypedObject.java @@ -0,0 +1,26 @@ +package com.microsoft.kiota.serialization; + +import java.util.Map; + +/** + * Represents an untyped node with object value. + */ +public class UntypedObject extends UntypedNode { + /** + * The constructor for the UntypedObject + */ + public UntypedObject(Map propertiesMap) { + properties = propertiesMap; + } + + private final Map properties; + + /** + * Gets the value assigned to untyped node. + * @return The Map of property keys and their values. + */ + @Override + public Map getValue() { + return properties; + } +} diff --git a/components/abstractions/src/main/java/com/microsoft/kiota/serialization/UntypedString.java b/components/abstractions/src/main/java/com/microsoft/kiota/serialization/UntypedString.java new file mode 100644 index 000000000..8bb024a87 --- /dev/null +++ b/components/abstractions/src/main/java/com/microsoft/kiota/serialization/UntypedString.java @@ -0,0 +1,24 @@ +package com.microsoft.kiota.serialization; + +/** + * Represents an untyped node with string value. + */ +public class UntypedString extends UntypedNode { + /** + * The constructor for the UntypedObject + */ + public UntypedString(String stringValue) { + value = stringValue; + } + + private final String value; + + /** + * Gets the value assigned to untyped node. + * @return The string value of the node. + */ + @Override + public String getValue() { + return value; + } +} diff --git a/gradle.properties b/gradle.properties index e56d8e1c7..ffd49b66e 100644 --- a/gradle.properties +++ b/gradle.properties @@ -25,8 +25,8 @@ org.gradle.caching=true mavenGroupId = com.microsoft.kiota mavenMajorVersion = 0 -mavenMinorVersion = 12 -mavenPatchVersion = 1 +mavenMinorVersion = 13 +mavenPatchVersion = 0 mavenArtifactSuffix = #These values are used to run functional tests From 65a1eb9c5024b248071c7807bb816d677d68f8bd Mon Sep 17 00:00:00 2001 From: Andrew Omondi Date: Mon, 5 Feb 2024 11:55:19 +0300 Subject: [PATCH 02/14] WIP: TODO test serialization --- .../kiota/serialization/JsonParseNode.java | 45 ++++++-- .../serialization/JsonParseNodeTests.java | 91 +++++++++++++++- .../JsonSerializationWriterTests.java | 50 +++++++++ .../mocks/UntypedTestEntity.java | 102 ++++++++++++++++++ 4 files changed, 279 insertions(+), 9 deletions(-) create mode 100644 components/serialization/json/src/test/java/com/microsoft/kiota/serialization/JsonSerializationWriterTests.java create mode 100644 components/serialization/json/src/test/java/com/microsoft/kiota/serialization/mocks/UntypedTestEntity.java diff --git a/components/serialization/json/src/main/java/com/microsoft/kiota/serialization/JsonParseNode.java b/components/serialization/json/src/main/java/com/microsoft/kiota/serialization/JsonParseNode.java index c543c97bc..abba7637b 100644 --- a/components/serialization/json/src/main/java/com/microsoft/kiota/serialization/JsonParseNode.java +++ b/components/serialization/json/src/main/java/com/microsoft/kiota/serialization/JsonParseNode.java @@ -13,14 +13,7 @@ import java.time.LocalDate; import java.time.LocalTime; import java.time.OffsetDateTime; -import java.util.ArrayList; -import java.util.Base64; -import java.util.EnumSet; -import java.util.Iterator; -import java.util.List; -import java.util.Map; -import java.util.Objects; -import java.util.UUID; +import java.util.*; import java.util.function.Consumer; import java.util.function.Function; @@ -195,9 +188,45 @@ private List iterateOnArray(Function fn) { @Nonnull public T getObjectValue(@Nonnull final ParsableFactory factory) { Objects.requireNonNull(factory, "parameter factory cannot be null"); final T item = factory.create(this); + if(item.getClass() == UntypedNode.class) + return (T) getUntypedValue(); assignFieldValues(item, item.getFieldDeserializers()); return item; } + @Nonnull private UntypedNode getUntypedValue(){ + return getUntypedValue(currentNode); + } + @Nonnull private UntypedNode getUntypedValue(JsonElement element) + { + if (element.isJsonNull()) return new UntypedNull(); + else if (element.isJsonPrimitive()) { + final JsonPrimitive primitive = element.getAsJsonPrimitive(); + if (primitive.isBoolean()) return new UntypedBoolean(primitive.getAsBoolean()); + else if (primitive.isString()) return new UntypedString(primitive.getAsString()); + else if (primitive.isNumber()) return new UntypedDouble(primitive.getAsDouble()); + else + throw new RuntimeException( + "Could not get the value during deserialization, unknown primitive type"); + } else if (element.isJsonObject()) { + HashMap propertiesMap = new HashMap<>(); + for (final Map.Entry fieldEntry : + element.getAsJsonObject().entrySet()) { + final String fieldKey = fieldEntry.getKey(); + final JsonElement fieldValue = fieldEntry.getValue(); + final JsonParseNode childNode = new JsonParseNode(fieldValue); + childNode.setOnBeforeAssignFieldValues(this.getOnBeforeAssignFieldValues()); + childNode.setOnAfterAssignFieldValues(this.getOnAfterAssignFieldValues()); + propertiesMap.put(fieldKey,childNode.getUntypedValue()); + } + return new UntypedObject(propertiesMap); + + } else if (element.isJsonArray()) { + return new UntypedArray(iterateOnArray(JsonParseNode::getUntypedValue)); + } + + throw new RuntimeException( + "Could not get the value during deserialization, unknown json value type"); + } @Nullable public > T getEnumValue(@Nonnull final ValuedEnumParser enumParser) { final String rawValue = this.getStringValue(); diff --git a/components/serialization/json/src/test/java/com/microsoft/kiota/serialization/JsonParseNodeTests.java b/components/serialization/json/src/test/java/com/microsoft/kiota/serialization/JsonParseNodeTests.java index 6f19e7ac2..8659b102f 100644 --- a/components/serialization/json/src/test/java/com/microsoft/kiota/serialization/JsonParseNodeTests.java +++ b/components/serialization/json/src/test/java/com/microsoft/kiota/serialization/JsonParseNodeTests.java @@ -1,6 +1,8 @@ package com.microsoft.kiota.serialization; -import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.*; + +import com.microsoft.kiota.serialization.mocks.UntypedTestEntity; import org.junit.jupiter.api.Test; @@ -10,6 +12,47 @@ class JsonParseNodeTests { private static final JsonParseNodeFactory _parseNodeFactory = new JsonParseNodeFactory(); private static final String contentType = "application/json"; + private static final String testUntypedJson = + "{\r\n" + + " \"@odata.context\":" + + " \"https://graph.microsoft.com/v1.0/$metadata#sites('contoso.sharepoint.com')/lists('fa631c4d-ac9f-4884-a7f5-13c659d177e3')/items('1')/fields/$entity\",\r\n" + + " \"id\": \"5\",\r\n" + + " \"title\": \"Project 101\",\r\n" + + " \"location\": {\r\n" + + " \"address\": {\r\n" + + " \"city\": \"Redmond\",\r\n" + + " \"postalCode\": \"98052\",\r\n" + + " \"state\": \"Washington\",\r\n" + + " \"street\": \"NE 36th St\"\r\n" + + " },\r\n" + + " \"coordinates\": {\r\n" + + " \"latitude\": 47.641942,\r\n" + + " \"longitude\": -122.127222\r\n" + + " },\r\n" + + " \"displayName\": \"Microsoft Building 92\",\r\n" + + " \"floorCount\": 50,\r\n" + + " \"hasReception\": true,\r\n" + + " \"contact\": null\r\n" + + " },\r\n" + + " \"keywords\": [\r\n" + + " {\r\n" + + " \"created\": \"2023-07-26T10:41:26Z\",\r\n" + + " \"label\": \"Keyword1\",\r\n" + + " \"termGuid\": \"10e9cc83-b5a4-4c8d-8dab-4ada1252dd70\",\r\n" + + " \"wssId\": 6442450942\r\n" + + " },\r\n" + + " {\r\n" + + " \"created\": \"2023-07-26T10:51:26Z\",\r\n" + + " \"label\": \"Keyword2\",\r\n" + + " \"termGuid\": \"2cae6c6a-9bb8-4a78-afff-81b88e735fef\",\r\n" + + " \"wssId\": 6442450943\r\n" + + " }\r\n" + + " ],\r\n" + + " \"detail\": null,\r\n" + + " \"extra\": {\r\n" + + " \"createdDateTime\":\"2024-01-15T00:00:00\\u002B00:00\"\r\n" + + " }\r\n" + + "}"; @Test void itDDoesNotFailForGetChildElementOnMissingKey() throws UnsupportedEncodingException { @@ -19,4 +62,50 @@ void itDDoesNotFailForGetChildElementOnMissingKey() throws UnsupportedEncodingEx final var result = parseNode.getChildNode("@odata.type"); assertNull(result); } + + @Test + public void GetEntityWithUntypedNodesFromJson() throws UnsupportedEncodingException { + final var rawResponse = new ByteArrayInputStream(testUntypedJson.getBytes("UTF-8")); + final var parseNode = _parseNodeFactory.getParseNode(contentType, rawResponse); + // Act + var entity = parseNode.getObjectValue(UntypedTestEntity::createFromDiscriminatorValue); + // Assert + assertNotNull(entity); + assertEquals("5", entity.getId()); + assertNotNull(entity.getLocation()); + assertInstanceOf(UntypedObject.class, entity.getLocation()); // creates untyped object + var location = (UntypedObject) entity.getLocation(); + var locationProperties = location.getValue(); + assertInstanceOf(UntypedObject.class, locationProperties.get("address")); + assertInstanceOf( + UntypedString.class, + (locationProperties.get("displayName"))); // creates untyped string + assertInstanceOf( + UntypedDouble.class, + (locationProperties.get("floorCount"))); // creates untyped number + assertInstanceOf( + UntypedBoolean.class, + locationProperties.get("hasReception")); // creates untyped boolean + assertInstanceOf( + UntypedNull.class, locationProperties.get("contact")); // creates untyped null + assertInstanceOf( + UntypedObject.class, locationProperties.get("coordinates")); // creates untyped null + var coordinates = (UntypedObject) locationProperties.get("coordinates"); + var coordinatesProperties = coordinates.getValue(); + assertInstanceOf( + UntypedDouble.class, + coordinatesProperties.get("latitude")); // creates untyped decimal + assertInstanceOf(UntypedDouble.class, coordinatesProperties.get("longitude")); + assertEquals( + "Microsoft Building 92", + ((UntypedString) locationProperties.get("displayName")).getValue()); + assertEquals(50, ((UntypedDouble) locationProperties.get("floorCount")).getValue()); + assertTrue(((UntypedBoolean) locationProperties.get("hasReception")).getValue()); + assertNull(((UntypedNull) locationProperties.get("contact")).getValue()); + assertNotNull(entity.getKeywords()); + assertInstanceOf(UntypedArray.class, entity.getKeywords()); // creates untyped array + assertNull(entity.getDetail()); + var extra = entity.getAdditionalData().get("extra"); + assertNotNull(extra); + } } diff --git a/components/serialization/json/src/test/java/com/microsoft/kiota/serialization/JsonSerializationWriterTests.java b/components/serialization/json/src/test/java/com/microsoft/kiota/serialization/JsonSerializationWriterTests.java new file mode 100644 index 000000000..33737d961 --- /dev/null +++ b/components/serialization/json/src/test/java/com/microsoft/kiota/serialization/JsonSerializationWriterTests.java @@ -0,0 +1,50 @@ +package com.microsoft.kiota.serialization; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.microsoft.kiota.Compatibility; +import com.microsoft.kiota.serialization.mocks.UntypedTestEntity; + +import org.junit.jupiter.api.Test; + +import java.io.IOException; +import java.util.HashMap; + +public class JsonSerializationWriterTests { + + @Test + public void WritesSampleObjectValueWithUntypedProperties() throws IOException { + // Arrange + var untypedTestEntity = new UntypedTestEntity(); + untypedTestEntity.setId("1"); + var locationObject = + new UntypedObject( + new HashMap<>() { + { + put("displayName", new UntypedString("Microsoft Building 92")); + put("floorCount", new UntypedInteger(50)); + put("hasReception", new UntypedBoolean(true)); + put("contact", new UntypedNull()); + } + }); + untypedTestEntity.setLocation(locationObject); + + var jsonSerializer = new JsonSerializationWriter(); + jsonSerializer.writeObjectValue("", untypedTestEntity); + var contentStream = jsonSerializer.getSerializedContent(); + var serializedJsonString = new String(Compatibility.readAllBytes(contentStream), "UTF-8"); + + // Assert + var expectedString = + "{\"id\":\"1\",\"title\":\"Title\"," + + "\"location\":{\"address\":{\"city\":\"Redmond\",\"postalCode\":\"98052\",\"state\":\"Washington\",\"street\":\"NE" + + " 36th St\"}," + + "\"coordinates\":{\"latitude\":47.641942,\"longitude\":-122.127222},\"displayName\":\"Microsoft" + + " Building 92\",\"floorCount\":50,\"hasReception\":true,\"contact\":null}," + + "\"keywords\":[" + + "{\"created\":\"2023-07-26T10:41:26Z\",\"label\":\"Keyword1\",\"termGuid\":\"10e9cc83-b5a4-4c8d-8dab-4ada1252dd70\",\"wssId\":6442450941}," + + "{\"created\":\"2023-07-26T10:51:26Z\",\"label\":\"Keyword2\",\"termGuid\":\"2cae6c6a-9bb8-4a78-afff-81b88e735fef\",\"wssId\":6442450942}]," + + "\"extra\":{\"createdDateTime\":\"2024-01-15T00:00:00\\u002B00:00\"}}"; + assertEquals(expectedString, serializedJsonString); + } +} diff --git a/components/serialization/json/src/test/java/com/microsoft/kiota/serialization/mocks/UntypedTestEntity.java b/components/serialization/json/src/test/java/com/microsoft/kiota/serialization/mocks/UntypedTestEntity.java new file mode 100644 index 000000000..2349197cb --- /dev/null +++ b/components/serialization/json/src/test/java/com/microsoft/kiota/serialization/mocks/UntypedTestEntity.java @@ -0,0 +1,102 @@ +package com.microsoft.kiota.serialization.mocks; + +import com.microsoft.kiota.serialization.*; + +import java.util.HashMap; +import java.util.Map; +import java.util.Objects; +import java.util.function.Consumer; + +public class UntypedTestEntity implements Parsable, AdditionalDataHolder { + private String _id; + + public String getId() { + return _id; + } + + public void setId(String _id) { + this._id = _id; + } + + private UntypedNode _location; + + public UntypedNode getLocation() { + return _location; + } + + public void setLocation(UntypedNode _location) { + this._location = _location; + } + + private UntypedNode _keywords; + + public UntypedNode getKeywords() { + return _keywords; + } + + public void setKeywords(UntypedNode _keywords) { + this._keywords = _keywords; + } + + private UntypedNode _detail; + + public UntypedNode getDetail() { + return _detail; + } + + public void setDetail(UntypedNode _detail) { + this._detail = _detail; + } + + @Override + public Map> getFieldDeserializers() { + return new HashMap<>() { + { + put( + "id", + (n) -> { + setId(n.getStringValue()); + }); + put( + "location", + (n) -> { + setLocation( + n.getObjectValue(UntypedNode::createFromDiscriminatorValue)); + }); + put( + "keywords", + (n) -> { + setKeywords( + n.getObjectValue(UntypedNode::createFromDiscriminatorValue)); + }); + put( + "detail", + (n) -> { + setDetail(n.getObjectValue(UntypedNode::createFromDiscriminatorValue)); + }); + } + }; + } + + @Override + public void serialize(SerializationWriter writer) { + Objects.requireNonNull(writer); + writer.writeStringValue("id", getId()); + writer.writeObjectValue("location", getLocation()); + writer.writeObjectValue("keywords", getDetail()); + writer.writeObjectValue("detail", getDetail()); + writer.writeAdditionalData(getAdditionalData()); + } + + private final Map _additionalData = new HashMap<>(); + + @Override + public Map getAdditionalData() { + return _additionalData; + } + + @jakarta.annotation.Nonnull public static UntypedTestEntity createFromDiscriminatorValue( + @jakarta.annotation.Nonnull final ParseNode parseNode) { + return new UntypedTestEntity(); + } +} From 823f268ddcdfaf8535eba2bbec73db72770fe420 Mon Sep 17 00:00:00 2001 From: Andrew Omondi Date: Mon, 5 Feb 2024 13:57:07 +0300 Subject: [PATCH 03/14] Tests working. Todo cleanup and linting. --- .../kiota/serialization/JsonParseNode.java | 18 ++-- .../JsonSerializationWriter.java | 73 +++++++++++++- .../serialization/JsonParseNodeTests.java | 2 +- .../JsonSerializationWriterTests.java | 94 +++++++++++++++++-- .../mocks/UntypedTestEntity.java | 2 +- 5 files changed, 169 insertions(+), 20 deletions(-) diff --git a/components/serialization/json/src/main/java/com/microsoft/kiota/serialization/JsonParseNode.java b/components/serialization/json/src/main/java/com/microsoft/kiota/serialization/JsonParseNode.java index abba7637b..72ce7598a 100644 --- a/components/serialization/json/src/main/java/com/microsoft/kiota/serialization/JsonParseNode.java +++ b/components/serialization/json/src/main/java/com/microsoft/kiota/serialization/JsonParseNode.java @@ -188,16 +188,16 @@ private List iterateOnArray(Function fn) { @Nonnull public T getObjectValue(@Nonnull final ParsableFactory factory) { Objects.requireNonNull(factory, "parameter factory cannot be null"); final T item = factory.create(this); - if(item.getClass() == UntypedNode.class) - return (T) getUntypedValue(); + if (item.getClass() == UntypedNode.class) return (T) getUntypedValue(); assignFieldValues(item, item.getFieldDeserializers()); return item; } - @Nonnull private UntypedNode getUntypedValue(){ + + @Nonnull private UntypedNode getUntypedValue() { return getUntypedValue(currentNode); } - @Nonnull private UntypedNode getUntypedValue(JsonElement element) - { + + @Nonnull private UntypedNode getUntypedValue(JsonElement element) { if (element.isJsonNull()) return new UntypedNull(); else if (element.isJsonPrimitive()) { final JsonPrimitive primitive = element.getAsJsonPrimitive(); @@ -216,16 +216,16 @@ else if (element.isJsonPrimitive()) { final JsonParseNode childNode = new JsonParseNode(fieldValue); childNode.setOnBeforeAssignFieldValues(this.getOnBeforeAssignFieldValues()); childNode.setOnAfterAssignFieldValues(this.getOnAfterAssignFieldValues()); - propertiesMap.put(fieldKey,childNode.getUntypedValue()); + propertiesMap.put(fieldKey, childNode.getUntypedValue()); } return new UntypedObject(propertiesMap); - + } else if (element.isJsonArray()) { return new UntypedArray(iterateOnArray(JsonParseNode::getUntypedValue)); } throw new RuntimeException( - "Could not get the value during deserialization, unknown json value type"); + "Could not get the value during deserialization, unknown json value type"); } @Nullable public > T getEnumValue(@Nonnull final ValuedEnumParser enumParser) { @@ -293,7 +293,7 @@ else if (element.isJsonPrimitive()) { else throw new RuntimeException( "Could not get the value during deserialization, unknown primitive type"); - } else if (element.isJsonObject() || element.isJsonArray()) return element; + } else if (element.isJsonObject() || element.isJsonArray()) return getUntypedValue(element); else throw new RuntimeException( "Could not get the value during deserialization, unknown primitive type"); diff --git a/components/serialization/json/src/main/java/com/microsoft/kiota/serialization/JsonSerializationWriter.java b/components/serialization/json/src/main/java/com/microsoft/kiota/serialization/JsonSerializationWriter.java index 5b7766168..dcaa53429 100644 --- a/components/serialization/json/src/main/java/com/microsoft/kiota/serialization/JsonSerializationWriter.java +++ b/components/serialization/json/src/main/java/com/microsoft/kiota/serialization/JsonSerializationWriter.java @@ -274,7 +274,16 @@ public void writeObjectValue( Stream.of(additionalValuesToMerge) .filter(Objects::nonNull) .collect(Collectors.toList()); - if (value != null || !nonNullAdditionalValuesToMerge.isEmpty()) { + final boolean serializingUntypedNode = value instanceof UntypedNode; + if (serializingUntypedNode) { + if (onBeforeObjectSerialization != null && value != null) { + onBeforeObjectSerialization.accept(value); + } + writeUntypedValue(key, (UntypedNode) value); + if (onAfterObjectSerialization != null && value != null) { + onAfterObjectSerialization.accept(value); + } + } else if (value != null || !nonNullAdditionalValuesToMerge.isEmpty()) { if (key != null && !key.isEmpty()) { writer.name(key); } @@ -315,6 +324,67 @@ public void writeObjectValue( } } + private void writeUntypedValue(String key, UntypedNode value) { + final Class valueClass = value.getClass(); + if (valueClass.equals(UntypedString.class)) + this.writeStringValue(key, ((UntypedString) value).getValue()); + else if (valueClass.equals(UntypedNull.class)) this.writeNullValue(key); + else if (valueClass.equals(UntypedDecimal.class)) + this.writeBigDecimalValue(key, ((UntypedDecimal) value).getValue()); + else if (valueClass.equals(UntypedBoolean.class)) + this.writeBooleanValue(key, ((UntypedBoolean) value).getValue()); + else if (valueClass.equals(UntypedFloat.class)) + this.writeFloatValue(key, ((UntypedFloat) value).getValue()); + else if (valueClass.equals(UntypedDouble.class)) + this.writeDoubleValue(key, ((UntypedDouble) value).getValue()); + else if (valueClass.equals(UntypedLong.class)) + this.writeLongValue(key, ((UntypedLong) value).getValue()); + else if (valueClass.equals(UntypedInteger.class)) + this.writeIntegerValue(key, ((UntypedInteger) value).getValue()); + else if (valueClass.equals(UntypedObject.class)) + this.writeUntypedObject(key, (UntypedObject) value); + else if (valueClass.equals(UntypedArray.class)) + this.writeUntypedArray(key, (UntypedArray) value); + else throw new RuntimeException("unknown type to serialize " + valueClass.getName()); + } + + private void writeUntypedObject(String key, UntypedObject value) { + if (value != null) { + try { + if (key != null && !key.isEmpty()) { + writer.name(key); + } + writer.beginObject(); + for (final Map.Entry fieldEntry : + value.getValue().entrySet()) { + final String fieldKey = fieldEntry.getKey(); + final UntypedNode fieldValue = fieldEntry.getValue(); + this.writeUntypedValue(fieldKey, fieldValue); + } + writer.endObject(); + } catch (IOException ex) { + throw new RuntimeException("could not serialize value", ex); + } + } + } + + private void writeUntypedArray(String key, UntypedArray value) { + if (value != null) { + try { + if (key != null && !key.isEmpty()) { + writer.name(key); + } + writer.beginArray(); + for (final UntypedNode entry : value.getValue()) { + this.writeUntypedValue(null, entry); + } + writer.endArray(); + } catch (IOException ex) { + throw new RuntimeException("could not serialize value", ex); + } + } + } + public > void writeEnumSetValue( @Nullable final String key, @Nullable final EnumSet values) { if (values != null && !values.isEmpty()) { @@ -414,6 +484,7 @@ else if (valueClass.equals(LocalDate.class)) this.writeLocalDateValue(key, (LocalDate) value); else if (valueClass.equals(LocalTime.class)) this.writeLocalTimeValue(key, (LocalTime) value); + else if (value instanceof UntypedNode) this.writeUntypedValue(key, (UntypedNode) value); else if (valueClass.equals(PeriodAndDuration.class)) this.writePeriodAndDurationValue(key, (PeriodAndDuration) value); else if (value instanceof Iterable) diff --git a/components/serialization/json/src/test/java/com/microsoft/kiota/serialization/JsonParseNodeTests.java b/components/serialization/json/src/test/java/com/microsoft/kiota/serialization/JsonParseNodeTests.java index 8659b102f..c3e280962 100644 --- a/components/serialization/json/src/test/java/com/microsoft/kiota/serialization/JsonParseNodeTests.java +++ b/components/serialization/json/src/test/java/com/microsoft/kiota/serialization/JsonParseNodeTests.java @@ -101,7 +101,7 @@ public void GetEntityWithUntypedNodesFromJson() throws UnsupportedEncodingExcept ((UntypedString) locationProperties.get("displayName")).getValue()); assertEquals(50, ((UntypedDouble) locationProperties.get("floorCount")).getValue()); assertTrue(((UntypedBoolean) locationProperties.get("hasReception")).getValue()); - assertNull(((UntypedNull) locationProperties.get("contact")).getValue()); + assertNull((locationProperties.get("contact")).getValue()); assertNotNull(entity.getKeywords()); assertInstanceOf(UntypedArray.class, entity.getKeywords()); // creates untyped array assertNull(entity.getDetail()); diff --git a/components/serialization/json/src/test/java/com/microsoft/kiota/serialization/JsonSerializationWriterTests.java b/components/serialization/json/src/test/java/com/microsoft/kiota/serialization/JsonSerializationWriterTests.java index 33737d961..7d3b26d7f 100644 --- a/components/serialization/json/src/test/java/com/microsoft/kiota/serialization/JsonSerializationWriterTests.java +++ b/components/serialization/json/src/test/java/com/microsoft/kiota/serialization/JsonSerializationWriterTests.java @@ -8,6 +8,7 @@ import org.junit.jupiter.api.Test; import java.io.IOException; +import java.util.Arrays; import java.util.HashMap; public class JsonSerializationWriterTests { @@ -21,6 +22,36 @@ public void WritesSampleObjectValueWithUntypedProperties() throws IOException { new UntypedObject( new HashMap<>() { { + put( + "address", + new UntypedObject( + new HashMap<>() { + { + put("city", new UntypedString("Redmond")); + put( + "postalCode", + new UntypedString("98052")); + put( + "state", + new UntypedString("Washington")); + put( + "street", + new UntypedString("NE 36th St")); + } + })); + put( + "coordinates", + new UntypedObject( + new HashMap<>() { + { + put( + "latitude", + new UntypedDouble(47.641942d)); + put( + "longitude", + new UntypedDouble(-122.127222d)); + } + })); put("displayName", new UntypedString("Microsoft Building 92")); put("floorCount", new UntypedInteger(50)); put("hasReception", new UntypedBoolean(true)); @@ -29,6 +60,52 @@ public void WritesSampleObjectValueWithUntypedProperties() throws IOException { }); untypedTestEntity.setLocation(locationObject); + var keyWordsCollection = + new UntypedArray( + Arrays.asList( + new UntypedObject( + new HashMap<>() { + { + put( + "created", + new UntypedString("2023-07-26T10:41:26Z")); + put("label", new UntypedString("Keyword1")); + put( + "termGuid", + new UntypedString( + "10e9cc83-b5a4-4c8d-8dab-4ada1252dd70")); + put("wssId", new UntypedLong(345345345L)); + } + }), + new UntypedObject( + new HashMap<>() { + { + put( + "created", + new UntypedString("2023-07-26T10:51:26Z")); + put("label", new UntypedString("Keyword2")); + put( + "termGuid", + new UntypedString( + "2cae6c6a-9bb8-4a78-afff-81b88e735fef")); + put("wssId", new UntypedLong(345345345L)); + } + }))); + + untypedTestEntity.setKeywords(keyWordsCollection); + untypedTestEntity + .getAdditionalData() + .put( + "extra", + new UntypedObject( + new HashMap<>() { + { + put( + "createdDateTime", + new UntypedString("2024-01-15T00:00:00+00:00")); + } + })); + var jsonSerializer = new JsonSerializationWriter(); jsonSerializer.writeObjectValue("", untypedTestEntity); var contentStream = jsonSerializer.getSerializedContent(); @@ -36,15 +113,16 @@ public void WritesSampleObjectValueWithUntypedProperties() throws IOException { // Assert var expectedString = - "{\"id\":\"1\",\"title\":\"Title\"," - + "\"location\":{\"address\":{\"city\":\"Redmond\",\"postalCode\":\"98052\",\"state\":\"Washington\",\"street\":\"NE" - + " 36th St\"}," - + "\"coordinates\":{\"latitude\":47.641942,\"longitude\":-122.127222},\"displayName\":\"Microsoft" - + " Building 92\",\"floorCount\":50,\"hasReception\":true,\"contact\":null}," + "{\"id\":\"1\"," + + "\"location\":{\"hasReception\":true,\"address\":{\"city\":\"Redmond\",\"street\":\"NE" + + " 36th" + + " St\",\"postalCode\":\"98052\",\"state\":\"Washington\"},\"displayName\":\"Microsoft" + + " Building" + + " 92\",\"floorCount\":50,\"contact\":null,\"coordinates\":{\"latitude\":47.641942,\"longitude\":-122.127222}}," + "\"keywords\":[" - + "{\"created\":\"2023-07-26T10:41:26Z\",\"label\":\"Keyword1\",\"termGuid\":\"10e9cc83-b5a4-4c8d-8dab-4ada1252dd70\",\"wssId\":6442450941}," - + "{\"created\":\"2023-07-26T10:51:26Z\",\"label\":\"Keyword2\",\"termGuid\":\"2cae6c6a-9bb8-4a78-afff-81b88e735fef\",\"wssId\":6442450942}]," - + "\"extra\":{\"createdDateTime\":\"2024-01-15T00:00:00\\u002B00:00\"}}"; + + "{\"wssId\":345345345,\"created\":\"2023-07-26T10:41:26Z\",\"label\":\"Keyword1\",\"termGuid\":\"10e9cc83-b5a4-4c8d-8dab-4ada1252dd70\"}," + + "{\"wssId\":345345345,\"created\":\"2023-07-26T10:51:26Z\",\"label\":\"Keyword2\",\"termGuid\":\"2cae6c6a-9bb8-4a78-afff-81b88e735fef\"}]," + + "\"extra\":{\"createdDateTime\":\"2024-01-15T00:00:00+00:00\"}}"; assertEquals(expectedString, serializedJsonString); } } diff --git a/components/serialization/json/src/test/java/com/microsoft/kiota/serialization/mocks/UntypedTestEntity.java b/components/serialization/json/src/test/java/com/microsoft/kiota/serialization/mocks/UntypedTestEntity.java index 2349197cb..b59aae756 100644 --- a/components/serialization/json/src/test/java/com/microsoft/kiota/serialization/mocks/UntypedTestEntity.java +++ b/components/serialization/json/src/test/java/com/microsoft/kiota/serialization/mocks/UntypedTestEntity.java @@ -83,7 +83,7 @@ public void serialize(SerializationWriter writer) { Objects.requireNonNull(writer); writer.writeStringValue("id", getId()); writer.writeObjectValue("location", getLocation()); - writer.writeObjectValue("keywords", getDetail()); + writer.writeObjectValue("keywords", getKeywords()); writer.writeObjectValue("detail", getDetail()); writer.writeAdditionalData(getAdditionalData()); } From 3b7697e471d889a50d75ae3f688d9f4293514048 Mon Sep 17 00:00:00 2001 From: Andrew Omondi Date: Mon, 5 Feb 2024 21:02:41 +0300 Subject: [PATCH 04/14] Cleanup --- .../microsoft/kiota/serialization/UntypedArray.java | 1 + .../kiota/serialization/UntypedBoolean.java | 1 + .../kiota/serialization/UntypedDecimal.java | 1 + .../microsoft/kiota/serialization/UntypedDouble.java | 1 + .../microsoft/kiota/serialization/UntypedFloat.java | 1 + .../kiota/serialization/UntypedInteger.java | 1 + .../microsoft/kiota/serialization/UntypedLong.java | 1 + .../microsoft/kiota/serialization/UntypedNode.java | 1 + .../microsoft/kiota/serialization/UntypedObject.java | 6 ++++-- .../microsoft/kiota/serialization/UntypedString.java | 1 + .../serialization/json/spotBugsExcludeFilter.xml | 1 + .../kiota/serialization/JsonSerializationWriter.java | 6 +++--- .../serialization/JsonSerializationWriterTests.java | 12 +++++------- .../kiota/serialization/mocks/UntypedTestEntity.java | 4 +++- 14 files changed, 25 insertions(+), 13 deletions(-) diff --git a/components/abstractions/src/main/java/com/microsoft/kiota/serialization/UntypedArray.java b/components/abstractions/src/main/java/com/microsoft/kiota/serialization/UntypedArray.java index 250707fd9..c5e398c1d 100644 --- a/components/abstractions/src/main/java/com/microsoft/kiota/serialization/UntypedArray.java +++ b/components/abstractions/src/main/java/com/microsoft/kiota/serialization/UntypedArray.java @@ -6,6 +6,7 @@ public class UntypedArray extends UntypedNode { /** * The constructor for the UntypedArray + * @param collection Collection to initialize with. */ public UntypedArray(Iterable collection) { value = collection; diff --git a/components/abstractions/src/main/java/com/microsoft/kiota/serialization/UntypedBoolean.java b/components/abstractions/src/main/java/com/microsoft/kiota/serialization/UntypedBoolean.java index 69070e531..80a9e218e 100644 --- a/components/abstractions/src/main/java/com/microsoft/kiota/serialization/UntypedBoolean.java +++ b/components/abstractions/src/main/java/com/microsoft/kiota/serialization/UntypedBoolean.java @@ -6,6 +6,7 @@ public class UntypedBoolean extends UntypedNode { /** * The constructor for the UntypedBoolean + * @param boolValue Boolean to create node with. */ public UntypedBoolean(Boolean boolValue) { value = boolValue; diff --git a/components/abstractions/src/main/java/com/microsoft/kiota/serialization/UntypedDecimal.java b/components/abstractions/src/main/java/com/microsoft/kiota/serialization/UntypedDecimal.java index 30b6d89c6..4a0d71093 100644 --- a/components/abstractions/src/main/java/com/microsoft/kiota/serialization/UntypedDecimal.java +++ b/components/abstractions/src/main/java/com/microsoft/kiota/serialization/UntypedDecimal.java @@ -8,6 +8,7 @@ public class UntypedDecimal extends UntypedNode { /** * The constructor for the UntypedDecimal + * @param decimalValue The decimal to create the node with. */ public UntypedDecimal(BigDecimal decimalValue) { value = decimalValue; diff --git a/components/abstractions/src/main/java/com/microsoft/kiota/serialization/UntypedDouble.java b/components/abstractions/src/main/java/com/microsoft/kiota/serialization/UntypedDouble.java index 88c48541b..c38b0288e 100644 --- a/components/abstractions/src/main/java/com/microsoft/kiota/serialization/UntypedDouble.java +++ b/components/abstractions/src/main/java/com/microsoft/kiota/serialization/UntypedDouble.java @@ -6,6 +6,7 @@ public class UntypedDouble extends UntypedNode { /** * The constructor for the UntypedDouble + * @param doubleValue The Double to create the node with. */ public UntypedDouble(Double doubleValue) { value = doubleValue; diff --git a/components/abstractions/src/main/java/com/microsoft/kiota/serialization/UntypedFloat.java b/components/abstractions/src/main/java/com/microsoft/kiota/serialization/UntypedFloat.java index f22738ff0..08725d2ce 100644 --- a/components/abstractions/src/main/java/com/microsoft/kiota/serialization/UntypedFloat.java +++ b/components/abstractions/src/main/java/com/microsoft/kiota/serialization/UntypedFloat.java @@ -6,6 +6,7 @@ public class UntypedFloat extends UntypedNode { /** * The constructor for the UntypedFloat + * @param floatValue The float value to create the node with. */ public UntypedFloat(Float floatValue) { value = floatValue; diff --git a/components/abstractions/src/main/java/com/microsoft/kiota/serialization/UntypedInteger.java b/components/abstractions/src/main/java/com/microsoft/kiota/serialization/UntypedInteger.java index 0fa1be066..ab561c1f1 100644 --- a/components/abstractions/src/main/java/com/microsoft/kiota/serialization/UntypedInteger.java +++ b/components/abstractions/src/main/java/com/microsoft/kiota/serialization/UntypedInteger.java @@ -6,6 +6,7 @@ public class UntypedInteger extends UntypedNode { /** * The constructor for the UntypedObject + * @param intValue The integer to create the node with. */ public UntypedInteger(Integer intValue) { value = intValue; diff --git a/components/abstractions/src/main/java/com/microsoft/kiota/serialization/UntypedLong.java b/components/abstractions/src/main/java/com/microsoft/kiota/serialization/UntypedLong.java index 726b6b3c1..9566a1c90 100644 --- a/components/abstractions/src/main/java/com/microsoft/kiota/serialization/UntypedLong.java +++ b/components/abstractions/src/main/java/com/microsoft/kiota/serialization/UntypedLong.java @@ -6,6 +6,7 @@ public class UntypedLong extends UntypedNode { /** * The constructor for the UntypedLong + * @param longValue The long value to create the node with. */ public UntypedLong(Long longValue) { value = longValue; diff --git a/components/abstractions/src/main/java/com/microsoft/kiota/serialization/UntypedNode.java b/components/abstractions/src/main/java/com/microsoft/kiota/serialization/UntypedNode.java index 80da6767e..47b060cc6 100644 --- a/components/abstractions/src/main/java/com/microsoft/kiota/serialization/UntypedNode.java +++ b/components/abstractions/src/main/java/com/microsoft/kiota/serialization/UntypedNode.java @@ -36,6 +36,7 @@ public Object getValue() { /** * Creates a new instance of the appropriate class based on discriminator value. + * @param parseNode The parse node to crate from * @return A new UntypedNode instance. */ public static UntypedNode createFromDiscriminatorValue(@Nonnull final ParseNode parseNode) { diff --git a/components/abstractions/src/main/java/com/microsoft/kiota/serialization/UntypedObject.java b/components/abstractions/src/main/java/com/microsoft/kiota/serialization/UntypedObject.java index 10888cb86..bcc8c83ef 100644 --- a/components/abstractions/src/main/java/com/microsoft/kiota/serialization/UntypedObject.java +++ b/components/abstractions/src/main/java/com/microsoft/kiota/serialization/UntypedObject.java @@ -1,5 +1,6 @@ package com.microsoft.kiota.serialization; +import java.util.HashMap; import java.util.Map; /** @@ -8,9 +9,10 @@ public class UntypedObject extends UntypedNode { /** * The constructor for the UntypedObject + * @param propertiesMap The Map to create the node with */ public UntypedObject(Map propertiesMap) { - properties = propertiesMap; + properties = new HashMap<>(propertiesMap); } private final Map properties; @@ -21,6 +23,6 @@ public UntypedObject(Map propertiesMap) { */ @Override public Map getValue() { - return properties; + return new HashMap<>(properties); } } diff --git a/components/abstractions/src/main/java/com/microsoft/kiota/serialization/UntypedString.java b/components/abstractions/src/main/java/com/microsoft/kiota/serialization/UntypedString.java index 8bb024a87..31b15b1c6 100644 --- a/components/abstractions/src/main/java/com/microsoft/kiota/serialization/UntypedString.java +++ b/components/abstractions/src/main/java/com/microsoft/kiota/serialization/UntypedString.java @@ -6,6 +6,7 @@ public class UntypedString extends UntypedNode { /** * The constructor for the UntypedObject + * @param stringValue The string to create the node with. */ public UntypedString(String stringValue) { value = stringValue; diff --git a/components/serialization/json/spotBugsExcludeFilter.xml b/components/serialization/json/spotBugsExcludeFilter.xml index 62bdde362..d0f06c8a8 100644 --- a/components/serialization/json/spotBugsExcludeFilter.xml +++ b/components/serialization/json/spotBugsExcludeFilter.xml @@ -10,6 +10,7 @@ xsi:schemaLocation="https://github.com/spotbugs/filter/3.0.0 https://raw.githubu + diff --git a/components/serialization/json/src/main/java/com/microsoft/kiota/serialization/JsonSerializationWriter.java b/components/serialization/json/src/main/java/com/microsoft/kiota/serialization/JsonSerializationWriter.java index dcaa53429..f92cf3de6 100644 --- a/components/serialization/json/src/main/java/com/microsoft/kiota/serialization/JsonSerializationWriter.java +++ b/components/serialization/json/src/main/java/com/microsoft/kiota/serialization/JsonSerializationWriter.java @@ -275,12 +275,12 @@ public void writeObjectValue( .filter(Objects::nonNull) .collect(Collectors.toList()); final boolean serializingUntypedNode = value instanceof UntypedNode; - if (serializingUntypedNode) { - if (onBeforeObjectSerialization != null && value != null) { + if (serializingUntypedNode && value != null) { + if (onBeforeObjectSerialization != null) { onBeforeObjectSerialization.accept(value); } writeUntypedValue(key, (UntypedNode) value); - if (onAfterObjectSerialization != null && value != null) { + if (onAfterObjectSerialization != null) { onAfterObjectSerialization.accept(value); } } else if (value != null || !nonNullAdditionalValuesToMerge.isEmpty()) { diff --git a/components/serialization/json/src/test/java/com/microsoft/kiota/serialization/JsonSerializationWriterTests.java b/components/serialization/json/src/test/java/com/microsoft/kiota/serialization/JsonSerializationWriterTests.java index 7d3b26d7f..2bdb4e8d8 100644 --- a/components/serialization/json/src/test/java/com/microsoft/kiota/serialization/JsonSerializationWriterTests.java +++ b/components/serialization/json/src/test/java/com/microsoft/kiota/serialization/JsonSerializationWriterTests.java @@ -14,7 +14,7 @@ public class JsonSerializationWriterTests { @Test - public void WritesSampleObjectValueWithUntypedProperties() throws IOException { + public void writesSampleObjectValueWithUntypedProperties() throws IOException { // Arrange var untypedTestEntity = new UntypedTestEntity(); untypedTestEntity.setId("1"); @@ -114,14 +114,12 @@ public void WritesSampleObjectValueWithUntypedProperties() throws IOException { // Assert var expectedString = "{\"id\":\"1\"," - + "\"location\":{\"hasReception\":true,\"address\":{\"city\":\"Redmond\",\"street\":\"NE" - + " 36th" - + " St\",\"postalCode\":\"98052\",\"state\":\"Washington\"},\"displayName\":\"Microsoft" - + " Building" + + "\"location\":{\"hasReception\":true,\"address\":{\"state\":\"Washington\",\"city\":\"Redmond\",\"street\":\"NE" + + " 36th St\",\"postalCode\":\"98052\"},\"displayName\":\"Microsoft Building" + " 92\",\"floorCount\":50,\"contact\":null,\"coordinates\":{\"latitude\":47.641942,\"longitude\":-122.127222}}," + "\"keywords\":[" - + "{\"wssId\":345345345,\"created\":\"2023-07-26T10:41:26Z\",\"label\":\"Keyword1\",\"termGuid\":\"10e9cc83-b5a4-4c8d-8dab-4ada1252dd70\"}," - + "{\"wssId\":345345345,\"created\":\"2023-07-26T10:51:26Z\",\"label\":\"Keyword2\",\"termGuid\":\"2cae6c6a-9bb8-4a78-afff-81b88e735fef\"}]," + + "{\"wssId\":345345345,\"label\":\"Keyword1\",\"termGuid\":\"10e9cc83-b5a4-4c8d-8dab-4ada1252dd70\",\"created\":\"2023-07-26T10:41:26Z\"}," + + "{\"wssId\":345345345,\"label\":\"Keyword2\",\"termGuid\":\"2cae6c6a-9bb8-4a78-afff-81b88e735fef\",\"created\":\"2023-07-26T10:51:26Z\"}]," + "\"extra\":{\"createdDateTime\":\"2024-01-15T00:00:00+00:00\"}}"; assertEquals(expectedString, serializedJsonString); } diff --git a/components/serialization/json/src/test/java/com/microsoft/kiota/serialization/mocks/UntypedTestEntity.java b/components/serialization/json/src/test/java/com/microsoft/kiota/serialization/mocks/UntypedTestEntity.java index b59aae756..b5051edda 100644 --- a/components/serialization/json/src/test/java/com/microsoft/kiota/serialization/mocks/UntypedTestEntity.java +++ b/components/serialization/json/src/test/java/com/microsoft/kiota/serialization/mocks/UntypedTestEntity.java @@ -2,6 +2,8 @@ import com.microsoft.kiota.serialization.*; +import jakarta.annotation.Nonnull; + import java.util.HashMap; import java.util.Map; import java.util.Objects; @@ -90,7 +92,7 @@ public void serialize(SerializationWriter writer) { private final Map _additionalData = new HashMap<>(); - @Override + @Nonnull @Override public Map getAdditionalData() { return _additionalData; } From 3de78166ed629776dc84519ff92806dfbb83046e Mon Sep 17 00:00:00 2001 From: Andrew Omondi Date: Mon, 5 Feb 2024 21:06:15 +0300 Subject: [PATCH 05/14] Adds cahngelog --- CHANGELOG.md | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index e35e6b310..7b9fd7206 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Changed +## [0.13.0] - 2024-02-05 + +### Added + +- Adds support for untyped json serialization/deserialization. + +### Changed + - Map `XXX` error status code range to Parsable Exception object if more specific error status code range is not found. ## [0.12.2] - 2024-02-01 From 83ff8552bdcb9b446f7828ee01850266d5c993da Mon Sep 17 00:00:00 2001 From: Andrew Omondi Date: Tue, 6 Feb 2024 10:26:54 +0300 Subject: [PATCH 06/14] Fix annotations --- .../com/microsoft/kiota/serialization/UntypedArray.java | 6 ++++-- .../com/microsoft/kiota/serialization/UntypedBoolean.java | 6 ++++-- .../com/microsoft/kiota/serialization/UntypedDecimal.java | 6 ++++-- .../com/microsoft/kiota/serialization/UntypedDouble.java | 6 ++++-- .../com/microsoft/kiota/serialization/UntypedFloat.java | 6 ++++-- .../com/microsoft/kiota/serialization/UntypedInteger.java | 6 ++++-- .../java/com/microsoft/kiota/serialization/UntypedLong.java | 6 ++++-- .../java/com/microsoft/kiota/serialization/UntypedNode.java | 3 ++- .../java/com/microsoft/kiota/serialization/UntypedNull.java | 4 +++- .../com/microsoft/kiota/serialization/UntypedObject.java | 6 ++++-- .../com/microsoft/kiota/serialization/UntypedString.java | 6 ++++-- 11 files changed, 41 insertions(+), 20 deletions(-) diff --git a/components/abstractions/src/main/java/com/microsoft/kiota/serialization/UntypedArray.java b/components/abstractions/src/main/java/com/microsoft/kiota/serialization/UntypedArray.java index c5e398c1d..034a72949 100644 --- a/components/abstractions/src/main/java/com/microsoft/kiota/serialization/UntypedArray.java +++ b/components/abstractions/src/main/java/com/microsoft/kiota/serialization/UntypedArray.java @@ -1,5 +1,7 @@ package com.microsoft.kiota.serialization; +import jakarta.annotation.Nonnull; + /** * Represents an untyped node with a collection of other untyped nodes. */ @@ -8,7 +10,7 @@ public class UntypedArray extends UntypedNode { * The constructor for the UntypedArray * @param collection Collection to initialize with. */ - public UntypedArray(Iterable collection) { + public UntypedArray(@Nonnull Iterable collection) { value = collection; } @@ -19,7 +21,7 @@ public UntypedArray(Iterable collection) { * @return The string value of the node. */ @Override - public Iterable getValue() { + @Nonnull public Iterable getValue() { return value; } } diff --git a/components/abstractions/src/main/java/com/microsoft/kiota/serialization/UntypedBoolean.java b/components/abstractions/src/main/java/com/microsoft/kiota/serialization/UntypedBoolean.java index 80a9e218e..7fa5b7a28 100644 --- a/components/abstractions/src/main/java/com/microsoft/kiota/serialization/UntypedBoolean.java +++ b/components/abstractions/src/main/java/com/microsoft/kiota/serialization/UntypedBoolean.java @@ -1,5 +1,7 @@ package com.microsoft.kiota.serialization; +import jakarta.annotation.Nonnull; + /** * Represents an untyped node with boolean value. */ @@ -8,7 +10,7 @@ public class UntypedBoolean extends UntypedNode { * The constructor for the UntypedBoolean * @param boolValue Boolean to create node with. */ - public UntypedBoolean(Boolean boolValue) { + public UntypedBoolean(@Nonnull Boolean boolValue) { value = boolValue; } @@ -19,7 +21,7 @@ public UntypedBoolean(Boolean boolValue) { * @return The bool value of the node. */ @Override - public Boolean getValue() { + @Nonnull public Boolean getValue() { return value; } } diff --git a/components/abstractions/src/main/java/com/microsoft/kiota/serialization/UntypedDecimal.java b/components/abstractions/src/main/java/com/microsoft/kiota/serialization/UntypedDecimal.java index 4a0d71093..7dac26e3f 100644 --- a/components/abstractions/src/main/java/com/microsoft/kiota/serialization/UntypedDecimal.java +++ b/components/abstractions/src/main/java/com/microsoft/kiota/serialization/UntypedDecimal.java @@ -1,5 +1,7 @@ package com.microsoft.kiota.serialization; +import jakarta.annotation.Nonnull; + import java.math.BigDecimal; /** @@ -10,7 +12,7 @@ public class UntypedDecimal extends UntypedNode { * The constructor for the UntypedDecimal * @param decimalValue The decimal to create the node with. */ - public UntypedDecimal(BigDecimal decimalValue) { + public UntypedDecimal(@Nonnull BigDecimal decimalValue) { value = decimalValue; } @@ -21,7 +23,7 @@ public UntypedDecimal(BigDecimal decimalValue) { * @return The BigDecimal value of the node. */ @Override - public BigDecimal getValue() { + @Nonnull public BigDecimal getValue() { return value; } } diff --git a/components/abstractions/src/main/java/com/microsoft/kiota/serialization/UntypedDouble.java b/components/abstractions/src/main/java/com/microsoft/kiota/serialization/UntypedDouble.java index c38b0288e..2d1a1d272 100644 --- a/components/abstractions/src/main/java/com/microsoft/kiota/serialization/UntypedDouble.java +++ b/components/abstractions/src/main/java/com/microsoft/kiota/serialization/UntypedDouble.java @@ -1,5 +1,7 @@ package com.microsoft.kiota.serialization; +import jakarta.annotation.Nonnull; + /** * Represents an untyped node with double value. */ @@ -8,7 +10,7 @@ public class UntypedDouble extends UntypedNode { * The constructor for the UntypedDouble * @param doubleValue The Double to create the node with. */ - public UntypedDouble(Double doubleValue) { + public UntypedDouble(@Nonnull Double doubleValue) { value = doubleValue; } @@ -19,7 +21,7 @@ public UntypedDouble(Double doubleValue) { * @return The double value of the node. */ @Override - public Double getValue() { + @Nonnull public Double getValue() { return value; } } diff --git a/components/abstractions/src/main/java/com/microsoft/kiota/serialization/UntypedFloat.java b/components/abstractions/src/main/java/com/microsoft/kiota/serialization/UntypedFloat.java index 08725d2ce..6f50fb98e 100644 --- a/components/abstractions/src/main/java/com/microsoft/kiota/serialization/UntypedFloat.java +++ b/components/abstractions/src/main/java/com/microsoft/kiota/serialization/UntypedFloat.java @@ -1,5 +1,7 @@ package com.microsoft.kiota.serialization; +import jakarta.annotation.Nonnull; + /** * Represents an untyped node with Float value. */ @@ -8,7 +10,7 @@ public class UntypedFloat extends UntypedNode { * The constructor for the UntypedFloat * @param floatValue The float value to create the node with. */ - public UntypedFloat(Float floatValue) { + public UntypedFloat(@Nonnull Float floatValue) { value = floatValue; } @@ -19,7 +21,7 @@ public UntypedFloat(Float floatValue) { * @return The float value of the node. */ @Override - public Float getValue() { + @Nonnull public Float getValue() { return value; } } diff --git a/components/abstractions/src/main/java/com/microsoft/kiota/serialization/UntypedInteger.java b/components/abstractions/src/main/java/com/microsoft/kiota/serialization/UntypedInteger.java index ab561c1f1..766234aa4 100644 --- a/components/abstractions/src/main/java/com/microsoft/kiota/serialization/UntypedInteger.java +++ b/components/abstractions/src/main/java/com/microsoft/kiota/serialization/UntypedInteger.java @@ -1,5 +1,7 @@ package com.microsoft.kiota.serialization; +import jakarta.annotation.Nonnull; + /** * Represents an untyped node with integer value. */ @@ -8,7 +10,7 @@ public class UntypedInteger extends UntypedNode { * The constructor for the UntypedObject * @param intValue The integer to create the node with. */ - public UntypedInteger(Integer intValue) { + public UntypedInteger(@Nonnull Integer intValue) { value = intValue; } @@ -19,7 +21,7 @@ public UntypedInteger(Integer intValue) { * @return The integer value of the node. */ @Override - public Integer getValue() { + @Nonnull public Integer getValue() { return value; } } diff --git a/components/abstractions/src/main/java/com/microsoft/kiota/serialization/UntypedLong.java b/components/abstractions/src/main/java/com/microsoft/kiota/serialization/UntypedLong.java index 9566a1c90..e4585d13a 100644 --- a/components/abstractions/src/main/java/com/microsoft/kiota/serialization/UntypedLong.java +++ b/components/abstractions/src/main/java/com/microsoft/kiota/serialization/UntypedLong.java @@ -1,5 +1,7 @@ package com.microsoft.kiota.serialization; +import jakarta.annotation.Nonnull; + /** * Represents an untyped node with Long value. */ @@ -8,7 +10,7 @@ public class UntypedLong extends UntypedNode { * The constructor for the UntypedLong * @param longValue The long value to create the node with. */ - public UntypedLong(Long longValue) { + public UntypedLong(@Nonnull Long longValue) { value = longValue; } @@ -19,7 +21,7 @@ public UntypedLong(Long longValue) { * @return The long value of the node. */ @Override - public Long getValue() { + @Nonnull public Long getValue() { return value; } } diff --git a/components/abstractions/src/main/java/com/microsoft/kiota/serialization/UntypedNode.java b/components/abstractions/src/main/java/com/microsoft/kiota/serialization/UntypedNode.java index 47b060cc6..50d783eea 100644 --- a/components/abstractions/src/main/java/com/microsoft/kiota/serialization/UntypedNode.java +++ b/components/abstractions/src/main/java/com/microsoft/kiota/serialization/UntypedNode.java @@ -1,6 +1,7 @@ package com.microsoft.kiota.serialization; import jakarta.annotation.Nonnull; +import jakarta.annotation.Nullable; import java.util.HashMap; import java.util.Map; @@ -30,7 +31,7 @@ public void serialize(@Nonnull SerializationWriter writer) {} * Gets the value assigned to untyped node. * @return The value assigned to untyped node. */ - public Object getValue() { + @Nullable public Object getValue() { return null; } diff --git a/components/abstractions/src/main/java/com/microsoft/kiota/serialization/UntypedNull.java b/components/abstractions/src/main/java/com/microsoft/kiota/serialization/UntypedNull.java index 24bfa034a..62cc1f132 100644 --- a/components/abstractions/src/main/java/com/microsoft/kiota/serialization/UntypedNull.java +++ b/components/abstractions/src/main/java/com/microsoft/kiota/serialization/UntypedNull.java @@ -1,5 +1,7 @@ package com.microsoft.kiota.serialization; +import jakarta.annotation.Nullable; + /** * Represents an untyped node with null value. */ @@ -16,7 +18,7 @@ public UntypedNull() { * @return null value. */ @Override - public Object getValue() { + @Nullable public Object getValue() { return null; } } diff --git a/components/abstractions/src/main/java/com/microsoft/kiota/serialization/UntypedObject.java b/components/abstractions/src/main/java/com/microsoft/kiota/serialization/UntypedObject.java index bcc8c83ef..b91f7150e 100644 --- a/components/abstractions/src/main/java/com/microsoft/kiota/serialization/UntypedObject.java +++ b/components/abstractions/src/main/java/com/microsoft/kiota/serialization/UntypedObject.java @@ -1,5 +1,7 @@ package com.microsoft.kiota.serialization; +import jakarta.annotation.Nonnull; + import java.util.HashMap; import java.util.Map; @@ -11,7 +13,7 @@ public class UntypedObject extends UntypedNode { * The constructor for the UntypedObject * @param propertiesMap The Map to create the node with */ - public UntypedObject(Map propertiesMap) { + public UntypedObject(@Nonnull Map propertiesMap) { properties = new HashMap<>(propertiesMap); } @@ -22,7 +24,7 @@ public UntypedObject(Map propertiesMap) { * @return The Map of property keys and their values. */ @Override - public Map getValue() { + @Nonnull public Map getValue() { return new HashMap<>(properties); } } diff --git a/components/abstractions/src/main/java/com/microsoft/kiota/serialization/UntypedString.java b/components/abstractions/src/main/java/com/microsoft/kiota/serialization/UntypedString.java index 31b15b1c6..748404c96 100644 --- a/components/abstractions/src/main/java/com/microsoft/kiota/serialization/UntypedString.java +++ b/components/abstractions/src/main/java/com/microsoft/kiota/serialization/UntypedString.java @@ -1,5 +1,7 @@ package com.microsoft.kiota.serialization; +import jakarta.annotation.Nonnull; + /** * Represents an untyped node with string value. */ @@ -8,7 +10,7 @@ public class UntypedString extends UntypedNode { * The constructor for the UntypedObject * @param stringValue The string to create the node with. */ - public UntypedString(String stringValue) { + public UntypedString(@Nonnull String stringValue) { value = stringValue; } @@ -19,7 +21,7 @@ public UntypedString(String stringValue) { * @return The string value of the node. */ @Override - public String getValue() { + @Nonnull public String getValue() { return value; } } From 7977a9fe3f028caa5bb37a6bd77dbabf66735bfb Mon Sep 17 00:00:00 2001 From: Andrew Omondi Date: Tue, 6 Feb 2024 11:35:26 +0300 Subject: [PATCH 07/14] Missing annotation --- .../main/java/com/microsoft/kiota/serialization/UntypedNode.java | 1 + 1 file changed, 1 insertion(+) diff --git a/components/abstractions/src/main/java/com/microsoft/kiota/serialization/UntypedNode.java b/components/abstractions/src/main/java/com/microsoft/kiota/serialization/UntypedNode.java index 50d783eea..1b7726c78 100644 --- a/components/abstractions/src/main/java/com/microsoft/kiota/serialization/UntypedNode.java +++ b/components/abstractions/src/main/java/com/microsoft/kiota/serialization/UntypedNode.java @@ -40,6 +40,7 @@ public void serialize(@Nonnull SerializationWriter writer) {} * @param parseNode The parse node to crate from * @return A new UntypedNode instance. */ + @Nonnull public static UntypedNode createFromDiscriminatorValue(@Nonnull final ParseNode parseNode) { return new UntypedNode(); } From 65089cc9d2a3626d25995c14ad6c4809758de015 Mon Sep 17 00:00:00 2001 From: Andrew Omondi Date: Tue, 6 Feb 2024 11:39:23 +0300 Subject: [PATCH 08/14] format --- .../java/com/microsoft/kiota/serialization/UntypedNode.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/components/abstractions/src/main/java/com/microsoft/kiota/serialization/UntypedNode.java b/components/abstractions/src/main/java/com/microsoft/kiota/serialization/UntypedNode.java index 1b7726c78..5597928a5 100644 --- a/components/abstractions/src/main/java/com/microsoft/kiota/serialization/UntypedNode.java +++ b/components/abstractions/src/main/java/com/microsoft/kiota/serialization/UntypedNode.java @@ -40,8 +40,7 @@ public void serialize(@Nonnull SerializationWriter writer) {} * @param parseNode The parse node to crate from * @return A new UntypedNode instance. */ - @Nonnull - public static UntypedNode createFromDiscriminatorValue(@Nonnull final ParseNode parseNode) { + @Nonnull public static UntypedNode createFromDiscriminatorValue(@Nonnull final ParseNode parseNode) { return new UntypedNode(); } } From 1432cbb77af6b6bd8499499c231e832a39cadd92 Mon Sep 17 00:00:00 2001 From: Andrew Omondi Date: Thu, 8 Feb 2024 09:39:55 +0300 Subject: [PATCH 09/14] Updates version and release notes. --- CHANGELOG.md | 4 ++++ gradle.properties | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index eac79cf6a..290636bd6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Changed +## [1.1.0] - 2024-02-14 + +- Adds support for untyped nodes. + ## [1.0.0] - 2024-02-07 ### Changed diff --git a/gradle.properties b/gradle.properties index d0d1ae918..5b73eeac9 100644 --- a/gradle.properties +++ b/gradle.properties @@ -25,7 +25,7 @@ org.gradle.caching=true mavenGroupId = com.microsoft.kiota mavenMajorVersion = 1 -mavenMinorVersion = 0 +mavenMinorVersion = 1 mavenPatchVersion = 0 mavenArtifactSuffix = From 7200636f5903e15909cd85d70f47d960af53306f Mon Sep 17 00:00:00 2001 From: Andrew Omondi Date: Tue, 27 Feb 2024 12:07:31 +0300 Subject: [PATCH 10/14] Fix merge errors --- .../com/microsoft/kiota/serialization/JsonParseNode.java | 1 + .../microsoft/kiota/serialization/JsonParseNodeTests.java | 6 ++---- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/components/serialization/json/src/main/java/com/microsoft/kiota/serialization/JsonParseNode.java b/components/serialization/json/src/main/java/com/microsoft/kiota/serialization/JsonParseNode.java index ff0a840e1..06a94ab5a 100644 --- a/components/serialization/json/src/main/java/com/microsoft/kiota/serialization/JsonParseNode.java +++ b/components/serialization/json/src/main/java/com/microsoft/kiota/serialization/JsonParseNode.java @@ -19,6 +19,7 @@ import java.util.ArrayList; import java.util.Base64; import java.util.EnumSet; +import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; diff --git a/components/serialization/json/src/test/java/com/microsoft/kiota/serialization/JsonParseNodeTests.java b/components/serialization/json/src/test/java/com/microsoft/kiota/serialization/JsonParseNodeTests.java index b86dd27e2..f4d4068c0 100644 --- a/components/serialization/json/src/test/java/com/microsoft/kiota/serialization/JsonParseNodeTests.java +++ b/components/serialization/json/src/test/java/com/microsoft/kiota/serialization/JsonParseNodeTests.java @@ -1,11 +1,9 @@ package com.microsoft.kiota.serialization; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertInstanceOf; -import static org.junit.jupiter.api.Assertions.assertNull; -import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.*; import com.google.gson.JsonParser; +import com.microsoft.kiota.serialization.mocks.UntypedTestEntity; import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest; From 58d4322fbbbc2eb40983378607e949355bea0ec3 Mon Sep 17 00:00:00 2001 From: Andrew Omondi Date: Tue, 27 Feb 2024 12:09:23 +0300 Subject: [PATCH 11/14] Fix changelog --- CHANGELOG.md | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8007cf233..aff538343 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Changed +## [1.1.0] - 2024-02-14 + +### Added + +- Adds support for untyped nodes. + ## [1.0.4] - 2024-02-26 ### Changed @@ -35,10 +41,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Allow authentication for localhost HTTP urls -## [1.1.0] - 2024-02-14 - -- Adds support for untyped nodes. - ## [1.0.0] - 2024-02-07 ### Changed From bf75056a89ecb46611525992a63981e3ba7e3cb9 Mon Sep 17 00:00:00 2001 From: Andrew Omondi Date: Mon, 4 Mar 2024 14:21:32 +0300 Subject: [PATCH 12/14] Apply suggestions --- .../java/com/microsoft/kiota/serialization/UntypedNode.java | 4 +++- .../com/microsoft/kiota/serialization/JsonParseNodeTests.java | 2 +- .../kiota/serialization/JsonSerializationWriterTests.java | 4 ++-- 3 files changed, 6 insertions(+), 4 deletions(-) diff --git a/components/abstractions/src/main/java/com/microsoft/kiota/serialization/UntypedNode.java b/components/abstractions/src/main/java/com/microsoft/kiota/serialization/UntypedNode.java index 5597928a5..321a37758 100644 --- a/components/abstractions/src/main/java/com/microsoft/kiota/serialization/UntypedNode.java +++ b/components/abstractions/src/main/java/com/microsoft/kiota/serialization/UntypedNode.java @@ -25,7 +25,9 @@ public Map> getFieldDeserializers() { * Serializes the current object */ @Override - public void serialize(@Nonnull SerializationWriter writer) {} + public void serialize(@Nonnull SerializationWriter writer) { + // no properties to serialize. This is handled by custom serialization logic. + } /** * Gets the value assigned to untyped node. diff --git a/components/serialization/json/src/test/java/com/microsoft/kiota/serialization/JsonParseNodeTests.java b/components/serialization/json/src/test/java/com/microsoft/kiota/serialization/JsonParseNodeTests.java index f4d4068c0..80464cbc2 100644 --- a/components/serialization/json/src/test/java/com/microsoft/kiota/serialization/JsonParseNodeTests.java +++ b/components/serialization/json/src/test/java/com/microsoft/kiota/serialization/JsonParseNodeTests.java @@ -96,7 +96,7 @@ void testInvalidOffsetDateTimeStringThrowsException(final String dateTimeString) } @Test - public void GetEntityWithUntypedNodesFromJson() throws UnsupportedEncodingException { + void GetEntityWithUntypedNodesFromJson() throws UnsupportedEncodingException { final var rawResponse = new ByteArrayInputStream(testUntypedJson.getBytes("UTF-8")); final var parseNode = _parseNodeFactory.getParseNode(contentType, rawResponse); // Act diff --git a/components/serialization/json/src/test/java/com/microsoft/kiota/serialization/JsonSerializationWriterTests.java b/components/serialization/json/src/test/java/com/microsoft/kiota/serialization/JsonSerializationWriterTests.java index 2bdb4e8d8..fe9348f0b 100644 --- a/components/serialization/json/src/test/java/com/microsoft/kiota/serialization/JsonSerializationWriterTests.java +++ b/components/serialization/json/src/test/java/com/microsoft/kiota/serialization/JsonSerializationWriterTests.java @@ -11,10 +11,10 @@ import java.util.Arrays; import java.util.HashMap; -public class JsonSerializationWriterTests { +class JsonSerializationWriterTests { @Test - public void writesSampleObjectValueWithUntypedProperties() throws IOException { + void writesSampleObjectValueWithUntypedProperties() throws IOException { // Arrange var untypedTestEntity = new UntypedTestEntity(); untypedTestEntity.setId("1"); From be64ce4d6dab21011baf7fe1b0868b5f5edc1eb1 Mon Sep 17 00:00:00 2001 From: Andrew Omondi Date: Tue, 5 Mar 2024 09:29:14 +0300 Subject: [PATCH 13/14] Fix changelog merge --- CHANGELOG.md | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 11fe3261f..e739e1e4b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Changed +## [1.1.0] - 2024-02-14 + +### Added + +- Adds support for untyped nodes. + ## [1.0.6] - 2023-03-04 ### Changed @@ -23,12 +29,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Added contentLength property to RequestInformation to facilitate in setting the content length of the Okhttp3 RequestBody object within the OkhttpRequestAdapter. -## [1.1.0] - 2024-02-14 - -### Added - -- Adds support for untyped nodes. - ## [1.0.4] - 2024-02-26 ### Changed From f6c931544884a16b767409aacfcd859ca9580d84 Mon Sep 17 00:00:00 2001 From: Andrew Omondi Date: Thu, 7 Mar 2024 14:05:28 +0300 Subject: [PATCH 14/14] consistency --- .../java/com/microsoft/kiota/serialization/UntypedNode.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/components/abstractions/src/main/java/com/microsoft/kiota/serialization/UntypedNode.java b/components/abstractions/src/main/java/com/microsoft/kiota/serialization/UntypedNode.java index 321a37758..a59dc01fe 100644 --- a/components/abstractions/src/main/java/com/microsoft/kiota/serialization/UntypedNode.java +++ b/components/abstractions/src/main/java/com/microsoft/kiota/serialization/UntypedNode.java @@ -34,7 +34,8 @@ public void serialize(@Nonnull SerializationWriter writer) { * @return The value assigned to untyped node. */ @Nullable public Object getValue() { - return null; + throw new UnsupportedOperationException( + "getValue is implemented for derived types of UntypedNode"); } /**