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.41k stars 438 forks source link

Support read_only nested serializer fields (use allOf to improve schema generation) #343

Open bendavis78 opened 5 years ago

bendavis78 commented 5 years ago

Serializer fields that point to other serializers and are read_only should be marked as readOnly in the schema.

For example, I have an AccountSerializer with a field called user that points to UserSerializer, and is read_only. The schema definition for the user field is generated as:

{
    "$ref": "#/definitions/User"
}

Instead it should be:

{
    "readOnly": true,
    "allOf": [
        {"$ref": "#/definitions/User"}
    ]
}
axnsan12 commented 5 years ago

This is indeed something that might need to be addressed. However I believe there are lots of tools which get confused by allOf constructs, so I think this would need to sit behind a feature flag.

I think there might also be other places where we could use allOf to improve detailing of schemas.

hedleyroos commented 5 years ago

@bendavis78 removing the line https://github.com/axnsan12/drf-yasg/blob/master/src/drf_yasg/inspectors/field.py#L136 respects the read_only value of the nested serializer. @axnsan12 can you perhaps explain how this works? The comment at https://github.com/axnsan12/drf-yasg/blob/master/src/drf_yasg/openapi.py#L504 does not fully explain why the property is popped.

axnsan12 commented 5 years ago

@hedleyroos because readOnly is not valid when a Schema is defined in a definitions section. It is only valid when it is a direct child of another Schema via properties. The proper way to use readOnly with references is already outlined in the OP.

MeRuslan commented 4 years ago

Soooo, whats' going on in here? Is it going to ever happen?

Arti3DPlayer commented 4 years ago

same issue +1

Schwankenson commented 3 years ago

Same issue: +1

NurElHuda commented 3 years ago

Same issue: +1

athish-t commented 3 years ago

Any updates on this?

Schwankenson commented 3 years ago

I think we have to update to https://github.com/tfranzel/drf-spectacular to get this feature...looks like a lot of work.