forked from swagger-api/swagger-core
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
swagger-api#4804: Only include validation constraints with no groups …
…by default The current process for adding constraints into the schema presumes that all javax or jakarta validation annotations enforce a constraint, and doesn't take into account the 'groups' property of the annotation. This results in constrains being added to the schema that would only be applied for certain request, so incorrectly marks fields as required in the resulting schema where they may only be mandatory on a subset of requests. To overcome this, a new `ValidationAnnotationFilter` is being introduced which only treats a validation annotation as constraining the schema where the conditions on the filter are met be the annotation. The default implementation of this filter only treats constraints from annotations with no groups as being enforced to bring it inline with how the default Java Beans validation operates.
- Loading branch information
Showing
10 changed files
with
268 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
...les/swagger-core/src/main/java/io/swagger/v3/core/jackson/ValidationAnnotationFilter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package io.swagger.v3.core.jackson; | ||
|
||
import javax.validation.constraints.DecimalMax; | ||
import javax.validation.constraints.DecimalMin; | ||
import javax.validation.constraints.Max; | ||
import javax.validation.constraints.Min; | ||
import javax.validation.constraints.NotBlank; | ||
import javax.validation.constraints.NotEmpty; | ||
import javax.validation.constraints.NotNull; | ||
import javax.validation.constraints.Pattern; | ||
import javax.validation.constraints.Size; | ||
|
||
public interface ValidationAnnotationFilter { | ||
|
||
default boolean isNotEmptyAnnotationApplicable(NotEmpty notEmpty) { | ||
return annotationGroupMatches(notEmpty.groups()); | ||
} | ||
|
||
default boolean isNotBlankAnnotationApplicable(NotBlank notBlank) { | ||
return annotationGroupMatches(notBlank.groups()); | ||
} | ||
|
||
default boolean isNotNullAnnotationApplicable(NotNull notNull) { | ||
return annotationGroupMatches(notNull.groups()); | ||
} | ||
|
||
default boolean isMaxAnnotationApplicable(Max max) { | ||
return annotationGroupMatches(max.groups()); | ||
} | ||
|
||
default boolean isMinAnnotationApplicable(Min min) { | ||
return annotationGroupMatches(min.groups()); | ||
} | ||
|
||
default boolean isDecimalMaxAnnotationApplicable(DecimalMax decimalMax) { | ||
return annotationGroupMatches(decimalMax.groups()); | ||
} | ||
|
||
default boolean isDecimalMinAnnotationApplicable(DecimalMin decimalMin) { | ||
return annotationGroupMatches(decimalMin.groups()); | ||
} | ||
|
||
default boolean isSizeAnnotationApplicable(Size size) { | ||
return annotationGroupMatches(size.groups()); | ||
} | ||
|
||
default boolean isPatternAnnotationApplicable(Pattern pattern) { | ||
return annotationGroupMatches(pattern.groups()); | ||
} | ||
|
||
boolean annotationGroupMatches(Class<?>[] annotationGroups); | ||
|
||
class DefaultValidationAnnotationFilter implements ValidationAnnotationFilter { | ||
|
||
@Override | ||
public boolean annotationGroupMatches(Class<?>[] annotationGroups) { | ||
return annotationGroups.length == 0; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
modules/swagger-core/src/test/java/io/swagger/v3/core/resolving/Ticket4804Test.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package io.swagger.v3.core.resolving; | ||
|
||
import java.math.BigDecimal; | ||
import java.util.Arrays; | ||
|
||
import javax.validation.constraints.DecimalMin; | ||
import javax.validation.constraints.Min; | ||
import javax.validation.constraints.NotEmpty; | ||
import javax.validation.constraints.NotNull; | ||
import javax.validation.constraints.Pattern; | ||
|
||
import io.swagger.v3.core.converter.AnnotatedType; | ||
import io.swagger.v3.core.converter.ModelConverters; | ||
import io.swagger.v3.core.converter.ResolvedSchema; | ||
import io.swagger.v3.core.matchers.SerializationMatchers; | ||
import io.swagger.v3.oas.models.media.Schema; | ||
import io.swagger.v3.oas.models.media.Schema.SchemaResolution; | ||
import org.testng.annotations.BeforeMethod; | ||
import org.testng.annotations.Test; | ||
|
||
public class Ticket4804Test { | ||
|
||
@BeforeMethod | ||
public void beforeTest() { | ||
ModelConverters.reset(); | ||
System.clearProperty(Schema.APPLY_SCHEMA_RESOLUTION_PROPERTY); | ||
} | ||
|
||
@Test | ||
public void shouldIncludeOnlyNonGroupedJakartaValidatedFieldsAsMandatoryByDefault() { | ||
ResolvedSchema schema = ModelConverters.getInstance(false).resolveAsResolvedSchema(new AnnotatedType().type(CustomClass.class)); | ||
String expectedJson = "{\"schema\":{\"required\":[\"nonGroupValidatedField\"],\"type\":\"object\",\"properties\":{\"nonGroupValidatedField\":{\"type\":\"string\"},\"singleGroupValidatedField\":{\"type\":\"integer\",\"format\":\"int32\"},\"multipleGroupValidatedField\":{\"type\":\"number\"},\"otherGroupValidatedField\":{\"type\":\"string\"},\"singleGroupValidatedField2\":{\"type\":\"string\"}}},\"referencedSchemas\":{\"CustomClass\":{\"required\":[\"nonGroupValidatedField\"],\"type\":\"object\",\"properties\":{\"nonGroupValidatedField\":{\"type\":\"string\"},\"singleGroupValidatedField\":{\"type\":\"integer\",\"format\":\"int32\"},\"multipleGroupValidatedField\":{\"type\":\"number\"},\"otherGroupValidatedField\":{\"type\":\"string\"},\"singleGroupValidatedField2\":{\"type\":\"string\"}}}}}"; | ||
SerializationMatchers.assertEqualsToJson(schema, expectedJson); | ||
} | ||
|
||
@Test | ||
public void shouldIncludeAllJakartaValidatedFieldsAsMandatoryIfFilterAlwaysTrue() { | ||
ResolvedSchema schema = ModelConverters.getInstance(false, SchemaResolution.DEFAULT, annotationGroups -> true).resolveAsResolvedSchema(new AnnotatedType().type(CustomClass.class)); | ||
String expectedJson = "{\"schema\":{\"required\":[\"nonGroupValidatedField\",\"singleGroupValidatedField2\"],\"type\":\"object\",\"properties\":{\"nonGroupValidatedField\":{\"type\":\"string\"},\"singleGroupValidatedField\":{\"minimum\":1,\"type\":\"integer\",\"format\":\"int32\"},\"multipleGroupValidatedField\":{\"minimum\":1,\"exclusiveMinimum\":false,\"type\":\"number\"},\"otherGroupValidatedField\":{\"pattern\":\".*\",\"type\":\"string\"},\"singleGroupValidatedField2\":{\"type\":\"string\"}}},\"referencedSchemas\":{\"CustomClass\":{\"required\":[\"nonGroupValidatedField\",\"singleGroupValidatedField2\"],\"type\":\"object\",\"properties\":{\"nonGroupValidatedField\":{\"type\":\"string\"},\"singleGroupValidatedField\":{\"minimum\":1,\"type\":\"integer\",\"format\":\"int32\"},\"multipleGroupValidatedField\":{\"minimum\":1,\"exclusiveMinimum\":false,\"type\":\"number\"},\"otherGroupValidatedField\":{\"pattern\":\".*\",\"type\":\"string\"},\"singleGroupValidatedField2\":{\"type\":\"string\"}}}}}"; | ||
SerializationMatchers.assertEqualsToJson(schema, expectedJson); | ||
} | ||
|
||
|
||
@Test | ||
public void shouldIncludeOnlyJakartaValidatedFieldsMatchingConditionInFilter() { | ||
ResolvedSchema schema = ModelConverters.getInstance(false, SchemaResolution.ALL_OF, annotationGroups -> annotationGroups.length == 0 || Arrays.stream(annotationGroups).anyMatch(group -> group == ValidationGroup.class)).resolveAsResolvedSchema(new AnnotatedType().type(CustomClass.class)); | ||
String expectedJson = "{\"schema\":{\"required\":[\"nonGroupValidatedField\",\"singleGroupValidatedField2\"],\"type\":\"object\",\"properties\":{\"nonGroupValidatedField\":{\"type\":\"string\"},\"singleGroupValidatedField\":{\"minimum\":1,\"type\":\"integer\",\"format\":\"int32\"},\"multipleGroupValidatedField\":{\"minimum\":1,\"exclusiveMinimum\":false,\"type\":\"number\"},\"otherGroupValidatedField\":{\"type\":\"string\"},\"singleGroupValidatedField2\":{\"type\":\"string\"}}},\"referencedSchemas\":{\"CustomClass\":{\"required\":[\"nonGroupValidatedField\",\"singleGroupValidatedField2\"],\"type\":\"object\",\"properties\":{\"nonGroupValidatedField\":{\"type\":\"string\"},\"singleGroupValidatedField\":{\"minimum\":1,\"type\":\"integer\",\"format\":\"int32\"},\"multipleGroupValidatedField\":{\"minimum\":1,\"exclusiveMinimum\":false,\"type\":\"number\"},\"otherGroupValidatedField\":{\"type\":\"string\"},\"singleGroupValidatedField2\":{\"type\":\"string\"}}}}}"; | ||
SerializationMatchers.assertEqualsToJson(schema, expectedJson); | ||
} | ||
|
||
|
||
private interface ValidationGroup { | ||
|
||
} | ||
|
||
private interface OtherValidationGroup { | ||
|
||
} | ||
|
||
|
||
private static class CustomClass { | ||
|
||
@NotNull | ||
public String nonGroupValidatedField; | ||
@Min(value = 1, groups = ValidationGroup.class) | ||
public Integer singleGroupValidatedField; | ||
@DecimalMin(value = "1.0", groups = {ValidationGroup.class, OtherValidationGroup.class}) | ||
public BigDecimal multipleGroupValidatedField; | ||
@Pattern(regexp = ".*", groups = OtherValidationGroup.class) | ||
public String otherGroupValidatedField; | ||
@NotEmpty(groups = ValidationGroup.class) | ||
public String singleGroupValidatedField2; | ||
} | ||
} |
Oops, something went wrong.