fix: support json inline tag for embedding anonymous fields in schema#1006
fix: support json inline tag for embedding anonymous fields in schema#1006lsdch wants to merge 2 commits intodanielgtaylor:mainfrom
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #1006 +/- ##
==========================================
+ Coverage 93.07% 93.08% +0.01%
==========================================
Files 23 23
Lines 4780 4787 +7
==========================================
+ Hits 4449 4456 +7
Misses 271 271
Partials 60 60 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
Pull request overview
Updates schema generation to treat json:",inline" on anonymous (embedded) Go struct fields as an instruction to inline/merge the embedded fields into the parent object schema, matching expectations for redundant-but-valid inline tagging.
Changes:
- Adjust
getFieldsto consider anonymous fields withjson:",inline"as embedded/inlined. - Add a schema test case validating inlining behavior when
json:",inline"is present.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| schema.go | Extends embedded-field detection to include json:",inline" tags. |
| schema_test.go | Adds coverage for the new json:",inline" embedded-field behavior. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
schema.go
Outdated
| if jsonTag := f.Tag.Get("json"); jsonTag == "" || jsonTag == ",inline" { | ||
| embedded = append(embedded, f) | ||
| continue |
There was a problem hiding this comment.
The embedded-field check only treats the JSON tag as inline when it equals exactly ",inline". Struct tags can legally include multiple comma-separated options (e.g. json:",inline,omitempty"), and the rest of this file parses JSON tags by splitting on commas. Consider parsing the tag into name/options and treating the field as embedded when the name part is empty and the options contain inline, so ,inline still works when combined with other options.
| if jsonTag := f.Tag.Get("json"); jsonTag == "" || jsonTag == ",inline" { | |
| embedded = append(embedded, f) | |
| continue | |
| if jsonTag := f.Tag.Get("json"); jsonTag == "" { | |
| embedded = append(embedded, f) | |
| continue | |
| } else { | |
| parts := strings.Split(jsonTag, ",") | |
| name := parts[0] | |
| inline := false | |
| for _, opt := range parts[1:] { | |
| if opt == "inline" { | |
| inline = true | |
| break | |
| } | |
| } | |
| if name == "" && inline { | |
| embedded = append(embedded, f) | |
| continue | |
| } |
Patches #978 to support
json:",inline"tags for embedding anonymous fields.Although a Go embedded field is implicitly inlined, and the presence of this tag on embedded fields is thus redundant, this is still valid syntax and should be supported.