ErikWittern / openapi-snippet

Generates code snippets for given Swagger / Open API documents
MIT License
116 stars 67 forks source link

swagger-to-har.js does not support components.securitySchemes of OpenAPI 3 #20

Closed jens-duttke closed 5 years ago

jens-duttke commented 5 years ago

If I try to import an OpenAPI 3 file with

{
    ...

    "security": [
                {
                        "bearerAuth": []
                }
        ],

    ...

    "components: {
        ...
        "securitySchemes": {
            "bearerAuth": {
            "type": "http",
            "scheme": "bearer",
            "bearerFormat": "JWT"
        }
    }
}

I get the error:

TypeError: Cannot read property 'bearerAuth' of undefined
    at getHeadersArray (\node_modules\swagger-snippet\swagger-to-har.js:253:56)
    at createHar (\node_modules\swagger-snippet\swagger-to-har.js:43:14)
    at Object.swaggerToHarList [as getAll] (\node_modules\swagger-snippet\swagger-to-har.js:306:19)
    at Object.getSwaggerSnippets \node_modules\swagger-snippet\index.js:44:30)

The problem is, that for OpenAPI 3, the securityDefinitions has been moved to components.securitySchemes.

A workaround is, doing:

api.securityDefinitions = api.components.securitySchemes;

before calling SwaggerSnippet.getSwaggerSnippets(), but a solution implemented in the package would be a better.

ErikWittern commented 5 years ago

@jens-duttke Thanks for filing this issue. In line 253, it looks like the lookup of bearerAuth is actually performed in components.securitySchemes, but your error indicates that lookup fails, despite the OpenAPI file containing a corresponding entry. Can you maybe try to provide some more details on the content of swagger.components.securitySchemes at that point in the code?

jens-duttke commented 5 years ago

@ErikWittern I've just realized that your version on npm does not match to the code on Github.

I've overwritten the code provided via npm by the files from Github, and now it works without problems.

Maybe you should consider to update your npm package.