Skip to content

Commit 0e51362

Browse files
authored
Wrapping APISpecsView get response in a try except (flasgger#420)
* Styling fixes, notes in README and 0.6.2 release * Wrapping APISpecsView get response in a try except * Wrapping APISpecsView get response in a try except * Wrapping APISpecsView get response in a try except * Wrapping APISpecsView get response in a try except * Wrapping APISpecsView get response in a try except * Fix TypeError caused by json version 2.0.9 * added test for flasgger.base.APISpecsView * added test for flasgger.base.APISpecsView * added test for flasgger.base.APISpecsView
1 parent a24d8d2 commit 0e51362

File tree

2 files changed

+28
-3
lines changed

2 files changed

+28
-3
lines changed

flasgger/base.py

+6-3
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
from flask import Blueprint
2020
from flask import Markup
2121
from flask import current_app
22-
from flask import jsonify
22+
from flask import jsonify, Response
2323
from flask import redirect
2424
from flask import render_template
2525
from flask import request, url_for
@@ -144,7 +144,11 @@ def get(self):
144144
"""
145145
The Swagger view get method outputs to /apispecs_1.json
146146
"""
147-
return jsonify(self.loader())
147+
try:
148+
return jsonify(self.loader())
149+
except Exception:
150+
specs = json.dumps(self.loader())
151+
return Response(specs, mimetype='application/json')
148152

149153

150154
class SwaggerDefinition(object):
@@ -635,7 +639,6 @@ def wrap_view(view):
635639
self.get_apispecs, endpoint=spec['endpoint'])
636640
))
637641
)
638-
639642
app.register_blueprint(blueprint)
640643

641644
def add_headers(self, app):

tests/test_apispecs.py

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#!/usr/bin/python3
2+
import pytest
3+
import sys
4+
import flasgger
5+
import flask
6+
import json
7+
from collections import defaultdict
8+
9+
def test_client():
10+
class FakeJson():
11+
def dumps(self, *args, **kwargs):
12+
"""
13+
Raises a type error
14+
"""
15+
raise TypeError
16+
flask.json._json = FakeJson()
17+
app = flask.Flask('test-app')
18+
flasgger.base.jsonify = flask.jsonify
19+
with app.app_context():
20+
specs = flasgger.base.APISpecsView(loader=lambda: {'test': 'test'})
21+
assert specs.get() != None
22+
flask.json._json = json

0 commit comments

Comments
 (0)