OpenAPITools / openapi-generator

OpenAPI Generator allows generation of API client libraries (SDK generation), server stubs, documentation and configuration automatically given an OpenAPI Spec (v2, v3)
https://openapi-generator.tech
Apache License 2.0
21.87k stars 6.59k forks source link

[BUG] [Python] to_dict() in Python SDK has inconsistent formatting for attribute names in dictionary #18719

Closed mohamuni closed 6 months ago

mohamuni commented 6 months ago

Bug Report Checklist

Description

In Python SDK while converting response to dictionary, there is inconsistency in format of attribute names in dictionary. For the properties in response whose value is dictionary and derived from allof schema, the attributes name in value dictionary is not in snake case as defined in attribute maps, instead they are presented as they are received in response.

openapi-generator version

6.1.0

OpenAPI declaration file content or url
openapi: 3.0.2
info:
  title: Sample spec file
  version: 1.0.0
servers:
  - url: 'http://127.0.0.1:3000'
paths:
  /electriccar:
    get:
      operationId: getElectricCar
      responses:
        '200':
          description: returns electric car data
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/vehicle.car.electric'
  /enginecar:
    get:
      operationId: getEngineCar
      responses:
        '200':
          description: returns engine car data
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/vehicle.car.engine'
components:
  schemas:
    vehicle:
      title: vehicle
      type: object
      properties:
        ManufacturerName:
          type: string
        Model:
          type: string
    vehicle.car:
      title: vehicle.car
      type: object
      properties:
        Make:
          $ref: '#/components/schemas/vehicle'
        Category:
          type: string
    vehicle.car.electric:
      title: vehicle.car.electric
      type: object
      allOf:
        - $ref: '#/components/schemas/vehicle.car'
        - type: object
          properties:
            BatterySize:
              type: string
    vehicle.car.engine:
      title: vehicle.car.engine
      type: object
      allOf:
        - $ref: '#/components/schemas/vehicle.car'
        - type: object
          properties:
            FuelTankSize:
              type: string
Generation Details
mvn clean install
java -jar openapi-generator-cli.jar generate -i example.yaml -g python -o pythonsdk
Steps to reproduce

Generate Python SDK from the specified spec file and call any endpoint as shown below and try converting the response to dictionary using to_dict() as shown below in example.

from openapi_client.api import default_api
from openapi_client import api_client
import openapi_client

configuration = openapi_client.Configuration(
    host = "http://127.0.0.1:3000"
)

# 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 = default_api.DefaultApi(api_client)

    try:
        # returns electric car data with nested properties
        api_response = api_instance.get_electric_car()
        print(api_response.to_dict())
        print(api_response.make.to_dict())
    except openapi_client.ApiException as e:
        print("Exception when calling DefaultApi->get_electric_car: %s\n" % e)

Actual Output of the above program is as shown below In output the make property is derived from allof schema. hence the attributes name in value dictionary must be manufacturer_name instead of 'ManufacturerName' and similarly 'model_name' instead of 'ModelName'

{'battery_size': '3 Kwh', 'category': 'Sedan', 'make': {'ManufacturerName': 'Tesla', 'ModelName': 'Model S'}}

Expected Output

{'battery_size': '3 Kwh', 'category': 'Sedan', 'make': {'manufacturer_name': 'Tesla', 'model_name': 'Model S'}}
mohamuni commented 6 months ago

works with latest master