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

Support adding response examples when using $ref #2260

Closed
1 task done
djbb7 opened this issue Jan 30, 2023 · 3 comments
Closed
1 task done

Support adding response examples when using $ref #2260

djbb7 opened this issue Jan 30, 2023 · 3 comments
Labels

Comments

@djbb7
Copy link

djbb7 commented Jan 30, 2023

Is there an existing issue that is already proposing this?

  • I have searched the existing issues

Is your feature request related to a problem? Please describe it

When using $ref as the response type of an endpoint, the other properties of the decorator (such as examples) are ignored. According to the openapi 3.0 spec:

If schema refers to some object defined in the components section, then you should make example a child of the media type keyword. This is needed because $ref overwrites all the siblings alongside it.

With the current implementation of this library it is only possible to add examples under schema

My use case is defining a general Error response DTO, and adding specific examples for each response code.

@ApiForbiddenResponse(
{
	description: 'Unauthorized.',
	type: HttpExceptionDTO,
	schema: {
		example: {
			message: 'Unauthorized',
			status: 403
		}
	}
)
@ApiNotFoundResponse(
	{
	description: 'Unauthorized.',
	type: HttpExceptionDTO,
	schema: {
		example: {
			message: 'Unauthorized',
			status: 404
		}
	}
)

Describe the solution you'd like

The solution would be to support defining examples/example at the top-level of the decorator rather than nested under schema.

{
	description: 'Unauthorized.',
	type: HttpExceptionDTO,
	example: {
		message: 'Unauthorized',
		status: 403
	}
)
@ApiNotFoundResponse(
	{
	description: 'Unauthorized.',
	type: HttpExceptionDTO,
	example: {
		message: 'Unauthorized',
		status: 404
	}
)

Teachability, documentation, adoption, migration strategy

No response

What is the motivation / use case for changing the behavior?

To support adding examples alongside $ref.

@djbb7 djbb7 added the feature label Jan 30, 2023
@kamilmysliwiec
Copy link
Member

Would you like to create a PR for this?

@alexproca
Copy link

I also have a similar problem. I miss the example functionality on @ApiOkResponse().

My workaround is:

    @ApiOkResponse({
      content: {
        'application/json': {
          schema: {
            type: 'array',
            items: {
              $ref: '#/components/schemas/CatDTO',
            }
          },
          example: [{
            name: 'Tom'
          }]
        }
      }
    })

But I would like to write it like this:

@ApiOkResponse({
      type: CatDTO,
      isArray: true,
      example: [{
        name: 'Tom'
      }]
    })

And the renderer would use my example and not try to infer the example based on the CatDTO. The practical usage for this Is that I have a fairly big DTO with a lot of optional fields and the swagger UI freezes trying to generate an example.

@lit26 lit26 mentioned this issue Mar 11, 2024
12 tasks
@shaunhurryup
Copy link

@alexproca Thanks bro! Other than a little verbose, that's fine 👍

My workaround is:

    @ApiOkResponse({
      content: {
        'application/json': {
          schema: {
            type: 'array',
            items: {
              $ref: '#/components/schemas/CatDTO',
            }
          },
          example: [{
            name: 'Tom'
          }]
        }
      }
    })

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants