OpenAPITools / OpenAPITools/openapi-generator

[REQ] Allow multiple bodyParams for multiple Content Types in RequestBody

Open
#3,990 8 comments 22 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Enhancement: Feature
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:

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 as bodyParams and return a list of CodegenParameter for fromRequestBody
  • Add a new contentType property to the CodegenParameter object to allow generators to know which contentType is associated with each bodyParam

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

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 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.