Skip to content

fix: support json inline tag for embedding anonymous fields in schema#1006

Open
lsdch wants to merge 1 commit into
danielgtaylor:mainfrom
lsdch:field-embed-inline-fix
Open

fix: support json inline tag for embedding anonymous fields in schema#1006
lsdch wants to merge 1 commit into
danielgtaylor:mainfrom
lsdch:field-embed-inline-fix

Conversation

@lsdch

@lsdch lsdch commented Apr 1, 2026

Copy link
Copy Markdown
Contributor

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.

Copilot AI review requested due to automatic review settings April 1, 2026 08:33
@codecov

codecov Bot commented Apr 1, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 93.15%. Comparing base (2b69054) to head (9b9c015).

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #1006   +/-   ##
=======================================
  Coverage   93.14%   93.15%           
=======================================
  Files          23       23           
  Lines        4901     4906    +5     
=======================================
+ Hits         4565     4570    +5     
  Misses        272      272           
  Partials       64       64           

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copilot AI 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.

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 getFields to consider anonymous fields with json:",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.

Comment thread schema.go Outdated
Comment on lines +707 to +709
if jsonTag := f.Tag.Get("json"); jsonTag == "" || jsonTag == ",inline" {
embedded = append(embedded, f)
continue

Copilot AI Apr 1, 2026

Copy link

Choose a reason for hiding this comment

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

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.

Suggested change
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
}

Copilot uses AI. Check for mistakes.
@wolveix

wolveix commented May 28, 2026

Copy link
Copy Markdown
Collaborator

Hey @lsdch! Would you mind reviewing Copilot's comments, please? :)

@lsdch

lsdch commented May 29, 2026

Copy link
Copy Markdown
Contributor Author

Hi @wolveix, I believe I did in 1debcd3
Sorry, I should have added a comment to signal it 😅
Please let me know if the PR still needs fixing

Comment thread schema.go Outdated
if f.Anonymous && f.Tag.Get("json") == "" {
embedded = append(embedded, f)
continue
if f.Anonymous {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

beware that json/v2 has the true support for inline on non anonymous fields.

type Foo struct {
  X Bar `json:",inline"`
}

A serialization of a Foo will just inline the serialisation of a Bar.

So you better hoist the logic outside the if Anonymous

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

This would be a better fix (totally untested)

		jsonTag := f.Tag.Get("json")
		parts := strings.Split(jsonTag, ",")

		if len(parts) > 0 && parts[0] == "" && slices.Contains(parts[1:], "inline") {
			embedded = append(embedded, f)
			continue
		}

		if f.Anonymous && (len(parts) == 0 || parts[0] == "") {
			embedded = append(embedded, f)
			continue
		}

and it avoid the nasty situation where the user wants his field called inline

@lsdch lsdch force-pushed the field-embed-inline-fix branch from 1debcd3 to 9b9c015 Compare June 6, 2026 21:12
@lsdch

lsdch commented Jun 6, 2026

Copy link
Copy Markdown
Contributor Author

Thanks @fredDJSonos for the review !
I used the patch you suggested, and added a test for this case.

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.

4 participants