pai
pai

Reputation: 121

swagger auto-generated flask server headers

I'm using swagger editor (version 2.10.5) to generate a flask api that uses custom headers and started to add the following line to each path:

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

the relative definition:

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

Then running the auto-generated flask server

$ python3 -m swagger_server

creates some problems:

What am I doing wrong? Why is swagger-editor (or codegen) setting all "-" to "_"?

Thanks in advance

Upvotes: 3

Views: 1096

Answers (1)

pai
pai

Reputation: 121

Ok, I figured out..

The problem was NOT with swagger-editor itself but how it generates the flask (Connexion) code.

Connexion request handling docs (url) says:

"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."

The solution is to remove all function attributes (related to headers) from the auto-generated controller and pick them from the request object, therefore:

From:

def health_get(X_Forwarded_Host):
    ...

To:

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

Bye!

Upvotes: 5

Related Questions