quart-motor icon indicating copy to clipboard operation
quart-motor copied to clipboard

Quart 0.18 & JSONEncoders

Open pgjones opened this issue 2 years ago • 3 comments

I've removed json_encoders from Quart 0.18 and replaced it with a JSONProvider interface (Flask is doing the same). I'm told this breaks Quart-Motor. Please ask if you need help migrating and thanks for maintaining this extension.

pgjones avatar Jul 28 '22 15:07 pgjones

Indeed, Quart-Motor uses following helper:

https://github.com/marirs/quart-motor/blob/d2d62916e4f35e66db97619af4e658268c9c3c15/quart_motor/helpers.py#L5

https://github.com/marirs/quart-motor/blob/d2d62916e4f35e66db97619af4e658268c9c3c15/quart_motor/helpers.py#L59-L96

Whereas Quart now uses:

https://github.com/pallets/quart/blob/547578f355b6b9438b08aefbbb65e6446c8290ef/src/quart/json/provider.py#L18-L35

class JSONProvider:
    """A standard set of JSON operations for an application. Subclasses
    of this can be used to customize JSON behavior or use different
    JSON libraries.

    To implement a provider for a specific library, subclass this base
    class and implement at least :meth:`dumps` and :meth:`loads`. All
    other methods have default implementations.

    To use a different provider, either subclass ``Quart`` and set
    :attr:`~quart.Quart.json_provider_class` to a provider class, or set
    :attr:`app.json <quart.Quart.json>` to an instance of the class.
    :param app: An application instance. This will be stored as a
        :class:`weakref.proxy` on the :attr:`_app` attribute.
    """


    def __init__(self, app: Quart) -> None:
        self._app = weakref.proxy(app)

Hoping maintainers can upgrade the code (many thanks to them).

jeanmonet avatar Sep 01 '22 13:09 jeanmonet

Will have a look this weekend

marirs avatar Sep 01 '22 14:09 marirs

FYI locally I have made the following changes that seem to work (haven't tested extensively):

# --- helpers.py ---

# class JSONEncoder(quart_json.JSONEncoder):
class JSONEncoder(quart_json.provider.DefaultJSONProvider):
    ...
    def __init__(self, app, json_options, *args, **kwargs):
        #              ^^^  added "app"

# --- __init__.py ---

    # Instead of:
    self._json_encoder = partial(JSONEncoder, json_options=json_options)
    # Replaced with:
    self._json_provider_class = partial(JSONEncoder, json_options=json_options)

    # Instead of:
    app.json_encoder = self._json_encoder
    # Replaced with:
    app.json = self._json_provider_class(app=app)

Attempts to mimic https://github.com/pallets/quart/blob/547578f355b6b9438b08aefbbb65e6446c8290ef/src/quart/app.py#L291

Also, JSONEncoder probably needs to be renamed JSONProvider.

jeanmonet avatar Sep 01 '22 21:09 jeanmonet

@marirs do you still plan on fixing this issue or is the project abandoned?

davideandres95 avatar Mar 20 '23 15:03 davideandres95

I would also love to have this fixed. Thank you for your work on this project!

JonathanBrenner avatar Apr 27 '23 22:04 JonathanBrenner

Hi all, sorry for the long delay, I'll have this fixed by end of coming week for sure. was so busy in a lot of personal stuff.

marirs avatar Apr 28 '23 02:04 marirs

Hi @JonathanBrenner @davideandres95 - can you test it. I've released the newer version and let me know.

Thanks to @clementwanjau for taking the time to update this.

marirs avatar May 02 '23 02:05 marirs

@marirs The new changes do seem to work for me! I did run into another incompatibility issue with Pymongo 4.0 and opened a pull request to fix it. https://github.com/marirs/quart-motor/pull/6

JonathanBrenner avatar May 03 '23 21:05 JonathanBrenner

@JonathanBrenner - Kindly check now, did the merge and update to pypi as well!

Thanks

marirs avatar May 07 '23 12:05 marirs

Everything looks good on my end. Thanks @marirs !

JonathanBrenner avatar May 07 '23 16:05 JonathanBrenner

Thanks a lot and sorry again for the delay :)

closing this issue finally ;)

marirs avatar May 08 '23 11:05 marirs