OpenAPITools / OpenAPITools/openapi-generator
[BUG][JAVA] jersey2 generated code for `oneOf` doesn't support nested bean validation
Nobody has claimed this yet.
- Dominant language
- Java
- Stars
- 26.8k
- Forks
- 7.7k
- PR merge metrics
- PR metrics pending
Description
Bug Report Checklist
- [Y] Have you provided a full/minimal spec to reproduce the issue?
- [Y] Have you validated the input using an OpenAPI validator (example)?
- [Y] Have you tested with the latest master to confirm the issue still exists?
- [Y] Have you searched for related issues/PRs?
- What's the actual output vs expected output?
Description
When using oneOf complex types, it appears that bean validation isn't run on the inner objects for the oneOf. The root cause being the generated models don't annotate with @Valid annotation.
openapi-generator version
latest.release
OpenAPI declaration file content or url
Type1:
title: Type1
type: object
properties:
type:
type: string
default: Type1
reasonCode:
type: string
required:
- reasonCode
- type
Type2:
title: Type2
type: object
properties:
type:
type: string
default: Type2
reasonCode:
type: string
required:
- reasonCode
- type
RequestInfoChoice:
title: RequestInfoChoice
oneOf:
- $ref: '#/Type1'
- $ref: '#/Type2'
discriminator:
propertyName: type
required:
- type
Generation Details
Leveraging gradle plugin with this config
openApiGenerate {
generatorName = "java"
library = "jersey2"
inputSpec = "$projectDir/***.yaml".toString()
outputDir = "$buildDir/generated".toString()
apiPackage = "*****"
modelPackage = "*****"
additionalProperties = [
useOneOfDiscriminatorLookup: "true"
]
configOptions = [
serializationLibrary: "jackson",
performBeanValidation: "true",
useBeanValidation: "true",
legacyDiscriminatorBehavior: "false",
dateLibrary: "java8",
hideGenerationTimestamp: "true",
]
}
Steps to reproduce
- Generate Code
- Create an empty
RequestInfoChoicelike so
RequestInfoChoice request = new RequestInfoChoice();
Type1 type = new Type1();
request.setActualInstance(type1)
- Invoke javax.validation on model
ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
javax.validation.Validator validator = factory.getValidator();
Set<ConstraintViolation<T>> violations = validator.validate(request);
Assert.assertFalse(violations.isEmpty()) <--- this is returning true when there should be a validation error.
Suggest a fix
I believe we should annotate the get methods on AbstractOpenApiSchema.java with @Valid to ensure nested validation is performed. here: https://github.com/OpenAPITools/openapi-generator/blob/master/modules/openapi-generator/src/main/resources/Java/libraries/jersey2/AbstractOpenApiSchema.mustache#L46
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with modules/openapi-generator/src/main/resources/Java/libraries/jersey2/AbstractOpenApiSchema.mustache, especially the generated accessors, and reproduce the issue using the supplied oneOf schema and jersey2 configuration. Validate a generated RequestInfoChoice containing an empty Type1; done means bean validation reports the missing required fields on the nested object.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java
- Domain
- api, tooling
- Issue type
- Bug
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 45/100