[REQ] Allow multiple bodyParams for multiple Content Types in RequestBody
Created by: jharriman
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
fromRequestBody
method ofDefaultCodegen
(https://github.com/OpenAPITools/openapi-generator/blob/master/modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultCodegen.java#L4776) only the a singleCodegenParameter
is returned. - Looking deeper in the stack, it looks like
ModelUtils.getSchemaFromRequestBody
andModelUtils.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
MediaTypes
asbodyParams
and return a list ofCodegenParameter
forfromRequestBody
- Add a new
contentType
property to theCodegenParameter
object to allow generators to know whichcontentType
is 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.