swagger-codegen icon indicating copy to clipboard operation
swagger-codegen copied to clipboard

[python-flask] from_dict(to_dict(obj)) does not work (snake_case vs CamelCase)

Open ThmX opened this issue 8 years ago • 3 comments

Description

When converting a model to and from a dict, the following command statement does not work. It removes all the fields being defined as CamelCase in swagger but implemented as snake_case in python.

settings.from_dict(settings.to_dict(obj))
Swagger-codegen version

2.3.1

Swagger declaration file content or url
...
definitions:
  Settings:
    type: object
    properties:
      someArg:
        type: string
...
Command line used for generation
java -jar swagger-codegen-cli-2.3.1.jar generate -i swagger.yml -l python-flask -o flask
Steps to reproduce
settings = Settings(some_args='something')
other_settings = settings.from_dict(settings.to_dict())
# -> other_settings.some_args is None
Related issues/PRs
  • [ ] I will create a PR to solve this issue if my fix is accepted by the community
Suggest a fix/enhancement

Replace swagger_server.util.deserialize_model(.) with the following version:

def deserialize_model(data, klass):
    """Deserializes list or dict to model.

    :param data: dict, list.
    :type data: dict | list
    :param klass: class literal.
    :return: model object.
    """
    instance = klass()

    if not instance.swagger_types:
        return data

    for attr, attr_type in six.iteritems(instance.swagger_types):
        if data is not None and isinstance(data, (list, dict)):
            value = None
            if instance.attribute_map[attr] in data:
                value = data[instance.attribute_map[attr]]
            elif attr in data:
                value = data[attr]

            setattr(instance, attr, _deserialize(value, attr_type))

    return instance

ThmX avatar Mar 14 '18 10:03 ThmX

I would recommand as a fix to modify this file: swagger-codegen/src/main/resources/flaskConnexion/base_model_.mustache

Add line: attr = o.attribute_map[attr]

after value = getattr(self, attr)

Because the issue is that the to_dict function isn't doing the field name transformation to camelCase.

MaelPic avatar May 01 '18 09:05 MaelPic

Any update on this? I am trying to deserialize a MongoDB object to a Python object using from_dict class method. But the method is not mapping the snake_case attributes to camelCase. only the 'single word' attributes which do not contain underscores get mapped and included in the "returned" instance while the rest get ignored. I also tried to replace swagger_server.util.deserialize_model(.) with the fix proposed by @ThmX but that's not working either.

hammadzf avatar Aug 20 '21 15:08 hammadzf

This fix seems to solve the problem.

file /src/main/resources/flaskConnexion/util.mustache

before

def _deserialize(data, klass):
    ...
    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])
    ...

after

def _deserialize(data, klass):
    ...
    elif hasattr(klass, '__origin__'):
        if klass.__origin__== list:
            return _deserialize_list(data, klass.__args__[0])
        if klass.__origin__== dict:
            return _deserialize_dict(data, klass.__args__[1])
    ...

pierre-quelin avatar Nov 17 '21 08:11 pierre-quelin