7

I am using the OpenApi 3.0 specification to document my API. It is basically a REST API which requires a valid Bearer token for each request except for the login request. The OpenAPI spec looks like this:

---
openapi: 3.0.0
info:
  title: MyTitle
  contact:
    email: mymail@mail.com
  version: 0.0.1
servers:
- url: http://localhost/api/v1
components:
  schemas:
    ...
  securitySchemes:
    bearerAuth:
      type: http
      bearerFormat: JWT
      scheme: bearer
security:
- bearerAuth: []

In the automatically generated swagger-ui that works perfectly fine, the same is true for postman when I download the OpenAPI spec and import it into Postman.

However, when using the openapi-generator using the following command:

docker run --user `id -u`:`id -g` --rm -v ${PWD}:/local openapitools/openapi-generator-cli generate -i /local/api.json -g python -o /local/api

However, instead of the bearer token, it tries to use username and password instead:

def get_basic_auth_token(self):
    """Gets HTTP basic authentication header (string).

    :return: The token for basic HTTP authentication.
    """
    return urllib3.util.make_headers(
        basic_auth=self.username + ':' + self.password
    ).get('authorization')

def auth_settings(self):
    """Gets Auth Settings dict for api client.

    :return: The Auth Settings information dict.
    """
    return {
        'bearerAuth':
            {
                'type': 'basic',
                'in': 'header',
                'key': 'Authorization',
                'value': self.get_basic_auth_token()
            },

    }

Note that here a username and password is used, and the auth_settings() function returns a basic auth. What do I have to do to use the Bearer authentication scheme in the generated code instead?

Daniel
  • 1,398
  • 4
  • 20
  • 47
  • 1
    It's a bug: https://github.com/OpenAPITools/openapi-generator/issues/1577 – Helen Jan 17 '19 at 08:41
  • Helen, thanks for googling it for me... I did look at the issues in GitHub but did not find it.... my bad. Could you please post this as an answer, I will accept it. – Daniel Jan 17 '19 at 15:04

1 Answers1

8

This was a bug. It was fixed in openapi-generator v. 4.0.0-beta2 and later.

Helen
  • 87,344
  • 17
  • 243
  • 314