swagger-api / swagger-api/swagger-codegen
Python: `obj.to_dict()` methods return wrong structures because they do not use `attribute_map`
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
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- 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