简体   繁体   中英

how to specify example value on drf-yasg swagger_auto_schema request_body?

I everyone. My question is similar to the one planted here , but in this case I would like to do the same with the request_body parameter. How can I add an example value for the request_body parameter, similary as it is done there with the response parameter? Thanks for your attention and your help

You should use the example parameter. Look at this example:

request_schema_dict = openapi.Schema(
    title=_("Update order"),
    type=openapi.TYPE_OBJECT,
    properties={
        'ordered_items': openapi.Schema(type=openapi.TYPE_ARRAY, description=_('Ordered items list'), 
            items=openapi.Schema(type=openapi.TYPE_OBJECT, description=_('Ordered item'),
                properties={
                    'item': openapi.Schema(type=openapi.TYPE_STRING, description=_('Item id'), example="123*123*0001"),
                    'quantity': openapi.Schema(type=openapi.TYPE_NUMBER, description=_('Ordered quantity'), example=12.33),
                    'sequence_number': openapi.Schema(type=openapi.TYPE_INTEGER, 
                    description=_('Sequence of item inclusion in the order.'), example=1),
                    }
            )
        ),
        'status': openapi.Schema(type=openapi.TYPE_STRING, description=_('Order status'), example=1, enum=[0,1,2,3,4,5]),
        'invoicing_date': openapi.Schema(type=openapi.TYPE_STRING, description=_('Invoice date'), 
        example="2022-05-27T12:48:07.256Z", format="YYYY-MM-DD HH:MM[:ss[.uuuuuu]][TZ]"),
        'invoice_number': openapi.Schema(type=openapi.TYPE_STRING, description=_('Invoice number'), example="123456789"),
        'note': openapi.Schema(type=openapi.TYPE_STRING, description=_('Client user note'), example=_("Client user note")),
        'agent_note': openapi.Schema(type=openapi.TYPE_STRING, description=_('Agent note'), example=_("Agent note")),
    }
)

@swagger_auto_schema(request_body=request_schema_dict, responses={200: 'Order updated.'}) 
def put(self, request, id):
..............

This will generate the following output on swagger: 在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM