This chapter provides some guidelines for developers how to build a TypeSchema code generator.
/definitions
for OpenAPI, AsyncAPI
and OpenRPC documents it is /components/schemas
. A processor
should have an option to set the location of the definitions
location. There is also no need for a
JSON Pointer implementation since we only resolve local schemas.definitions
location.
Object types can then reference these types. This is required since a processor needs a unique name for each
object type, which is the definition key of the type (i.e. which is used as class name).The following TypeSchema shows most keywords with a concrete reference to the specification, which should give you a first overview of the available keywords.
{
"$import": {
"my_ns": "file:///generic.json"
},
"definitions": {
"Human": {
"type": "struct",
"properties": {
"firstName": {
"type": "string"
},
"lastName": {
"type": "string"
},
"age": {
"type": "integer"
}
}
},
"Student": {
"parent": "Human",
"type": "struct",
"properties": {
"matricleNumber": {
"type": "integer"
}
}
},
"StudentMap": {
"$ref": "Map",
"$template": {
"T": "Student"
}
},
"Map": {
"type": "struct",
"properties": {
"totalResults": {
"type": "integer"
},
"entries": {
"type": "array",
"items": {
"$generic": "T"
}
}
}
}
},
"$ref": "StudentMap"
}
The following algorithm shows how to create an object in-memory representation of a type. It assumes that you pass the raw JSON decoded object into this function and it returns a concrete type.
function parseType(object json): Type
Type type
= newType(json)
type
is an instance of StructType
type
is an instance of MapType
type
is an instance of ArrayType
type
is an instance of BooleanType
return type
type
is an instance of NumberType
json
and set them to type
return type
type
is an instance of StringType
json
and set them to type
return type
type
is an instance of AnyType
return type
type
is an instance of GenericType
return type
type
is an instance of UnionType
type
is an instance of IntersectionType
Array<Type> items
= Create an empty arrayproperty
in json.oneOf
items[]
= parseType(property)
items
to type
json
and set them to type
return type
type
is an instance of ReferenceType
json
and set them to type
return type
The following algorithm shows how to detect the correct type from a decoded JSON value.
function newType(object json): Type|null
json.type
is available
json.type
is equal to object
json.properties
is available
return new StructType
json.additionalProperties
is available
return new MapType
json.type
is equal to array
return new ArrayType
json.type
is equal to boolean
return new BooleanType
json.type
is equal to number
return new NumberType
json.type
is equal to string
return new StringType
json.type
is equal to any
return new AnyType
json.allOf
is available
return new IntersectionType
json.oneOf
is available
return new UnionType
json.$ref
is available
return new ReferenceType
json.$generic
is available
return new GenericType
return null
The following algorithm shows how to resolve a reference.
function resolveReference(ReferenceType type, Definitions definitions): Type
string ref = type.ref
#/definitions/
and #/components/schemas/
on ref
with an empty stringref
contains not a colon :
ref
is available under the definitions
return definitions[ref]
ref
string based on the colon :
into two partsstring namespace = parts[0]
string name = parts[1]
$import
keyword
name