gpt4 book ai didi

flask - swagger 自动生成的 flask 服务器头文件

转载 作者:行者123 更新时间:2023-12-03 17:26:35 25 4
gpt4 key购买 nike

我正在使用 swagger 编辑器(版本 2.10.5)生成一个使用自定义 header 的 flask api,并开始向每个路径添加以下行:

 parameters:
- $ref: '#/parameters/X-Forwarded-Host'

相对定义:

 X-Forwarded-Host:
name: 'X-Forwarded-Host'
in: header
description: Forwarded host header
required: true
type: string

然后运行自动生成的 flask 服务器

$ python3 -m swagger_server

产生了一些问题:

  • 发出 curl 请求时,未正确评估 header :

    $ curl -X GET --header 'Accept: application/json' --header 'X-Forwarded-Host: example.com' http://localhost:8080

    返回

    health_get() missing required positional argument: 'X_Forwarded_Host'
  • 自动生成的测试也没有用:

    headers = [('X_Forwarded_Host', 'X_Forwarded_Host_example'), ...

我做错了什么?为什么 swagger-editor(或 codegen)将所有“-”设置为“_”?

提前致谢

最佳答案

好吧,我想通了..

问题不在于 swagger-editor 本身,而在于它如何生成 flask (Connexion) 代码。

连接请求处理文档 ( url ) 说:

"Currently, header parameters are not passed to the handler functions as parameters. But they can be accessed through the underlying connexion.request.headers object which aliases the flask.request.headers object."

解决方案是从自动生成的 Controller 中删除所有函数属性(与 header 相关)并从请求对象中选取它们,因此:

来自:

def health_get(X_Forwarded_Host):
...

收件人:

def health_get():
forwarded_host = connexion.request.headers['X-Forwarded-Host']

再见!

关于flask - swagger 自动生成的 flask 服务器头文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42574465/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com