OpenAPI / JSON Schema Generation
The same typeinfo field walk that drives serialization also produces API
contracts. Mapping int → "integer", bool → "boolean", and str →
"string" at compile time yields a ready-made OpenAPI properties block — the
same pattern applies to GraphQL SDL, protobuf messages, or typed HTTP clients.
Walk typeinfo(Product).fields and map each type.kind to a JSON Schema type string, producing a ready-made OpenAPI "properties" block at compile time.
08-api-openapi.zolo
// Use case 5 — BINDINGS / API (OpenAPI · JSON Schema · GraphQL · protobuf)
//
// Generate an OpenAPI / JSON-Schema `properties` fragment from a type. Each
// field's `type.kind` maps to a JSON-Schema type; the same walk produces
// GraphQL SDL, protobuf messages, or a typed client. Computed at compile time.
struct Product {
id: int,
title: str,
in_stock: bool,
}
let schema_json = comptime {
var s = "\"properties\": \{\n"
var first = true
for f in typeinfo(Product).fields {
if first == false { s = s + ",\n" }
var jstype = "string"
if f.type.kind == "int" { jstype = "integer" }
if f.type.kind == "bool" { jstype = "boolean" }
s = s + " \"" + f.name + "\": \{ \"type\": \"" + jstype + "\" \}"
first = false
}
s = s + "\n\}"
s
}
print(schema_json)
// expected:
// "properties": {
// "id": { "type": "integer" },
// "title": { "type": "string" },
// "in_stock": { "type": "boolean" }
// }