noirbizarre / flask-restplus

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

Output should respect Namespace.produces() #590

Open aparamon opened 5 years ago

aparamon commented 5 years ago

Currently, Content-Type is selected from all global mediatype representations, regardless of per-route annotations.

To reproduce:

from flask import Flask, make_response
from flask_restplus import Resource, Api

app = Flask(__name__)
api = Api(app)
ns = api.namespace('test')

@api.representation('text/html')
def html(data, code, headers=None):
    html = '''
<!doctype html>
<head>
  <title>Hello, world!</title>
</head>
'''
    response = make_response(html, code)
    response.headers.extend(headers or {})
    return response

@ns.route('/hello')
@ns.produces('application/json')
class HelloWorld(Resource):
    def get(self):
        return {'hello': 'world'}

if __name__ == '__main__':
    app.run(debug=True)
>curl localhost:5000/test/hello -H "Accept: text/html"
<!doctype html>
<head>
  <title>Hello, world!</title>
</head>

Expected output is though:

{
    "hello": "world"
}
aparamon commented 5 years ago

OpenAPI 3.0 only has per-route, not global mediatypes: https://swagger.io/docs/specification/media-types