Skip to content

openapi3gen: inline embedded struct with options-only JSON tag#1229

Merged
fenollp merged 1 commit into
getkin:masterfrom
mvanhorn:fix/924-inline-nameless-embedded
Jul 19, 2026
Merged

openapi3gen: inline embedded struct with options-only JSON tag#1229
fenollp merged 1 commit into
getkin:masterfrom
mvanhorn:fix/924-inline-nameless-embedded

Conversation

@mvanhorn

Copy link
Copy Markdown
Contributor

Summary

openapi3gen emits a nested object property instead of inlining an embedded struct when the embedded field's JSON tag carries options but no name, e.g. *NestedStruct \json:",omitempty"`. This inlines such fields, matching encoding/json`, so the embedded struct's fields are promoted into the parent schema.

Background

encoding/json flattens an anonymous struct field unless its JSON tag gives it an explicit name; json:",omitempty" has options but no name, so it is still inlined. openapi3gen's appendFields only inlined anonymous fields when the tag was completely empty (jsonTag == ""), so a nameless-but-optioned tag fell through and became a nested object keyed by the Go field name. EnriqueL8 (#924) and a second commenter both hit this with the common ?include=a,b,c query-parameter pattern, where the wrapper struct is embedded with json:",omitempty".

The fix reads the tag's name segment with strings.Cut(jsonTag, ",") and, when the name is empty (and the tag is not -), recurses via appendFields exactly as the empty-tag path does. Explicitly-named anonymous fields still become nested objects, and json:"-" is still skipped. Added regression tests for both pointer and value embedded structs with an options-only tag.

Closes #924

@fenollp fenollp changed the title fix(openapi3gen): inline embedded struct with options-only JSON tag openapi3gen: inline embedded struct with options-only JSON tag Jul 19, 2026
@fenollp

fenollp commented Jul 19, 2026

Copy link
Copy Markdown
Collaborator

Thanks!

@fenollp
fenollp merged commit c11a481 into getkin:master Jul 19, 2026
5 checks passed
@mvanhorn

Copy link
Copy Markdown
Contributor Author

Thanks @fenollp - embedded structs with options-only JSON tags inline correctly in openapi3gen now.

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.

openapi3gen: inline golang struct creates incorrect schema

2 participants