-
Notifications
You must be signed in to change notification settings - Fork 271
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
function based views @api_view and request.POST (form based) parameters #279
Comments
Is OpenApiViewExtension a way to handle this with @api_view ? Can someone give an idea how to work this out ? Thanks. https://drf-spectacular.readthedocs.io/en/latest/customization.html# |
hi @axilaris you refer to 2 different things.
the customization doc is quite thorough and the tests cover pretty much all use-cases. |
@tfranzel Thank you very much, I think this is very helpful and the most straightforward migration way. How can I find out more information on configuring the the parameter types in OpenApiParameter ? In my case, I'd like to have an input parameter as a string of |
spectacular is mostly self-explanatory given some basic knowledge about DRF and OpenApi3. There is also the documentation, and finally the tests describe every supported feature. https://drf-spectacular.readthedocs.io/en/latest/customization.html#workflow-schema-customization
this sounds not like a parameter but a request body. however, if that is what you actually want there are ways of encoding parameters with |
Im sorry for my ignorance. with drf_yasg, I can achieve the form based parameters in this way.
as for drf-spectacular, I manage to create swagger in this way but i am not clear where to define in_form parameter
Could you provide some guidance ? I'm still interested to get drf-spectacular work. Thanks. |
i think i now get the confusion. this is even discouraged in yasg: https://stackoverflow.com/questions/62930918/drf-yasg-swagger-setup-parameters-in-body-is-not-worked this is likely what you want and is pretty much identical to yasg usage: @extend_schema(request=OpenApiTypes.STR, responses=OpenApiTypes.STR)
@api_view(['POST'])
def test_token(request):
... it you are mainly after |
I tried this (like you suggest):
well, its pretty close but i guess its missing
this is what it is generating now with drf-spectacular:
I am looking to generate this:
Based on what you wrote, can you confirm drf-spectacular does not support this type of swagger document generation ? Thanks |
drf-spectacular supports this in general, just not this particular override case. |
|
Hello, I have been digging quite a bit on how to provide a minimal POST json data API with specs and hitting the same question (despite all the great documentation and content, sorry if it is obvious). Is there a way to extend_schema with a Request Body Object? (that way we could provide the schema of the body via the 'content' similarly to the examples in https://swagger.io/specification/#request-body-object. Or I am mistaken and there is no way to actually provide a schema for the request body) Or, by design should the schema of the json body be defined by a serializer and provided to the extend_schema? A compromise might also be to drop trying to type the json body and provide an OpenApiExample with a json string instead.
I saw nowadays many API not using parameters anymore but json body instead, e.g. Thanks! |
hi, you cannot explicitly replace request body object, but you can get very close with this (1&2): @extend_schema(
examples=[
OpenApiExample('Serializer C Example RO',value={"field": 111})
],
responses={
(200, 'application/json'): OpenApiTypes.OBJECT, # or anything else
}
request={
'application/json': OpenApiTypes.OBJECT
},
) you can also manually specify a schema. there several ways to override the response and raw dict is one of them. you have complete freedom there (2): drf-spectacular/tests/test_extend_schema.py Lines 160 to 186 in 6f12e8d
of course the more manual you go, you alos loose a lot of benefits. i hope this answers your question |
Thanks a lot @tfranzel ! |
Hey @tfranzel. First, thanks for the great library :) Have been trying for days now to get this manual override to work without luck. My initial idea was to do like this:
But this does nothing. It just fails silently without telling me whats wrong and the endpoint ends up having its normal description, request etc, without any of the data specified in the extended schema annotation. Next thing i tried was to specify the response and the request body directly in the extended schema annotation but this also did nothing. So:
@extend_schema( |
@AndersJensenBikemap, i moved your question to #421 as it looks orthogonal to this issue. |
To specify the body the approach of:
|
@xavierrigau, the structure has to come from somewhere. you don't need to create serializers for one-off usages. do this instead: @extend_schema(
request=inline_serializer(
name='InlineOneOffSerializer',
fields={
'long_url': serializers.URLField(),
}
)
)
# or with explicit content type override (usually not needed)
@extend_schema(
request={
'application/json': inline_serializer(...),
}
)
we have a very consistent API for this. you have to let go of your old drf-yasg patterns to make full use of spectacular. |
Is it possible to create a serializer from a json schema string? Danke schön |
I am going through a migration process from 1.11 to 3.1.5. And I found out drf-spectacular may provide a way to do this API migration.
I'ved got the question posted here in detail:
https://stackoverflow.com/questions/65918969/django-rest-framework-custom-post-url-endpoints-with-defined-parameter-request?noredirect=1#comment116622930_65918969
Basically, I'd like to find a way to create REST API using drf-spectacular that is equivalent to this:
url.py
url(r'^api/test_token$', api.test_token, name='test_token'),
api.py
In the documentation, there is mentioned how to handle @api_view
https://drf-spectacular.readthedocs.io/en/latest/customization.html#
Many libraries use @api_view or APIView instead of ViewSet or GenericAPIView. In those cases, introspection has very little to work with. The purpose of this extension is to augment or switch out the encountered view (only for schema generation). Simply extending the discovered class class Fixed(self.target_class) with a queryset or serializer_class attribute will often solve most issues.
I am not sure how its done, could someone give some guidance how to get this to work ? Thanks.
The text was updated successfully, but these errors were encountered: