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

Allow specifying response as a reference #757

Conversation

vmax
Copy link
Contributor

@vmax vmax commented Nov 28, 2021

We're trying to support a usecase of defining responses in one place and then referencing them in the schema. This is useful for specifying generic errors (e.g. 500 is always "Server Error"). Examples from resulting OpenAPI YAML fragments:

responses:
  NotFound:
    description: "Requested object not found."
  ServerError:
    description: "Internal server error."

... 
paths:
  /example_resource/:
    get:
      # ... omitted for brevity
      responses:
        404:
          $ref: "#/responses/NotFound"
        500:
          $ref: "#/responses/ServerError"

drf_yasg custom code would look like this:

class ResponseRef(_Ref):
    def __init__(self, schema_name):
        super().__init__(None, schema_name, 'responses', None, ignore_unresolved=True)

@method_decorator(name='list', decorator=swagger_auto_schema(
    responses={
        404: ResponseRef('NotFound'),
        500: ResponseRef('ServerError'),
    },
))
class SomeViewSet(ListModelMixin, BaseApiViewSet):
    # ...

@vmax vmax force-pushed the allow-inserting-arbitrary-references branch from 7369f5c to 6670052 Compare January 6, 2022 09:42
@JoelLefkowitz JoelLefkowitz changed the base branch from master to 1.21.x July 17, 2022 16:37
@JoelLefkowitz JoelLefkowitz added enhancement 1.21.x Release target in 1.21.x labels Jul 17, 2022
@JoelLefkowitz JoelLefkowitz merged commit 6745ddb into axnsan12:1.21.x Jul 17, 2022
@vmax vmax deleted the allow-inserting-arbitrary-references branch July 17, 2022 17:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
1.21.x Release target in 1.21.x
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants