axnsan12 / drf-yasg

Automated generation of real Swagger/OpenAPI 2.0 schemas from Django REST Framework code.
https://drf-yasg.readthedocs.io/en/stable/
Other
3.42k stars 439 forks source link

How to document path parameter defined in router url? #394

Open csdenboer opened 5 years ago

csdenboer commented 5 years ago

Is it possible to document a path parameter that is defined in an url, e.g. document 'currency' in url(r'^v1/object/(?P<currency>\busd\b|\beur\b|\bkrw\b)/$', View.as_view())? Is yes, how? Thanks!

AlexandreSi commented 5 years ago

Hi @csdenboer, This is the way I did it on a project of mine:

urls.py


urlpatterns = [
    ...,
    url(
        r'website/(?P<website_pk>[^/.]+)/product/(?P<product_id>[^/.]+)/reco/',
        get_similar_products_by_client_reference
    ),
]

search.py

@swagger_auto_schema(tags=["Recommendation"],
                     method="GET",
                     responses={"200": LightProductSerializer(many=True)},
                     manual_parameters=[
                         Parameter(name="product_id",
                                   required=True,
                                   type="string",
                                   in_="path",
                                   description="Client reference for the product",)
                     ])
@api_view(["GET"])
@permission_classes((ReadRecommendationPermission,))
def get_similar_products_by_client_reference(request, website_pk, product_id):
    ...

And this is what you get: image

daviwesley commented 4 years ago

it works like a charm!

abenartz commented 4 years ago

Is there any possibility to set the response with multiple serializers lists? For example when my view return the next response:

Response({ 'listA': ASerializer(many=True), 'listB':BSerializer(many=True), 'listC': CSerializer(many=True) })