Skip to content

Add support for multiple custom JSON content types in the request body #100

@joaopedroft

Description

@joaopedroft

From what I could understand the request body is limited to the use of application/json content-type. I believe that the use of custom content type should be supported e.g.:

OAS:

paths:
  /pet:
    put:
      tags:
        - pet
      summary: Update an existing pet
      description: Update an existing pet by Id
      operationId: updatePet
      requestBody:
        description: Update an existent pet in the store
        content:
          application/vnd.dog+json:
            schema:
              $ref: '#/components/schemas/Dog'
          application/vnd.cat+json:
            schema:
              $ref: '#/components/schemas/Cat'

Code:

class Dog(BaseModel):
    class Config:
        content_type = "application/vnd.dog+json"

class Cat(BaseModel):
    class Config:
        content_type = "application/vnd.cat+json"


@app.put('/pet')
def update_pet(body: Union[Dog, Cat]):
    if isinstance(body, Dog):
        ...
    else:
        ...

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions