Skip to content
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

Json encoder customization #199

Merged
merged 2 commits into from Mar 31, 2016

Conversation

rafaelcaricio
Copy link
Collaborator

Fixes #196.

Connexion should remain as must compatible as possible with usual Flask applications. This PR adds back the support of developers to customize the json_encoder in Connexion applications exactly as in pure Flask apps.

Changes proposed in this pull request:

  • Make use the flask JSONEncoder internally in Connexion.
  • Relay on flask.Flask.json_encoder for customization of encoding. This makes possible for users of Connexion to override the encoder they wanna use.
  • Adds documentation for this feature.

@coveralls
Copy link

Coverage Status

Coverage increased (+0.003%) to 99.438% when pulling 26d7d51 on rafaelcaricio:json-encoder-customization into 21e290f on zalando:master.

@jmcs
Copy link
Contributor

jmcs commented Mar 30, 2016

👍

@@ -124,7 +124,7 @@ def wrapper(*args, **kwargs):
logger.debug('Endpoint returned an empty response (204)', extra={'url': url, 'mimetype': self.mimetype})
return '', 204, headers

data = json.dumps(data, indent=2, cls=JSONEncoder)
data = json.dumps(data, indent=2)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How is the custom JSON encoder now used by json.dumps?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had the same question, but somehow it's working on the tests.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This json.dumps come from flask namespace (http://flask.pocoo.org/docs/0.10/api/#flask.json.dumps) which changes the behaviour to use as encoder the class referenced by the flask app attribute json_encoder. That is the expected behaviour while using JSON encoders with flask. So you can personalize the encoder changing the app.json_encoder.

References:
http://flask.pocoo.org/snippets/119/
http://flask.pocoo.org/docs/0.10/api/#flask.Flask.json_encoder

@rafaelcaricio
Copy link
Collaborator Author

Can we merge that?

@jmcs
Copy link
Contributor

jmcs commented Mar 31, 2016

@rafaelcaricio if you rebase the branch, yes.

@rafaelcaricio
Copy link
Collaborator Author

@jmcs Didn't realize it got conflicts. Should be good now. 👍

@coveralls
Copy link

Coverage Status

Coverage increased (+0.003%) to 99.457% when pulling 7cd60a9 on rafaelcaricio:json-encoder-customization into ddcb4ce on zalando:master.

@hjacobs hjacobs merged commit 5e7c946 into spec-first:master Mar 31, 2016
@jmcs jmcs removed the in progress label Mar 31, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants