Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Fields with not null constraint annotations are considered optional if groups attribute is set #4797

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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 @@ -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;
}