swagger-api / swagger-api/swagger-codegen

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

Open
#8,948 8 comments 11 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Mustache
Stars
17.8k
Forks
6k
PR merge metrics
No merged PRs in 30d

Description

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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

The issue concerns generated Python model obj.to_dict() methods and the ApiClient.sanitize_for_serialization path. Start by tracing the Python generation template and compare both conversion paths, using the supplied command to reproduce a model with a camelCaseProperty. Done means to_dict() applies attribute_map and returns the same property-key structure as the client serializer.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
backend-api-design, tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.