express-openapi-validator icon indicating copy to clipboard operation
express-openapi-validator copied to clipboard

express-openapi-validator and swagger-jsdoc?

Open tamis-laan opened this issue 3 years ago • 5 comments

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.

tamis-laan avatar Dec 31 '21 13:12 tamis-laan

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

wparad avatar Dec 31 '21 14:12 wparad

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 avatar Dec 31 '21 15:12 tamis-laan

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

tamis-laan avatar Dec 31 '21 15:12 tamis-laan

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

What does the output say?

wparad avatar Dec 31 '21 15:12 wparad

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.

tamis-laan avatar Dec 31 '21 18:12 tamis-laan