From 0cad75d3cf6b357316f4ed7b94f21202112e48a0 Mon Sep 17 00:00:00 2001 From: ZAOUZAOU Yacine Date: Sat, 2 Mar 2024 16:51:45 +0100 Subject: [PATCH] feat: support types declared as arrays when adding @Valid annotation when using jsr303 annotations --- .../jsonschema2pojo/rules/PropertyRule.java | 21 +++++++++++++++++-- .../resources/schema/jsr303/validArray.json | 2 +- .../resources/schema/jsr303/validObject.json | 2 +- 3 files changed, 21 insertions(+), 4 deletions(-) diff --git a/jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/rules/PropertyRule.java b/jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/rules/PropertyRule.java index 9bb0ecb72..5ebd6f4ee 100644 --- a/jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/rules/PropertyRule.java +++ b/jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/rules/PropertyRule.java @@ -32,6 +32,9 @@ import com.sun.codemodel.JVar; import org.apache.commons.lang.StringUtils; +import java.util.Iterator; +import java.util.stream.StreamSupport; + /** * Applies the schema rules that represent a property definition. * @@ -221,11 +224,25 @@ private JsonNode resolveRefs(JsonNode node, Schema parent) { } private boolean isObject(JsonNode node) { - return node.path("type").asText().equals("object"); + return checkType(node, "object"); } private boolean isArray(JsonNode node) { - return node.path("type").asText().equals("array"); + return checkType(node, "array"); + } + + private boolean checkType(JsonNode node, String type) { + return checkSimpleType(node, type) || checkArrayOfTypes(node, type); + } + + private static boolean checkArrayOfTypes(JsonNode node, String type) { + return StreamSupport.stream(node.path("type").spliterator(), false) + .map(JsonNode::asText) + .anyMatch(type::equals); + } + + private static boolean checkSimpleType(JsonNode node, String type) { + return node.path("type").asText().equals(type); } private JType getReturnType(final JDefinedClass c, final JFieldVar field, final boolean required, final boolean usesOptional) { diff --git a/jsonschema2pojo-integration-tests/src/test/resources/schema/jsr303/validArray.json b/jsonschema2pojo-integration-tests/src/test/resources/schema/jsr303/validArray.json index dc45ec4ad..bd089bcf3 100644 --- a/jsonschema2pojo-integration-tests/src/test/resources/schema/jsr303/validArray.json +++ b/jsonschema2pojo-integration-tests/src/test/resources/schema/jsr303/validArray.json @@ -20,7 +20,7 @@ } }, "refarray" : { - "type": "array", + "type": [ "array" ], "items": { "$ref": "#definitions/product" } diff --git a/jsonschema2pojo-integration-tests/src/test/resources/schema/jsr303/validObject.json b/jsonschema2pojo-integration-tests/src/test/resources/schema/jsr303/validObject.json index 5134066d9..d962a31a4 100644 --- a/jsonschema2pojo-integration-tests/src/test/resources/schema/jsr303/validObject.json +++ b/jsonschema2pojo-integration-tests/src/test/resources/schema/jsr303/validObject.json @@ -5,7 +5,7 @@ "type": "string" }, "objectfield" : { - "type": "object", + "type": [ "object" ] , "properties": { "childprimitivefield" : { "type": "string",