Skip to content

Commit

Permalink
Merge pull request #938 from microsoft/bugfix/get-child-node
Browse files Browse the repository at this point in the history
- fixes get child node on missing property
  • Loading branch information
baywet authored Dec 14, 2023
2 parents 34752a4 + 50ae690 commit 0d23968
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 9 deletions.
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.11.1] - 2023-12-14

### Changed

- Fixed a bug where trying to get a child node for a non exsiting property in JSON would fail instead of returning null.

## [0.11.0] - 2023-12-06

### Changed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,12 @@ public JsonParseNode(@Nonnull final JsonElement node) {
Objects.requireNonNull(identifier, "identifier parameter is required");
if (currentNode.isJsonObject()) {
final JsonObject object = currentNode.getAsJsonObject();
final Consumer<Parsable> onBefore = this.onBeforeAssignFieldValues;
final Consumer<Parsable> onAfter = this.onAfterAssignFieldValues;
return new JsonParseNode(object.get(identifier)) {
{
this.setOnBeforeAssignFieldValues(onBefore);
this.setOnAfterAssignFieldValues(onAfter);
}
};
final JsonElement childNodeElement = object.get(identifier);
if (childNodeElement == null) return null;
final JsonParseNode result = new JsonParseNode(childNodeElement);
result.setOnBeforeAssignFieldValues(this.onBeforeAssignFieldValues);
result.setOnAfterAssignFieldValues(this.onAfterAssignFieldValues);
return result;
} else return null;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.microsoft.kiota.serialization;

import static org.junit.jupiter.api.Assertions.assertNull;

import org.junit.jupiter.api.Test;

import java.io.ByteArrayInputStream;
import java.io.UnsupportedEncodingException;

class JsonParseNodeTests {
private static final JsonParseNodeFactory _parseNodeFactory = new JsonParseNodeFactory();
private static final JsonSerializationWriterFactory _serializationWriterFactory =
new JsonSerializationWriterFactory();
private static final String contentType = "application/json";

@Test
void ItDDoesNotFailForGetChildElementOnMissingKey() throws UnsupportedEncodingException {
final var initialString = "{displayName\": \"Microsoft Teams Meeting\"}";
final var rawResponse = new ByteArrayInputStream(initialString.getBytes("UTF-8"));
final var parseNode = _parseNodeFactory.getParseNode(contentType, rawResponse);
final var result = parseNode.getChildNode("@odata.type");
assertNull(result);
}
}
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 = 11
mavenPatchVersion = 0
mavenPatchVersion = 1
mavenArtifactSuffix =

#These values are used to run functional tests
Expand Down

0 comments on commit 0d23968

Please sign in to comment.