Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] Description #17059

Open
4 of 6 tasks
SebastianKuehn opened this issue Nov 13, 2023 · 1 comment
Open
4 of 6 tasks

[BUG] Description #17059

SebastianKuehn opened this issue Nov 13, 2023 · 1 comment

Comments

@SebastianKuehn
Copy link

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 OpenAPI Java generator creates for optional (non-required) request bodies always a Optional parameter.

openapi-generator version
  • Version 7.1.0 has regression
  • Version 7.0.1 works as expected
OpenAPI declaration file content or url
openapi: 3.0.3
info:
  title: OpenAPI-Regression
  description: Regression in OpenAPI 7.1.0
  version: 0.0.1
paths:
  /:
    put:
      operationId: example
      requestBody:
        required: false
        content:
          application/json:
            schema:
              type: string
      responses:
        200:
          description: result
          content:
            application/json:
              schema:
                type: string
Generation Details
  • Generator: spring
  • Library: spring-boot
  • useOptional is false

But happens also with other Java generators.

Expected (like Version 7.0.1)

ResponseEntity<String> example(
         @Valid @RequestBody(required = false) String body

Actual with 7.1.0

ResponseEntity<String> example(
         @Valid @RequestBody(required = false) Optional<String> body
Steps to reproduce
  1. Create a Spec with a non-required request body
  2. set useOptional to false

Version 7.0.1 generates without Optional, 7.1.0 with Optional, which is unexpected.

Related issues/PRs

#16972
#16971

We now have the opposite effect of #16971:

Spring supports optional RequestBody just like the Query Parameter. However, this is currently implemented, even if Optional is not set as a configuration in the parameter.

Suggest a fix

Not sure, but use optionalDataType only if useOptional is set

Instead of:

{{^reactive}}{{>optionalDataType}}{{/reactive}}

use:

{{^reactive}}{{^useOptional}}{{{dataType}}}{{/useOptional}}{{useOptional}}{{>optionalDataType}}{{/useOptional}}{{/reactive}}
@wing328
Copy link
Member

wing328 commented Nov 14, 2023

thanks for reporting the issue. can you please file a PR with the suggested fix?

P1tt187 added a commit to P1tt187/openapi-generator that referenced this issue Aug 7, 2024
adjust template to generate optional datatypes only if useoptional is set
P1tt187 added a commit to P1tt187/openapi-generator that referenced this issue Aug 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants