So I'm using openapi-generator to generate a flask server to serve my api.
I am having no trouble generating the server running it and viewing endpoints in my browser. However I get a CORS error when I make a GET request from my React Web App.
I've tried a few things to enable CORS.
I tried adding the header to my endpoint in my .yaml.
/pipelines:
get:
summary: 'Returns a list of pipelines.'
operationId: get_pipelines
responses:
'200':
description: 'A JSON array of pipelines'
content:
application/json:
schema:
$ref: '#/components/schemas/Pipeline'
headers:
Access-Control-Allow-Origin: '*'
default:
description: Unexpected error
content:
'application/json':
schema:
$ref: '#/components/schemas/ErrorMessage'
When I try adding the header via the .yaml and try to regenerate the server module, I get this error:
-attribute paths.'/pipelines'(get).responses.200.Access-Control-Allow-Origin is not of type `object
I also tried installing and importing flask_cors in the main.py of the server module.
#!/usr/bin/env python3
import connexion
from openapi_server import encoder
from flask_cors import CORS
def main():
app = connexion.App(__name__, specification_dir='./openapi/')
CORS(app.app)
app.app.json_encoder = encoder.JSONEncoder
app.add_api('openapi.yaml',
arguments={'title': 'ICDR API'},
pythonic_params=True)
app.run(port=5050)
if __name__ == '__main__':
main()
And I tried both those together. These fixes I found in the swagger-codegen petstore.yaml example and in the connexion docs.
However as I stated I'm using openapi-generator so its a little different than either of those other tools, but I'm having a lot of trouble finding any information on how to set this up correctly. Has anyone worked with openapi-generator before who can help me out?
OpenApi code generator uses the connexion library under the hood, in their documentation they propose using flask_cors see: here
I am currently using custom mustache templates forked from the originals, so in the \__main__.mustache
I added the lines
from flask_cors import CORS
....
CORS(app.app)
app.run(port=.....)
make sure to install flask_cors
or add it to your requirements.txt
P.S. you can specify the template location with generate ... -t TEMPALTE/FOLDER?LOCATION