Skip to content

fix(openapi): include Go comments in generated schemas#1764

Merged
moshloop merged 2 commits intomainfrom
fix/openapi-schema-comments-1730
Feb 6, 2026
Merged

fix(openapi): include Go comments in generated schemas#1764
moshloop merged 2 commits intomainfrom
fix/openapi-schema-comments-1730

Conversation

@adityathebe
Copy link
Copy Markdown
Member

@adityathebe adityathebe commented Feb 6, 2026

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.

resolves: #1730

Summary by CodeRabbit

  • New Features

    • Schema generation now enriches produced JSON schemas with source comments, improving field descriptions and context.
  • Documentation

    • Enhanced resource selection schemas with detailed descriptions for agent, scope, cache, search, health, types, and statuses.
  • Tests

    • Added a test verifying generated schemas include descriptive metadata for ResourceSelector fields.

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.
@github-actions
Copy link
Copy Markdown

github-actions bot commented Feb 6, 2026

Benchstat

Base: 5fd6dbe8823187c8e004f24489fa2266b250e77a
Head: 9ba2afe8b06502e87710015f8224a3244e824560

goos: linux
goarch: amd64
pkg: github.com/flanksource/duty/bench
cpu: AMD EPYC 7763 64-Core Processor                
                                                │ bench-base.txt │          bench-head.txt           │
                                                │     sec/op     │   sec/op     vs base              │
Main/Sample-15000/catalog_changes/Without_RLS-4      5.162m ± 0%   5.128m ± 2%       ~ (p=0.394 n=6)
Main/Sample-15000/catalog_changes/With_RLS-4         130.1m ± 2%   127.9m ± 0%  -1.69% (p=0.002 n=6)
Main/Sample-15000/config_changes/Without_RLS-4       5.152m ± 1%   5.110m ± 2%       ~ (p=0.132 n=6)
Main/Sample-15000/config_changes/With_RLS-4          126.7m ± 1%   128.2m ± 0%  +1.15% (p=0.004 n=6)
Main/Sample-15000/config_detail/Without_RLS-4        3.857m ± 1%   3.839m ± 0%  -0.47% (p=0.015 n=6)
Main/Sample-15000/config_detail/With_RLS-4           126.2m ± 1%   124.1m ± 1%  -1.72% (p=0.002 n=6)
Main/Sample-15000/config_names/Without_RLS-4         12.60m ± 1%   12.57m ± 0%  -0.25% (p=0.041 n=6)
Main/Sample-15000/config_names/With_RLS-4            124.4m ± 1%   125.3m ± 1%  +0.67% (p=0.026 n=6)
Main/Sample-15000/config_summary/Without_RLS-4       59.94m ± 1%   59.88m ± 0%       ~ (p=0.818 n=6)
Main/Sample-15000/config_summary/With_RLS-4          742.1m ± 1%   740.3m ± 1%       ~ (p=0.818 n=6)
Main/Sample-15000/configs/Without_RLS-4              7.033m ± 1%   7.085m ± 3%  +0.75% (p=0.015 n=6)
Main/Sample-15000/configs/With_RLS-4                 122.6m ± 1%   123.8m ± 0%  +1.05% (p=0.002 n=6)
Main/Sample-15000/analysis_types/Without_RLS-4       3.809m ± 1%   3.826m ± 0%       ~ (p=0.065 n=6)
Main/Sample-15000/analysis_types/With_RLS-4          3.840m ± 0%   3.824m ± 2%       ~ (p=0.065 n=6)
Main/Sample-15000/analyzer_types/Without_RLS-4       3.695m ± 2%   3.751m ± 1%  +1.50% (p=0.041 n=6)
Main/Sample-15000/analyzer_types/With_RLS-4          3.706m ± 4%   3.700m ± 1%       ~ (p=0.310 n=6)
Main/Sample-15000/change_types/Without_RLS-4         5.112m ± 1%   5.123m ± 0%       ~ (p=0.180 n=6)
Main/Sample-15000/change_types/With_RLS-4            5.111m ± 3%   5.180m ± 0%       ~ (p=0.065 n=6)
Main/Sample-15000/config_classes/Without_RLS-4       3.262m ± 0%   3.264m ± 1%       ~ (p=0.818 n=6)
Main/Sample-15000/config_classes/With_RLS-4          125.9m ± 1%   123.4m ± 0%  -1.98% (p=0.002 n=6)
Main/Sample-15000/config_types/Without_RLS-4         3.894m ± 2%   3.936m ± 4%  +1.09% (p=0.041 n=6)
Main/Sample-15000/config_types/With_RLS-4            123.0m ± 1%   125.6m ± 2%  +2.13% (p=0.002 n=6)
geomean                                              18.98m        18.99m       +0.08%

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai bot commented Feb 6, 2026

Walkthrough

GenerateSchema 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

Cohort / File(s) Summary
Schema Generation Enhancement
schema/openapi/generate.go
Replaces direct jsonschema.Reflect usage with a jsonschema.Reflector flow that resolves the object's import path and package dir, loads Go comments into the reflector, sanitizes comment markers, unwraps wrapper types for lookup, and improves error wrapping. Adds helpers: getObjectPath, addGoComments, sanitizeComments, sanitizeComment, resolveSchemaType, resolvePackageDir. Imports os/exec, reflect, strings.
Test Coverage
schema/openapi/openapi_test.go
Adds a test exercising GenerateSchema for types.ResourceSelector, parsing the produced JSON and asserting that the generated definition includes a description for the agent property.
Schema Definitions
schema/openapi/resource_selector.schema.json, schema/openapi/resource_selectors.schema.json
Adds descriptive description metadata to fields (agent, scope, cache, search, health, types, statuses) without changing structure or validation logic.

Sequence Diagram

sequenceDiagram
    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
Loading

Suggested reviewers

  • moshloop
🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and accurately describes the main change: including Go comments in generated OpenAPI schemas to preserve field documentation.
Linked Issues check ✅ Passed The PR successfully addresses issue #1730 by implementing Go comment extraction into JSONSchema descriptions, enabling context preservation that was missing before.
Out of Scope Changes check ✅ Passed All changes align with the stated objective: GenerateSchema enhancements for comment inclusion, test coverage for schema generation, and schema file regeneration with populated descriptions.
Docstring Coverage ✅ Passed Docstring coverage is 88.89% which is sufficient. The required threshold is 80.00%.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch fix/openapi-schema-comments-1730

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@adityathebe adityathebe requested a review from moshloop February 6, 2026 10:23
Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

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 | 🟡 Minor

Typo: "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.Chdir is process-global — not safe for concurrent use.

If GenerateSchema is ever called concurrently (e.g., generating schemas for multiple types in parallel, or in parallel tests), the os.Chdir will 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
@moshloop moshloop merged commit 15beea2 into main Feb 6, 2026
14 checks passed
@moshloop moshloop deleted the fix/openapi-schema-comments-1730 branch February 6, 2026 13:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

openapi pkg generates JSONSchema without the comments.

2 participants