Skip to content

Commit 1dafa44

Browse files
burneyyDaniel Berninghoff
andauthored
fix: apply minItems/minLength from @NotEmpty/@notblank regardless of requiredMode (#5033)
Co-authored-by: Daniel Berninghoff <[email protected]>
1 parent b313104 commit 1dafa44

File tree

2 files changed

+37
-5
lines changed

2 files changed

+37
-5
lines changed

modules/swagger-core/src/main/java/io/swagger/v3/core/jackson/ModelResolver.java

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,7 @@ public Schema resolve(AnnotatedType annotatedType, ModelConverterContext context
349349
model = openapi31 ? primitiveType.createProperty31() : primitiveType.createProperty();
350350
isPrimitive = true;
351351
}
352-
}
352+
}
353353

354354
if (model == null) {
355355
PrimitiveType primitiveType = PrimitiveType.fromType(type);
@@ -1841,7 +1841,7 @@ protected boolean applyBeanValidatorAnnotations(Schema property, Annotation[] an
18411841
}
18421842
}
18431843

1844-
if (annos.containsKey("javax.validation.constraints.NotEmpty") && applyNotNullAnnotations ) {
1844+
if (annos.containsKey("javax.validation.constraints.NotEmpty")) {
18451845
NotEmpty anno = (NotEmpty) annos.get("javax.validation.constraints.NotEmpty");
18461846
boolean apply = checkGroupValidation(anno.groups(), invocationGroups, acceptNoGroups);
18471847
if (apply) {
@@ -1863,11 +1863,13 @@ protected boolean applyBeanValidatorAnnotations(Schema property, Annotation[] an
18631863
modified = true;
18641864
}
18651865
}
1866-
modified = updateRequiredItem(parent, property.getName()) || modified;
1866+
if (applyNotNullAnnotations) {
1867+
modified = updateRequiredItem(parent, property.getName()) || modified;
1868+
}
18671869
}
18681870
}
18691871

1870-
if (annos.containsKey("javax.validation.constraints.NotBlank") && applyNotNullAnnotations ) {
1872+
if (annos.containsKey("javax.validation.constraints.NotBlank")) {
18711873
NotBlank anno = (NotBlank) annos.get("javax.validation.constraints.NotBlank");
18721874
boolean apply = checkGroupValidation(anno.groups(), invocationGroups, acceptNoGroups);
18731875
if (apply) {
@@ -1878,7 +1880,9 @@ protected boolean applyBeanValidatorAnnotations(Schema property, Annotation[] an
18781880
modified = true;
18791881
}
18801882
}
1881-
modified = updateRequiredItem(parent, property.getName()) || modified;
1883+
if (applyNotNullAnnotations) {
1884+
modified = updateRequiredItem(parent, property.getName()) || modified;
1885+
}
18821886
}
18831887
}
18841888
if (annos.containsKey("javax.validation.constraints.Min")) {

modules/swagger-core/src/test/java/io/swagger/v3/core/converting/ModelPropertyTest.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,20 @@ public void testNotBlankNotEmptyWithRequiredModeNotRequired() {
184184
assertFalse(model.getRequired() != null && model.getRequired().contains("notBlankNotRequired"));
185185
assertFalse(model.getRequired() != null && model.getRequired().contains("notEmptyListNotRequired"));
186186
assertFalse(model.getRequired() != null && model.getRequired().contains("notEmptySetNotRequired"));
187+
188+
// @NotBlank should generate minLength: 1
189+
Schema notBlankSchema = (Schema) model.getProperties().get("notBlankNotRequired");
190+
assertNotNull(notBlankSchema);
191+
assertEquals(notBlankSchema.getMinLength(), Integer.valueOf(1));
192+
193+
// @NotEmpty should generate minItems: 1
194+
ArraySchema listSchema = (ArraySchema) model.getProperties().get("notEmptyListNotRequired");
195+
assertNotNull(listSchema);
196+
assertEquals(listSchema.getMinItems(), Integer.valueOf(1));
197+
198+
ArraySchema setSchema = (ArraySchema) model.getProperties().get("notEmptySetNotRequired");
199+
assertNotNull(setSchema);
200+
assertEquals(setSchema.getMinItems(), Integer.valueOf(1));
187201
}
188202

189203
@Test
@@ -193,6 +207,20 @@ public void testNotBlankNotEmptyWithRequiredModeRequired() {
193207
assertTrue(model.getRequired().contains("notBlankRequired"));
194208
assertTrue(model.getRequired().contains("notEmptyListRequired"));
195209
assertTrue(model.getRequired().contains("notEmptySetRequired"));
210+
211+
// @NotBlank should generate minLength: 1
212+
Schema notBlankSchema = (Schema) model.getProperties().get("notBlankRequired");
213+
assertNotNull(notBlankSchema);
214+
assertEquals(notBlankSchema.getMinLength(), Integer.valueOf(1));
215+
216+
// @NotEmpty should generate minItems: 1
217+
ArraySchema listSchema = (ArraySchema) model.getProperties().get("notEmptyListRequired");
218+
assertNotNull(listSchema);
219+
assertEquals(listSchema.getMinItems(), Integer.valueOf(1));
220+
221+
ArraySchema setSchema = (ArraySchema) model.getProperties().get("notEmptySetRequired");
222+
assertNotNull(setSchema);
223+
assertEquals(setSchema.getMinItems(), Integer.valueOf(1));
196224
}
197225

198226
class Family {

0 commit comments

Comments
 (0)