OpenAPITools / OpenAPITools/openapi-generator

[BUG] [JavaSpring]: optional parameters in delegate method signature are not wrapped in Optional<?>

Open
#1,649 1 comment 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

Description

When generating a spring-mvc controller from an opan api yaml with a non required parameter (required: false) the generated method annotated with request mapping annotations declared the parameter type wrapped in Optional (e.g. Optional myparam )

     @RequestMapping(value = "/search",
        produces = { "application/json" }, 
        method = RequestMethod.GET)
    default ResponseEntity<List<String>> _searchGet(@ApiParam(value = "...") @Valid @RequestParam(value = "query", required = false) Optional<String> query) {
        return searchGet(query);
    }

The method that the call is delegated to ( in this example searchGet) does not declare the parameter as Optional Type:

default ResponseEntity<List<String>> searchGet(String query) {
        return ...
    }

That results in compiler errors in the generated code!

openapi-generator version

3.3.0, master

OpenAPI declaration file content or url

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 the Spring generator's optionalDataType template and the delegate-pattern output described in the issue. Reproduce the case with the supplied OpenAPI query parameter and Maven configuration, then inspect the generated controller and delegated method. Done means optional parameters use compatible types in both generated signatures and the generated project compiles.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.