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.32k stars 6.45k forks source link

[BUG] [Python] Arbitrary typed arrays are not handled property in OpenAPI version 2.0 #19265

Open rc65 opened 1 month ago

rc65 commented 1 month ago

Bug Report Checklist

Description

I am trying to generate a Python client from a Swagger 2.0 schema but the generated types are not what I expect them to be.

openapi-generator version

I am using version 7.8.0-SNAPSHOT but have also downloaded and tried the 8.0.0-SNAPSHOT.

OpenAPI declaration file content or url
swagger: '2.0'
paths:
  /workflows/start:
    post:
      parameters:
        - in: body
          name: body
          required: true
          schema:
            $ref: '#/definitions/PayloadDefinition'
      responses:
        '200':
          description: Request succeeded
definitions:
  PayloadDefinition:
    properties:
      ArrayArgs:
        type: array
        items: {}

If I convert this to what I think is an equivalent OpenAPI 3.0.3 schema like:

openapi: '3.0.3'
info:
  title: Example API
  version: 1.0.0
paths:
  /workflows/start:
    post:
      requestBody:
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/PayloadDefinition'
      responses:
        '200':
          description: Request succeeded
components:
  schemas:
    PayloadDefinition:
      properties:
        ArrayArgs:
          type: array
          items: {}

the generated type is correct, i.e. array_args: Optional[List[Any]] = Field(default=None, alias="ArrayArgs").

Generation Details

openapi-generator generate -i ./schema.yaml -g python -o ./client

Steps to reproduce
  1. Run the generate command above
  2. Check client/openapi_client/models/payload_definition.py
  3. Line 29 is incorrect:
    array_args: Optional[List[Dict[str, Any]]] = Field(default=None, alias="ArrayArgs")

    The type annotation should be Optional[List[Any]]

Related issues/PRs

N/A

Suggest a fix

The generated type should be a list of Any.

rc65 commented 1 month ago

Tagging @multani as a member of the Python generator technical committee for visibility.