OpenAPITools / OpenAPITools/openapi-generator

[BUG] Description

Open
#17,059 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 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

https://github.com/OpenAPITools/openapi-generator/pull/16972
https://github.com/OpenAPITools/openapi-generator/issues/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}}

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 and other Java generator templates involved in request-body parameters, focusing on the optionalDataType and useOptional Mustache logic shown in the issue. Reproduce the OpenAPI 3.0.3 example with generator spring, library spring-boot, and useOptional=false, then verify that the generated parameter matches the 7.0.1 behavior without Optional.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.