Skip to content

Commit 17dcc48

Browse files
committed
Revert changes to mock, reintroduce checks for primitive values
1 parent 16964b2 commit 17dcc48

File tree

3 files changed

+26
-34
lines changed

3 files changed

+26
-34
lines changed

components/serialization/json/src/main/java/com/microsoft/kiota/serialization/JsonParseNode.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -72,39 +72,39 @@ public JsonParseNode(@Nonnull final JsonElement node, @Nonnull final Gson gson)
7272
}
7373

7474
@Nullable public String getStringValue() {
75-
return gson.fromJson(currentNode, String.class);
75+
return currentNode.isJsonPrimitive() ? gson.fromJson(currentNode, String.class) : null;
7676
}
7777

7878
@Nullable public Boolean getBooleanValue() {
79-
return gson.fromJson(currentNode, Boolean.class);
79+
return currentNode.isJsonPrimitive() ? gson.fromJson(currentNode, Boolean.class) : null;
8080
}
8181

8282
@Nullable public Byte getByteValue() {
83-
return gson.fromJson(currentNode, Byte.class);
83+
return currentNode.isJsonPrimitive() ? gson.fromJson(currentNode, Byte.class) : null;
8484
}
8585

8686
@Nullable public Short getShortValue() {
87-
return gson.fromJson(currentNode, Short.class);
87+
return currentNode.isJsonPrimitive() ? gson.fromJson(currentNode, Short.class) : null;
8888
}
8989

9090
@Nullable public BigDecimal getBigDecimalValue() {
91-
return gson.fromJson(currentNode, BigDecimal.class);
91+
return currentNode.isJsonPrimitive() ? gson.fromJson(currentNode, BigDecimal.class) : null;
9292
}
9393

9494
@Nullable public Integer getIntegerValue() {
95-
return gson.fromJson(currentNode, Integer.class);
95+
return currentNode.isJsonPrimitive() ? gson.fromJson(currentNode, Integer.class) : null;
9696
}
9797

9898
@Nullable public Float getFloatValue() {
99-
return gson.fromJson(currentNode, Float.class);
99+
return currentNode.isJsonPrimitive() ? gson.fromJson(currentNode, Float.class) : null;
100100
}
101101

102102
@Nullable public Double getDoubleValue() {
103-
return gson.fromJson(currentNode, Double.class);
103+
return currentNode.isJsonPrimitive() ? gson.fromJson(currentNode, Double.class) : null;
104104
}
105105

106106
@Nullable public Long getLongValue() {
107-
return gson.fromJson(currentNode, Long.class);
107+
return currentNode.isJsonPrimitive() ? gson.fromJson(currentNode, Long.class) : null;
108108
}
109109

110110
@Nullable public UUID getUUIDValue() {
@@ -311,6 +311,6 @@ public void setOnAfterAssignFieldValues(@Nullable final Consumer<Parsable> value
311311
}
312312

313313
@Nullable public byte[] getByteArrayValue() {
314-
return gson.fromJson(currentNode, byte[].class);
314+
return currentNode.isJsonPrimitive() ? gson.fromJson(currentNode, byte[].class) : null;
315315
}
316316
}

components/serialization/json/src/test/java/com/microsoft/kiota/serialization/mocks/IntersectionTypeMock.java

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.microsoft.kiota.serialization.mocks;
22

3-
import com.google.gson.JsonSyntaxException;
43
import com.microsoft.kiota.serialization.ComposedTypeWrapper;
54
import com.microsoft.kiota.serialization.Parsable;
65
import com.microsoft.kiota.serialization.ParseNode;
@@ -22,18 +21,16 @@ public class IntersectionTypeMock implements Parsable, ComposedTypeWrapper {
2221
@jakarta.annotation.Nonnull final ParseNode parseNode) {
2322
Objects.requireNonNull(parseNode);
2423
final var result = new IntersectionTypeMock();
25-
try {
24+
if (parseNode.getStringValue() != null) {
2625
result.setStringValue(parseNode.getStringValue());
27-
} catch (JsonSyntaxException e) {
28-
if (parseNode.getCollectionOfObjectValues(TestEntity::createFromDiscriminatorValue)
29-
!= null) {
30-
result.setComposedType3(
31-
parseNode.getCollectionOfObjectValues(
32-
TestEntity::createFromDiscriminatorValue));
33-
} else {
34-
result.setComposedType1(new TestEntity());
35-
result.setComposedType2(new SecondTestEntity());
36-
}
26+
} else if (parseNode.getCollectionOfObjectValues(TestEntity::createFromDiscriminatorValue)
27+
!= null) {
28+
result.setComposedType3(
29+
parseNode.getCollectionOfObjectValues(
30+
TestEntity::createFromDiscriminatorValue));
31+
} else {
32+
result.setComposedType1(new TestEntity());
33+
result.setComposedType2(new SecondTestEntity());
3734
}
3835
return result;
3936
}

components/serialization/json/src/test/java/com/microsoft/kiota/serialization/mocks/UnionTypeMock.java

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.microsoft.kiota.serialization.mocks;
22

3-
import com.google.gson.JsonSyntaxException;
43
import com.microsoft.kiota.serialization.ComposedTypeWrapper;
54
import com.microsoft.kiota.serialization.Parsable;
65
import com.microsoft.kiota.serialization.ParseNode;
@@ -29,17 +28,13 @@ public class UnionTypeMock implements Parsable, ComposedTypeWrapper {
2928
} else if ("#microsoft.graph.secondTestEntity".equalsIgnoreCase(mappingValue)) {
3029
result.setComposedType2(new SecondTestEntity());
3130
}
32-
} else {
33-
try {
34-
result.setStringValue(parseNode.getStringValue());
35-
} catch (JsonSyntaxException e) {
36-
if (parseNode.getCollectionOfObjectValues(TestEntity::createFromDiscriminatorValue)
37-
!= null) {
38-
result.setComposedType3(
39-
parseNode.getCollectionOfObjectValues(
40-
TestEntity::createFromDiscriminatorValue));
41-
}
42-
}
31+
} else if (parseNode.getStringValue() != null) {
32+
result.setStringValue(parseNode.getStringValue());
33+
} else if (parseNode.getCollectionOfObjectValues(TestEntity::createFromDiscriminatorValue)
34+
!= null) {
35+
result.setComposedType3(
36+
parseNode.getCollectionOfObjectValues(
37+
TestEntity::createFromDiscriminatorValue));
4338
}
4439
return result;
4540
}

0 commit comments

Comments
 (0)