Skip to content

feat: Add JSON Schema generator for Go structs#1219

Merged
jakobmoellerdev merged 38 commits into
open-component-model:mainfrom
jakobmoellerdev:jsonschemagen
Nov 28, 2025
Merged

feat: Add JSON Schema generator for Go structs#1219
jakobmoellerdev merged 38 commits into
open-component-model:mainfrom
jakobmoellerdev:jsonschemagen

Conversation

@jakobmoellerdev

@jakobmoellerdev jakobmoellerdev commented Nov 17, 2025

Copy link
Copy Markdown
Member

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=true tag to mark them for schema generation.

Which issue(s) this PR fixes

part of open-component-model/ocm-project#760

@github-actions github-actions Bot added kind/feature new feature, enhancement, improvement, extension size/l Large labels Nov 17, 2025
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>
…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>
@github-actions github-actions Bot added the component/github-actions Changes on GitHub Actions or within `.github/` directory label Nov 19, 2025
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>
@jakobmoellerdev jakobmoellerdev marked this pull request as ready for review November 22, 2025 13:04
@jakobmoellerdev jakobmoellerdev requested a review from a team as a code owner November 22, 2025 13:04
…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>
Comment thread bindings/go/generator/jsonschemagen/cmd/main.go Outdated
Comment thread bindings/go/generator/jsonschemagen/extractor_test.go
Comment thread bindings/go/generator/jsonschemagen/extractor_test.go
Comment thread bindings/go/generator/jsonschemagen/extractor_test.go
Comment thread bindings/go/generator/jsonschemagen/schema.go Outdated
Comment thread bindings/go/generator/jsonschemagen/schema.go Outdated
Comment thread bindings/go/generator/jsonschemagen/schema.go Outdated
Comment thread bindings/go/generator/jsonschemagen/schema.go Outdated
Signed-off-by: Jakob Möller <contact@jakob-moeller.com>
…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>
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>
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>
Comment thread bindings/go/generator/jsonschemagen/writer/writer.go

@Skarlso Skarlso left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
matthiasbruns previously approved these changes Nov 27, 2025

@matthiasbruns matthiasbruns left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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>
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>
Comment thread bindings/go/generator/jsonschemagen/writer/writer.go
Comment thread bindings/go/generator/jsonschemagen/cmd/main.go Outdated
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>
@jakobmoellerdev jakobmoellerdev enabled auto-merge (squash) November 28, 2025 10:33
@jakobmoellerdev jakobmoellerdev merged commit 3e0b899 into open-component-model:main Nov 28, 2025
65 checks passed
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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

component/github-actions Changes on GitHub Actions or within `.github/` directory kind/feature new feature, enhancement, improvement, extension size/l Large

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants