Skip to content

feat(optional): support omitempty, omitzero and pointer fields as optional (v3 too)#2156

Open
Hoffs wants to merge 7 commits into
swaggo:v2from
Hoffs:v2
Open

feat(optional): support omitempty, omitzero and pointer fields as optional (v3 too)#2156
Hoffs wants to merge 7 commits into
swaggo:v2from
Hoffs:v2

Conversation

@Hoffs

@Hoffs Hoffs commented Mar 25, 2026

Copy link
Copy Markdown

Describe the PR
cherry-picked omitempty support from master branch and added omitzero support as well as pointer type to be treated as optional

Additional context
v2 is out of sync with master by a large margin

andyatmiami and others added 4 commits March 25, 2026 10:14
Enhance field parser to recognize 'omitempty' in JSON tags, updating IsRequired logic and adding corresponding test cases. Update README to document the new behavior.

Signed-off-by: Andy Stoneberg <astonebe@redhat.com>
@Hoffs Hoffs changed the title feat(optional): support omitempty, omitzero and pointer fields as optional feat(optional): support omitempty, omitzero and pointer fields as optional (v3 too) Mar 25, 2026
Hoffs and others added 3 commits May 14, 2026 23:31
BuildCustomSchemaV3's ARRAY case was setting result.Spec.AdditionalProperties
when it should have set result.Spec.Items. AdditionalProperties is the
JSON Schema field for maps with arbitrary value types, not for array
element schemas. Downstream code that walks schema.Items.Schema.Spec
(e.g. complementSchema's array branch) nil-derefs when a field uses
`swaggertype:"array,T"` on a Go type whose natural schema isn't already
array-shaped — typically generic wrappers like CommaArray[T].

Aligns with the v2 path, which uses spec.ArrayProperty(schema) to wrap
the item schema with a properly populated Items.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
ShouldSkip was returning true for any field whose json tag was missing
or empty, even though FieldName already falls back to the form tag (and
then the configured naming strategy) when JsonName returns empty. The
short-circuit hid form-only fields entirely — most visibly, query
parameter structs whose fields are bound via `form:"..."` (gin's
ShouldBindQuery convention) disappeared from the generated spec under
--requiredByDefault.

Realign with the v2 behavior: only skip on the `json:"-"` exclude
idiom; absence of a json tag falls through. Extract the predicate as
isJsonIgnored() so the intent reads at the call site.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
The v3 doc writer copied the v2 writer's printDoc, which prepends a
`"schemes": {{ marshal .Schemes }}` line. schemes is a Swagger 2.0 field
with no place in OpenAPI 3.1, so the rendered doc carried a stray
`"schemes": null` that strict 3.1 validators reject. Drop the injection
from the oas3 path; the v2 writer keeps it.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
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.

2 participants