Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 422
carlos de la morena Avatar asked Dec 13 '25 10:12

carlos de la morena


1 Answers

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: enter image description here

like image 148
Pedro Madureira Avatar answered Dec 15 '25 22:12

Pedro Madureira