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

Add custom schemas to Swagger UI #169

Closed
igoose1 opened this issue Jul 6, 2021 · 9 comments
Closed

Add custom schemas to Swagger UI #169

igoose1 opened this issue Jul 6, 2021 · 9 comments

Comments

@igoose1
Copy link
Contributor

igoose1 commented Jul 6, 2021

Hey!

Is there a way to append schemas which aren't shown in Swagger?

I've coded a trick from #86 to use a complex object in the GET query but a schema I've specified is not in generated Swagger docs as it's not a real Schema for django-ninja, it's just a string field. I had a look at Router and NinjaAPI methods but couldn't find something similar to "add_schema".

@vitalik
Copy link
Owner

vitalik commented Jul 8, 2021

Hi @igoose1

at this moment - you can override the NinjaAPI.get_openapi_schema

class MyApi(NinjaAPI):
    def get_openapi_schema(self, *a, **kw):
        schema = super().get_openapi_schema(*a, **kw)
        schema["paths"]["/foo/bar"]["get"][xxx] = yyy
        return schema

api = MyApi()

@igoose1
Copy link
Contributor Author

igoose1 commented Jul 8, 2021 via email

@stephenrauch
Copy link
Contributor

@igoose1 What do you view as the concern / difference between overriding get_openapi_schema() and instead offering a method for pre-registering some openapi schema to add?

@igoose1
Copy link
Contributor Author

igoose1 commented Aug 3, 2021 via email

@vitalik
Copy link
Owner

vitalik commented Aug 5, 2021

I like the idea. However, if I understand correctly, right way is to add parameters in every get/post/put/delete of NinjaAPI and Router... Then these parameters must be written in Operation. There are many parameters already and copy-pasting at least 2 lines of code 5 * 2 times doesn't look nice.

Hm... then I'm not sure I understand your problem correctly...
maybe you can mock the idea of your application structure ?

you just want extra documentation on OpenAPI/swagger ?

or want to add some common sets of get parameters to multiple operations at once ?

@igoose1
Copy link
Contributor Author

igoose1 commented Aug 5, 2021 via email

@igoose1
Copy link
Contributor Author

igoose1 commented Aug 5, 2021

Github doesn't understand code from mails so that's the whole formatted code in the last step:

import json

from ninja import Field, NinjaAPI, Query, Schema
from pydantic import validator

api = NinjaAPI()


class Padding(Schema):
    limit: int = 30
    offset: int = 0


class Filter(Schema):
    padding: str = Field(json.dumps(Padding().dict()))
    min_value: int

    @validator("padding")
    def convert_padding(cls, v):
        return Padding(**json.loads(v))


@api.get("/", response=None)
def f(request, filter: Filter = Query(...)):
    pass

@igoose1
Copy link
Contributor Author

igoose1 commented Aug 5, 2021

Oh, and I forgot to summarize. Yes, I want extra auto-generated documentation of Padding.

maybe you can mock the idea of your application structure ?

An example could be:

@api.get("/", request_parameters={Filter()}, response=None)
def f(request, filter: Filter = Query(...)):
    """
    That GET endpoint mentions `Padding` as that schema's in default `Filter()`.
    As GET request has no body `Padding` docs can be generated at least in "components" in openapi.json.
    """
    pass

That requires inspecting "children" of parameters recursively in order to find every schema. And it's not trivial. That's almost a total support of complex objects in Query.

The issue can be closed as "wontfix".

@igoose1
Copy link
Contributor Author

igoose1 commented Aug 5, 2021

And, well, I forgot the aim of the issue again :-/

As auto-generating Query with objects is difficult it's enough to implement api.add_custom_schema which would append a schema in "components" list.

@vitalik vitalik closed this as completed Sep 19, 2021
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

3 participants