如何在openapi-codegen生成的python代码中使用承载认证

waz*_*ari 5 python swagger openapi servicestack-openapi

我正在使用 OpenApi 3.0 规范来记录我的 API。它基本上是一个 REST API,除了登录请求之外,每个请求都需要一个有效的 Bearer 令牌。OpenAPI 规范如下所示:

---
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: []
Run Code Online (Sandbox Code Playgroud)

在自动生成的 swagger-ui 中工作得非常好,当我下载 OpenAPI 规范并将其导入 Postman 时,邮递员也是如此。

但是,当使用以下命令使用 openapi-generator 时:

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
Run Code Online (Sandbox Code Playgroud)

但是,它尝试使用用户名和密码代替不记名令牌:

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()
            },

    }
Run Code Online (Sandbox Code Playgroud)

请注意,这里使用了用户名和密码,并且 auth_settings() 函数返回基本身份验证。我必须做什么才能在生成的代码中使用承载身份验证方案?

Hel*_*len 6

这是一个错误。它在 openapi-generator v. 4.0.0-beta2 及更高版本中得到修复。