Datamodel-code-generator: Support Kubernetes Open API Spec or JSON Schema

Created on 12 Mar 2020  路  7Comments  路  Source: koxudaxi/datamodel-code-generator

I've tried to run this against Kubernetes OpenAPI Spec but ran into some errors.

curl -O https://raw.githubusercontent.com/kubernetes/kubernetes/master/api/openapi-spec/swagger.json
datamodel-codegen --input swagger.json --input-file-type=openapi --output=mode.py
[... error abridged ...]
  File "/proj/.venv/lib/python3.7/site-packages/datamodel_code_generator/parser/base.py", line 164, in parse
    self.parse_raw()
  File "/proj/.venv/lib/python3.7/site-packages/datamodel_code_generator/parser/openapi.py", line 12, in parse_raw
    spec_string=self.text, backend='openapi-spec-validator'
  File "/proj/.venv/lib/python3.7/site-packages/prance/__init__.py", line 106, in __init__
    self.parse()
  File "/proj/.venv/lib/python3.7/site-packages/prance/__init__.py", line 137, in parse
    self._validate()
  File "/proj/.venv/lib/python3.7/site-packages/prance/__init__.py", line 172, in _validate
    validator(parsed)
  File "/proj/.venv/lib/python3.7/site-packages/prance/__init__.py", line 237, in _validate_openapi_spec_validator
    raise_from(ValidationError, type_ex)
  File "/proj/.venv/lib/python3.7/site-packages/prance/util/exceptions.py", line 16, in raise_from
    raise klass(*from_value.args) from from_value
prance.ValidationError: unhashable type: 'dict'



md5-94243ddea730c1ef5c3a3d43bfe584ca



pip install openapi2jsonschema
openapi2jsonschema swagger.json --kubernetes
cd schemas
datamodel-codegen --input pod.yaml --input-file-type=jsonschema --output=mode.py



md5-9909ec5864809db82e38f40fa3efaa4d



Traceback (most recent call last):
  File "/proj/.venv/bin/datamodel-codegen", line 8, in <module>
    sys.exit(main())
  File "/proj/.venv/lib/python3.7/site-packages/datamodel_code_generator/__main__.py", line 168, in main
    result = parser.parse()
  File "/proj/.venv/lib/python3.7/site-packages/datamodel_code_generator/parser/base.py", line 164, in parse
    self.parse_raw()
  File "/proj/.venv/lib/python3.7/site-packages/datamodel_code_generator/parser/jsonschema.py", line 472, in parse_raw
    self.parse_raw_obj(obj_name, raw_obj)
  File "/proj/.venv/lib/python3.7/site-packages/datamodel_code_generator/parser/jsonschema.py", line 455, in parse_raw_obj
    obj = JsonSchemaObject.parse_obj(raw)
  File "/proj/.venv/lib/python3.7/site-packages/pydantic/main.py", line 402, in parse_obj
    return cls(**obj)
  File "/proj/.venv/lib/python3.7/site-packages/pydantic/main.py", line 283, in __init__
    raise validation_error
pydantic.error_wrappers.ValidationError: 2 validation errors for JsonSchemaObject
properties -> apiVersion -> type
  str type expected (type=type_error.str)
properties -> kind -> type
  str type expected (type=type_error.str)

Can this library support Kubernetes Open API Spec or JSON Schema?

bug enhancement

All 7 comments

@petermorrow
Thank you for your reporting.
Interesting.

I found two reasons which happen to the problem.

  1. The code-generator does not support an array of the type attribute.
    You can see the spec of JSONschema.

The value of this keyword MUST be either a string or an array. If it is an array, elements of the array MUST be strings and MUST be unique.
https://json-schema.org/draft/2019-09/json-schema-validation.html#rfc.section.6.1.1

The codegen was born for openapi. Jsonschema and Openapi are similar, but It's not the same.
I should implement the feature for JSONschema to clear the problem.

  1. A few model name is not supported include special characters.
    pod.json has a object which name is io.k8s.apimachinery.pkg.apis.meta.v1.ObjectMeta
    The name includes . . But, the code-gen could not treat . and another special character name.
    It's a bug. Sorry, I will fix it.

Thanks @koxudaxi! How can I help?

@petermorrow

Thank you. If I need your help, then I write it here :slightly_smiling_face:

I need time to fix it. I will implement it tonight again.

BTW, I don't know the openapi file is a valid openapi format.
I can see an openapi validator happens error from this trace.
I think the reasons are Invalid format or the openapi validator has a bug.

  File "/proj/.venv/lib/python3.7/site-packages/prance/__init__.py", line 237, in _validate_openapi_spec_validator
    raise_from(ValidationError, type_ex)
  File "/proj/.venv/lib/python3.7/site-packages/prance/util/exceptions.py", line 16, in raise_from
    raise klass(*from_value.args) from from_value
prance.ValidationError: unhashable type: 'dict'

That might have something to do with Kubernetes only supporting Open API V2. There is an open PR for supporting V3 kubernetes/enhancements#1263.

That's why I tried going the JSON Schema route with instrumenta/openapi2jsonschema.

@petermorrow

That might have something to do with Kubernetes only supporting Open API V2.

I understand why the code-generator can't parse the YAML.

I'm trying to support the JsonSchem, but It is not complete.
I have to implement a more pattern to support the JsonSchem.

@petermorrow
I have released new version as 0.4.6
The version can parse pod.json and create models.
Could you please try it?

@koxudaxi It works! Thank you

Was this page helpful?
0 / 5 - 0 ratings