-
Notifications
You must be signed in to change notification settings - Fork 508
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
stop relying on ujson (fix #589, #587, #507) #637
Open
ziirish
wants to merge
3
commits into
master
Choose a base branch
from
fix-drop-ujson
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,3 +9,4 @@ pytest-mock==1.6.3 | |
pytest-profiling==1.2.11 | ||
pytest-sugar==0.9.0 | ||
tzlocal | ||
ujson |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import flask_restplus.representations as rep | ||
|
||
from json import dumps, loads | ||
from ujson import dumps as udumps, loads as uloads | ||
|
||
payload = { | ||
'id': 1, | ||
'name': 'toto', | ||
'address': 'test', | ||
} | ||
|
||
|
||
def test_representations_serialization_output_correct(app): | ||
r = rep.output_json(payload, 200) | ||
assert loads(r.get_data(True)) == loads(dumps(payload)) | ||
|
||
|
||
def test_config_custom_serializer_is_module(app): | ||
# now reset serializer | ||
rep.serializer = None | ||
# then enforce a custom serializer | ||
app.config['RESTPLUS_JSON_SERIALIZER'] = 'ujson' | ||
r2 = rep.output_json(payload, 200) | ||
assert uloads(r2.get_data(True)) == uloads(udumps(payload)) | ||
assert rep.serializer == udumps | ||
|
||
|
||
def test_config_custom_serializer_is_function(app): | ||
# test other config syntax | ||
rep.serializer = None | ||
app.config['RESTPLUS_JSON_SERIALIZER'] = 'ujson.dumps' | ||
rep.output_json(payload, 200) | ||
assert rep.serializer == udumps | ||
|
||
|
||
def test_config_custom_serializer_fallback(app): | ||
# test fallback | ||
rep.serializer = None | ||
app.config['RESTPLUS_JSON_SERIALIZER'] = 'ujson.lol.dumps' | ||
rep.output_json(payload, 200) | ||
assert rep.serializer == dumps |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is testing several different things within a single test case - how do you feel about splitting it up into several test cases so that it's clearer to see the individual assertions? Something like:
This makes the intended behaviour clearer and is easier to see from a test failure where the issue is 😄
I think it's a good idea to at least log a message when a fallback to
json.dumps
takes place. It can fallback without breaking, but the user's should be aware that it's happening (adding it to the docs is still good though so thank you for that). Perhaps something like:There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks!
I have spit the test.
I'm not quite sure about the
warnings.warn
yet though. I think we should define a communication with upper apps policy first so people know how to deal with those.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great thank you! Sorry, I'm not sure what you mean by
Could you please clarify? Did you mean define a way in which Flask-RESTPlus should interact with other applications?
Using
warnings/warn
is a common practice for Flask extensions that wish to communicate misconfiguration/possible error to the user. For example:Or using
logger.warning
is also a common approach.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I mean we should document what kind of communication people using FLask-RESTPlus should expect.
I know
warnings.warn
is a common practice, but we should document this.The reason is the default filter might hide some messages hence this doesn't provide any help to the users:
Result in:
So by default, unless people know they should expect a
DeprecationWarning
they won't notice it.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah yes I understand - thank you for clarifying 😄 Yes I agree that this should definitely be documented - it's good to be clear to the users so thank you for pointing that out 👍 .
How about changing this:
https://github.com/noirbizarre/flask-restplus/pull/637/files#diff-dce4048f760a2254db20f3a78d7fcfa1R362 to say something like:
And then include an example of the output when starting the application, for example using
logging
:Or using
warnings
:I'm just very hesitant to fallback completely silently (even if it is documented) as the user will not be able to tell when it has happened.
What do you think? 😄
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The only problem I see with this is the code path in which this warning will be raised is not deterministic (it will happen only after some route returns and if it needs marshalling).
Maybe the solution is to load the serializer within the
Api
constructor.This way, people know when/where to catch the
warnings
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have added the
warnings.warn
as suggested.This should happen early in the initialization of the Api unless you pass it a Blueprint instead of a Flask app (in which case the warning will be raised when calling a route that needs serialization.
The loaded serializer is cached within the
app.config
. I don't think this is the best place to do so, but I didn't find any other...