openapi-python-client icon indicating copy to clipboard operation
openapi-python-client copied to clipboard

Add support for recursively defined schemas

Open sp-schoen opened this issue 4 years ago • 13 comments
trafficstars

Describe the bug I tried to create a python client from an OpenApi-Spec with the command openapi-python-client generate --path secret_server_openapi3.json. Then I got multple warnings:

  • invalid data in items of array settings
  • Could not find reference in parsed models or enums
  • Cannot parse response for status code 200, response will be ommitted from generated client I searched the schemas, which are responsible for the errors and they all had in common, that they either reference another schema which is recursively defined or reference themself. For example one of the problematic schemas:
{
  "SecretDependencySetting": {
        "type": "object",
        "properties": {
          "active": {
            "type": "boolean",
            "description": "Indicates the setting is active."
          },
          "childSettings": {
            "type": "array",
            "description": "The Child Settings that would be used  for list of options.",
            "items": {
              "$ref": "#/components/schemas/SecretDependencySetting"
            }
          }
        },
        "description": "Secret Dependency Settings - Mostly used internally"
      }
}

To Reproduce Define a schema recursively and then try to create a client from it.

Expected behavior The software can also parse a recursively defined schema.

OpenAPI Spec File It's 66000 Lines, so I'm not sure if this will help you, or github will allow me to post it :smile: Just ask if you need specific parts of the spec, aside from what I already provided above.

Desktop (please complete the following information):

  • OS: Linux Manjaro
  • Python Version: 3.9.1
  • openapi-python-client version 0.7.3

sp-schoen avatar Feb 17 '21 13:02 sp-schoen

Thanks for reporting @sp-schoen! This should be fixed by #329 which all being well should make it into the 0.8 release we have coming up.

emann avatar Feb 17 '21 14:02 emann

I checked out the PR you suggested and tried it, but I still get the same error.

sp-schoen avatar Feb 17 '21 16:02 sp-schoen

Ah, looking it over again you're totally right. The stuff in there will be needed to make recursive schemas work however, so whenever that is reviewed/merged work can begin on adding support for recursive definitions.

emann avatar Feb 17 '21 17:02 emann

Just FYI @sp-schoen, @p1-ra has graciously begun working on supporting recursive definitions in the aforementioned PR!

emann avatar Feb 19 '21 16:02 emann

Was this fixed by #366 ?

jkinkead avatar May 05 '21 16:05 jkinkead

I'm not sure 😅 probably not though. #366 was just about fixing reference resolution in general, my guess is that generation will still fail for recursive models, we'll need to add a special case to those.

dbanty avatar May 05 '21 16:05 dbanty

Cool, thanks! It wasn't clear given that #329 was closed in favor of #366. :)

jkinkead avatar May 05 '21 16:05 jkinkead

Based on @p1-ra's feedback in #366, it seems like recursive references do in fact work - @jkinkead if you could try using your schema with 0.9.0 and let us know how it shakes out that'd be awesome!

emann avatar May 05 '21 21:05 emann

Hmm, I still get errors with 0.9.0. :/

The repro file at the top of the issue still produces errors:

$ openapi-python-client --version
openapi-python-client version: 0.9.0
$ openapi-python-client generate --path repro.json 
Error(s) encountered while generating, client was not created

Failed to parse OpenAPI document

3 validation errors for OpenAPI
info
  field required (type=value_error.missing)
paths
  field required (type=value_error.missing)
openapi
  field required (type=value_error.missing)


If you believe this was a mistake or this tool is missing a feature you need, please open an issue at https://github.com/triaxtec/openapi-python-client/issues/new/choose

jkinkead avatar May 05 '21 21:05 jkinkead

Does that repro.json that you made just contain the block at the top of the issue? You'll need to provide a full OpenAPI document with all of the required fields in it - try one of the samples @p1-ra provided in #366 (assuming they match what your use case is).

emann avatar May 05 '21 21:05 emann

@emann I also tried it again with 0.9.0 and still get the same errors.

sp-schoen avatar May 06 '21 06:05 sp-schoen

Sorry, yes - I was using just that json file, but my local repro is much too large to paste in.

I'll add a shorter repro if I have time!

jkinkead avatar May 07 '21 16:05 jkinkead

My bad, It was to early on the morning it looks I've messed up with my tests; probably did them on the wrong branch ....; I've redo the tests using today main branch, I still have the issues.

All sample define here are NOK

p1-ra avatar May 07 '21 16:05 p1-ra