-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Closed
Labels
Description
Description of the problem/issue
When a field is annotated with both a Bean Validation annotation (@NotEmpty or @NotBlank) and @Schema(requiredMode = ...), the validation constraints are not reflected in the generated OpenAPI schema:
@NotEmptyon collections should generateminItems: 1@NotBlankon strings should generateminLength: 1
This works correctly when:
- Only the validation annotation is present (no
@Schema) @Schemais present but withoutrequiredMode(e.g., onlydescription)
This is broken when:
@Schema(requiredMode = REQUIRED)is combined with the validation annotation@Schema(requiredMode = NOT_REQUIRED)is combined with the validation annotation
Affected Version
- Broken in: 2.2.41 ❌
- Works in: 2.2.40 ✅
Steps to Reproduce
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotEmpty;
import java.util.Set;
public class ParentDto {
// ❌ BUG: minItems is null (should be 1)
@NotEmpty
@Schema(requiredMode = Schema.RequiredMode.REQUIRED)
public Set<String> requiredItems;
// ✅ WORKS: minItems is 1
@NotEmpty
public Set<String> optionalItems;
// ❌ BUG: minItems is null (should be 1)
@NotEmpty
@Schema(requiredMode = Schema.RequiredMode.NOT_REQUIRED)
public Set<String> nonRequiredItems;
// ✅ WORKS: minItems is 1
@NotEmpty
@Schema(description = "Items with description only")
public Set<String> itemsWithDescription;
// ❌ BUG: minLength is null (should be 1)
@NotBlank
@Schema(requiredMode = Schema.RequiredMode.REQUIRED)
public String requiredString;
// ✅ WORKS: minLength is 1
@NotBlank
public String optionalString;
// ❌ BUG: minLength is null (should be 1)
@NotBlank
@Schema(requiredMode = Schema.RequiredMode.NOT_REQUIRED)
public String nonRequiredString;
// ✅ WORKS: minLength is 1
@NotBlank
@Schema(description = "String with description only")
public String stringWithDescription;
}Expected Behavior
| Field | Annotations | Expected minItems/minLength |
|---|---|---|
requiredItems |
@NotEmpty + @Schema(requiredMode=REQUIRED) |
1 |
optionalItems |
@NotEmpty |
1 |
nonRequiredItems |
@NotEmpty + @Schema(requiredMode=NOT_REQUIRED) |
1 |
itemsWithDescription |
@NotEmpty + @Schema(description=...) |
1 |
requiredString |
@NotBlank + @Schema(requiredMode=REQUIRED) |
1 |
optionalString |
@NotBlank |
1 |
nonRequiredString |
@NotBlank + @Schema(requiredMode=NOT_REQUIRED) |
1 |
stringWithDescription |
@NotBlank + @Schema(description=...) |
1 |
Actual Behavior
| Field | Annotations | Actual minItems/minLength |
Status |
|---|---|---|---|
requiredItems |
@NotEmpty + @Schema(requiredMode=REQUIRED) |
null |
❌ |
optionalItems |
@NotEmpty |
1 |
✅ |
nonRequiredItems |
@NotEmpty + @Schema(requiredMode=NOT_REQUIRED) |
null |
❌ |
itemsWithDescription |
@NotEmpty + @Schema(description=...) |
1 |
✅ |
requiredString |
@NotBlank + @Schema(requiredMode=REQUIRED) |
null |
❌ |
optionalString |
@NotBlank |
1 |
✅ |
nonRequiredString |
@NotBlank + @Schema(requiredMode=NOT_REQUIRED) |
null |
❌ |
stringWithDescription |
@NotBlank + @Schema(description=...) |
1 |
✅ |
Additional Context
- Possibly introduced by fix: NotBlank and NotEmpty annotations follow NotNull behavior #4968 ❓
- Suggested fix: fix: apply minItems/minLength from @NotEmpty/@NotBlank regardless of requiredMode #5033
Checklist
- I have searched the existing issues and this is not a duplicate.
- I have provided sufficient information for maintainers to reproduce the issue.