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 @@ -663,7 +663,11 @@ public Parameter buildParam(ParameterInfo parameterInfo, Components components,
* @param isParameterObject the is parameter object
* @param openapiVersion the openapi version
*/
public void applyBeanValidatorAnnotations(final MethodParameter methodParameter, final Parameter parameter, final List<Annotation> annotations, final boolean isParameterObject, String openapiVersion) {
public void applyBeanValidatorAnnotations(final MethodParameter methodParameter,
final Parameter parameter,
final List<Annotation> annotations,
final boolean isParameterObject,
String openapiVersion) {
boolean annotatedNotNull = annotations != null && SchemaUtils.annotatedNotNull(annotations);
if (annotatedNotNull && !isParameterObject) {
parameter.setRequired(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Schema.RequiredMode;
import io.swagger.v3.oas.models.media.Schema;
import jakarta.validation.Constraint;
import jakarta.validation.constraints.DecimalMax;
import jakarta.validation.constraints.DecimalMin;
import jakarta.validation.constraints.Max;
Expand All @@ -30,6 +31,7 @@
import jakarta.validation.constraints.Positive;
import jakarta.validation.constraints.PositiveOrZero;
import jakarta.validation.constraints.Size;
import org.hibernate.validator.constraints.Range;
import org.springdoc.core.properties.SpringDocConfigProperties.ApiDocs.OpenApiVersion;

import org.springframework.lang.Nullable;
Expand Down Expand Up @@ -229,7 +231,7 @@ public boolean fieldRequired(Field field, io.swagger.v3.oas.annotations.media.Sc
* @param openapiVersion the openapi version
*/
public static void applyValidationsToSchema(Schema<?> schema, List<Annotation> annotations, String openapiVersion) {
annotations.forEach(anno -> {
removeComposingConstraints(filterConstraintAnnotations(annotations)).forEach(anno -> {
String annotationName = anno.annotationType().getSimpleName();
if (annotationName.equals(Positive.class.getSimpleName())) {
if (OpenApiVersion.OPENAPI_3_1.getVersion().equals(openapiVersion)) {
Expand Down Expand Up @@ -295,6 +297,10 @@ else if (OPENAPI_STRING_TYPE.equals(type)) {
if (annotationName.equals(Pattern.class.getSimpleName())) {
schema.setPattern(((Pattern) anno).regexp());
}
if (annotationName.equals(Range.class.getSimpleName())) {
schema.setMinimum(BigDecimal.valueOf(((Range) anno).min()));
schema.setMaximum(BigDecimal.valueOf(((Range) anno).max()));
}
});
if (schema!=null && annotatedNotNull(annotations)) {
String specVersion = schema.getSpecVersion().name();
Expand All @@ -304,6 +310,40 @@ else if (OPENAPI_STRING_TYPE.equals(type)) {
}
}

/**
* Get all annotations of type {@link Constraint}.
*
* @param annotations annotations
* @return the {@link Constraint} annotations
*/
private static List<Annotation> filterConstraintAnnotations(List<Annotation> annotations) {
return annotations.stream()
.filter(annotation -> annotation.annotationType().isAnnotationPresent(Constraint.class))
.toList();
}

/**
* Remove the composing constraints from the annotations. This is necessary since otherwise the annotations may
* default to the composing constraints' default value (dependent on the annotation ordering).
* An example is {@link Range} being a composed constraint for {@link Min} and {@link Max}.
* So {@link Min} and {@link Max} are removed to ensure that the constraint values are read from {@link Range}.
*
* @param constraintAnnotations constraint annotations
* @return the annotations where known composing constraints have been removed
*/
private static List<Annotation> removeComposingConstraints(List<Annotation> constraintAnnotations) {
Set<Class<? extends Annotation>> composingTypes = new HashSet<>();
for (Annotation ann : constraintAnnotations) {
Class<? extends Annotation> type = ann.annotationType();
for (Annotation meta : type.getAnnotations()) {
if (meta.annotationType().isAnnotationPresent(Constraint.class)) {
composingTypes.add(meta.annotationType());
}
}
}
return constraintAnnotations.stream().filter(annotation -> !composingTypes.contains(annotation.annotationType())).toList();
}

/**
* Nullable from annotations boolean.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Size;

import org.hibernate.validator.constraints.Range;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
Expand Down Expand Up @@ -72,4 +73,14 @@ public List<Person> findByLastName(@RequestParam(name = "lastName", required = t
return hardCoded;

}

@RequestMapping(path = "/persons", method = RequestMethod.GET)
public List<Person> findPersons(
@RequestParam(name = "setsOfShoes") @Range(min = 1, max = 4) int setsOfShoes,
@RequestParam(name = "height") @Range(max = 200) int height,
@RequestParam(name = "age") @Range(min = 2) int age
) {
return List.of();

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Size;

import org.hibernate.validator.constraints.Range;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
Expand Down Expand Up @@ -72,4 +73,14 @@ public List<Person> findByLastName(@RequestParam(name = "lastName", required = t
return hardCoded;

}

@RequestMapping(path = "/persons", method = RequestMethod.GET)
public List<Person> findPersons(
@RequestParam(name = "setsOfShoes") @Range(min = 1, max = 4) int setsOfShoes,
@RequestParam(name = "height") @Range(max = 200) int height,
@RequestParam(name = "age") @Range(min = 2) int age
) {
return List.of();

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,12 @@
"required": true
},
"responses": {
"415": {
"description": "Unsupported Media Type",
"500": {
"description": "Internal Server Error",
"content": {
"*/*": {
"schema": {
"$ref": "#/components/schemas/ErrorMessage"
"$ref": "#/components/schemas/Problem"
}
}
}
Expand All @@ -84,12 +84,12 @@
}
}
},
"500": {
"description": "Internal Server Error",
"415": {
"description": "Unsupported Media Type",
"content": {
"*/*": {
"schema": {
"$ref": "#/components/schemas/Problem"
"$ref": "#/components/schemas/ErrorMessage"
}
}
}
Expand All @@ -107,6 +107,64 @@
}
}
},
"/persons": {
"get": {
"tags": [
"person-controller"
],
"operationId": "findPersons",
"parameters": [
{
"name": "setsOfShoes",
"in": "query",
"required": true,
"schema": {
"maximum": 4,
"minimum": 1,
"type": "integer",
"format": "int32"
}
},
{
"name": "height",
"in": "query",
"required": true,
"schema": {
"maximum": 200,
"minimum": 0,
"type": "integer",
"format": "int32"
}
},
{
"name": "age",
"in": "query",
"required": true,
"schema": {
"maximum": 9223372036854775807,
"minimum": 2,
"type": "integer",
"format": "int32"
}
}
],
"responses": {
"200": {
"description": "OK",
"content": {
"*/*": {
"schema": {
"type": "array",
"items": {
"$ref": "#/components/schemas/Person"
}
}
}
}
}
}
}
},
"/personByLastName": {
"get": {
"tags": [
Expand Down Expand Up @@ -161,12 +219,12 @@
}
],
"responses": {
"415": {
"description": "Unsupported Media Type",
"500": {
"description": "Internal Server Error",
"content": {
"*/*": {
"schema": {
"$ref": "#/components/schemas/ErrorMessage"
"$ref": "#/components/schemas/Problem"
}
}
}
Expand All @@ -181,12 +239,12 @@
}
}
},
"500": {
"description": "Internal Server Error",
"415": {
"description": "Unsupported Media Type",
"content": {
"*/*": {
"schema": {
"$ref": "#/components/schemas/Problem"
"$ref": "#/components/schemas/ErrorMessage"
}
}
}
Expand All @@ -210,17 +268,6 @@
},
"components": {
"schemas": {
"ErrorMessage": {
"type": "object",
"properties": {
"errors": {
"type": "array",
"items": {
"type": "string"
}
}
}
},
"Problem": {
"type": "object",
"properties": {
Expand All @@ -232,6 +279,17 @@
}
}
},
"ErrorMessage": {
"type": "object",
"properties": {
"errors": {
"type": "array",
"items": {
"type": "string"
}
}
}
},
"Person": {
"required": [
"lastName"
Expand Down Expand Up @@ -273,4 +331,4 @@
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,64 @@
}
}
},
"/persons": {
"get": {
"tags": [
"person-controller"
],
"operationId": "findPersons",
"parameters": [
{
"name": "setsOfShoes",
"in": "query",
"required": true,
"schema": {
"maximum": 4,
"minimum": 1,
"type": "integer",
"format": "int32"
}
},
{
"name": "height",
"in": "query",
"required": true,
"schema": {
"maximum": 200,
"minimum": 0,
"type": "integer",
"format": "int32"
}
},
{
"name": "age",
"in": "query",
"required": true,
"schema": {
"maximum": 9223372036854775807,
"minimum": 2,
"type": "integer",
"format": "int32"
}
}
],
"responses": {
"200": {
"description": "OK",
"content": {
"*/*": {
"schema": {
"type": "array",
"items": {
"$ref": "#/components/schemas/Person"
}
}
}
}
}
}
}
},
"/personByLastName": {
"get": {
"tags": [
Expand Down Expand Up @@ -273,4 +331,4 @@
}
}
}
}
}