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

"KeyError: properties" when parsing OpenAPI spec from Kubernetes #54

Open
larsks opened this issue Oct 22, 2021 · 3 comments · May be fixed by #55
Open

"KeyError: properties" when parsing OpenAPI spec from Kubernetes #54

larsks opened this issue Oct 22, 2021 · 3 comments · May be fixed by #55

Comments

@larsks
Copy link

larsks commented Oct 22, 2021

I would like to be able to validate manifests targeting our local k8s cluster. There are invariably CRDs in these manifests. I'm fetching the complete openapi spec from the cluster like this:

curl -o openapi.json -H 'Authorization: Bearer ...' https://api.cluster.example.com:6443/openapi/v2

And then attempting to convert these into json schemas like this:

openapi2jsonschema openapi.json --expanded --kubernetes --strict

This fails with:

Generating shared definitions
Traceback (most recent call last):
  File "/home/lars/.local/bin/openapi2jsonschema", line 8, in <module>
    sys.exit(default())
  File "/home/lars/.local/pipx/venvs/openapi2jsonschema/lib64/python3.9/site-packages/click/core.py", line 829, in __call__
    return self.main(*args, **kwargs)
  File "/home/lars/.local/pipx/venvs/openapi2jsonschema/lib64/python3.9/site-packages/click/core.py", line 782, in main
    rv = self.invoke(ctx)
  File "/home/lars/.local/pipx/venvs/openapi2jsonschema/lib64/python3.9/site-packages/click/core.py", line 1066, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "/home/lars/.local/pipx/venvs/openapi2jsonschema/lib64/python3.9/site-packages/click/core.py", line 610, in invoke
    return callback(*args, **kwargs)
  File "/home/lars/.local/pipx/venvs/openapi2jsonschema/lib64/python3.9/site-packages/openapi2jsonschema/command.py", line 99, in default
    if expanded and "apiVersion" in type_def["properties"]:
KeyError: 'properties'

I've attached the openapi spec to this issue.

openapi.json.txt

@larsks
Copy link
Author

larsks commented Oct 22, 2021

This seems to allow things to run, but I'm not sure it's the correct solution:

diff --git a/openapi2jsonschema/command.py b/openapi2jsonschema/command.py
index 9cd1bfc..f196ebd 100644
--- a/openapi2jsonschema/command.py
+++ b/openapi2jsonschema/command.py
@@ -99 +99 @@ def default(output, schema, prefix, stand_alone, expanded, kubernetes, strict):
-                            if expanded and "apiVersion" in type_def["properties"]:
+                            if expanded and "apiVersion" in type_def.get("properties", {}):
@@ -111 +111 @@ def default(output, schema, prefix, stand_alone, expanded, kubernetes, strict):
-                            if "kind" in type_def["properties"]:
+                            if "kind" in type_def.get("properties", {}):

larsks added a commit to larsks/openapi2jsonschema that referenced this issue Oct 22, 2021
@ReDestroyDeR
Copy link

+1. Encountered the same issue, these changes fix it

@paulcwarren
Copy link

+1 Seems to work for me too

tricktron pushed a commit to tricktron/openapi2jsonschema that referenced this issue Jan 31, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants