OpenAPITools / OpenAPITools/openapi-generator

[BUG] [Java] [JaxRS] readOnly/writeOnly not respected in "required" attribute bean validaion

Open
#9,654 0 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Issue: Bug
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

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
  1. Generate Code
  2. Check contents of generated Example.java -> getters of displayName_r/displayName_w define @NotNull
Related issues/PRs

#9222: "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}}

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with beanValidation.mustache and the generated Example.java described in the reproduction. Generate the JAX-RS output using the provided configuration and inspect the getters for displayName_r and displayName_w. Done means readOnly and writeOnly required properties no longer receive the inappropriate @NotNull annotation, while ordinary required properties retain it.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
api, backend
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.