Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Trouble handling recursive definitions. #586

Closed
ngnpope opened this issue Oct 22, 2021 · 2 comments
Closed

Trouble handling recursive definitions. #586

ngnpope opened this issue Oct 22, 2021 · 2 comments
Labels
enhancement New feature or request fix confirmation pending issue has been fixed and confirmation from issue reporter is pending

Comments

@ngnpope
Copy link
Contributor

ngnpope commented Oct 22, 2021

djangorestframework-recursive provides a RecursiveField(), e.g.

    class TreeSerializer(serializers.Serializer):
        children = ListField(child=RecursiveField())

    class ListSerializer(serializers.Serializer):
        next = RecursiveField(allow_null=True)

I started thinking how to use OpenApiSerializerFieldExtension, but wasn't sure how to refer to the serializer/component that contains the field:

class RecursiveFieldFix(OpenApiSerializerFieldExtension):
    target_class = "rest_framework_recursive.fields.RecursiveField"
    priority = 1

    def map_serializer_field(self, auto_schema: AutoSchema, direction):
        # Somehow reference the serializer that contains this field. 

How might this be achieved?

@tfranzel
Copy link
Owner

Hey,

we do support recursion, but i achieved this manually before: https://github.com/tfranzel/drf-spectacular/blob/master/tests/test_recursion.py

had a quick look and by accident implemented it in a couple of minutes. 😄 usually we achieve this through self.target.parent, but somehow their magic prevents this from working. they introduce the proxied variable for that purpose. have a look at the commit. rather easy actually.

@tfranzel tfranzel added enhancement New feature or request fix confirmation pending issue has been fixed and confirmation from issue reporter is pending labels Oct 23, 2021
@ngnpope
Copy link
Contributor Author

ngnpope commented Nov 9, 2021

Sorry for the delay getting back to you. The fix seems to work nicely. Thanks! 😄

@ngnpope ngnpope closed this as completed Nov 9, 2021
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
enhancement New feature or request fix confirmation pending issue has been fixed and confirmation from issue reporter is pending
Projects
None yet
Development

No branches or pull requests

2 participants