swagger自动生成烧瓶服务器头

时间:2017-03-03 08:54:41

标签: flask swagger-editor swagger-codegen

我使用swagger编辑器(版本2.10.5)生成使用自定义标头的烧瓶api,并开始将以下行添加到每个路径:

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

相对定义:

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

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

$ python3 -m swagger_server

会产生一些问题:

  • 在发出curl请求时,标题未正确评估:

    $ 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)设置全部" - "到" _"?

提前致谢

1 个答案:

答案 0 :(得分:1)

好的,我想出来了..

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

Connexion请求处理文档(url)说:

  

"目前,标头参数不作为参数传递给处理函数。但是可以通过底层的connexion.request.headers对象访问它们,该对象将使flask.request.headers对象别名。"

解决方案是从自动生成的控制器中删除所有函数属性(与标题相关),然后从请求对象中选择它们,因此:

自:

def health_get(X_Forwarded_Host):
    ...

要:

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

再见!

相关问题