Drf-yasg: Manual parameters to override fields from a serializer used in a modelviewset

Created on 15 Oct 2018  路  7Comments  路  Source: axnsan12/drf-yasg

Hi,

I have a situation where request and response value for a field in a serializer differ - in response it is a subserializer of a model, in request body it is the id (integer).
What would be a good approach to reflect this? Manual parameters don't work with IN_BODY parameters, and the only paramater of a viewsets methods that can be overriden is 'data' ....

Any ideas are welcome, tnx!

question

Most helpful comment

request_body=openapi.Schema(type=openapi.TYPE_OBJECT, properties={'id': openapi.Schema(type=openapi.TYPE_INTEGER)})

should work. Or, if you have a serializer for parsing the body, just

request_body=IdRequestSerializer

All 7 comments

request_body=openapi.Schema(type=openapi.TYPE_OBJECT, properties={'id': openapi.Schema(type=openapi.TYPE_INTEGER)})

should work. Or, if you have a serializer for parsing the body, just

request_body=IdRequestSerializer

I am using one single serializer. Ofc, I can define another one for parsing, but it would be an overkill to define new serializer just for documentation purposes.

This I already tried:

request_body=openapi.Schema(type=openapi.TYPE_OBJECT, properties={'id': openapi.Schema(type=openapi.TYPE_INTEGER)})

But the problem there is that other fields from the serializer are then not shown in the docs, only the 'id' :( I wanted to have something like "all the fields from the serializer but e.g. 'id' with custom schema" ...

Sorry for reopening the issue, if inappropriate...

other fields from the serializer are then not shown in the docs, only the 'id'

responses={200: YourSerialiser}

Should work in that case.

Yep, I added that and it fixed the problem for the response body, but request body schema is showing only the 'id' and no other fields from the serializer.

Maybe it is not possible to achieve what I want without two serializers?

Oh, now I see what you mean. I misunderstood initially.

What you're asking for is essentially a way to generate different OpenAPI schemas for the request and response bodies of a same operation, from a single serializer. This is in direct conflict with drf-yasg's model of a one-to-one mapping between Serializer and Schema, as discussed in #70, #136 .

Your options are basically:

  1. Have different read/write serializers and use them with request_body/responses (cleanest IMO)
  2. Write custom logic in SwaggerAutoSchema and/or SerializerInspectors to somehow differentiate between request/response and produce different Schemas.

Tnx mate, I appreciate your help!
I will go with option 1 and later on try to experiment with SerializerInspector and maybe even file a PR if something generic enough comes out of it.

Cheers.

What to do while using custom request_body for choice_fields

Was this page helpful?
0 / 5 - 0 ratings

Related issues

hnykda picture hnykda  路  3Comments

csdenboer picture csdenboer  路  3Comments

mkurnikov picture mkurnikov  路  3Comments

phihag picture phihag  路  5Comments

agethecoolguy picture agethecoolguy  路  4Comments