Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1841,7 +1841,7 @@ protected boolean applyBeanValidatorAnnotations(Schema property, Annotation[] an
}
}

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

if (annos.containsKey("javax.validation.constraints.NotBlank")) {
if (annos.containsKey("javax.validation.constraints.NotBlank") && applyNotNullAnnotations ) {
NotBlank anno = (NotBlank) annos.get("javax.validation.constraints.NotBlank");
boolean apply = checkGroupValidation(anno.groups(), invocationGroups, acceptNoGroups);
if (apply) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,14 @@
import io.swagger.v3.oas.models.media.StringSchema;
import org.testng.annotations.Test;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Set;

import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertFalse;
Expand Down Expand Up @@ -114,6 +119,8 @@ public void testRequiredProperty() {
assertTrue(model.getRequired().contains("modeRequired"));
assertFalse(model.getRequired().contains("modeNotRequired"));
assertFalse(model.getRequired().contains("modeNotRequiredWithAnnotation"));
assertFalse(model.getRequired().contains("modeNotRequiredWithAnnotationForNotBlank"));
assertFalse(model.getRequired().contains("modeNotRequiredWithAnnotationForNotEmpty"));
}

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

@Test
public void testNotNullWithNotBlankAndNotEmpty() {
final Map<String, Schema> models = ModelConverters.getInstance().readAll(NotNullWithNotBlankNotEmptyModel.class);
Schema model = models.get("NotNullWithNotBlankNotEmptyModel");
assertTrue(model.getRequired().contains("notNullAndNotBlank"));
assertTrue(model.getRequired().contains("notNullAndNotEmptyList"));
assertTrue(model.getRequired().contains("notNullAndNotEmptySet"));
}

@Test
public void testCollectionWithNotEmpty() {
final Map<String, Schema> models = ModelConverters.getInstance().readAll(CollectionWithNotEmptyModel.class);
Schema model = models.get("CollectionWithNotEmptyModel");
ArraySchema listSchema = (ArraySchema) model.getProperties().get("notEmptyList");
assertNotNull(listSchema);
assertEquals(listSchema.getMinItems(), Integer.valueOf(1));
ArraySchema setSchema = (ArraySchema) model.getProperties().get("notEmptySet");
assertNotNull(setSchema);
assertEquals(setSchema.getMinItems(), Integer.valueOf(1));
}

@Test
public void testStringWithNotBlankAndSize() {
final Map<String, Schema> models = ModelConverters.getInstance().readAll(StringWithNotBlankAndSizeModel.class);
Schema model = models.get("StringWithNotBlankAndSizeModel");
Schema strSchema = (Schema) model.getProperties().get("notBlankAndSized");
assertNotNull(strSchema);
assertEquals(strSchema.getMinLength(), Integer.valueOf(5));
assertEquals(strSchema.getMaxLength(), Integer.valueOf(10));
}

@Test
public void testNotBlankNotEmptyWithRequiredModeNotRequired() {
final Map<String, Schema> models = ModelConverters.getInstance().readAll(NotBlankNotEmptyWithRequiredModeNotRequiredModel.class);
Schema model = models.get("NotBlankNotEmptyWithRequiredModeNotRequiredModel");
assertFalse(model.getRequired() != null && model.getRequired().contains("notBlankNotRequired"));
assertFalse(model.getRequired() != null && model.getRequired().contains("notEmptyListNotRequired"));
assertFalse(model.getRequired() != null && model.getRequired().contains("notEmptySetNotRequired"));
}

@Test
public void testNotBlankNotEmptyWithRequiredModeRequired() {
final Map<String, Schema> models = ModelConverters.getInstance().readAll(NotBlankNotEmptyWithRequiredModeRequiredModel.class);
Schema model = models.get("NotBlankNotEmptyWithRequiredModeRequiredModel");
assertTrue(model.getRequired().contains("notBlankRequired"));
assertTrue(model.getRequired().contains("notEmptyListRequired"));
assertTrue(model.getRequired().contains("notEmptySetRequired"));
}

class Family {
public Date membersSince;
public List<Person> members;
Expand All @@ -162,4 +218,60 @@ class IsModelTest {
public Boolean is_happy;
public String name;
}

static class NotNullWithNotBlankNotEmptyModel {
@NotNull
@NotBlank
public String notNullAndNotBlank;

@NotNull
@NotEmpty
public List<String> notNullAndNotEmptyList;

@NotNull
@NotEmpty
public Set<String> notNullAndNotEmptySet;
}

static class CollectionWithNotEmptyModel {
@NotEmpty
public List<String> notEmptyList;

@NotEmpty
public Set<String> notEmptySet;
}

static class StringWithNotBlankAndSizeModel {
@NotBlank
@Size(min = 5, max = 10)
public String notBlankAndSized;
}

static class NotBlankNotEmptyWithRequiredModeRequiredModel {
@NotBlank
@io.swagger.v3.oas.annotations.media.Schema(requiredMode = io.swagger.v3.oas.annotations.media.Schema.RequiredMode.REQUIRED)
public String notBlankRequired;

@NotEmpty
@io.swagger.v3.oas.annotations.media.Schema(requiredMode = io.swagger.v3.oas.annotations.media.Schema.RequiredMode.REQUIRED)
public List<String> notEmptyListRequired;

@NotEmpty
@io.swagger.v3.oas.annotations.media.Schema(requiredMode = io.swagger.v3.oas.annotations.media.Schema.RequiredMode.REQUIRED)
public Set<String> notEmptySetRequired;
}

static class NotBlankNotEmptyWithRequiredModeNotRequiredModel {
@NotBlank
@io.swagger.v3.oas.annotations.media.Schema(requiredMode = io.swagger.v3.oas.annotations.media.Schema.RequiredMode.NOT_REQUIRED)
public String notBlankNotRequired;

@NotEmpty
@io.swagger.v3.oas.annotations.media.Schema(requiredMode = io.swagger.v3.oas.annotations.media.Schema.RequiredMode.NOT_REQUIRED)
public List<String> notEmptyListNotRequired;

@NotEmpty
@io.swagger.v3.oas.annotations.media.Schema(requiredMode = io.swagger.v3.oas.annotations.media.Schema.RequiredMode.NOT_REQUIRED)
public Set<String> notEmptySetNotRequired;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
import io.swagger.v3.oas.annotations.media.Schema;

import javax.validation.constraints.NotNull;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotEmpty;

import java.util.List;
import java.util.Optional;

public class RequiredFields {
Expand Down Expand Up @@ -42,4 +46,12 @@ public class RequiredFields {
@Schema(description = "mode not required with annotation", requiredMode = Schema.RequiredMode.NOT_REQUIRED)
@NotNull
public Long modeNotRequiredWithAnnotation;

@Schema(description = "mode not required with annotation for NotBlank", requiredMode = Schema.RequiredMode.NOT_REQUIRED)
@NotBlank
public String modeNotRequiredWithAnnotationForNotBlank;

@Schema(description = "mode not required with annotation for NotEmpty", requiredMode = Schema.RequiredMode.NOT_REQUIRED)
@NotEmpty
public List<String> modeNotRequiredWithAnnotationForNotEmpty;
}
Loading