Bishwas-py / djapy

No bullshit, Django Rest API Framework
https://djapy.io
59 stars 3 forks source link

`@computed_field`'s schema should be visible in Swagger/OpenAPI #18

Closed Bishwas-py closed 4 months ago

Bishwas-py commented 4 months ago

Currently,


class SimpleCommentSchema(TypedDict):
    comment_count: int | None
    last_comment: CommentSchema | None

@computed_field
    def comments(self, *args, **kwargs) -> SimpleCommentSchema:
        comments = PolymorphicComments.objects.filter(
            parent_id=self._source_obj.id,
            parent_type=ContentType.objects.get_for_model(self._source_obj)).alive()
        print(comments.count())
        return {
            'comment_count': comments.count(),
            'last_comment': comments.last() if comments.exists() else None,
        }

like fields' schema is not being address in Swagger.

FIx: https://github.com/pydantic/pydantic/discussions/6298

Bishwas-py commented 4 months ago

Fix: openapi/openapi_path.py, line 152 be replaced with:

prepared_schema = response_model.model_json_schema(ref_template=REF_MODAL_TEMPLATE, mode='serialization')