elysiajs / elysia-swagger

A plugin for Elysia to auto-generate Swagger page
MIT License
84 stars 45 forks source link

feat: handle nullish response types #19

Closed LeandroPereiraDaCruz closed 1 year ago

LeandroPereiraDaCruz commented 1 year ago

This merge request addresses an issue that was wrongly opened in the elysia repository: https://github.com/elysiajs/elysia/issues/124

Using responses types like t.Void(), t.Undefined() or t.Null() means that in fact there will be no content in the response. It could happen in a scenario of status code 204, when no respose is expected.

But current behaviour is breaking the view, since it generates a response with a content that is not acceptable by swagger:

An example: Code:

{
    body: "ActivationUserRequest",
    response: {
      200: t.Void({
        description: "User account was activated successfully",
      }),
      404: t.Void({
        description: "Pending account confirmation was not found",
      }),
      409: t.Void({
        description: "Account was already confirmed",
      }),
    },
    detail: {
      description:
        "Activate a user account based on previous secret that was sent by email",
      tags: ["User"],
      summary: "Activate a user account",
    },
  }

Screen with current behavior: image Screen with desirable behaviour: image

brunoeduardodev commented 1 year ago

hmm, when I'm working with values that can be string | null for example, I usually create an union between t.Null() and t.String()

t.Union([t.Null(), t.String()])

but having a helper package to do it would be very helpful