Honor json:"-" on embedded fields during initial field walk#113
Conversation
The first field-walk pass in GetReference resolved embedded types before checking their struct tag, so an embed marked `json:"-"` was still pulled into the type graph even though it would be omitted from the schema. With a qualified pointer embed (e.g. `*pkg.Type` `json:"-"`) the inner type assertion to `*ast.Ident` also fails silently, passing nil to resolveType and panicking on `typ.Obj`. The nested-walk loop further down already skips `json:"-"`; apply the same check in the first pass so the embed is never resolved and the schema-side filter remains the single source of truth for tag handling.
Coverage Report for CI Build 26028825186Coverage increased (+0.3%) to 53.695%Details
Uncovered ChangesNo uncovered changes found. Coverage RegressionsNo coverage regressions found. Coverage Stats
💛 - Coveralls |
type PaymentMethod struct {
*stripe.PaymentMethod `json:"-"`
...
}Interesting, I wasn't aware you could add JSON tags in embedded references 🤔 But it sounds reasonable since you don't have to change the Stripe SDK. Have you tried the https://github.com/Teamwork/kommentaar/blob/master/doc/syntax.markdown#parameter-properties Like: type PaymentMethod struct {
// {omitdoc}
*stripe.PaymentMethod
...
} |
That also did the trick! Still - I think the fix is still valid as atm one could forget the |
|
Yep, it's a valid scenario yeah 👍
|
The human is talking in this part:
Hey lads, I don't really know kommentaar that well to delve in myself so I got the robots to do it for me. I have come across what I think is a bug but maybe I'm missing something intentional in kommentaars design (go panic aside). Either way, would be good to get eyes on this and tell me if we've got a genuine fix here.
We came across this in a private repo, specifically in a case like this:
Where that struct is returned in a handler that we annotate for kommentaar
Running kommentaar against our repo with this would result in a panic:
We did a workaround for the moment by stripping all embedded fields with
json:"-"before kommentaar runs but its probably better its fixed here instead.Robot talking:
Summary
GetReferencewalks struct fields in two passes. The first pass resolves embedded types (so they can be merged later); the second walks nested types for non-embed fields. The second pass already skips fields taggedjson:"-", but the first pass does not — so an embed markedjson:"-"was still resolved despite being excluded from the schema.For an embedded qualified pointer type (e.g.
*pkg.Typewithjson:"-"), the first-passStarExprbranch additionally assertst.X.(*ast.Ident), which fails silently for*ast.SelectorExprand passesniltoresolveType— panicking ontyp.Obj.Reproducing the bug
A common Go pattern that hits this: embed a third-party type for method promotion, hide it from JSON output. To reproduce end-to-end:
1. Create a minimal package:
2. Run kommentaar against
master:Expected on master:
3. Run against this branch:
Expected on this branch (exit 0, valid swagger with the embed correctly excluded):
What changed
docparse/find.go: skipjson:"-"embeds in the first field-walk pass, mirroring the existing skip in the nested-walk pass below. This also sidesteps the unsafe*ast.Identassertion for the most common crash case.docparse/test.go: addtestEmbedJSONDashfixture —*mail.Address \json:"-"`` embedded alongside a regular field.docparse/docparse_test.go: addTestGetReference_EmbedJSONDashcovering the panic regression and assertingmail.Addressis not pulled intoprog.References.To just run the new unit test:
go test -run TestGetReference_EmbedJSONDash ./docparse/Not addressed here
The
t.X.(*ast.Ident)assertion in the first pass is still unsafe — embedding a*pkg.Typewithoutjson:"-"will still nil-panic. Worth a separate fix; kept out of this PR to stay minimal.🤖 Generated with Claude Code