Skip to content

Commit

Permalink
fix: Fields with not null constraint annotations are considered optio…
Browse files Browse the repository at this point in the history
…nal if groups attribute is set
  • Loading branch information
T3rm1 committed Nov 22, 2024
1 parent dc8785e commit 3a81c0a
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@
import java.util.Optional;
import java.util.Set;
import java.util.Objects;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.Stream;

Expand Down Expand Up @@ -1532,9 +1533,7 @@ protected void applyBeanValidatorAnnotations(Schema property, Annotation[] annot
}
}
if (parent != null && annotations != null && applyNotNullAnnotations) {
boolean requiredItem = Arrays.stream(annotations).anyMatch(annotation ->
NOT_NULL_ANNOTATIONS.contains(annotation.annotationType().getSimpleName())
);
boolean requiredItem = Arrays.stream(annotations).anyMatch(this::requiredByAnnotation);
if (requiredItem) {
addRequiredItem(parent, property.getName());
}
Expand Down Expand Up @@ -1591,6 +1590,14 @@ protected void applyBeanValidatorAnnotations(Schema property, Annotation[] annot
}
}

private boolean requiredByAnnotation(Annotation annotation) {
boolean hasNotNullAnnotation = NOT_NULL_ANNOTATIONS.contains(annotation.annotationType().getSimpleName());
if (hasNotNullAnnotation && annotation.annotationType().getCanonicalName().contains(".validation.constraints")) {
return !hasGroupsAttribute(annotation);
}
return hasNotNullAnnotation;
}

private boolean resolveSubtypes(Schema model, BeanDescription bean, ModelConverterContext context, JsonView jsonViewAnnotation) {
final List<NamedType> types = _intr.findSubtypes(bean.getClassInfo());
if (types == null) {
Expand Down Expand Up @@ -3078,4 +3085,20 @@ protected Schema buildRefSchemaIfObject(Schema schema, ModelConverterContext con
}
return result;
}

private boolean hasGroupsAttribute(Annotation annotation) {
return Arrays.stream(annotation.annotationType().getDeclaredMethods())
.filter(m -> "groups".equals(m.getName()))
.findFirst()
.map(m -> {
try {
return m.invoke(annotation);
} catch (Exception e) {
return null;
}
})
.map(Class[].class::cast)
.filter(g -> g.length > 0)
.isPresent();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ public void testRequiredProperty() {
assertTrue(model.getRequired().contains("modeRequired"));
assertFalse(model.getRequired().contains("modeNotRequired"));
assertFalse(model.getRequired().contains("modeNotRequiredWithAnnotation"));
assertFalse(model.getRequired().contains("requiredByAnnotationWithGroupsAttribute"));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,7 @@ public class RequiredFields {
@Schema(description = "mode not required with annotation", requiredMode = Schema.RequiredMode.NOT_REQUIRED)
@NotNull
public Long modeNotRequiredWithAnnotation;

@NotNull(groups = RequiredFields.class)
public Long requiredByAnnotationWithGroupsAttribute;
}

0 comments on commit 3a81c0a

Please sign in to comment.