scottie1984 / swagger-ui-express

Adds middleware to your express app to serve the Swagger UI bound to your Swagger document. This acts as living documentation for your API hosted from within your app.
MIT License
1.43k stars 228 forks source link

$ref: must be a string (JSON-Ref) #317

Closed AndreiSoroka closed 2 years ago

AndreiSoroka commented 2 years ago

Hi! how to fix:

openapi: "3.0.1"
info:
  title: "test"
  version: "1.0.0"
servers:
  - url: "/"
components:
  schemas:
    Sort:
      type: string
      enum:
        - ASC
        - DESC
image
Resolver error at components.schemas.Sort.$parsed.$ref
$ref: must be a string (JSON-Ref)
// example
let swaggerDoc = yaml2json.load(readFileSync('./doc.yaml', 'utf8'));
// ...
app.use(path, swaggerUi.serve, swaggerUi.setup(swaggerDoc));
AndreiSoroka commented 2 years ago

Find reason, problem with swagger-typescript-api

// ...
import swaggerUi from 'swagger-ui-express';
import sta from 'swagger-typescript-api';

const swaggerSpec = swaggerJsdoc({
  definition: {
    openapi: '3.0.1',
    info: {
      title: 'Rewards',
      version: '1.0.0',
    },
    servers: [{url: "/"}],
  },
  apis: ['./routes/*.mjs'],
});

let swaggerComponents = yaml2json.load(readFileSync('./components.yaml', 'utf8'));
export const swaggerDoc = { ...swaggerSpec, ...swaggerComponents };

// ...
// generate TS
const __dirname = new URL('.', import.meta.url).pathname;
sta.generateApi({
  name: "Api.ts",
  output: __dirname+'./build/',
  spec: JSON.parse(JSON.stringify(swaggerDoc)), // <-- it is fix, ERROR was: generateApi modified object
})
// ...

export function addSwagger(path, app){
  app.use(path, swaggerUi.serve, swaggerUi.setup(swaggerDoc));
  // ...
}

FYI: @acacode