marshmallow-code / flask-smorest

DB agnostic framework to build auto-documented REST APIs with Flask and marshmallow
https://flask-smorest.readthedocs.io
MIT License
651 stars 72 forks source link

Flask json #561

Closed lafrech closed 6 months ago

lafrech commented 11 months ago

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 commented 8 months ago

@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.

codecov[bot] commented 6 months ago

Codecov Report

All modified and coverable lines are covered by tests :white_check_mark:

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

Additional details and impacted files ```diff @@ 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 ```

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.

lafrech commented 6 months ago

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.