Skip to content

Commit

Permalink
feat: support types declared as arrays when adding @Valid annotation …
Browse files Browse the repository at this point in the history
…when using jsr303 annotations
  • Loading branch information
ZAOUZAOU Yacine committed Mar 2, 2024
1 parent 72050f6 commit 0cad75d
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
}
},
"refarray" : {
"type": "array",
"type": [ "array" ],
"items": {
"$ref": "#definitions/product"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"type": "string"
},
"objectfield" : {
"type": "object",
"type": [ "object" ] ,
"properties": {
"childprimitivefield" : {
"type": "string",
Expand Down

0 comments on commit 0cad75d

Please sign in to comment.