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

Circular references for swagger_serializer_method #628

Open vchepurko opened 4 years ago

vchepurko commented 4 years ago

Is it a way to create circular references to generate open API schema using rest_framework.serializers?

To build tree structure Serializer needs to reference for self.

class ItemSerializer(Serializer):
    item = serializers.SerializerMethodField()

    class Meta:
        swagger_schema_fields = item_schema

    @swagger_serializer_method(serializer_or_field=ItemSerializer)
    def get_item(self, item):
        return ItemSerializer(item).data

    def to_representation(self, item):
        return {
            "items": [self.get_item(item) for item in item.items]
        }

class TreeSerializer(Serializer):
    item = serializers.SerializerMethodField()

    class Meta:
        swagger_schema_fields = tree_schema

    @swagger_serializer_method(serializer_or_field=ItemSerializer)
    def get_item(self, data):
        return ItemSerializer(data, markdown_to_html=self.markdown_to_html).data

    def to_representation(self, data):
        return {
            "items": [self.get_item(item) for item in data]
        }

It is incorrect code, is it exists how to correctly create tree structure for swagger?

citos88 commented 2 years ago

I have similar problem. Has anyone found a solution?