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

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

Open
Ark-kun opened this issue Nov 26, 2018 · 7 comments
Assignees

Comments

@Ark-kun
Copy link

Ark-kun commented Nov 26, 2018

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 added a commit to Ark-kun/swagger-codegen that referenced this issue Dec 27, 2018
@JesseFinch
Copy link

any upates?

@HugoMario
Copy link
Contributor

going to assign to myself, thanks for remind

@HugoMario HugoMario self-assigned this May 24, 2019
@JavascriptMick
Copy link

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
Copy link

JavascriptMick commented Nov 13, 2019

revised in light of additional bug (#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

@mloskot
Copy link

mloskot commented Apr 27, 2020

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

@Change586
Copy link

Change586 commented Dec 1, 2022

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

@cong08
Copy link

cong08 commented Dec 25, 2023

+1

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

No branches or pull requests

7 participants