question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[BUG] [PYTHON] Generated client breaks on to_dict() method

See original GitHub issue

Bug 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:closed
  • Created 2 years ago
  • Reactions:4
  • Comments:7 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
valmozcommented, Aug 18, 2021

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

0reactions
ghostcommented, Jan 14, 2022

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

[BUG][Python] Client generated allOf model broken since 5.2.0
Suggest a fix. Debugging the code, it seems that the method _from_openapi_data of a model calls validate_get_composed_info in model_utils.py .
Read more >
Changelog — Python 3.11.1 documentation
open_connection() raises, break reference cycles generated by local exception and future instances (which has exception instance as its member var). Patch by ...
Read more >
Changelog - Python client library | Google Cloud
Added the VertexAiResourceNoun.to_dict() method (#588) (b478075) ... Removes breaking change from v1 version of AI Platform protos (6a99b12) ...
Read more >
Handling Application Errors — Flask Documentation (2.2.x)
Register handlers by decorating a function with errorhandler() . ... your code breaks while handling a request (and you have no error handlers...
Read more >
netapp_ontap API documentation
NetApp ONTAP The Python client library is a package you can use when writing ... volume = Volume(**data) # Example 3 - using...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found