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

Responses with Nested Serializers #823

Open
jamesstonehill opened this issue Nov 8, 2022 · 0 comments
Open

Responses with Nested Serializers #823

jamesstonehill opened this issue Nov 8, 2022 · 0 comments

Comments

@jamesstonehill
Copy link

Feature Request

I'd like to be able to nest serializers within the response documentation under string keys.

Description

In my api, all of our responses are nested under a "data" key like so:

{
  "data": {
    "first_name": "Foo",
    "email": "test@example.com"
  },
  "message": "User account successfully created!"
}

To achieve this response the routes return responses that look something like this...

        return Response(
            ResponseSerializer(
                dict(
                    data=UserSerializer(user).data,
                    message="User account successfully created!",
                )
            ).data,
            status=status.HTTP_200_OK,
        )

Describe the solution you'd like

I'd like to be able to do this:

USER_RESPONSES = {
    200: {
        "data": UserSerializer(),
        "message": "User account successfully created!"
    }
}

However that doesn't work, so I'm forced to manually write out all the attributes within the UserSerializer again within the documentation.

Describe alternatives you've considered

Are there any workarounds? I've investigated, but I haven't been able to find any good options for how to achieve this behaviour.

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

No branches or pull requests

1 participant