OpenAPITools / OpenAPITools/openapi-generator
[REQ] Allow multiple bodyParams for multiple Content Types in RequestBody
Nobody has claimed this yet.
- Dominant language
- Java
- Stars
- 26.8k
- Forks
- 7.7k
- PR merge metrics
- PR metrics pending
Description
Is your feature request related to a problem? Please describe.
The requestBody specification for OpenAPI 3.0 (https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.0.md#requestBodyObject) allows you to specify multiple MediaType names and schemas for the Content section of the requestBody.
Leveraging the ability to use multiple MediaTypes in the Content section of the requestBody is quite helpful when your MediaTypes are more meaningful than application/json e.g.
put:
operationId: updatePet
requestBody:
content:
application/vnd.petstore.pet.v1+json:
schema:
$ref: '#/components/schemas/PetV1'
application/vnd.petstore.pet.v2+json:
schema:
$ref: '#/components/schemas/PetV2'
Unfortunately, the current implementation of openapi-generator assumes that you will only ever specify one MediaType in you content definition:
- In the
fromRequestBodymethod ofDefaultCodegen(https://github.com/OpenAPITools/openapi-generator/blob/master/modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultCodegen.java#L4776) only the a singleCodegenParameteris returned. - Looking deeper in the stack, it looks like
ModelUtils.getSchemaFromRequestBodyandModelUtils.getSchemaFromContent(https://github.com/OpenAPITools/openapi-generator/blob/master/modules/openapi-generator/src/main/java/org/openapitools/codegen/utils/ModelUtils.java#L766) assume that there will only ever be one MediaType. At least an warning is printed letting you know that something is wrong, but that's still unhelpful when you actually want to support multiple MediaTypes in the Content section.
Describe the solution you'd like
It looks like the DefaultCodegen class is already setup to support multiple bodyParams, but the current implementation only ever adds one bodyParam to the list.
Specifically the following changes should be made
- Parse all possible Content
MediaTypesasbodyParamsand return a list ofCodegenParameterforfromRequestBody - Add a new
contentTypeproperty to theCodegenParameterobject to allow generators to know whichcontentTypeis associated with eachbodyParam
Describe alternatives you've considered
You could alternatively add a property to the consumes object specifying which bodyParam belongs to the listed contentType . . . but that’s means you’d also have to add an identifier to each bodyParam anyway.
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 DefaultCodegen.fromRequestBody and trace ModelUtils.getSchemaFromRequestBody and getSchemaFromContent to understand the current single-media-type flow. Review CodegenParameter and the existing bodyParams handling, then verify that each requestBody media type can remain associated with its generated parameter and that all are returned.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java, openapi
- Domain
- api, tooling
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 42/100