Generic sub-model schema definition not found
shixinla opened this issue · comments
Describe the bug
- Geting
Could not resolve reference: Could not resolve pointer: /definitions/Test does not exist in document
error in swagger - versions
pydantic==1.10.2
sanic==22.9.0
sanic_ext==22.9.0
To Reproduce
my models.py
from datetime import date
from pydantic import BaseModel, Field
from pydantic.generics import GenericModel
from typing import Generic, TypeVar, Optional
T = TypeVar('T')
class RespModel(GenericModel, Generic[T]):
code: int = Field(description="business code", example=0, default=0)
data: T = Field(description="business data")
msg: str = Field(description="msg", example="success", default="success")
class Test(BaseModel):
foo: str = Field(description="Foo Description", example="FOOO")
bar: str = "test"
@openapi.definition(
summary="Test post api",
parameter=[
# query
Parameter(name="test", schema=str, location="query"),
# cookie
Parameter(name="cookie", schema=str, location="cookie"),
# header parameter
Parameter(name="token", schema=str, location="header")
],
body=RequestBody(
{'application/json': Test.schema()}, required=True
),
response=[
Response(
{'application/json': RespModel[Test].schema()},
description="success",
status=200
)
],
)
Additional context
If I am using the wrong method, please point it out, thank you
This is not a bug.
I think you need to do two things:
- Add
Test
as a component:
openapi.component(Test)
- Built the
RespModel
with the correct ref template:
response=[
Response(
{
"application/json": RespModel[Test].schema(
ref_template="#/components/schemas/{model}"
)
},
description="success",
status=200,
)
],
Looking at it closer, I think there is an even better solution since Pydantic is including Test
inside the RespModel
spec. This way, you do not need to add the Test
component.
response=[
Response(
{
"application/json": RespModel[Test].schema(
ref_template="#/definitions/{model}"
)
},
description="success",
status=200,
)
],
What/where is this #5
coming from?
What is the question?
What is the question?
In your last response, you have a ref_template
value that begins with "#5" ... what is that "5", how is it resolving to anything valid?
In your last response, you have a
ref_template
value that begins with "#5" ... what is that "5", how is it resolving to anything valid?
Its not. Was a typo. 🤦♂️