Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
vincent-raman opened this issue Sep 28, 2020 · 5 comments

Comments

@vincent-raman
Copy link

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);
}
@delenius
Copy link
Contributor

I was just about to file a report for the same issue. It is still there in the latest master as of writing this (now at 5.1.0-SNAPSHOT).

Here is a repo to easily reproduce, in case it helps.

Note: The suggested validator in the issue filing system complains about the recursive type, but e.g. the Swagger online editor does not. In any case, clearly one has to be allowed to have recursive types for trees etc.

@vincent-raman, could you file a PR, since you found a fix? Otherwise, I might take a crack at it

@eak24
Copy link
Contributor

eak24 commented Feb 2, 2022

Also fails in python-experimental

eak24 added a commit to eak24/openapi-generator that referenced this issue Feb 6, 2022
spacether pushed a commit that referenced this issue Feb 6, 2022
* Add cycle detection (#7532)

* Review feedback

* Including ContextAwareNodes to detect cycles more accurately.

* Add test

* Add forest to test.

* No longer need ContextAwareNode

* Review feedback

* Update samples
@spacether
Copy link
Contributor

spacether commented Feb 6, 2022

The above PR fixes the java layer of python clients
Similar PRs may be needed to prevent infinite recursion in python clients

@RichiCoder1
Copy link

I still appear to be getting this error (or at least one very similiar to it) when using python-experimental and https://instana.github.io/openapi/openapi.yaml

@vincent-raman
Copy link
Author

vincent-raman commented Oct 12, 2022

It seems fixed in python-prior (renamed from python) in v6.2.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants