[BUG] [Java] [JaxRS] readOnly/writeOnly not respected in "required" attribute bean validaion
Created by: mickroll
Bug Report Checklist
-
Have you provided a full/minimal spec to reproduce the issue? -
Have you validated the input using an OpenAPI validator (example)? -
Have you tested with the latest master to confirm the issue still exists? -
Have you searched for related issues/PRs? -
What's the actual output vs expected output? -
[Optional] Sponsorship to speed up the bug fix or feature request (example)
Description
Currently, the attribute parameters readOnly
and writeOnly
are ignored when generating bean validation @NotNull
annotation.
Spec says:
If a readOnly or writeOnly property is included in the required list, required affects just the relevant scope. see https://swagger.io/docs/specification/data-models/data-types/#readonly-writeonly
Using @NotNull
on these fields is wrong, because it fails validation in the other direction, where the attribute is intentionally not present.
openapi-generator version
5.1.1
OpenAPI declaration file content or url
"Example": {
"type": "object",
"required": ["displayName_r", "displayName_w"],
"properties": {
"displayName_r": {
"type": "string",
"readOnly": true
},
"displayName_w": {
"type": "string",
"writeOnly": true
}
}
},
Generation Details
<generatorName>jaxrs-spec</generatorName>
<configOptions>
<interfaceOnly>true</interfaceOnly>
<returnResponse>false</returnResponse>
<sourceFolder>java</sourceFolder>
<useSwaggerAnnotations>false</useSwaggerAnnotations>
<generatePom>false</generatePom>
<dateLibrary>java8</dateLibrary>
</configOptions>
Steps to reproduce
- Generate Code
- Check contents of generated
Example.java
-> getters ofdisplayName_r
/displayName_w
define@NotNull
Related issues/PRs
#9222 (closed): "readOnly/writeOnly not respected in jackson annotation"
Suggest a fix
Update beanValidation.mustache
to respect readOnly/writeOnly markers:
{{#required}}{{^isReadOnly}}{{^isWriteOnly}}
@NotNull
{{/isWriteOnly}}{{/isReadOnly}}{{/required}}
{{>beanValidationCore}}
instead of
{{#required}}
@NotNull
{{/required}}
{{>beanValidationCore}}