Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
joaopedroft opened this issue Aug 16, 2023 · 3 comments
Open
Labels

Comments

@joaopedroft
Copy link

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:
        ...
@luolingchun
Copy link
Owner

If the body is not of type BaseModel, we must re-implement the data validation function (source code), it will be a lot of work.

So I suggest another way:

class UnionModel(BaseModel):
    __root__: Union[Dog, Cat]


@app.put('/pet')
def update_pet(body: UnionModel):
    print(body)
    print(type(body))
    return body.json()

@joaopedroft
Copy link
Author

I understand thank you, I believe that I can work with the alternative.

@raisachatterjee
Copy link

I want to work on this.

@github-actions github-actions bot added the Stale label Nov 1, 2023
@github-actions github-actions bot removed the Stale label Apr 1, 2024
@github-actions github-actions bot added the Stale label Jun 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants