Openapi extension generates an invalid json file when used with Pydantic
TimurDela opened this issue · comments
TimurDela commented
Is there an existing issue for this?
- I have searched the existing issues
Describe the bug
When creating a complex class (containing elements from another class) for either the request body or the response, the pydantic schema puts a lot of information in a field called definitions
and then $ref
to it.
This causes two problems:
definitions
is not a valid openAPI field inrequestBody.content.application/json.schema
norresponses.200.content.application/json.schema
- the
$ref
paths get broken
Code snippet
from sanic import Sanic
from sanic.request import Request
from sanic.response import JSONResponse, json
from sanic_ext import openapi, validate
from pydantic import BaseModel
class Simple(BaseModel):
a: int
b: float
class Complex(BaseModel):
d: Simple
app = Sanic("BasicApp")
Extend(app)
@app.get("/myRoute")
@openapi.definition(
body={'application/json': Complex.schema()},
)
async def get(request):
return json({})
Expected Behavior
Everything under definitions
(both for the body and the response) should go to a components
field at the top level and all $ref
should be updated accordingly.
How do you run Sanic?
As a module
Operating System
macOS 13.4.1
Sanic Version
Sanic 23.3.0; Routing 22.8.0
Additional context
No response
TimurDela commented
I now realise I posted this on the wrong repo. Sorry