Workflow and tools for JSON Schema generation

497 views
Skip to first unread message

Johann Höchtl

unread,
Sep 3, 2020, 3:05:02 AM9/3/20
to golang-nuts

Hi,
I would like to accomplish the following:

An existing golang package on github provides a struct definition I am interested in to be used as part of a REST API interface I design
  1. I would like to automatically go generate json schema for this struct
  2. I would like to incorporate this generated schema into an OpenAPI definition and generate go boilerplate for that

For step 2 I am using https://github.com/deepmap/oapi-codegen which works great. It also supports $ref to external schemas.

Still looking to accomplish step 1: I am searching for a tool which go generates JSON schema from a golang source file or package. Any help?

Marcin Romaszewicz

unread,
Sep 4, 2020, 1:58:13 PM9/4/20
to Johann Höchtl, golang-nuts
Have you considered reversing the workflow? You write the OpenAPI spec, and have a code generator produce the Schemas and server boilerplate?

If that's ok, check out my project :)

We successfully use it for many API's in production.

-- Marcin


--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/f0e099de-7008-478d-ba92-f2febd0c40bdn%40googlegroups.com.

Marcin Romaszewicz

unread,
Sep 4, 2020, 2:13:42 PM9/4/20
to Johann Höchtl, golang-nuts
Oh wow, my email client showed your email in a truncated way, so i didn't see you were already using my project, sorry for the silly response.

It would be possible to add external type definitions to oapi-codegen, I believe. I'm in the process of thinking about a v2 version which is more stable, and less convoluted. V1 was a learning experience.

Johann Höchtl

unread,
Sep 6, 2020, 2:06:52 AM9/6/20
to golang-nuts
mar...@gmail.com schrieb am Freitag, 4. September 2020 um 20:13:42 UTC+2:
Oh wow, my email client showed your email in a truncated way, so i didn't see you were already using my project, sorry for the silly response.

It would be possible to add external type definitions to oapi-codegen, I believe. I'm in the process of thinking about a v2 version which is more stable, and less convoluted. V1 was a learning experience.

You project is awesome, I already use it!

My current workflow is as following:
1. I create my models in code. Thereby I embed structs of external packages.
2. As I am lazy and do not feel like re-creating all the OpenApi3 model definitions declaratively is use go-swagger, only using model annotations like // swagger:model and exporting the models only
3. As go swagger is stuck in OpenApi 2.0 I use a  sed - script to update the created YAML-models by go-swagger and fix the $refs to create a crippled yet valid OpenApi3 - spec
4. I define my API using an OpenApi3 - editor. Currently I am using editor.swagger.io as it doesn't mess with external refs to models
5. I scaffold  my go app using oapi-codegen and the newly added  -import-mapping, treating my own model as an external ref (which it somehow is, as I am embedding external struct members)

Ugly hack, mostly because of go-swagger stuck in OpenApi2, but a hack that works :)

Paul Jolly

unread,
Sep 10, 2020, 4:15:59 AM9/10/20
to Johann Höchtl, golang-nuts
CUE (https://cuelang.org/) will (in the near future) be able to help
with the Go -> JSON Schema part (or indeed JSON Schema -> Go).

Please feel free to ask any questions of the community via
https://cuelang.org/community/
Reply all
Reply to author
Forward
0 new messages