ormar
ormar copied to clipboard
Add @property_field to the openapi schema generated by FastAPI
Is your feature request related to a problem? Please describe.
I use an OpenAPI generator cli for generating Typescript types/models/etc. based on my FastAPI w/ ormar backend. I recently used an ormar @property_field
but noticed it wasn't appearing in the OpenAPI schema and therefore wasn't picked up by the cli. I'm not sure how possible this is, given that a regular @property
on a pydantic model also doesn't seem to get picked up in OpenAPI.
Describe the solution you'd like
@property_field
s are picked up by OpenAPI via FastAPI. Consider the following small example:
from fastapi import APIRouter
import ormar
from ormar import property_field
from app.db import MainMeta
router = APIRouter()
class Foo(ormar.Model):
class Meta(MainMeta):
pass
id = ormar.Integer(primary_key=True)
name = ormar.String(max_length=255, unique=True)
@property_field
def prefixed_name(self) -> str:
return f"prefix_{self.name}"
@router.get("/foo", response_model=Foo)
async def foo() -> Foo:
return await Foo.objects.first()
We can see only name
is present in the api schema:
Describe alternatives you've considered none that i can think of
I will take a look at this, I'm modifying the example value already so will check if adding property field to openapi schema causes some errors or not
fastapi.get_open_api -> fastapi.get_model_definitions -> pydanic.model_process_schema -> pydantic.model_type_schema
In the model_type_schema
, it use the model.__fields__
to generate field definitions.
so the property_field
not in the swagger definitions.
The property_field
's type is not sure and not the ModelField
.
I will take a look at this, I'm modifying the example value already so will check if adding property field to openapi schema causes some errors or not