OpenAPITools / OpenAPITools/openapi-generator
[BUG] Description
Nobody has claimed this yet.
- 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
- Create a Spec with a non-required request body
- set
useOptionaltofalse
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
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- 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