Skip to content

Instantly share code, notes, and snippets.

@yawaramin
Created August 11, 2023 02:57
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yawaramin/6f56f4b053d2b090353d7b7d40a722ce to your computer and use it in GitHub Desktop.
Save yawaramin/6f56f4b053d2b090353d7b7d40a722ce to your computer and use it in GitHub Desktop.
OpenAPI spec schemas in KCL - https://kcl-lang.io/
"""
Bindings for OpenAPI 3.0.0 - https://swagger.io/specification/#schema-object
ℹ️ JSON Schema validation for OpenAPI 3.0.0 must conform to this version:
https://datatracker.ietf.org/doc/html/draft-wright-json-schema-validation-00
"""
schema Ref:
$ref: str
schema Contact:
"""
https://swagger.io/specification/#contact-object
"""
name?: str
url?: str
email?: str
schema Description:
description?: str
schema SchemaString(Description):
type: "string" = "string"
enum?: [str]
default?: str
format?: "byte" | "binary" | "date" | "date-time" | "password"
schema SchemaNumber(Description):
type: "number" = "number"
minimum?: float
maximum?: float
exclusiveMinimum?: bool
exclusiveMaximum?: bool
schema SchemaInteger(Description):
type: "integer" = "integer"
minimum?: int
maximum?: int
exclusiveMinimum?: bool
exclusiveMaximum?: bool
schema SchemaArray(Description):
type: "array" = "array"
items: Schema
schema SchemaObject(Description):
type: "object" = "object"
properties?: {str: Schema}
additionalProperties?: False | Schema
required?: [str]
schema SchemaOneOf(Description):
oneOf: [Schema]
type Schema = SchemaString | SchemaNumber | SchemaInteger | SchemaArray | SchemaObject | SchemaOneOf | Ref
"""
https://swagger.io/specification/#schema-object
"""
schemaRef = lambda name: str -> Ref {
Ref {
$ref: "#/components/schemas/${name}"
}
}
schema ParameterBase(Description):
name: str
in: "query" | "header" | "path" | "cookie"
required?: bool
deprecated?: bool
allowEmptyValue?: bool
schema: Schema
type Parameter = ParameterBase | Ref
"""
https://swagger.io/specification/#parameter-object
"""
parameterRef = lambda name: str -> Ref {
Ref {
$ref: "#/components/parameters/${name}"
}
}
schema HeaderBase(Description):
required?: bool
deprecated?: bool
allowEmptyValue?: bool
style?: "form" | "simple"
explode?: bool
schema?: Schema
type Header = HeaderBase | Ref
"""
https://swagger.io/specification/#header-object
"""
headerRef = lambda name: str -> Ref {
Ref {
$ref: "#/components/headers/${name}"
}
}
schema Example(Description):
"""
https://swagger.io/specification/#header-object
"""
summary?: str
value: str
schema MediaType:
"""
https://swagger.io/specification/#media-type-object
"""
schema?: Schema
example?: str
examples?: {str: Example}
schema ResponseBase(Description):
headers?: {str: Header}
content?: {str: MediaType}
type Response = ResponseBase | Ref
"""
https://swagger.io/specification/#response-object
"""
responseRef = lambda name: str -> Ref {
Ref {
$ref: "#/components/responses/${name}"
}
}
schema SecuritySchemeBase(Description):
type: "apiKey"
name: str
in: "query" | "header" | "cookie"
type SecurityScheme = SecuritySchemeBase | Ref
"""
https://swagger.io/specification/#security-scheme-object
"""
securitySchemeRef = lambda name: str -> Ref {
Ref {
$ref: "#/components/securitySchemes/${name}"
}
}
schema License:
"""
https://swagger.io/specification/#license-object
"""
name: str
url?: str
schema Info(Description):
"""
https://swagger.io/specification/#info-object
"""
title: str
termsOfService?: str
contact?: Contact
license?: License
version: str
schema Server(Description):
"""
https://swagger.io/specification/#server-object
"""
url: str
schema Operation(Description):
"""
https://swagger.io/specification/#operation-object
"""
tags?: [str]
summary?: str
operationId?: str
parameters?: [Parameter]
responses: {str: Response}
deprecated?: bool
security?: [{str: [str]}]
schema PathItemBase(Description):
summary?: str
get?: Operation
type PathItem = PathItemBase | Ref
"""
https://swagger.io/specification/#path-item-object
"""
schema Components:
"""
https://swagger.io/specification/#components-object
"""
schemas?: {str: Schema}
responses?: {str: Response}
parameters?: {str: Parameter}
headers?: {str: Header}
securitySchemes?: {str: SecurityScheme}
schema Tag(Description):
"""
https://swagger.io/specification/#tag-object
"""
name: str
schema OpenAPI:
"""
https://swagger.io/specification/#openapi-object
"""
openapi: "3.0.0" = "3.0.0"
info: Info
servers?: [Server]
paths: {str: PathItem}
components?: Components
tags?: [Tag]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment