feat: Add JSON Schema generator for Go structs#1219
Merged
jakobmoellerdev merged 38 commits intoNov 28, 2025
Conversation
This commit introduces a tool to generate JSON Schemas for annotated Go structs. Implementation includes a main entry point, schema generation logic, and utilities to parse, validate, and embed schemas. Signed-off-by: Jakob Möller <contact@jakob-moeller.com>
Signed-off-by: Jakob Möller <contact@jakob-moeller.com>
This commit cleans up JSON schema description fields by removing schema generation-related annotations. The change ensures descriptions remain concise and free of implementation-specific comments. Signed-off-by: Jakob Möller <contact@jakob-moeller.com>
This commit ensures proper error handling when writing schema JSON files and embedding files by returning and logging errors instead of panicking. Signed-off-by: Jakob Möller <contact@jakob-moeller.com>
Replaced inline type definitions with `$defs` references for runtime types and standardized schema structures across multiple JSON schema files. Removed redundant annotations and improved schema reusability. Signed-off-by: Jakob Möller <contact@jakob-moeller.com>
b7682dc to
8ec4761
Compare
…hemas Added `$comment` fields to provide additional context for runtime types and deprecated fields across multiple JSON schema files. Enhanced schema generation logic to support marking deprecated fields. Updated `Taskfile.yml` to include JSON schema generator in the code generator pipeline. Signed-off-by: Jakob Möller <contact@jakob-moeller.com>
Signed-off-by: Jakob Möller <contact@jakob-moeller.com>
Signed-off-by: Jakob Möller <contact@jakob-moeller.com>
Signed-off-by: Jakob Möller <contact@jakob-moeller.com>
Signed-off-by: Jakob Möller <contact@jakob-moeller.com>
Signed-off-by: Jakob Möller <contact@jakob-moeller.com>
Signed-off-by: Jakob Möller <contact@jakob-moeller.com>
Signed-off-by: Jakob Möller <contact@jakob-moeller.com>
Signed-off-by: Jakob Möller <contact@jakob-moeller.com>
…ma generation Refactored `boolPtr` to a generic `Ptr` helper for better reusability. Added extensive tests for schema generation and extraction functionality. Signed-off-by: Jakob Möller <contact@jakob-moeller.com>
…ests Enhanced JSON tag handling using `reflect.StructTag` for better reliability. Added comprehensive tests to validate schema generation for various type structures, including aliases, maps, structs, arrays, and cycles. Signed-off-by: Jakob Möller <contact@jakob-moeller.com>
frewilhelm
reviewed
Nov 26, 2025
Signed-off-by: Jakob Möller <contact@jakob-moeller.com>
78e0b6e to
e4d82e7
Compare
…action Standardized line breaks and whitespace in JSON schema comments. Refactored and enhanced comment handling in the generator to support block comments, code blocks, and better edge trimming. Added additional tests for improved coverage. Signed-off-by: Jakob Möller <contact@jakob-moeller.com>
e4d82e7 to
f53d675
Compare
Signed-off-by: Jakob Möller <contact@jakob-moeller.com>
Added embedded schemas for `Raw` and `Type` types using `//go:embed`. Updated dependencies in `go.mod` and `go.sum`, including adding `golang.org/x/tools`, `golang.org/x/mod`, and `golang.org/x/sync`. Enhanced the `Universe` loader to include OCM Runtime package with improved handling using `golang.org/x/tools/go/packages`. That ensures the tests can use runtime objects in the universe at all times Signed-off-by: Jakob Möller <contact@jakob-moeller.com>
12a491f to
ae8e629
Compare
Extracted JSON tag parsing logic into a new `parseJSONTag` helper function for improved clarity and reusability. Updated `jsonTagName` and `jsonTagHasOmitEmpty` to utilize this function. Simplified code and improved error handling. Signed-off-by: Jakob Möller <contact@jakob-moeller.com>
Updated generator to use `JSONSchemaDraft202012` structure instead of the previous `Schema` type. Refactored generator methods for schema building, property definitions, and validations to align with Draft 2020-12. Adjusted tests and embedded schemas accordingly, ensuring compatibility with the updated draft and improved functionality. Signed-off-by: Jakob Möller <contact@jakob-moeller.com>
c8f1574 to
9cca492
Compare
Skarlso
reviewed
Nov 27, 2025
Skarlso
reviewed
Nov 27, 2025
Skarlso
left a comment
Contributor
There was a problem hiding this comment.
The rest looks okay apart from the - thing.
Replaced hardcoded schema URLs with `JSONSchemaDraft202012URL` constant for better maintainability. Refactored JSON tag parsing by introducing `parseJSONTagWithFieldNameFallback` for cleaner logic and field name fallback. Updated handling of `omitempty` tags and adjusted schema requirements accordingly. Added test to ensure fields with `"-"` JSON tags are excluded from `required`. Signed-off-by: Jakob Möller <contact@jakob-moeller.com>
matthiasbruns
previously approved these changes
Nov 27, 2025
matthiasbruns
left a comment
Contributor
There was a problem hiding this comment.
lgtm- my browser died reviewing this 😭
Introduced `enum` handling in JSON schema generation with support for type and field-level markers. Consolidated and refactored schema generation methods by combining `schemaForExpr` and `schemaForExprWithField`. Adjusted primitives to remove unused formats. Added a GitHub workflow to lint and validate JSON schemas. Updated embedded schemas to include enums and enhanced marker extraction logic. Signed-off-by: Jakob Möller <contact@jakob-moeller.com>
3a7d372 to
352c4f9
Compare
Signed-off-by: Jakob Möller <contact@jakob-moeller.com>
# Conflicts: # bindings/go/rsa/signing/handler/handler_test.go
Signed-off-by: Jakob Möller <contact@jakob-moeller.com>
Introduced support for const-based enums in JSON schema generation with metadata extraction for descriptions and deprecations. Refactored and consolidated handling of type and field-level markers. Extended type introspection to include constants through AST analysis. Updated schemas and generator logic to support the enhancement. Signed-off-by: Jakob Möller <contact@jakob-moeller.com>
Enhanced JSON schema generation to include deprecation metadata for enums. Adjusted schema definitions across multiple components for consistency and added direct references to the `JSONSchemaDraft202012URL` constant. Refactored generator logic by consolidating and cleaning up redundant methods. Signed-off-by: Jakob Möller <contact@jakob-moeller.com>
…e discovery Refined linting command in GitHub workflow to utilize `git ls-files` for schema file selection, ensuring stricter and more accurate validation. Signed-off-by: Jakob Möller <contact@jakob-moeller.com>
piotrjanik
reviewed
Nov 27, 2025
piotrjanik
reviewed
Nov 27, 2025
Adjusted error message to reflect updated usage syntax, including support for multiple root directories and the `--help` flag. Signed-off-by: Jakob Möller <contact@jakob-moeller.com>
matthiasbruns
approved these changes
Nov 28, 2025
piotrjanik
approved these changes
Nov 28, 2025
fabianburth
added a commit
that referenced
this pull request
Dec 2, 2025
<!-- markdownlint-disable MD041 --> #### What this PR does / why we need it During the plugin manager refactoring, constants were moved to different packages. This updates the helm plugin to import the updated packages. **Notes for the Reviewer** I couldn't update the `runtime` module to 0.0.4 yet, as the plugin module will have to be updated to the JSON schema generator introduced [here](#1219). #### Which issue(s) this PR fixes <!-- Usage: `Fixes #<issue number>`, or `Fixes (paste link of issue)`. --> Contributes to open-component-model/ocm-project#760 --------- Signed-off-by: Fabian Burth <fabian.burth@sap.com> Co-authored-by: Matthias Bruns <github@matthiasbruns.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What this PR does / why we need it
This PR introduces a new tool and process to generate JSON Schema documents automatically from Go struct definitions. A generator (jsonschemagen) is added under bindings/go/generator/jsonschemagen/, and many schema‐JSON files are added corresponding to existing Go types in the project. The PR also annotates relevant Go structs with a
+ocm:jsonschema-gen=truetag to mark them for schema generation.Which issue(s) this PR fixes
part of open-component-model/ocm-project#760