drf-spectacular | flexible OpenAPI 3 schema generation for Django REST | REST library
kandi X-RAY | drf-spectacular Summary
kandi X-RAY | drf-spectacular Summary
Sane and flexible OpenAPI 3 schema generation for Django REST framework.
Support
Quality
Security
License
Reuse
Top functions reviewed by kandi - BETA
- Postprocess schema enums
- Log a warning
- Emit a message
- Get the override value for a property
- Map serializer to serializer
- Return the name of the serializer
- Force a serializer instance
- Filter the supported arguments
- Render the given schema
- Return a JSON response
- Create a lazy serializer
- Maps the serializer to a mapping
- Check the schema
- Handle GET requests
- Builds a GeoFeatureModelSerializerSerializer
- Get API endpoints
- Load the target class
- Returns the serializer for the given auto_schema
- Return a serializer for the given auto_schema
- Returns a list of parameters for a given auto_schema
- Return a fixed view
- Handles GET request
- Replacement for view replacement
- Maps the serializer to the given direction
- Creates a replacement for SubscriptionSerializer
- Handle GET request
drf-spectacular Key Features
drf-spectacular Examples and Code Snippets
from drf_spectacular.utils import extend_schema, OpenApiParameter, inline_serializer
# ...
@extend_schema(
parameters=[
OpenApiParameter(name="callsign", required=True, type=str),
],
description="Get an APRS-IS passco
@extend_schema(summary="short summary")
@action(detail=True, methods=["GET"], url_name="command")
def command(self, request, pk) -> Union[Response, Http404]: # Retrieve Command
data = request.data
status = g
@extend_schema(
parameters=[
OpenApiParameter(name='callsign', description='',
required=True, type=str),
],
description='More descriptive text'
)
def get_passcode(request):
callsign = reque
class ProjectView(mixins.RetrieveModelMixin, mixins.CreateModelMixin, viewsets.GenericViewSet):
permission_classes = [permissions.IsAuthenticated, ]
serializer_class = ProjectSerializer
queryset = Project.objects.all()
d
class EmptyPayloadAPI(APIView):
@extend_schema(request=None, responses=EmptyPayloadResponseSerializer)
def post(self, request, *args, **kwargs):
# some actions
return Response(data={"detail": "Success"})
from drf_spectacular.utils import OpenApiResponse, extend_schema
@extend_schema(
description='creation description',
responses={
201: OpenApiResponse(response=int, description='creation with int response.'),
204:
Community Discussions
Trending Discussions on drf-spectacular
QUESTION
To give as much context as possible; I have two problems while using drf-spectacular
to build my API documentation;
With my default configuration, I cannot even load the documentation's page because I have a custom auth backend (using
firebase_admin 5.2.0
), the swagger (or ReDoc) are part of my REST API, therefore, would have to pass a valid token to load the page allocated in the documentation's endpoint (which is not possible from a browser).Second, and most importantly, I am not able to configure my custom firebase auth with
drf-spectacular
to implement a Swagger authentication method to execute against my API endpoints. I would be just fine having the possibility to add a token in the Swagger doc, do not need to have all Firebase auth credentials, URLs, and flow.
- api/urls.py
ANSWER
Answered 2022-Mar-27 at 23:01- By default spectacular uses
'SERVE_PERMISSIONS': ['rest_framework.permissions.AllowAny'],
which should allow opening the swagger page even if not authenticated. MaybeFirebaseBackend
bails hard which prevents you ever getting toAllowAny
. Try this to take Firebase out of the equation for the schema views:
QUESTION
Using DRF's built-in way of documenting the API, I was able to write a docstring like the following and each action was documented by its corresponding line:
...ANSWER
Answered 2022-Feb-01 at 16:06I have just found out that the library provides very in-depth documentation functionality via their decorators. Without overriding any of the ViewSet methods, the above docstring could be written as:
QUESTION
So,
I'm documenting the following piece of code using drf-spectacular
:
ANSWER
Answered 2022-Jan-01 at 18:40If you don't have a Serializer
, you can use inline_serializer
:
QUESTION
hi I have a @extend_schema of drf_spectacular library in my code I need to use it over my @action to customize the detail in OpenAPI, but I get errors like that
...ANSWER
Answered 2022-Jan-03 at 01:55I fixed this problem by adding more @extend_schema to other extra endpoints, if did not that you get an error like that I take
QUESTION
I implemented a modification of the to_representation
method of a serializer in order to flatten a nested relation through
ANSWER
Answered 2021-Dec-28 at 23:42You can try to define fields used in GeneralInformationSerializer
and use source
property eg.:
QUESTION
I'm using djangorestframework
together with drf-spectacular
modules for a Django project, and I'm trying to build some basic API methods for my Project
model. Its structure looks like this:
ANSWER
Answered 2021-Nov-27 at 18:45Swagger Grabs the fields from a serializer_class variable. I really recommend you change the format to Class-Based Views. Something using mixins or generic class.
Your view could be like
QUESTION
if there are more than one fields with relations to other models and depth>=1 is specified on the serializer Meta class, in the example generated for the corresponding url, all of the relation fields have the same object as their value, for example:
model:
...ANSWER
Answered 2021-Nov-24 at 12:21This is from the DRF documentation:
The depth option should be set to an integer value that indicates the depth of relationships that should be traversed before reverting to a flat representation. If you want to customize the way the serialization is done you'll need to define the field yourself.
The problem is that drf-spectacular has a hard time knowing how the nested fields should be represented beforehand, since there is no "explicit" serializer.
I believe when depth
is used, DRF simply passes nested data through a on-the-fly ModelSerializer
and then does json.dump
.
Your second OrderSerializer
is the preferred way. It gives you more guarantees for you data exposure, and as an added benefit is parse-able by spectacular.
TL;DR: depth
is not supported at the moment. If you think this should be supported, open a github issue.
QUESTION
We have a bunch of api's with different versions in urls.py, eg
- api/v1
- api/v2
- api/v3
. We want to implement swagger with drf-spectacular, but we only want to expose api/v3 endpoints.
Is there a way to do this? I cannot make sense of the documentation.
Thanks
...ANSWER
Answered 2021-Aug-27 at 18:45Worked it out. Copied the custom preprocessing hook function, changed the pass statement to filter what I did not require in the endpoints, then mapped the location of the file and function in my spectacular settings for preprocessed hooks.
QUESTION
Can I create an custom description of JSON headers in drf_spectacular without using serializers class in @extend_schema decorator?
...ANSWER
Answered 2021-Nov-01 at 15:53You could do this with inline_serializer:
QUESTION
I am using drf-spectacular to generate an OpenAPI schema for django. As I am not using serializers, I am defining everything in the extend_schema
decorator. Now my question is, if it is possible to manually define a component schema.
Here is an example what my api view looks like:
...ANSWER
Answered 2021-Nov-01 at 15:25OpenApiTypes.OBJECT
means that the response object can have any amount of fields. Of course Swagger UI cannot know those fields in advance and thus it shows {}
, which may be unintuitive but it is correct.
What you want is to give your response some structure. Everything in spectacular revolves around serializers/components. If you don't want to have explicit serializers, you can create implicit ones with inline_serializer
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install drf-spectacular
You can use drf-spectacular like any standard Python library. You will need to make sure that you have a development environment consisting of a Python distribution including header files, a compiler, pip, and git installed. Make sure that your pip, setuptools, and wheel are up to date. When using pip it is generally recommended to install packages in a virtual environment to avoid changes to the system.
Support
Reuse Trending Solutions
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items
Find more librariesStay Updated
Subscribe to our newsletter for trending solutions and developer bootcamps
Share this Page