fix(openapi): include Go comments in generated schemas#1764
Conversation
Use jsonschema.Reflector with AddGoComments to populate field descriptions from Go docs based on the object package path. Add schema generation test coverage and regenerate resource selector schemas.
BenchstatBase: |
WalkthroughGenerateSchema now uses a jsonschema.Reflector and enriches generated JSON Schema with Go comments by resolving the object's import path and package directory, loading and sanitizing source comments, and merging them into the reflected schema. Related schema files and tests were updated to include and validate descriptions. Changes
Sequence DiagramsequenceDiagram
participant Caller as Caller
participant Gen as GenerateSchema
participant Ref as Reflector
participant Resolver as PathResolver
participant FS as FileSystem
participant Out as SchemaOutput
Caller->>Gen: GenerateSchema(obj)
Gen->>Ref: create Reflector for obj
Ref->>Out: produce base JSON schema
Gen->>Resolver: getObjectPath(obj)
Resolver->>FS: run `go list` to resolve package dir
FS-->>Resolver: package import path + sourceDir
Resolver-->>Gen: importPath, sourceDir
Gen->>FS: change dir & load Go comments from sourceDir
FS-->>Gen: raw comments
Gen->>Ref: addGoComments(reflector, importPath, sourceDir)
Ref->>Out: merge comments into schema definitions
Gen->>Gen: sanitizeComments(reflector)
Gen-->>Caller: enriched JSON schema bytes
Suggested reviewers
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing touches
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (1)
schema/openapi/generate.go (1)
145-147:⚠️ Potential issue | 🟡 MinorTypo: "unabled" → "unable".
Proposed fix
- return fmt.Errorf("unabled to write schema to path[%s]: %w", path, err) + return fmt.Errorf("unable to write schema to path[%s]: %w", path, err)
🤖 Fix all issues with AI agents
In `@schema/openapi/generate.go`:
- Around line 122-136: The resolvePackageDir function currently uses
cmd.CombinedOutput which can mix stderr warnings into the returned directory;
change it to use cmd.Output() to capture stdout only, assign output, and on
error detect an *exec.ExitError to extract exitErr.Stderr (include it in the
error message) while still wrapping the original error with %w; keep trimming
the stdout output and returning the same formatted errors otherwise.
🧹 Nitpick comments (2)
schema/openapi/generate.go (2)
61-79:os.Chdiris process-global — not safe for concurrent use.If
GenerateSchemais ever called concurrently (e.g., generating schemas for multiple types in parallel, or in parallel tests), theos.Chdirwill race across goroutines. For a build-time codegen tool called sequentially this is acceptable, but worth a brief comment noting the constraint.
93-106: Consider filtering additional marker directives.Currently only
+kubebuilder:and+k8s:prefixes are stripped. Other common markers like+optional,+required,+listType,+listMapKey, etc., would also leak into user-facing descriptions if present on fields.A broader prefix check (e.g., any line starting with
+) may be more future-proof, depending on whether you have comments that legitimately start with+.
- Use cmd.Output() instead of CombinedOutput() to avoid mixing stderr - Extract stderr properly via *exec.ExitError on command failure - Fix typo: unabled → unable in WriteSchemaToFile error message
Use
jsonschema.Reflectorwith AddGoComments to populate field descriptions from Go docs based on the object package path.Add schema generation test coverage and regenerate resource selector schemas.
resolves: #1730
Summary by CodeRabbit
New Features
Documentation
Tests