sanic-org / sanic

Accelerate your web app development | Build fast. Run fast.

Home Page:https://sanic.dev

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Openapi extension generates an invalid json file when used with Pydantic

TimurDela opened this issue · comments

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:

  1. definitions is not a valid openAPI field in requestBody.content.application/json.schema nor responses.200.content.application/json.schema
  2. 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

I now realise I posted this on the wrong repo. Sorry