Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature/composed types #441

Merged
merged 2 commits into from
Jun 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Added

## [0.4.5] - 2023-06-27

### Changed

- Fixed a bug where composed types would not serialize properly.

## [0.4.4] - 2023-06-09

### Added
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.microsoft.kiota.serialization;
/** Marks a model as a composed type wrapper for serialization */
public interface ComposedTypeWrapper {

}
Original file line number Diff line number Diff line change
Expand Up @@ -253,14 +253,17 @@ public <T extends Parsable> void writeObjectValue(@Nullable final String key, @N
Objects.requireNonNull(additionalValuesToMerge);
try {
final List<Parsable> nonNullAdditionalValuesToMerge = Stream.of(additionalValuesToMerge).filter(Objects::nonNull).collect(Collectors.toList());
if(value != null || nonNullAdditionalValuesToMerge.size() > 0) {
if(value != null || !nonNullAdditionalValuesToMerge.isEmpty()) {
if(key != null && !key.isEmpty()) {
writer.name(key);
}
if(onBeforeObjectSerialization != null && value != null) {
onBeforeObjectSerialization.accept(value);
}
writer.beginObject();
final boolean serializingComposedType = value instanceof ComposedTypeWrapper;
if(!serializingComposedType) {
writer.beginObject();
}
if(value != null) {
if(onStartObjectSerialization != null) {
onStartObjectSerialization.accept(value, this);
Expand All @@ -279,7 +282,9 @@ public <T extends Parsable> void writeObjectValue(@Nullable final String key, @N
onAfterObjectSerialization.accept(additionalValueToMerge);
}
}
writer.endObject();
if(!serializingComposedType) {
writer.endObject();
}
if(onAfterObjectSerialization != null && value != null) {
onAfterObjectSerialization.accept(value);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@
import java.io.IOException;
import java.io.UnsupportedEncodingException;

public class IntersectionWrapperParseTests {
class IntersectionWrapperParseTests {
private static final JsonParseNodeFactory _parseNodeFactory = new JsonParseNodeFactory();
private static final JsonSerializationWriterFactory _serializationWriterFactory = new JsonSerializationWriterFactory();
private static final String contentType = "application/json";
@Test
public void ParsesIntersectionTypeComplexProperty1() throws UnsupportedEncodingException {
void ParsesIntersectionTypeComplexProperty1() throws UnsupportedEncodingException {
final var initialString = "{\"displayName\":\"McGill\",\"officeLocation\":\"Montreal\", \"id\": \"opaque\"}";
final var rawResponse = new ByteArrayInputStream(initialString.getBytes("UTF-8"));
final var parseNode = _parseNodeFactory.getParseNode(contentType, rawResponse);
Expand All @@ -31,7 +31,7 @@ public void ParsesIntersectionTypeComplexProperty1() throws UnsupportedEncodingE
assertEquals("McGill", result.getComposedType2().getDisplayName());
}
@Test
public void ParsesIntersectionTypeComplexProperty2() throws UnsupportedEncodingException {
void ParsesIntersectionTypeComplexProperty2() throws UnsupportedEncodingException {
final var initialString = "{\"displayName\":\"McGill\",\"officeLocation\":\"Montreal\", \"id\": 10}";
final var rawResponse = new ByteArrayInputStream(initialString.getBytes("UTF-8"));
final var parseNode = _parseNodeFactory.getParseNode(contentType, rawResponse);
Expand All @@ -46,7 +46,7 @@ public void ParsesIntersectionTypeComplexProperty2() throws UnsupportedEncodingE
assertEquals("McGill", result.getComposedType2().getDisplayName());
}
@Test
public void ParsesIntersectionTypeComplexProperty3() throws UnsupportedEncodingException {
void ParsesIntersectionTypeComplexProperty3() throws UnsupportedEncodingException {
final var initialString = "[{\"@odata.type\":\"#microsoft.graph.TestEntity\",\"officeLocation\":\"Ottawa\", \"id\": \"11\"}, {\"@odata.type\":\"#microsoft.graph.TestEntity\",\"officeLocation\":\"Montreal\", \"id\": \"10\"}]";
final var rawResponse = new ByteArrayInputStream(initialString.getBytes("UTF-8"));
final var parseNode = _parseNodeFactory.getParseNode(contentType, rawResponse);
Expand All @@ -60,7 +60,7 @@ public void ParsesIntersectionTypeComplexProperty3() throws UnsupportedEncodingE
assertEquals("Ottawa", result.getComposedType3().get(0).getOfficeLocation());
}
@Test
public void ParsesIntersectionTypeStringValue() throws UnsupportedEncodingException {
void ParsesIntersectionTypeStringValue() throws UnsupportedEncodingException {
final var initialString = "\"officeLocation\"";
final var rawResponse = new ByteArrayInputStream(initialString.getBytes("UTF-8"));
final var parseNode = _parseNodeFactory.getParseNode(contentType, rawResponse);
Expand All @@ -73,21 +73,21 @@ public void ParsesIntersectionTypeStringValue() throws UnsupportedEncodingExcept
assertEquals("officeLocation", result.getStringValue());
}
@Test
public void SerializesIntersectionTypeStringValue() throws IOException {
void SerializesIntersectionTypeStringValue() throws IOException {
try (final var writer = _serializationWriterFactory.getSerializationWriter(contentType)) {
var model = new IntersectionTypeMock() {{
setStringValue("officeLocation");
}};

model.serialize(writer);
writer.writeObjectValue("", model);
try (final var result = writer.getSerializedContent()) {
final String text = new String(result.readAllBytes(), StandardCharsets.UTF_8);
assertEquals("\"officeLocation\"", text);
}
}
}
@Test
public void SerializesIntersectionTypeComplexProperty1() throws IOException {
void SerializesIntersectionTypeComplexProperty1() throws IOException {
try (final var writer = _serializationWriterFactory.getSerializationWriter(contentType)) {
var model = new IntersectionTypeMock() {{
setComposedType1(new TestEntity() {{
Expand All @@ -99,15 +99,15 @@ public void SerializesIntersectionTypeComplexProperty1() throws IOException {
}});
}};

model.serialize(writer);
writer.writeObjectValue("", model);
try (final var result = writer.getSerializedContent()) {
final String text = new String(result.readAllBytes(), StandardCharsets.UTF_8);
assertEquals("{\"id\":\"opaque\",\"officeLocation\":\"Montreal\",\"displayName\":\"McGill\"}", text);
}
}
}
@Test
public void SerializesIntersectionTypeComplexProperty2() throws IOException {
void SerializesIntersectionTypeComplexProperty2() throws IOException {
try (final var writer = _serializationWriterFactory.getSerializationWriter(contentType)) {
var model = new IntersectionTypeMock() {{
setComposedType2(new SecondTestEntity() {{
Expand All @@ -116,15 +116,15 @@ public void SerializesIntersectionTypeComplexProperty2() throws IOException {
}});
}};

model.serialize(writer);
writer.writeObjectValue("", model);
try (final var result = writer.getSerializedContent()) {
final String text = new String(result.readAllBytes(), StandardCharsets.UTF_8);
assertEquals("{\"displayName\":\"McGill\",\"id\":10}", text);
}
}
}
@Test
public void SerializesIntersectionTypeComplexProperty3() throws IOException {
void SerializesIntersectionTypeComplexProperty3() throws IOException {
try (final var writer = _serializationWriterFactory.getSerializationWriter(contentType)) {
var model = new IntersectionTypeMock() {{
setComposedType3(new ArrayList<>() {{
Expand All @@ -139,7 +139,7 @@ public void SerializesIntersectionTypeComplexProperty3() throws IOException {
}});
}};

model.serialize(writer);
writer.writeObjectValue("", model);
try (final var result = writer.getSerializedContent()) {
final String text = new String(result.readAllBytes(), StandardCharsets.UTF_8);
assertEquals("[{\"id\":\"10\",\"officeLocation\":\"Montreal\"},{\"id\":\"11\",\"officeLocation\":\"Ottawa\"}]", text);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@
import java.io.IOException;
import java.io.UnsupportedEncodingException;

public class UnionWrapperParseTests {
class UnionWrapperParseTests {
private static final JsonParseNodeFactory _parseNodeFactory = new JsonParseNodeFactory();
private static final JsonSerializationWriterFactory _serializationWriterFactory = new JsonSerializationWriterFactory();
private static final String contentType = "application/json";
@Test
public void ParsesUnionTypeComplexProperty1() throws UnsupportedEncodingException {
void ParsesUnionTypeComplexProperty1() throws UnsupportedEncodingException {
final var initialString = "{\"@odata.type\":\"#microsoft.graph.testEntity\",\"officeLocation\":\"Montreal\", \"id\": \"opaque\"}";
final var rawResponse = new ByteArrayInputStream(initialString.getBytes("UTF-8"));
final var parseNode = _parseNodeFactory.getParseNode(contentType, rawResponse);
Expand All @@ -31,7 +31,7 @@ public void ParsesUnionTypeComplexProperty1() throws UnsupportedEncodingExceptio
assertEquals("opaque", result.getComposedType1().getId());
}
@Test
public void ParsesUnionTypeComplexProperty2() throws UnsupportedEncodingException {
void ParsesUnionTypeComplexProperty2() throws UnsupportedEncodingException {
final var initialString = "{\"@odata.type\":\"#microsoft.graph.secondTestEntity\",\"officeLocation\":\"Montreal\", \"id\": 10}";
final var rawResponse = new ByteArrayInputStream(initialString.getBytes("UTF-8"));
final var parseNode = _parseNodeFactory.getParseNode(contentType, rawResponse);
Expand All @@ -44,7 +44,7 @@ public void ParsesUnionTypeComplexProperty2() throws UnsupportedEncodingExceptio
assertEquals(10, result.getComposedType2().getId());
}
@Test
public void ParsesUnionTypeComplexProperty3() throws UnsupportedEncodingException {
void ParsesUnionTypeComplexProperty3() throws UnsupportedEncodingException {
final var initialString = "[{\"@odata.type\":\"#microsoft.graph.TestEntity\",\"officeLocation\":\"Ottawa\", \"id\": \"11\"}, {\"@odata.type\":\"#microsoft.graph.TestEntity\",\"officeLocation\":\"Montreal\", \"id\": \"10\"}]";
final var rawResponse = new ByteArrayInputStream(initialString.getBytes("UTF-8"));
final var parseNode = _parseNodeFactory.getParseNode(contentType, rawResponse);
Expand All @@ -58,7 +58,7 @@ public void ParsesUnionTypeComplexProperty3() throws UnsupportedEncodingExceptio
assertEquals("11", result.getComposedType3().get(0).getId());
}
@Test
public void ParsesUnionTypeStringValue() throws UnsupportedEncodingException {
void ParsesUnionTypeStringValue() throws UnsupportedEncodingException {
final var initialString = "\"officeLocation\"";
final var rawResponse = new ByteArrayInputStream(initialString.getBytes("UTF-8"));
final var parseNode = _parseNodeFactory.getParseNode(contentType, rawResponse);
Expand All @@ -71,21 +71,21 @@ public void ParsesUnionTypeStringValue() throws UnsupportedEncodingException {
assertEquals("officeLocation", result.getStringValue());
}
@Test
public void SerializesUnionTypeStringValue() throws IOException {
void SerializesUnionTypeStringValue() throws IOException {
try (final var writer = _serializationWriterFactory.getSerializationWriter(contentType)) {
var model = new UnionTypeMock() {{
setStringValue("officeLocation");
}};

model.serialize(writer);
writer.writeObjectValue("", model);
try (final var result = writer.getSerializedContent()) {
final String text = new String(result.readAllBytes(), StandardCharsets.UTF_8);
assertEquals("\"officeLocation\"", text);
}
}
}
@Test
public void SerializesUnionTypeComplexProperty1() throws IOException {
void SerializesUnionTypeComplexProperty1() throws IOException {
try (final var writer = _serializationWriterFactory.getSerializationWriter(contentType)) {
var model = new UnionTypeMock() {{
setComposedType1(new TestEntity() {{
Expand All @@ -97,15 +97,15 @@ public void SerializesUnionTypeComplexProperty1() throws IOException {
}});
}};

model.serialize(writer);
writer.writeObjectValue("", model);
try (final var result = writer.getSerializedContent()) {
final String text = new String(result.readAllBytes(), StandardCharsets.UTF_8);
assertEquals("{\"id\":\"opaque\",\"officeLocation\":\"Montreal\"}", text);
}
}
}
@Test
public void SerializesUnionTypeComplexProperty2() throws IOException {
void SerializesUnionTypeComplexProperty2() throws IOException {
try (final var writer = _serializationWriterFactory.getSerializationWriter(contentType)) {
var model = new UnionTypeMock() {{
setComposedType2(new SecondTestEntity() {{
Expand All @@ -114,15 +114,15 @@ public void SerializesUnionTypeComplexProperty2() throws IOException {
}});
}};

model.serialize(writer);
writer.writeObjectValue("", model);
try (final var result = writer.getSerializedContent()) {
final String text = new String(result.readAllBytes(), StandardCharsets.UTF_8);
assertEquals("{\"displayName\":\"McGill\",\"id\":10}", text);
}
}
}
@Test
public void SerializesUnionTypeComplexProperty3() throws IOException {
void SerializesUnionTypeComplexProperty3() throws IOException {
try (final var writer = _serializationWriterFactory.getSerializationWriter(contentType)) {
var model = new UnionTypeMock() {{
setComposedType3(new ArrayList<>() {{
Expand All @@ -137,7 +137,7 @@ public void SerializesUnionTypeComplexProperty3() throws IOException {
}});
}};

model.serialize(writer);
writer.writeObjectValue("", model);
try (final var result = writer.getSerializedContent()) {
final String text = new String(result.readAllBytes(), StandardCharsets.UTF_8);
assertEquals("[{\"id\":\"10\",\"officeLocation\":\"Montreal\"},{\"id\":\"11\",\"officeLocation\":\"Ottawa\"}]", text);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@
import java.util.Objects;
import java.util.function.Consumer;

import com.microsoft.kiota.serialization.ComposedTypeWrapper;
import com.microsoft.kiota.serialization.Parsable;

public class IntersectionTypeMock implements Parsable {
public class IntersectionTypeMock implements Parsable, ComposedTypeWrapper {
private TestEntity _composedType1;
private SecondTestEntity _composedType2;
private String _stringValue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@

import com.microsoft.kiota.serialization.ParseNode;
import com.microsoft.kiota.serialization.SerializationWriter;
import com.microsoft.kiota.serialization.ComposedTypeWrapper;
import com.microsoft.kiota.serialization.Parsable;

public class UnionTypeMock implements Parsable {
public class UnionTypeMock implements Parsable, ComposedTypeWrapper {
private TestEntity _composedType1;
private SecondTestEntity _composedType2;
private String _stringValue;
Expand All @@ -21,9 +22,9 @@ public static UnionTypeMock createFromDiscriminatorValue(@javax.annotation.Nonnu
final ParseNode mappingValueNode = parseNode.getChildNode("@odata.type");
if (mappingValueNode != null) {
final String mappingValue = mappingValueNode.getStringValue();
if (mappingValue.equals("#microsoft.graph.testEntity")) {
if ("#microsoft.graph.testEntity".equalsIgnoreCase(mappingValue)) {
result.setComposedType1(new TestEntity());
} else if (mappingValue.equals("#microsoft.graph.secondTestEntity")) {
} else if ("#microsoft.graph.secondTestEntity".equalsIgnoreCase(mappingValue)) {
result.setComposedType2(new SecondTestEntity());
}
} else if (parseNode.getStringValue() != null) {
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ org.gradle.caching=true
mavenGroupId = com.microsoft.kiota
mavenMajorVersion = 0
mavenMinorVersion = 4
mavenPatchVersion = 4
mavenPatchVersion = 5
mavenArtifactSuffix =

#These values are used to run functional tests
Expand Down