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

[REQ] Python-Flask - Python 3.7 util._deserialization #1866

Closed
nathan5280 opened this issue Jan 9, 2019 · 3 comments
Closed

[REQ] Python-Flask - Python 3.7 util._deserialization #1866

nathan5280 opened this issue Jan 9, 2019 · 3 comments

Comments

@nathan5280
Copy link

Is your feature request related to a problem? Please describe.

Unable to deserialize model objects when using Python 3.7.

When running with Python 3.7 openapi_server.util._deserialize fails because typing.GenericMeta was removed from 3.7.

Describe the solution you'd like

I'm not sure on where we are at with support for 3.7, but I worked on this fix and figured I should pass it along for when we get ready to support 3.7. Other than this I haven't found other issues.

Describe alternatives you've considered

Python 3.6

def _deserialize(data, klass):
    """Deserializes dict, list, str into an object.

    :param data: dict, list or str.
    :param klass: class literal, or string of class name.

    :return: object.
    """
    from cdst_places_app.openapi_server.models.base_model_ import Model

    if data is None:
        return None

    if klass in six.integer_types or klass in (float, str, bool):
        return _deserialize_primitive(data, klass)
    elif klass == object:
        return _deserialize_object(data)
    elif klass == datetime.date:
        return deserialize_date(data)
    elif klass == datetime.datetime:
        return deserialize_datetime(data)
    elif type(klass) == typing.GenericMeta:
        if klass.__extra__ == list:
            return _deserialize_list(data, klass.__args__[0])
        if klass.__extra__ == dict:
            return _deserialize_dict(data, klass.__args__[1])
    else:
        return deserialize_model(data, klass)

Python 3.7

import typing_inspect


def _deserialize(data, klass):
    """Deserializes dict, list, str into an object.

    :param data: dict, list or str.
    :param klass: class literal, or string of class name.

    :return: object.
    """
    from cdst_places_app.openapi_server.models.base_model_ import Model

    if data is None:
        return None

    if klass in six.integer_types or klass in (float, str, bool):
        return _deserialize_primitive(data, klass)
    elif klass == object:
        return _deserialize_object(data)
    elif klass == datetime.date:
        return deserialize_date(data)
    elif klass == datetime.datetime:
        return deserialize_datetime(data)
    elif typing_inspect.is_generic_type(klass):
        if typing_inspect.get_origin(klass) == list:
            return _deserialize_list(data, klass.__args__[0])
        if typing_inspect.get_origin(klass) == dict:
            return _deserialize_dict(data, klass.__args__[1])
    else:
        return deserialize_model(data, klass)

I'm not sure if the dict piece

        if typing_inspect.get_origin(klass) == dict:
            return _deserialize_dict(data, klass.__args__[1])

works or not. I don't have a spec that triggers this code.

Additional context

@spacether
Copy link
Contributor

spacether commented Jan 11, 2019

We have a test in the python swagger 2.0 spec which tests deserialization here:
https://github.com/OpenAPITools/openapi-generator/blob/master/samples/client/petstore/python/tests/test_deserialization.py
A similar test could be added to python-flask to verify solutions to this bug.

@cassinaooo
Copy link
Contributor

cassinaooo commented Jan 16, 2019

Thanks for the hard work put in this tool!

I had the same problem with swagger-codegen and ended up migrating to openapi-codegen for unrelated reasons. My work around is on this issue swagger-api/swagger-codegen#8921, in case anyone is interested.

It does not depends on the typing_inspect, altough I am not sure that is relevant for every use case. However, my work around its not backwards compatible with python < 3.7.

@wing328
Copy link
Member

wing328 commented May 30, 2019

#2884 has been merged into master

Please pull the latest master to give it a try.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants