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 typetype is an instance of NumberType
json and set them to typereturn typetype is an instance of StringType
json and set them to typereturn typetype is an instance of AnyType
return typetype is an instance of GenericType
return typetype 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 typejson and set them to typereturn typetype is an instance of ReferenceType
json and set them to typereturn typeThe 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 StructTypejson.additionalProperties is available
return new MapTypejson.type is equal to array
return new ArrayTypejson.type is equal to boolean
return new BooleanTypejson.type is equal to number
return new NumberTypejson.type is equal to string
return new StringTypejson.type is equal to any
return new AnyTypejson.allOf is available
return new IntersectionTypejson.oneOf is available
return new UnionTypejson.$ref is available
return new ReferenceTypejson.$generic is available
return new GenericTypereturn nullThe 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