[BUG] [PYTHON] Generated client breaks on to_dict() method
See original GitHub issueBug Report Checklist
- Have you provided a full/minimal spec to reproduce the issue?
- Have you validated the input using an OpenAPI validator (example)?
- Have you tested with the latest master to confirm the issue still exists?
- Have you searched for related issues/PRs?
- What’s the actual output vs expected output?
- [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description
The .to_dict() function causes “AttributeError: ‘dict’ object has no attribute” error.
openapi-generator version
5.2.1
OpenAPI declaration file content or url
I used $ref with external files: https://gist.github.com/Valmoz/16503c5ce996d27982722959733b6955 the paths are:
- openapi.yaml
- models/responses/UserInfoResponse.yaml
- models/schemas/User.yaml
Generation Details
openapi-generator-cli generate -i openapi.yaml -g python -o generated/openapi/python/
Steps to reproduce
I tried to use the generated client from a dedicated python script:
from openapi_client.model.user_info_response import UserInfoResponse
from openapi_client.api import user_api
configuration = openapi_client.Configuration(
host = 'http://api-v2.example.it'
)
configuration.access_token = access_token
# Enter a context with an instance of the API client
with openapi_client.ApiClient(configuration) as api_client:
# Create an instance of the API class
api_instance = user_api.UserApi(api_client)
try:
# Get User Info
api_response: UserInfoResponse = api_instance.get_user_info()
return api_response.to_dict()
except openapi_client.ApiException as e:
print("Exception when calling UserApi->get_user_info: %s\n" % e)
When this code is executed, (more precisely: the to_dict() function) I obtain this error: AttributeError: ‘dict’ object has no attribute ‘_composed_schemas’
Manually modifying the generated model_utils.py file at line 1634 (see next session), I was able to fix this error, but then I obtained another one: AttributeError: ‘dict’ object has no attribute ‘_data_store’
Modifying also line 1640, I am able to retrieve:
{
"data": {
"email": "mrossi@example.com",
"first_name": "Matteo",
"hash": "44444",
"id": 2,
"last_name": "Rossi",
"name": "Matteo Rossi",
"picture": null
},
"email_confirmation_state": {
"need_confirmation": false
},
"info": {
"need_marketing_consents_confirmation": false,
"need_password_change": false,
"need_terms_of_service_confirmation": false
}
}
Related issues/PRs
I didn’t find any…
Suggest a fix
it seems possible to avoid the issues checking if model_instance contains the missing keys.
I modified the generated model_utils.py line 1634 as follows:
if '_composed_schemas' in model_instance and model_instance._composed_schemas:
...
For the second error, I wrapped the for statement at line 1640 in an if statement as follows:
if '_data_store' in model_instance:
for attr, value in model_instance._data_store.items():
...
N.B.: I also noticed that lines 1652-1664 have been indented with one space less than the rest of the file (3 instead of 4)…
Issue Analytics
- State:
- Created 2 years ago
- Reactions:4
- Comments:7 (5 by maintainers)
Top GitHub Comments
I think that it should be enough to modify the following file: modules/openapi-generator/src/main/resources/python/model_utils.mustache lines 1336-1342
I also produced this with version 5.2.1. I upgrade to 5.3.1 and the error was immediately obvious to me. I had set a property as either (integer, nullable) but was receiving a string. At least in my case, it wasn’t a bug, just good o’l user error and an un-helpful error message.