Skip to main content
Version: 0.5.2

OpenAPI to KCL

To convert from models defined in the OpenAPI spec file to KCL schema, we could run the following script:

kcl-openapi generate model -f ${your_open_api_spec.yaml} -t ${the_kcl_files_output_dir}

For example:

  • There is the OpenAPI spec file that we need to convert: test_open_api_spec.yaml
definitions:
v1.TestInt:
type: object
properties:
name:
type: string
format: int-or-string
required:
- name
x-kcl-type:
import:
package: v1.test_int
alias: test_int
type: TestInt
swagger: "2.0"
info:
title: KCL
version: v0.0.2
paths: {}
  • The script will be:
kcl-openapi generate model -f test_open_api_spec.yaml -t ~/ --skip-validation
  • Then we can find the generated file here: ~/models/v1/test_int.k
"""
This is the test_int module in v1 package.
This file was generated by the KCL auto-gen tool. DO NOT EDIT.
Editing this file might prove futile when you re-run the KCL auto-gen generate command.
"""


schema TestInt:
"""v1 test int

Attributes
----------
name : int | str, default is Undefined, required
name
"""


name: int | str