Skip to content

[BUG][Python] Stackoverflow error when using required recursive properties #7532

@vincent-raman

Description

@vincent-raman
Description

If we want to define a recursive type (like a tree), the client generation for python crashes with a StackOverflowError in the PythonClientCodegen.toExampleValueRecursive method.

openapi-generator version

4.3.1

OpenAPI declaration file content or url
{
  "openapi": "3.0.0",
  "info": {
    "version": "1.0.0",
    "title": "Test swagger file"
  },
  "paths": {
    "/tree": {
      "post": {
        "description": "Create",
        "operationId": "createTree",
        "requestBody": {
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/Tree"
              }
            }
          },
          "description": "The tree to create",
          "required": true
        },
        "tags": [
          "Test"
        ],
        "responses": {
          "200": {
            "description": "Successfully created the tree",
            "content": {
              "*/*": {
                "schema": {
                  "$ref": "#/components/schemas/Tree"
                }
              }
            }
          },
          "400": {
            "description": "Bad request"
          }
        }
      },
    }
  },
  "components": {
    "schemas": {
      "Tree": {
        "type": "object",
        "required": [
          "id",
          "name",
          "children"
        ],
        "properties": {
          "id": {
            "type": "integer"
          },
          "name": {
            "type": "string"
          },
          "description": {
            "type": "string"
          },
          "children": {
            "type": "array",
            "items": {
              "$ref": "#/components/schemas/Tree"
            }
          }
        }
      }
    }
  }
}
Generation Details

Generation of a client with language "python" and this schema definition

Steps to reproduce

Try to generate a python client

Suggest a fix

In the method PythonClientCodegen.toExampleValueRecursive, when treating an object (if (ModelUtils.isObjectSchema(schema)), the already treated properties are removed.

for (String toRemove : included_schemas) {
    if (reqs.contains(toRemove)) {
        reqs.remove(toRemove);
    }
}

But, just after, the "required" properties are added back, making this safeguard useless

if (null != schema.getRequired()) for (Object toAdd : schema.getRequired()) {
    reqs.add((String) toAdd);
}

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions