Skip to content

Commit

Permalink
Automatically add required if a field is @NotNull or @notblank. Fixes #…
Browse files Browse the repository at this point in the history
  • Loading branch information
bnasslahsen committed Dec 29, 2024
1 parent 1461070 commit 39ef9f4
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -159,11 +159,12 @@ private static Stream<MethodParameter> fromGetterOfField(Class<?> paramClass, Fi
return Stream.empty();
}
String prefix = fieldNamePrefix + resolveName(parameter, schema).orElse(field.getName()) + DOT;
boolean notNullAnnotationsPresent = AbstractRequestService.hasNotNullAnnotation(Arrays.stream(field.getDeclaredAnnotations())
Set<String> annotationSimpleNames = Arrays.stream(field.getDeclaredAnnotations())
.map(Annotation::annotationType)
.map(Class::getSimpleName)
.collect(Collectors.toSet()));
return extractFrom(type, prefix, parentRequired && resolveRequired(schema, parameter, !notNullAnnotationsPresent));
.collect(Collectors.toSet());
boolean notNullAnnotationsPresent = AbstractRequestService.hasNotNullAnnotation(annotationSimpleNames);
return extractFrom(type, prefix, resolveRequired(schema, parameter, !notNullAnnotationsPresent));
}
}

Expand Down Expand Up @@ -270,10 +271,12 @@ private static Stream<MethodParameter> fromSimpleClass(Class<?> paramClass, Fiel
return Stream.empty();
}

boolean isNotRequired = !(isParentRequired && resolveRequired(schema, parameter, !AbstractRequestService.hasNotNullAnnotation(Arrays.stream(fieldAnnotations)
Set<String> annotationSimpleNames = Arrays.stream(field.getDeclaredAnnotations())
.map(Annotation::annotationType)
.map(Class::getSimpleName)
.collect(Collectors.toSet()))));
.collect(Collectors.toSet());

boolean isNotRequired = !resolveRequired(schema, parameter, !AbstractRequestService.hasNotNullAnnotation(annotationSimpleNames));
Annotation[] notNullFieldAnnotations = Arrays.stream(fieldAnnotations)
.filter(annotation -> AbstractRequestService.hasNotNullAnnotation(List.of(annotation.annotationType().getSimpleName())))
.toArray(Annotation[]::new);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import org.springdoc.core.annotations.ParameterObject;

Expand Down Expand Up @@ -72,15 +73,24 @@ public void nestedParameterObjectWithOptionalParentField(@ParameterObject Parame
}

public record ParameterObjectWithOptionalField(
@Schema(requiredMode = Schema.RequiredMode.NOT_REQUIRED) NestedRequiredParameterObject schemaNotRequiredNestedParameterObject,
@Parameter NestedRequiredParameterObject parameterNotRequiredNestedParameterObject,
@Parameter(required = true) NestedRequiredParameterObject requiredNestedParameterObject
NestedRequiredParameterObject schemaNotRequiredNestedParameterObject,
NestedNonNullParameterObject nestedNonNullParameterObject,
NestedNotBlankParameterObject nestedNotBlankParameterObject
) {

}

public record NestedRequiredParameterObject(
@Schema(requiredMode = Schema.RequiredMode.REQUIRED) @NotNull String requiredParameterField) {
@Schema(requiredMode = Schema.RequiredMode.REQUIRED) String requiredParameterField) {
}

public record NestedNonNullParameterObject(
@NotNull String requiredParameterField) {
}

public record NestedNotBlankParameterObject(
@NotBlank String requiredParameterField) {
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -60,21 +60,21 @@
{
"name": "schemaNotRequiredNestedParameterObject.requiredParameterField",
"in": "query",
"required": false,
"required": true,
"schema": {
"type": "string"
}
},
{
"name": "parameterNotRequiredNestedParameterObject.requiredParameterField",
"name": "nestedNonNullParameterObject.requiredParameterField",
"in": "query",
"required": false,
"required": true,
"schema": {
"type": "string"
}
},
{
"name": "requiredNestedParameterObject.requiredParameterField",
"name": "nestedNotBlankParameterObject.requiredParameterField",
"in": "query",
"required": true,
"schema": {
Expand Down Expand Up @@ -114,4 +114,4 @@
}
},
"components": {}
}
}

0 comments on commit 39ef9f4

Please sign in to comment.