Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is it possible to generate a component schema directly from a serializer class? #1238

Closed
james-mchugh opened this issue May 15, 2024 · 1 comment

Comments

@james-mchugh
Copy link

james-mchugh commented May 15, 2024

Describe the bug
My project currently uses DRF Spectacular for generating an OpenAPI spec and Swagger Docs page. Thank you for making that very easy.

What I would like to be able to do now though is get a component schema for an individual serializer class. For example, something like this

serializer_component_schema = to_schema(MySerializerClass)

Is that possible?

I tried doing something like,

a = AutoSchema()
a.registry = ComponentRegistry()

component = a.resolve_serializer(MySerializerClass, "response)

But it was unhappy that no view was set.

Sorry if this is already somewhere in the docs, but I did try looking for it, going through issues, and exploring the code to see how this could be done prior to opening this issue.

Thank you!

@tfranzel
Copy link
Owner

Hi, spectacular is not designed to be executed standalone on a serializer. There is a lot of stuff happening in the background with regards to paths, methods, direction, views, querysets, etc. The task is somewhat underspecified, if you want to do it right. This will give you something, but with no guarantees. Your mileage may vary.

    insp = AutoSchema()
    insp.registry = ComponentRegistry()
    insp.view = APIView()
    insp.view.request = build_mock_request("GET", "/", insp.view, None)
    insp.resolve_serializer(MySerializerClass, "request")
    schema = insp.registry.build({})

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants