openapi2jsonschema icon indicating copy to clipboard operation
openapi2jsonschema copied to clipboard

"KeyError: properties" when parsing OpenAPI spec from Kubernetes

Open larsks opened this issue 3 years ago • 3 comments

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 avatar Oct 22 '21 15:10 larsks

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 avatar Oct 22 '21 15:10 larsks

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

ReDestroyDeR avatar Feb 24 '22 15:02 ReDestroyDeR

+1 Seems to work for me too

paulcwarren avatar Mar 30 '22 22:03 paulcwarren