elysiajs / elysia-swagger

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

Fix: Swagger Component Integration #14

Closed igrschmidt closed 1 year ago

igrschmidt commented 1 year ago

This pull request addresses an issue where the Swagger plugin for ElysiaJS was not effectively merging custom components provided at the application level.

Example of the problem:

export const swaggerConfig: ElysiaSwaggerConfig = {
  documentation: {
    info: {
      title: "Endpoint Documentation",
      version: "1.0.0",
      description: "A very good description.",
    },
    components: {
      schemas: {
        User: {
          description: "string",
        },
      },
      securitySchemes: {
        JwtAuth: {
          type: "http",
          scheme: "bearer",
          bearerFormat: "JWT",
          description: "Enter JWT Bearer token **_only_**",
        },
      },
    },
  },
};
``;

Would result on a /swagger/json like this:

{
  "openapi": "3.0.3",
  "info": {
    "title": "Endpoint Documentation",
    "description": "A very good description.",
    "version": "1.0.0"
  },
  "components": {
    "schemas": {
    } ❌  Wheres our schema?
    ❌  Where's our securitySchemes?
  },
  "paths": {
  // rest of json
  },

With this fix, the plugin now correctly merges the user-defined components from the documentation configuration with the default schemas from app.meta.defs. This ensures that all user-defined Swagger components are accurately reflected in the generated documentation.

{
  "openapi": "3.0.3",
  "info": {
    "title": "Endpoint Documentation",
    "description": "A very good description.",
    "version": "1.0.0"
  "components": {
    "schemas": { ✅  Schema is present
      "User": {
        "description": "string"
      }
    },
    "securitySchemes": { ✅  securitySchemes is present
      "JwtAuth": {
        "type": "http",
        "scheme": "bearer",
        "bearerFormat": "JWT",
        "description": "Enter JWT Bearer token **_only_**"
      }
    }
  },
  "paths": {
  // rest of json
  },
SaltyAom commented 1 year ago

Got it, it seems like this like I forgot user provided schema. Thanks a lot!