Skip to content

Commit

Permalink
Replace JSONSchemaProps with a dummy object type to prevent circula…
Browse files Browse the repository at this point in the history
…r references
  • Loading branch information
hectorm committed Feb 4, 2024
1 parent e4c9185 commit c215f95
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 33 deletions.
3 changes: 3 additions & 0 deletions src/fixtures/kubernetes/schemas/_definitions.json
Original file line number Diff line number Diff line change
Expand Up @@ -2797,6 +2797,9 @@
"type": "integer"
}
]
},
"io.k8s.apiextensions-apiserver.pkg.apis.apiextensions.v1.JSONSchemaProps": {
"type": "object"
}
}
}
3 changes: 3 additions & 0 deletions src/fixtures/kubernetes/schemas/all.json
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,9 @@
},
{
"$ref": "_definitions.json#/definitions/io.k8s.apimachinery.pkg.util.intstr.IntOrString"
},
{
"$ref": "_definitions.json#/definitions/io.k8s.apiextensions-apiserver.pkg.apis.apiextensions.v1.JSONSchemaProps"
}
]
}
43 changes: 10 additions & 33 deletions src/openapi2jsonschema/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import jsonschema # type: ignore
import yaml

from .definitions import kubernetes_definitions
from .errors import UnsupportedError
from .log import debug, error, info
from .util import (
Expand Down Expand Up @@ -91,24 +92,18 @@ def default(output, schema, prefix, stand_alone, expanded, kubernetes, strict):
info("Generating shared definitions")
definitions = data["definitions"]
if kubernetes:
definitions["io.k8s.apimachinery.pkg.util.intstr.IntOrString"] = {
"oneOf": [{"type": "string"}, {"type": "integer"}]
}
# Although the kubernetes api does not allow `number` as valid
# Quantity type - almost all kubenetes tooling
# recognizes it is valid. For this reason, we extend the API definition to
# allow `number` values.
definitions["io.k8s.apimachinery.pkg.api.resource.Quantity"] = {
"oneOf": [{"type": "string"}, {"type": "number"}]
}

# Add Kubernetes specific definitions
definitions.update(kubernetes_definitions)
for type_name in definitions:
type_def = definitions[type_name]
# Skip non-object types
if not isinstance(type_def.get("properties"), dict):
continue
# For Kubernetes, populate `apiVersion` and `kind` properties from `x-kubernetes-group-version-kind`
has_group_version_kind = (
"x-kubernetes-group-version-kind" in type_def
)
for prop_name in type_def.get("properties", []):
for prop_name in type_def["properties"]:
if prop_name == "apiVersion":
if has_group_version_kind and expanded:
for kube_ext in type_def[
Expand All @@ -131,7 +126,9 @@ def default(output, schema, prefix, stand_alone, expanded, kubernetes, strict):
]:
kind = kube_ext["kind"]
append_no_duplicates(
type_def["properties"]["kind"], "enum", kind
type_def["properties"]["kind"],
"enum",
kind,
)
# Enum values in properties should be unique
if "enum" in type_def["properties"][prop_name]:
Expand Down Expand Up @@ -191,26 +188,6 @@ def default(output, schema, prefix, stand_alone, expanded, kubernetes, strict):
% title
)

# This list of Kubernetes types carry around jsonschema for Kubernetes and don't
# currently work with openapi2jsonschema
if (
kubernetes
and stand_alone
and kind
in [
"jsonschemaprops",
"jsonschemapropsorarray",
"customresourcevalidation",
"customresourcedefinition",
"customresourcedefinitionspec",
"customresourcedefinitionlist",
"customresourcedefinitionspec",
"jsonschemapropsorstringarray",
"jsonschemapropsorbool",
]
):
raise UnsupportedError("%s not currently supported" % kind)

updated = change_dict_values(specification, prefix, version)
specification = updated

Expand Down
17 changes: 17 additions & 0 deletions src/openapi2jsonschema/definitions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
kubernetes_definitions = {
# Although the kubernetes api does not allow `number` as valid
# Quantity type - almost all kubenetes tooling
# recognizes it is valid. For this reason, we extend the API definition to
# allow `number` values.
"io.k8s.apimachinery.pkg.util.intstr.IntOrString": {
"oneOf": [{"type": "string"}, {"type": "integer"}]
},
"io.k8s.apimachinery.pkg.api.resource.Quantity": {
"oneOf": [{"type": "string"}, {"type": "number"}]
},
# Replace `JSONSchemaProps` with a dummy object type to prevent circular
# references
"io.k8s.apiextensions-apiserver.pkg.apis.apiextensions.v1.JSONSchemaProps": {
"type": "object",
},
}

0 comments on commit c215f95

Please sign in to comment.