I have server and client code generated based on a YAML file, which is packaged into a JAR and used across multiple microservices, so I can’t apply the @NotNull annotation at the code level.
For example, I have a User class with an email field, which doesn’t and shouldn’t have null validation, but does have a Size validation. Then there’s a subclass UserRegister, where the email field should have both NotNull and Size validations. So I need something like this:
definitions:
User:
type: object
properties:
email:
type: string
minLength: 1
UserRegister:
required: (it doesn't generate @NotNull in my case)
- email
allOf:
- $ref: '#/definitions/User'
type: object
However, required only works at the level where some property is defined and doesn’t see email directly in UserRegister. Using required at the User level isn’t suitable due to business requirements.
If I define a similar property in UserRegister with both validations, it fails with a cascading validation error because it validates twice.
The only idea I have is to create new object schemas for this case, but that would require creating a lot of code, new mappers etc. Is there a way to avoid this?
OpenAPI version: 3.0.1