cdimascio / express-openapi-validator

🦋 Auto-validates api requests, responses, and securities using ExpressJS and an OpenAPI 3.x specification
MIT License
919 stars 210 forks source link

express-openapi-validator and swagger-jsdoc? #687

Open tamis-laan opened 2 years ago

tamis-laan commented 2 years ago

Is it possible to combine swagger-jsdoc with express-openapi-validator?

The following code:

var schema = swaggerJsdoc( {
    swaggerDefinition: {
      openapi: '3.0.0',
      info: {
        title: 'Title',
          description: 'API',
          version: '1.0.0',
       },
       servers: [{url: '/'}]
    },
    apis: ['./src/**/*.js']
})

// OpenApi validation
app.use(OpenApiValidator.middleware({
  apiSpec: schema,
  validateRequests: true,
  validateResponses: false
}))

Gives the error:

error: openapi.validator: args.apiDoc was invalid.  See the output.
wparad commented 2 years ago

If swagger-jsdoc is compliant, then it should work if you JSON.stringify(schema) before passing it in.

tamis-laan commented 2 years ago

Didn't work:

error: openapi.validator: spec could not be read at {"openapi":"3.0.0","info":{"title":"Project ","description":"API","version":"1.0.0"},"servers........

Seams like it is interpreting the string as a file path rather then then a schema.

tamis-laan commented 2 years ago

As far as I can see my first example should work, you can pass an object as the apiSpec: https://github.com/cdimascio/express-openapi-validator#%EF%B8%8F-apispec-required

wparad commented 2 years ago

error: openapi.validator: args.apiDoc was invalid. See the output.

What does the output say?

tamis-laan commented 2 years ago

Looks like it works now, turns out I had an error in my schema.

This indicates that the schema is invalid:

error: openapi.validator: args.apiDoc was invalid. See the output.

What I don't understand is, if validation is done on the schema, why it does not tell me what is wrong with the schema.

This would be great for debugging.