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

Apply multi responses to one HttpStatus or apply arbitrary str as responses key #136

Open
p3rs1st opened this issue Jan 12, 2024 · 3 comments
Labels

Comments

@p3rs1st
Copy link

p3rs1st commented Jan 12, 2024

In current flask-openapi3, I can only map 200 to Resp, 201: Resp1.

app = OpenAPI(__name__)

class Resp(BaseModel):
  a: int

class Resp1(BaseModel):
  b: int

@app.get("/", responses={200: Resp, 201: Resp1})
def book():
  pass

But I want to map all of these to one HttpStatus, because they are both HttpStatus 200. like

responses = {200: [Resp, Resp1]}

Or allow the key of responses can be arbitrary str like

responses = {"success": Resp, "success1": Resp1}
@luolingchun
Copy link
Owner

You can do this:

https://docs.pydantic.dev/latest/concepts/models/#rootmodel-and-custom-root-types

from pydantic import RootModel

class Resp(BaseModel):
  a: int

class Resp1(BaseModel):
  b: int

class UnionRespResp2(RootModel):
    root:Union[Resp, Resp1]

@app.get("/", responses={200: UnionRespResp2})
def book():
  pass

@p3rs1st
Copy link
Author

p3rs1st commented Jan 15, 2024

Thanks, now I can see the schema of the responses, but I want the example value to be shown both of them.

image

However, I can only see one of the example value like

image

If I want to see the both example value of the responses, how should I do?

@luolingchun
Copy link
Owner

When you don't have a configuration example, the Swagger UI automatically generates one. You can do this by configuring openapi_extra fields in the model_config.

from typing import Union

from pydantic import BaseModel, RootModel

from flask_openapi3 import OpenAPI

app = OpenAPI(__name__)


class Resp(BaseModel):
    a: str


class Resp1(BaseModel):
    b: int


class UnionRespResp2(RootModel):
    root: Union[Resp, Resp1]

    model_config = {
        "openapi_extra": {
            "examples": {
                "Success Example": {
                    "summary": "Success Example",
                    "value": {
                        "a": "sss"
                    }
                },
                "Fail Example": {
                    "summary": "Fail Example",
                    "value": {
                        "b": -1
                    }
                }
            }
        }
    }


@app.get("/", responses={200: UnionRespResp2})
def book():
    return "ok"


if __name__ == "__main__":
    app.run(debug=True)

image

@github-actions github-actions bot added the Stale label Apr 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

2 participants