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

Flask json #561

Merged
merged 2 commits into from
Feb 26, 2024
Merged

Flask json #561

merged 2 commits into from
Feb 26, 2024

Conversation

lafrech
Copy link
Member

@lafrech lafrech commented Oct 2, 2023

Fixes #517.

Ping @kaibr.

I need to figure out why tests don't pass. Opening a PR to avoid forgetting about it again.

(Wondering if we should subclass DefaultJSONProvider in the test. It wouldn't matter for the test but for example sake.)

@ddorian
Copy link
Contributor

ddorian commented Jan 12, 2024

@lafrech the reason the test fails, is because that value is generated here:
https://github.com/ddorian/apispec/blob/e4e962b08b351caa655fe93045bbe01db4e3582e/src/apispec/ext/marshmallow/field_converter.py#L227

That calls fields.Decimal.serialize() which returns a Decimal.
Why were you expecting to return a float? Which is non-exact data type.
At most you would expect it to return a string, which would work, by setting dec = ma.fields.Decimal(load_default=Decimal("0.5"), as_string=True).

At that code, it's not connected at all to app.json = ..., it's no json-conversion at all inside the test.

Copy link

codecov bot commented Feb 26, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 99.88%. Comparing base (5021607) to head (b00c8d2).

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #561      +/-   ##
==========================================
- Coverage   99.88%   99.88%   -0.01%     
==========================================
  Files          14       14              
  Lines         885      883       -2     
  Branches      192      192              
==========================================
- Hits          884      882       -2     
  Partials        1        1              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@lafrech
Copy link
Member Author

lafrech commented Feb 26, 2024

The reason the tests failed was because I tested the type in the output of spec.to_dict, before actually serializing to JSON... This is fixed.

In the meantime, Decimal support was added to flask default JSON encoder, so I rewrote the test to serialize a dummy custom type.

@lafrech lafrech merged commit 1bb18c3 into master Feb 26, 2024
11 checks passed
@lafrech lafrech deleted the flask_json branch February 26, 2024 20:56
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.

Apispec 6.1.0: Accessing API docs throws TypeError: Object of type Decimal is not JSON serializable
2 participants