1

Attempting to use swagger codegen to auto-generate some a client library for a project I'm doing for work. Apparently swagger doesn't natively have a python version but they link to a community supported swagger-py codegen. Installed this with pip, tried running on my projects OpenAPI repo and I'm getting the error "RecursionError: maximum recursion depth exceeded in instancecheck"

Here's the stack trace:

Traceback (most recent call last):
  File "/home/user/.local/bin/swagger_py_codegen", line 8, in <module>
    sys.exit(generate())
  File "/home/user/.local/lib/python3.8/site-packages/click/core.py", line 722, in __call__
    return self.main(*args, **kwargs)
  File "/home/user/.local/lib/python3.8/site-packages/click/core.py", line 697, in main
    rv = self.invoke(ctx)
  File "/home/user/.local/lib/python3.8/site-packages/click/core.py", line 895, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "/home/user/.local/lib/python3.8/site-packages/click/core.py", line 535, in invoke
    return callback(*args, **kwargs)
  File "/home/user/.local/lib/python3.8/site-packages/swagger_py_codegen/command.py", line 200, in generate
    for code in generator.generate():
  File "/home/user/.local/lib/python3.8/site-packages/swagger_py_codegen/base.py", line 44, in generate
    for code in g:
  File "/home/user/.local/lib/python3.8/site-packages/swagger_py_codegen/base.py", line 47, in generate
    for code in self._process():
  File "/home/user/.local/lib/python3.8/site-packages/swagger_py_codegen/jsonschema.py", line 110, in _process
    yield Schema(build_data(self.swagger))
  File "/home/user/.local/lib/python3.8/site-packages/swagger_py_codegen/jsonschema.py", line 51, in build_data
    for path, _ in swagger.search(['paths', '*']):
  File "/home/user/.local/lib/python3.8/site-packages/swagger_py_codegen/parser.py", line 118, in search
    for p, d in dpath.util.search(
  File "/home/user/.local/lib/python3.8/site-packages/dpath/util.py", line 222, in yielder
    for segments, found in dpath.segments.walk(obj):
  File "/home/user/.local/lib/python3.8/site-packages/dpath/segments.py", line 73, in walk
    for found in walk(v, location + (k,)):
  File "/home/user/.local/lib/python3.8/site-packages/dpath/segments.py", line 73, in walk
    for found in walk(v, location + (k,)):
  File "/home/user/.local/lib/python3.8/site-packages/dpath/segments.py", line 73, in walk
    for found in walk(v, location + (k,)):
  [Previous line repeated 981 more times]
  File "/home/user/.local/lib/python3.8/site-packages/dpath/segments.py", line 58, in walk
    if not leaf(obj):
  File "/home/user/.local/lib/python3.8/site-packages/dpath/segments.py", line 33, in leaf
    return isinstance(thing, leaves)
RecursionError: maximum recursion depth exceeded in __instancecheck__

If it were simply this community python implementation of swagger codegen, I'd understand, but its happening in isInstance.

It looks as though it gets to dpath site package (not familiar with this, maybe swagger_py_codegen added it), it gets to segments, and then there's a function that checks if something is a leaf.

Here's the relevant section from segments.py:

def leaf(thing):
    '''
    Return True if thing is a leaf, otherwise False.

    leaf(thing) -> bool
    '''
    leaves = (bytes, str, int, float, bool, type(None))

    return isinstance(thing, leaves)

I'm pretty sure isinstance is just part of the standard library, so its not as if I can tweak that. But looking at this leaf(thing) method from dpath, I can't see what could be causing the error.

What I've tried so far:

I tried editing segments.py to increase the max recursion count to 10,000. This just leads to running forever. I left it alone for 5 minutes and it still never completed.

So its SOME sort of infinite recursive loop, but assuming isinstance is correct, I cannot for the life of me figure out what is causing the issue from that simple leaf(thing) call

Derek1st
  • 63
  • 6
  • Not an answer to your question - but [Swagger Codegen](https://github.com/swagger-api/swagger-codegen/) does have built-in Python client generators for both OpenAPI 2.0 and 3.0. – Helen Oct 06 '21 at 18:47
  • @Helen I had a suspicion that was the case but I for the life of me couldn't find it. I assumed the fact that they do link to a python version made by the community indicated that it didn't exist. Would you by any chance know where to find it? – Derek1st Oct 06 '21 at 18:59
  • Online version: Paste your OpenAPI YAML/JSON spec into https://editor.swagger.io and select **Generate Client > python** from the menu. CLI version (assuming OAS3): `java -jar swagger-codegen-cli-3.0.29.jar generate -l python -i ./path/to/MyAPI.yaml -o OUT_DIR`. CLI JAR can be downloaded from [Maven Central](https://repo1.maven.org/maven2/io/swagger/codegen/v3/swagger-codegen-cli/). – Helen Oct 06 '21 at 19:27
  • @Helen I wonder what the appeal would be for doing anything but the online implementation. Thank you a lot – Derek1st Oct 06 '21 at 19:34
  • 1
    CLI is useful if you have a multi-file API definition with relative $refs (e.g. `$ref: '../another/file.yaml'`). Online editor/codegen can only resolve https:// $refs. – Helen Oct 06 '21 at 19:56
  • 1
    I have this same issue and just submitted an issue on github https://github.com/dpath-maintainers/dpath-python/issues/184 – user3768495 Mar 09 '23 at 06:40
  • 1
    @user3768495 thanks for following up. I should always make a point of reporting issues I experience (whether they're my fault or not) to github either for clarity, or to help others with the issue. – Derek1st May 09 '23 at 14:57

0 Answers0