Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

swagger_extra_fields should be allowed even when we don't call get_schema #142

Merged
merged 7 commits into from
Jun 16, 2018

Conversation

andrewyager
Copy link
Contributor

It appears that it is possible to write a Schema Definition for use with a SchemaRef without creating the object through get_schema. The side effect of this is that if you have specified schema_extra_fields with parameters you care about (such as examples) they never get set.

This patch adds support for custom fields even when building a SchemaRef because it is possible for field_to_swagger_object to get called without calling get_schema. The below example illustrates a case where this happens.

Example:

class TestObj(object):
    foobar = None
    barfoo = None

    def __init__(self, **kwargs):
        setattr(self, 'foobar', kwargs.get('foobar'))
        setattr(self, 'barfoo', kwargs.get('barfoo'))


class TestSerializer(serializers.Serializer):
    barfoo = serializers.CharField(max_length=50)
    foobar = serializers.CharField(max_length=300)

    def create(self, data):
        return TestObj(**data)

    class Meta:
        swagger_schema_fields = {
            'example': {
                'barfoo': '1234',
                'foobar': '456'
            }
        }


class TestView(viewsets.ViewSet):
    """
    Test example view
    """

    permission_classes = [permissions.IsAuthenticated]
    lookup_field = 'address_partial'

    @swagger_auto_schema(
        operation_id='testview_get',
        description='#TestSerializer Example',
        responses={
            '200': TestSerializer(many=True)
        },
        permission_classes=[permissions.IsAuthenticated])
    def retrieve(self, request, pk=None, address_partial=None):
        response = {
            'hits': [
                {
                  'barfoo': 'asdf',
                  'foobar': 'foobar'
                }
            ]
        }
        serializer = TestSerializer(
            instance=response['hits'],
            many=True)
        return Response(serializer.data, status=status.HTTP_200_OK)

Without the patch, this will omit the example additional field. With the patch, it won't.

@andrewyager
Copy link
Contributor Author

(In my defence, the build was failing before this PR 😄)

@axnsan12 axnsan12 force-pushed the features/schemaref-extra-fields branch from a56ff15 to a96bcf5 Compare June 16, 2018 13:31
@axnsan12 axnsan12 merged commit 4825ec7 into axnsan12:master Jun 16, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants