OpenAPITools / OpenAPITools/openapi-generator

[BUG] [sprind-cloud] Bean Validation for Type Optional is wrong

Open
#8,690 0 comments 0 reactions 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

Invalid code for the type Optional with Bean Validation is generated.

openapi-generator version

5.0.1

OpenAPI declaration file content or url

If you post the code inline, please wrap it with

  /foo:
    post:
      operationId: createFoo
      responses:
        '200':
             content:
                text/plain:
                  schema:
                    type: string
                    format: binary
      parameters:
        - name: filename
          schema:
            type: string
            maxLength: 120
            pattern: '^[a-zA-Z0-9_-]*$'
Generation Details

Maven plugin

          <configuration>
                            <generatorName>spring</generatorName>
                            <configOptions>
                                <sortParamsByRequiredFlag>true</sortParamsByRequiredFlag>
                                <library>spring-cloud</library>
                                <java8>true</java8>
                                <dateLibrary>java8</dateLibrary>
                                <useOptional>true</useOptional>
                                <skipDefaultInterface>true</skipDefaultInterface>
                                <useBeanValidation>true</useBeanValidation>
                            </configOptions>
ResponseEntity<String> getFoo(
         @Pattern( regexp = "^[a-zA-Z0-9_-]*$" ) @Size( max = 120 )
         @ApiParam( value = "The filename of the Aspect Model input file." ) @Valid
         @RequestParam( value = "filename", required = false ) Optional<String> filename );
Steps to reproduce

Use bean validation with optionals

Suggest a fix

Generate valid code. The constraints have to be included in the Optional tag

ResponseEntity<String> getFoo(
       
         @ApiParam( value = "The filename of the Aspect Model input file." ) @Valid
         @RequestParam( value = "filename", required = false ) Optional< @Pattern( regexp = "^[a-zA-Z0-9_-]*$" ) @Size( max = 120 )String> filename );
`

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 by reproducing the issue with the Maven plugin using the spring generator, spring-cloud library, useOptional=true, and useBeanValidation=true, based on the inline OpenAPI parameter. Trace the generated Java method signature and the Bean Validation annotation templates; done means constraints for an optional parameter are placed inside Optional<> and the generated code is valid.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, spring
Domain
api, backend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.