Skip to content

Commit 23d2971

Browse files
authored
fix: NotBlank and NotEmpty annotations follow NotNull behavior (#4968)
1 parent 75af075 commit 23d2971

File tree

3 files changed

+126
-2
lines changed

3 files changed

+126
-2
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1841,7 +1841,7 @@ protected boolean applyBeanValidatorAnnotations(Schema property, Annotation[] an
18411841
}
18421842
}
18431843

1844-
if (annos.containsKey("javax.validation.constraints.NotEmpty")) {
1844+
if (annos.containsKey("javax.validation.constraints.NotEmpty") && applyNotNullAnnotations ) {
18451845
NotEmpty anno = (NotEmpty) annos.get("javax.validation.constraints.NotEmpty");
18461846
boolean apply = checkGroupValidation(anno.groups(), invocationGroups, acceptNoGroups);
18471847
if (apply) {
@@ -1867,7 +1867,7 @@ protected boolean applyBeanValidatorAnnotations(Schema property, Annotation[] an
18671867
}
18681868
}
18691869

1870-
if (annos.containsKey("javax.validation.constraints.NotBlank")) {
1870+
if (annos.containsKey("javax.validation.constraints.NotBlank") && applyNotNullAnnotations ) {
18711871
NotBlank anno = (NotBlank) annos.get("javax.validation.constraints.NotBlank");
18721872
boolean apply = checkGroupValidation(anno.groups(), invocationGroups, acceptNoGroups);
18731873
if (apply) {

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

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,14 @@
1515
import io.swagger.v3.oas.models.media.StringSchema;
1616
import org.testng.annotations.Test;
1717

18+
import javax.validation.constraints.NotBlank;
19+
import javax.validation.constraints.NotEmpty;
20+
import javax.validation.constraints.NotNull;
21+
import javax.validation.constraints.Size;
1822
import java.util.Date;
1923
import java.util.List;
2024
import java.util.Map;
25+
import java.util.Set;
2126

2227
import static org.testng.Assert.assertEquals;
2328
import static org.testng.Assert.assertFalse;
@@ -114,6 +119,8 @@ public void testRequiredProperty() {
114119
assertTrue(model.getRequired().contains("modeRequired"));
115120
assertFalse(model.getRequired().contains("modeNotRequired"));
116121
assertFalse(model.getRequired().contains("modeNotRequiredWithAnnotation"));
122+
assertFalse(model.getRequired().contains("modeNotRequiredWithAnnotationForNotBlank"));
123+
assertFalse(model.getRequired().contains("modeNotRequiredWithAnnotationForNotEmpty"));
117124
}
118125

119126
@Test
@@ -139,6 +146,55 @@ public void testIssue1743() {
139146
assertEquals(is.getEnum().get(1), new Integer(2));
140147
}
141148

149+
@Test
150+
public void testNotNullWithNotBlankAndNotEmpty() {
151+
final Map<String, Schema> models = ModelConverters.getInstance().readAll(NotNullWithNotBlankNotEmptyModel.class);
152+
Schema model = models.get("NotNullWithNotBlankNotEmptyModel");
153+
assertTrue(model.getRequired().contains("notNullAndNotBlank"));
154+
assertTrue(model.getRequired().contains("notNullAndNotEmptyList"));
155+
assertTrue(model.getRequired().contains("notNullAndNotEmptySet"));
156+
}
157+
158+
@Test
159+
public void testCollectionWithNotEmpty() {
160+
final Map<String, Schema> models = ModelConverters.getInstance().readAll(CollectionWithNotEmptyModel.class);
161+
Schema model = models.get("CollectionWithNotEmptyModel");
162+
ArraySchema listSchema = (ArraySchema) model.getProperties().get("notEmptyList");
163+
assertNotNull(listSchema);
164+
assertEquals(listSchema.getMinItems(), Integer.valueOf(1));
165+
ArraySchema setSchema = (ArraySchema) model.getProperties().get("notEmptySet");
166+
assertNotNull(setSchema);
167+
assertEquals(setSchema.getMinItems(), Integer.valueOf(1));
168+
}
169+
170+
@Test
171+
public void testStringWithNotBlankAndSize() {
172+
final Map<String, Schema> models = ModelConverters.getInstance().readAll(StringWithNotBlankAndSizeModel.class);
173+
Schema model = models.get("StringWithNotBlankAndSizeModel");
174+
Schema strSchema = (Schema) model.getProperties().get("notBlankAndSized");
175+
assertNotNull(strSchema);
176+
assertEquals(strSchema.getMinLength(), Integer.valueOf(5));
177+
assertEquals(strSchema.getMaxLength(), Integer.valueOf(10));
178+
}
179+
180+
@Test
181+
public void testNotBlankNotEmptyWithRequiredModeNotRequired() {
182+
final Map<String, Schema> models = ModelConverters.getInstance().readAll(NotBlankNotEmptyWithRequiredModeNotRequiredModel.class);
183+
Schema model = models.get("NotBlankNotEmptyWithRequiredModeNotRequiredModel");
184+
assertFalse(model.getRequired() != null && model.getRequired().contains("notBlankNotRequired"));
185+
assertFalse(model.getRequired() != null && model.getRequired().contains("notEmptyListNotRequired"));
186+
assertFalse(model.getRequired() != null && model.getRequired().contains("notEmptySetNotRequired"));
187+
}
188+
189+
@Test
190+
public void testNotBlankNotEmptyWithRequiredModeRequired() {
191+
final Map<String, Schema> models = ModelConverters.getInstance().readAll(NotBlankNotEmptyWithRequiredModeRequiredModel.class);
192+
Schema model = models.get("NotBlankNotEmptyWithRequiredModeRequiredModel");
193+
assertTrue(model.getRequired().contains("notBlankRequired"));
194+
assertTrue(model.getRequired().contains("notEmptyListRequired"));
195+
assertTrue(model.getRequired().contains("notEmptySetRequired"));
196+
}
197+
142198
class Family {
143199
public Date membersSince;
144200
public List<Person> members;
@@ -162,4 +218,60 @@ class IsModelTest {
162218
public Boolean is_happy;
163219
public String name;
164220
}
221+
222+
static class NotNullWithNotBlankNotEmptyModel {
223+
@NotNull
224+
@NotBlank
225+
public String notNullAndNotBlank;
226+
227+
@NotNull
228+
@NotEmpty
229+
public List<String> notNullAndNotEmptyList;
230+
231+
@NotNull
232+
@NotEmpty
233+
public Set<String> notNullAndNotEmptySet;
234+
}
235+
236+
static class CollectionWithNotEmptyModel {
237+
@NotEmpty
238+
public List<String> notEmptyList;
239+
240+
@NotEmpty
241+
public Set<String> notEmptySet;
242+
}
243+
244+
static class StringWithNotBlankAndSizeModel {
245+
@NotBlank
246+
@Size(min = 5, max = 10)
247+
public String notBlankAndSized;
248+
}
249+
250+
static class NotBlankNotEmptyWithRequiredModeRequiredModel {
251+
@NotBlank
252+
@io.swagger.v3.oas.annotations.media.Schema(requiredMode = io.swagger.v3.oas.annotations.media.Schema.RequiredMode.REQUIRED)
253+
public String notBlankRequired;
254+
255+
@NotEmpty
256+
@io.swagger.v3.oas.annotations.media.Schema(requiredMode = io.swagger.v3.oas.annotations.media.Schema.RequiredMode.REQUIRED)
257+
public List<String> notEmptyListRequired;
258+
259+
@NotEmpty
260+
@io.swagger.v3.oas.annotations.media.Schema(requiredMode = io.swagger.v3.oas.annotations.media.Schema.RequiredMode.REQUIRED)
261+
public Set<String> notEmptySetRequired;
262+
}
263+
264+
static class NotBlankNotEmptyWithRequiredModeNotRequiredModel {
265+
@NotBlank
266+
@io.swagger.v3.oas.annotations.media.Schema(requiredMode = io.swagger.v3.oas.annotations.media.Schema.RequiredMode.NOT_REQUIRED)
267+
public String notBlankNotRequired;
268+
269+
@NotEmpty
270+
@io.swagger.v3.oas.annotations.media.Schema(requiredMode = io.swagger.v3.oas.annotations.media.Schema.RequiredMode.NOT_REQUIRED)
271+
public List<String> notEmptyListNotRequired;
272+
273+
@NotEmpty
274+
@io.swagger.v3.oas.annotations.media.Schema(requiredMode = io.swagger.v3.oas.annotations.media.Schema.RequiredMode.NOT_REQUIRED)
275+
public Set<String> notEmptySetNotRequired;
276+
}
165277
}

modules/swagger-core/src/test/java/io/swagger/v3/core/oas/models/RequiredFields.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
import io.swagger.v3.oas.annotations.media.Schema;
44

55
import javax.validation.constraints.NotNull;
6+
import javax.validation.constraints.NotBlank;
7+
import javax.validation.constraints.NotEmpty;
8+
9+
import java.util.List;
610
import java.util.Optional;
711

812
public class RequiredFields {
@@ -42,4 +46,12 @@ public class RequiredFields {
4246
@Schema(description = "mode not required with annotation", requiredMode = Schema.RequiredMode.NOT_REQUIRED)
4347
@NotNull
4448
public Long modeNotRequiredWithAnnotation;
49+
50+
@Schema(description = "mode not required with annotation for NotBlank", requiredMode = Schema.RequiredMode.NOT_REQUIRED)
51+
@NotBlank
52+
public String modeNotRequiredWithAnnotationForNotBlank;
53+
54+
@Schema(description = "mode not required with annotation for NotEmpty", requiredMode = Schema.RequiredMode.NOT_REQUIRED)
55+
@NotEmpty
56+
public List<String> modeNotRequiredWithAnnotationForNotEmpty;
4557
}

0 commit comments

Comments
 (0)