openapi-generators / openapi-generators/openapi-python-client
Security key doesn't respect endpoint explicitly setting anonymous access (via `{}`)
还没有人认领这个 Issue。
- 主要语言
- Python
- 星标
- 2k
- 派生
- 293
- 平均合并
- 34 分钟
- 30 天内合并 PR
- 1
描述
Thanks for this wonderful library! It's been SO helpful :)
Describe the bug
The client args in generated methods (Client vs AuthenticatedClient) don't match those described by security in the spec, when explicitly anonymous access is permitted in an endpoint's security list, as allowed in spec:
An empty Security Requirement Object ({}) indicates anonymous access is supported. [ref, with examples]
Example:
/auth-optional-explicit:
get:
summary: Requires auth but has empty object
security: # <--- client: AuthenticatedClient (EXPECTED: Client | AuthenticatedClient)
- {}
- ApiKeyAuth: []
responses:
'200':
description: OK
This is due to this line
>>> bool([])
False
>>> bool([{"ApiKeyAuth": []}])
True
>>> bool([{"ApiKeyAuth": []}, {}])
True
>>> bool([{}])
True
We would want the last two to return False. We should check if {} is anywhere in a list (when it's a list).
This would do the trick:
requires_security_check = lambda sec: bool(sec or []) and {} not in (sec or [])
OpenAPI Spec File
openapi: 3.1.0
info:
title: Security Test API
version: 1.0.0
servers:
- url: https://example.com
paths:
/no-auth-specified:
get:
summary: Truly anonymous
security: [] # client: Client | AuthenticatedClient (EXPECTED: same)
responses:
'200':
description: OK
/explicit-anon:
get:
summary: Requires auth but has empty object
security: # <--- client: AuthenticatedClient (EXPECTED: not sure, but def not this)
- {}
responses:
'200':
description: OK
/auth-optional-explicit:
get:
summary: Requires auth but has empty object
security: # <--- client: AuthenticatedClient (EXPECTED: Client | AuthenticatedClient)
- {}
- ApiKeyAuth: []
responses:
'200':
description: OK
/auth-required:
get:
summary: Requires API key
security: # <--- client: AuthenticatedClient
- ApiKeyAuth: []
responses:
'200':
description: OK
components:
securitySchemes:
ApiKeyAuth:
type: apiKey
in: header
name: X-API-Key
Desktop (please complete the following information):
- OS: [e.g. macOS 10.15.1]
- Python Version: [e.g. 3.8.0]
- openapi-python-client version [e.g. 0.1.0]
Additional context
...
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
调研方向
从 parser/openapi.py 中引用位置附近的第 423 行开始,那里会将 endpoint security value 转换为 requires-security-check result。比较 issue 中的四种 security configurations,尤其是包含 {} 的列表。完成的标准是:生成的 client 参数预期能够正确区分真正匿名、显式匿名、可选身份验证和必须身份验证的 endpoint;issue 中没有指定测试文件。
由索引模型根据 Issue 内容生成。
评估
- 技术栈
- openapi, python
- 领域
- api, security, tooling
- Issue 类型
- 缺陷
- 难度
- 2/5
- 预计耗时
- 1-3 小时
- 活跃度
- 停滞
- 描述清晰度
- 描述清楚
- 新手友好度
- 55/100