blog

12022-01-16

SEAN K.H. LIAO

proto to jsonschema / openapi

So you have message definitions declared as protocol buffers, and now you them in JSON Schema or OpenAPI format.

You could use google's protoc-gen-openapi and protoc-gen-jsonschema but I think their output is weird (multi files, no output?). There's also protoc-gen-openapiv2 but the output also looks sort of weird (not much content?).

cue

Now cue is more of its own language which can handle schema definition, validation and data configuration. But that's not too important for us: all we care is that it can import protobuf, converting it to cue, and export openapi / jsonschema from those cue definitions:

$ cue mod init
$ cue import example.proto
$ cue export --out openapi .
$ cue export --out jsonschema .

The generated schemas look fairly solid: all in one file, handles maps/oneof properly, and it can even include validation like regex/pattern (with cue specific field options, def in cue.proto).