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

Python: `obj.to_dict()` methods return wrong structures because they do not use `attribute_map`

Open Ark-kun opened this issue 5 years ago • 8 comments

Description

Generated model code does not make any use of the attribute_map table which holds the mapping between the OpenAPI property names and pythonic property names.

If you have an object with camelCaseProperty, then swagger will generate a model with the camel_case_property field and record the mapping in the attribute_map. Unfortunately, when calling obj.to_dict() this map is not used and the resulting object will be incorrect.

The proper conversion is be performed by the ApiClient.sanitize_for_serialization method which requires first creating the client object. I'm arguing that this conversion should be built-in the obj.to_dict() methods. These methods should never return incorrect objects.

Swagger-codegen version

2.3.1

Swagger declaration file content or url
Command line used for generation

java -jar swagger-codegen-cli.jar generate -l python -i experiment.swagger.json -o $DIR -c config.json

Suggest a fix/enhancement

The attribute_map conversion should be built-in the obj.to_dict() methods.

Ark-kun avatar Nov 26 '18 22:11 Ark-kun

any upates?

JesseFinch avatar May 24 '19 12:05 JesseFinch

going to assign to myself, thanks for remind

HugoMario avatar May 24 '19 13:05 HugoMario

in case you get tired of waiting :)

import six
def to_good_dict(o):
    """Returns the model properties as a dict"""
    result = {}
    o_map = o.attribute_map

    for attr, _ in six.iteritems(o.swagger_types):
        value = getattr(o, attr)
        if isinstance(value, list):
            result[o_map[attr]] = list(map(
                lambda x: to_good_dict(x) if hasattr(x, "to_dict") else x,
                value
            ))
        elif hasattr(value, "to_dict"):
            result[o_map[attr]] = to_good_dict(value)
        elif isinstance(value, dict):
            result[o_map[attr]] = dict(map(
                lambda item: (item[0], to_good_dict(item[1]))
                if hasattr(item[1], "to_dict") else item,
                value.items()
            ))
        else:
            result[o_map[attr]] = value

    return result

JavascriptMick avatar Jun 07 '19 04:06 JavascriptMick

revised in light of additional bug (https://github.com/swagger-api/swagger-codegen/issues/9847) I found @HugoMario would be cool if we could fix this up.

def to_good_dict(o):
    """Returns the model properties as a dict with correct object names"""
    def val_to_dict(val):
        if hasattr(val, "to_dict"):
            return to_good_dict(val)
        elif isinstance(val, list):
            return list(map(
                lambda x: to_good_dict(x) if hasattr(x, "to_dict") else x,
                val
            ))
        else:
            return val

    result = {}
    o_map = o.attribute_map

    for attr, _ in six.iteritems(o.swagger_types):
        value = getattr(o, attr)
        if isinstance(value, list):
            result[o_map[attr]] = list(map(
                lambda x: to_good_dict(x) if hasattr(x, "to_dict") else x,
                value
            ))
        elif hasattr(value, "to_dict"):
            result[o_map[attr]] = to_good_dict(value)
        elif isinstance(value, dict):
            result[o_map[attr]] = dict(map(
                lambda item: (item[0], val_to_dict(item[1])),
                value.items()
            ))
        else:
            result[o_map[attr]] = value

    return result

JavascriptMick avatar Nov 13 '19 01:11 JavascriptMick

@HugoMario Is there any update on this issue? Any hints where the fix should be deployed in the generator to generate the to_dict as suggested by @JavascriptMick in #9847 ?

I'm using the latest version 3.0.19 and the bug is still there.

mloskot avatar Apr 27 '20 12:04 mloskot

I'm using the latest version 3.0.25 and the bug is still there.

Change586 avatar Dec 01 '22 10:12 Change586

+1

cong08 avatar Dec 25 '23 09:12 cong08

Any update on this bug? It would be great to have this fix merged.

Vhaelan avatar May 20 '24 20:05 Vhaelan