noirbizarre / flask-restplus

Fully featured framework for fast, easy and documented API development with Flask
http://flask-restplus.readthedocs.org
Other
2.73k stars 506 forks source link

Swagger header field in documentation not working correctly #337

Closed ivarflakstad-zz closed 6 years ago

ivarflakstad-zz commented 6 years ago

Consider this minimal-ish example:

from flask import Flask
from flask_restplus import Api, Resource

application = Flask(__name__)
api = Api(
    application,
    title='test header functionality',
)

@api.route('/test')
@api.header('Accept', 'Accept header!')
class TestResource(Resource):
    def get(self):
        return {'result': api.mediatypes()}

if __name__ == '__main__':
    application.run(debug=True)

Which results in the following bug in swagger:

swagger

The problem must be in swagger itself, right? Should I open a issue there?

It works great with curl:

curl

naeioan commented 6 years ago

I think you need to register the MIME type "application/javascript" ( check this http://flask-restful.readthedocs.io/en/0.3.5/extending.html ). I think the only MIME type supported by default by Swagger is "application/json". Look also in flask_restplus/api.py in the constructor: DEFAULT_REPRESENTATIONS = [('application/json', output_json)] self.representations = OrderedDict(DEFAULT_REPRESENTATIONS)

ivarflakstad-zz commented 6 years ago

If that was the issue, why would it work with curl?

naeioan commented 6 years ago

In your example code, the output is normal in Swagger, because if you look at the implementation: default_mediatype='application/json', decorators=None. The difference between Swagger in curl is exactly that :D , curl is not Swagger. Look at how Swagger creates the documentation (http://docs.swagger.io/spec.html) in 5.2.2.1 Object Example: { "apiVersion": "1.0.0", "swaggerVersion": "1.2", "basePath": "http://petstore.swagger.wordnik.com/api", "resourcePath": "/store", "produces": [ "application/json" ], Flask-rest+ changes the "produces" fields in the Swagger XML's based on the registered MIME representation. Curl does not temper with that. This is my opinion anyhow. Curl and Swagger are clients and behave differently.

ivarflakstad-zz commented 6 years ago

Of course curl and swagger behave differently, was never any doubt about that. I did not know there was such an elaborate response content-type scheme in swagger, so I treated it as I would any other header. For the project I am working on, it would be much easier to do it the way I intended, but I can make it work like this as well. This solved my problem, thank you for the help! :+1:

kenneho commented 6 years ago

Is it the using the @api.representation decorator you're referring to?