OpenAPITools / OpenAPITools/openapi-generator

[BUG] [Spring] Params with provided default values should not be `Optional`s with useOptional

Open
#16,631 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

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

The useOptional config value wraps non-required parameters in Java Optional<>. However, if you specify a default value for that parameter, the value won't be null, so it makes it burdensome and harder to read the code if we wrap parameters that do have a default value in Optional.

openapi-generator version

Latest on master

OpenAPI declaration file content or url

This issue exists in the sample for spring-useoptional: https://github.com/OpenAPITools/openapi-generator/blob/master/samples/server/petstore/springboot-useoptional/src/main/java/org/openapitools/api/FakeApi.java#L450

Generation Details

./run-in-docker.sh /gen/bin/generate-samples.sh /gen/bin/configs/spring-boot-useoptional.yaml

Steps to reproduce
  • Generate code against sample with the provided spring-boot-useoptional.yaml config.
  • See that testEnumParameters has a parameter with a default value but is still an Optional: @ApiParam(value = "Query parameter enum test (string)", allowableValues = "_abc, -efg, (xyz)", defaultValue = "-efg") @Valid @RequestParam(value = "enum_query_string", required = false, defaultValue = "-efg") Optional<String> enumQueryString
  • I would expect the output to be: @ApiParam(value = "Query parameter enum test (string)", allowableValues = "_abc, -efg, (xyz)", defaultValue = "-efg") @Valid @RequestParam(value = "enum_query_string", required = false, defaultValue = "-efg") String enumQueryString
Related issues/PRs
Suggest a fix

I have a PR I'll link here to fix

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-useoptional sample, especially samples/server/petstore/springboot-useoptional/src/main/java/org/openapitools/api/FakeApi.java and spring-boot-useoptional.yaml. Run ./run-in-docker.sh /gen/bin/generate-samples.sh /gen/bin/configs/spring-boot-useoptional.yaml and inspect testEnumParameters. Done means parameters with default values are generated as String rather than Optional while other useOptional behavior remains unchanged.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.