Skip to content

Commit de91f3d

Browse files
Merge pull request #1670 from sigpwned/bug/1669/validation-annotations-minlength-and-maxlength-not-being-generated-for-array-types
Fix missing JSR303 size annotations on array types
2 parents a31723a + dccc64f commit de91f3d

11 files changed

Lines changed: 88 additions & 637 deletions

File tree

jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/model/JAnnotatedClass.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import com.sun.codemodel.JFormatter;
3232
import com.sun.codemodel.JPackage;
3333
import com.sun.codemodel.JPrimitiveType;
34+
import com.sun.codemodel.JType;
3435
import com.sun.codemodel.JTypeVar;
3536

3637
/**
@@ -128,6 +129,11 @@ public JAnnotatedClass annotated(Class<? extends Annotation> clazz) {
128129
return annotated(owner().ref(clazz));
129130
}
130131

132+
@Override
133+
public JType elementType() {
134+
return basis.elementType();
135+
}
136+
131137
/**
132138
* Returns a new JAnnotatedClass with the given annotation added.
133139
*

jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/rules/MinItemsMaxItemsRule.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -63,21 +63,21 @@ && isApplicableType(field)) {
6363
}
6464

6565
private boolean isApplicableType(JFieldVar field) {
66+
// Per https://github.com/joelittlejohn/jsonschema2pojo/issues/1669
67+
// Need to check arrays first, because JType.fullName() returns names like byte[] or java.lang.String[]
68+
// but Class.forName needs names like "[B" or "[Ljava.lang.String;" and will throw ClassNotFoundException.
69+
if (field.type().isArray()) {
70+
return true;
71+
}
72+
6673
try {
67-
String typeName = field.type().boxify().fullName();
68-
// For collections, the full name will be something like 'java.util.List<String>' and we
69-
// need just 'java.util.List'.
70-
int genericsPos = typeName.indexOf('<');
71-
if (genericsPos > -1) {
72-
typeName = typeName.substring(0, genericsPos);
73-
}
74+
String typeName = field.type().boxify().erasure().fullName();
7475

7576
Class<?> fieldClass = Class.forName(typeName);
7677
return String.class.isAssignableFrom(fieldClass)
7778
|| Collection.class.isAssignableFrom(fieldClass)
7879
|| Map.class.isAssignableFrom(fieldClass)
79-
|| Array.class.isAssignableFrom(fieldClass)
80-
|| field.type().isArray();
80+
|| fieldClass.isArray();
8181
} catch (ClassNotFoundException ignore) {
8282
return false;
8383
}

jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/rules/MinLengthMaxLengthRule.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -63,21 +63,21 @@ && isApplicableType(field)) {
6363
}
6464

6565
private boolean isApplicableType(JFieldVar field) {
66+
// Per https://github.com/joelittlejohn/jsonschema2pojo/issues/1669
67+
// Need to check arrays first, because JType.fullName() returns names like byte[] or java.lang.String[]
68+
// but Class.forName needs names like "[B" or "[Ljava.lang.String;" and will throw ClassNotFoundException.
69+
if (field.type().isArray()) {
70+
return true;
71+
}
72+
6673
try {
67-
String typeName = field.type().boxify().fullName();
68-
// For collections, the full name will be something like 'java.util.List<String>' and we
69-
// need just 'java.util.List'.
70-
int genericsPos = typeName.indexOf('<');
71-
if (genericsPos > -1) {
72-
typeName = typeName.substring(0, genericsPos);
73-
}
74+
String typeName = field.type().boxify().erasure().fullName();
7475

7576
Class<?> fieldClass = Class.forName(typeName);
7677
return String.class.isAssignableFrom(fieldClass)
7778
|| Collection.class.isAssignableFrom(fieldClass)
7879
|| Map.class.isAssignableFrom(fieldClass)
79-
|| Array.class.isAssignableFrom(fieldClass)
80-
|| field.type().isArray();
80+
|| fieldClass.isArray();
8181
} catch (ClassNotFoundException ignore) {
8282
return false;
8383
}

jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/rules/MinimumMaximumRule.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public JFieldVar apply(String nodeName, JsonNode node, JsonNode parent, JFieldVa
6565

6666
private boolean isApplicableType(JFieldVar field) {
6767
try {
68-
Class<?> fieldClass = Class.forName(field.type().boxify().fullName());
68+
Class<?> fieldClass = Class.forName(field.type().boxify().erasure().fullName());
6969
// Support Strings and most number types except Double and Float, per docs on DecimalMax/Min annotations
7070
return String.class.isAssignableFrom(fieldClass) ||
7171
(Number.class.isAssignableFrom(fieldClass) &&

jsonschema2pojo-core/src/test/java/org/jsonschema2pojo/rules/MinItemsMaxItemsRuleTest.java

Lines changed: 0 additions & 207 deletions
This file was deleted.

0 commit comments

Comments
 (0)