Skip to content

Commit

Permalink
Add @Valid on all collections, not just those with items type "object"
Browse files Browse the repository at this point in the history
Closes #158.
  • Loading branch information
joelittlejohn committed Mar 5, 2014
1 parent b92b336 commit 8a5e566
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ public JDefinedClass apply(String nodeName, JsonNode node, JDefinedClass jclass,

ruleFactory.getMinLengthMaxLengthRule().apply(nodeName, node, field, schema);

if (isObject(node) || isArrayOfObjects(node, schema)) {
if (isObject(node) || isArray(node)) {
ruleFactory.getValidRule().apply(nodeName, node, field, schema);
}

Expand All @@ -134,12 +134,10 @@ private JsonNode resolveRefs(JsonNode node, Schema parent) {
}

private boolean isObject(JsonNode node) {
return node.has("type") && node.get("type").asText().equals("object");
return node.path("type").asText().equals("object");
}

private boolean isArrayOfObjects(JsonNode node, Schema parent) {
node = resolveRefs(node.path("items"), parent);
return node.path("type").asText().equals("object") || node.path("type").asText().equals("array");
private boolean isArray(JsonNode node) {
return node.path("type").asText().equals("array");
}

private JMethod addGetter(JDefinedClass c, JFieldVar field, String jsonPropertyName) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@

package org.jsonschema2pojo.integration.config;

import static org.jsonschema2pojo.integration.util.CodeGenerationHelper.*;
import static java.util.Arrays.*;
import static org.hamcrest.Matchers.*;
import static org.jsonschema2pojo.integration.util.CodeGenerationHelper.*;
import static org.junit.Assert.*;

import java.beans.PropertyDescriptor;
Expand All @@ -32,9 +32,8 @@
import javax.validation.Validator;

import org.hamcrest.Matcher;
import org.junit.Test;

import org.jsonschema2pojo.integration.util.FileSearchMatcher;
import org.junit.Test;

@SuppressWarnings("rawtypes")
public class IncludeJsr303AnnotationsIT {
Expand Down Expand Up @@ -218,7 +217,7 @@ public void jsr303SizeValidationIsAddedForSchemaRuleMaxLength() throws ClassNotF
}

@Test
public void jsr303ValidAnnotationIsAddedForSchemaRuleValidOnObject() throws ClassNotFoundException {
public void jsr303ValidAnnotationIsAddedForObject() throws ClassNotFoundException {
ClassLoader resultsClassLoader = generateAndCompile("/schema/jsr303/validObject.json", "com.example",
config("includeJsr303Annotations", true));

Expand All @@ -237,7 +236,7 @@ public void jsr303ValidAnnotationIsAddedForSchemaRuleValidOnObject() throws Clas
}

@Test
public void jsr303ValidAnnotationIsAddedForSchemaRuleValidOnArrayOfObjects() throws ClassNotFoundException, NoSuchFieldException {
public void jsr303ValidAnnotationIsAddedForArray() throws ClassNotFoundException, NoSuchFieldException {
ClassLoader resultsClassLoader = generateAndCompile("/schema/jsr303/validArray.json", "com.example",
config("includeJsr303Annotations", true));

Expand Down

0 comments on commit 8a5e566

Please sign in to comment.