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

fix: Store all Response examples by name #1571

Merged
merged 4 commits into from
Mar 17, 2024
Merged

Conversation

MiguelSavignano
Copy link
Contributor

@MiguelSavignano MiguelSavignano commented Mar 2, 2024

Allow use multiple times the @Response decorator with the same status and different examples.

expected result:

Controller with @Response status 400 and 2 response examples.

  @Response<ErrorResponseModel>('400', 'Bad Request', { status: 400, message: 'reason 1' })
  @Response<ErrorResponseModel>('400', 'Bad Request', { status: 400, message: 'reason 2' })
  @Response<ErrorResponseModel>('401', 'Unauthorized')
  @Response<ErrorResponseModel>('default', 'Unexpected error', { status: 500, message: 'Something went wrong!' })
  @Get('MultiResponse')
  public async multiResponse(): Promise<TestModel> {
    return new ModelService().getModel();
  }

Swagger.json:

"responses": {
  "400": {
    "description": "Bad Request",
    "content": {
      "application/json": {
        "schema": {},
        "examples": {
          "Example 1": {
            "value": {
              "status": 400,
              "message": "reason 1"
            }
          },
          "Example 2": {
            "value": {
              "status": 400,
              "message": "reason 2"
            }
          }
        }
      }
    }
  }
}

All Submissions:

  • Have you followed the guidelines in our Contributing document?
  • Have you checked to ensure there aren't other open Pull Requests for the same update/change?
  • Have you written unit tests?
  • Have you written unit tests that cover the negative cases (i.e.: if bad data is submitted, does the library respond properly)?
  • This PR is associated with an existing issue?

Closing issues

Closes #1570

If this is a new feature submission:

  • Has the issue had a maintainer respond to the issue and clarify that the feature is something that aligns with the goals and philosophy of the project?

Potential Problems With The Approach

Test plan

Allow use multiple times the @response decorator with the same status and different examples.
Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello there MiguelSavignano 👋

Thank you and congrats 🎉 for opening your first PR on this project.✨

We will review the following PR soon! 👀

@MiguelSavignano MiguelSavignano marked this pull request as ready for review March 9, 2024 16:33
@WoH WoH changed the title Store all Response examples by name fix: Store all Response examples by name Mar 17, 2024
@WoH WoH merged commit 8c8b3b6 into lukeautry:master Mar 17, 2024
18 checks passed
@WoH
Copy link
Collaborator

WoH commented Mar 17, 2024

LGTM, thanks!

louis-amhild pushed a commit to louis-amhild/tsoa-deno that referenced this pull request Mar 27, 2024
* Store all Response examples by name

Allow use multiple times the @response decorator with the same status and different examples.

* type Record

* reanme responseExamplesByName

* test multiple response same status code
@mishelen
Copy link

mishelen commented Apr 14, 2024

hey @WoH
I'm using tsoa@6.2.0 for openapi3.x and seems that this PR is messing up some data

Здымак экрана ад 2024-04-14 16-48-26

then in the spec it overwrites MIME types:

Здымак экрана ад 2024-04-14 16-50-11

So, there is only available single type text/plain, obviously, as generated section for 400 looks this way, so it also breaks schema for response!

{
  "400": {
    "description": "Bad Request",
    "content": {
      "text/plain": {
        "schema": {
          "type": "string"
        },
        "examples": {
          "Example 1": {
            "value": {
              "message": "Validation Error",
              "details": {
                "body.sessionId": {
                  "message": "'sessionId' is required"
                }
              }
            }
          },
          "Example 2": {
            "value": "Unsupported data provided"
          },
          "Example 3": {
            "value": "Session already exists"
          }
        }
      }
    }
  }
}

Update:
When I remove produces param, then all responses are marked as application/json
So in that cases it ignores generic type provided to decorator:

  @Response<ValidationError>(400, ReasonPhrases.BAD_REQUEST, ValidationErrorSample)
  @Response<string>(400, ReasonPhrases.BAD_REQUEST, 'Unsupported data provided')
  @Response<string>(400, ReasonPhrases.BAD_REQUEST, 'Session already exists')

Produced spec has also broken schema -- just string:

{
  "400": {
    "description": "Bad Request",
    "content": {
      "application/json": {
        "schema": {
          // it's not just a string
          "type": "string"
        },
        "examples": {
          "Example 1": {
            "value": {
              "message": "Validation Error",
              "details": {
                "fieldName": {
                  "message": "Description of the specific error"
                }
              }
            }
          },
          "Example 2": {
            "value": "Unsupported data provided"
          },
          "Example 3": {
            "value": "Session already exists"
          }
        }
      }
    }
  }
}

And then if I remove generic type and add produces --produces is ignored at all.

Ideally I want to get MIME type from Response generic type, to not provide produces, while produces should be used as last-ditch effort when defining MIME type.

Possible problem:

  • openapi 2x doesn't support multiply schemas per response code, schemas could be provided as samples.
  • openapi 3x -- does it via oneOf.

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.

Multiple Examples with @Response Decorator not possible anymore
3 participants