Skip to content

Commit

Permalink
Use Object as the type in case schema property contains mixed types
Browse files Browse the repository at this point in the history
Closes #1425
  • Loading branch information
unkish committed Feb 7, 2023
1 parent 25bfc76 commit dad10ce
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;

import org.jsonschema2pojo.GenerationConfig;
import org.jsonschema2pojo.Schema;
Expand Down Expand Up @@ -119,19 +122,22 @@ public JType apply(String nodeName, JsonNode node, JsonNode parent, JClassContai
}

private String getTypeName(JsonNode node) {
if (node.has("type") && node.get("type").isArray() && node.get("type").size() > 0) {
for (JsonNode jsonNode : node.get("type")) {
String typeName = jsonNode.asText();
if (!typeName.equals("null")) {
return typeName;
if (node.has("type")) {
final JsonNode typeNode = node.get("type");
if (typeNode.isArray() && typeNode.size() > 0) {
final List<String> typeValues = StreamSupport.stream(typeNode.spliterator(), false)
.map(JsonNode::asText)
.filter(n -> !"null".equals(n))
.collect(Collectors.toList());

if (typeValues.size() == 1) {
return typeValues.get(0);
}
} else if (typeNode.isTextual()) {
return typeNode.asText();
}
}

if (node.has("type") && node.get("type").isTextual()) {
return node.get("type").asText();
}

return DEFAULT_TYPE_NAME;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -282,13 +282,13 @@ public void useDoubleNumbersFalseCausesPrimitiveNumbersToBecomeFloats() throws C
}

@Test
public void unionTypesChooseFirstTypePresent() throws ClassNotFoundException, SecurityException, NoSuchMethodException {
public void unionTypesChooseFirstTypePresent() throws ReflectiveOperationException {

Class<?> classWithUnionProperties = schemaRule.generateAndCompile("/schema/type/unionTypes.json", "com.example").loadClass("com.example.UnionTypes");

Method booleanGetter = classWithUnionProperties.getMethod("getBooleanProperty");
Method nullTypeGetter = classWithUnionProperties.getMethod("getNullProperty");

assertThat(booleanGetter.getReturnType().getName(), is("java.lang.Boolean"));
assertThat(nullTypeGetter.getReturnType().getName(), is("java.lang.Object"));

Method stringGetter = classWithUnionProperties.getMethod("getStringProperty");

Expand All @@ -297,6 +297,20 @@ public void unionTypesChooseFirstTypePresent() throws ClassNotFoundException, Se
Method integerGetter = classWithUnionProperties.getMethod("getIntegerProperty");

assertThat(integerGetter.getReturnType().getName(), is("java.lang.Integer"));

Method booleanGetter = classWithUnionProperties.getMethod("getBooleanProperty");
assertThat(booleanGetter.getReturnType().getName(), is("java.lang.Boolean"));
}

@Test
public void mixedUnionTypesReturnObject() throws ReflectiveOperationException {
Class<?> classWithMixedUnionProperties = schemaRule.generateAndCompile("/schema/type/mixedUnionTypes.json", "com.example").loadClass("com.example.MixedUnionTypes");

Method mixedTypesGetter = classWithMixedUnionProperties.getMethod("getMixedTypesProperty");
assertThat(mixedTypesGetter.getReturnType().getName(), is("java.lang.Object"));

Method mixedTypesWithNullGetter = classWithMixedUnionProperties.getMethod("getMixedTypesWithNullProperty");
assertThat(mixedTypesWithNullGetter.getReturnType().getName(), is("java.lang.Object"));
}

@SuppressWarnings("rawtypes")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -271,13 +271,13 @@ public void useDoubleNumbersFalseCausesPrimitiveNumbersToBecomeFloats() throws C
}

@Test
public void unionTypesChooseFirstTypePresent() throws ClassNotFoundException, SecurityException, NoSuchMethodException {
public void unionTypesChooseFirstTypePresent() throws ReflectiveOperationException {

Class<?> classWithUnionProperties = schemaRule.generateAndCompile("/schema/yaml/type/unionTypes.yaml", "com.example", config("sourceType", "yamlschema")).loadClass("com.example.UnionTypes");

Method booleanGetter = classWithUnionProperties.getMethod("getBooleanProperty");
Method nullTypeGetter = classWithUnionProperties.getMethod("getNullProperty");

assertThat(booleanGetter.getReturnType().getName(), is("java.lang.Boolean"));
assertThat(nullTypeGetter.getReturnType().getName(), is("java.lang.Object"));

Method stringGetter = classWithUnionProperties.getMethod("getStringProperty");

Expand All @@ -286,6 +286,20 @@ public void unionTypesChooseFirstTypePresent() throws ClassNotFoundException, Se
Method integerGetter = classWithUnionProperties.getMethod("getIntegerProperty");

assertThat(integerGetter.getReturnType().getName(), is("java.lang.Integer"));

Method booleanGetter = classWithUnionProperties.getMethod("getBooleanProperty");
assertThat(booleanGetter.getReturnType().getName(), is("java.lang.Boolean"));
}

@Test
public void mixedUnionTypesReturnObject() throws ReflectiveOperationException {
Class<?> classWithMixedUnionProperties = schemaRule.generateAndCompile("/schema/yaml/type/mixedUnionTypes.yaml", "com.example", config("sourceType", "yamlschema")).loadClass("com.example.MixedUnionTypes");

Method mixedTypesGetter = classWithMixedUnionProperties.getMethod("getMixedTypesProperty");
assertThat(mixedTypesGetter.getReturnType().getName(), is("java.lang.Object"));

Method mixedTypesWithNullGetter = classWithMixedUnionProperties.getMethod("getMixedTypesWithNullProperty");
assertThat(mixedTypesWithNullGetter.getReturnType().getName(), is("java.lang.Object"));
}

@SuppressWarnings("rawtypes")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"type" : "object",
"properties" : {
"mixedTypesProperty": {
"type": ["boolean", "string"]
},
"mixedTypesWithNullProperty": {
"type": ["null", "string", "boolean"]
}
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
{
"type" : "object",
"properties" : {
"booleanProperty" : {
"type" : ["boolean", "string"]
"nullProperty" : {
"type" : ["null"]
},
"stringProperty" : {
"type" : ["string"]
},
"integerProperty" : {
"type" : ["integer", "null"]
},
"booleanProperty": {
"type" : ["null", "boolean"]
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
properties:
mixedTypesProperty:
type:
- boolean
- string
mixedTypesWithNullProperty:
type:
- 'null'
- string
- boolean
type: object
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
properties:
booleanProperty:
nullProperty:
type:
- boolean
- string
- 'null'
integerProperty:
type:
- integer
- 'null'
stringProperty:
type:
- string
booleanProperty:
type:
- 'null'
- boolean
type: object

0 comments on commit dad10ce

Please sign in to comment.