Skip to content

feat: json:omitempty marks field as optional - #2041

Merged
sdghchj merged 1 commit into
swaggo:masterfrom
andyatmiami:feat/omitempty-as-optional
Jul 16, 2025
Merged

sdghchj merged 1 commit into
swaggo:masterfrom
andyatmiami:feat/omitempty-as-optional

Conversation

@andyatmiami

@andyatmiami andyatmiami commented Jul 15, 2025 •

Copy link
Copy Markdown
Contributor

Describe the PR

  • Enhances field parser to recognize 'omitempty' in json: tag as required: false, updating IsRequired logic (following existing pattern present in parsing logic) and adding corresponding test case.
  • Update README to document the new behavior w.r.t json:omitempty

Relation issue
Related: #2040

Additional context
While I believe this behavior makes the most sense to use in conjunction with the --requiredByDefault flag - there is no hard requirement or dependency to force both these options to co-exist. Implementation gives preference to existing supported tag values ( binding + validate) so anything presently relying on them for required calculation is unaffected by this change.

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>
@sdghchj
sdghchj merged commit 15a183b into swaggo:master Jul 16, 2025
@ant1g

ant1g commented Dec 3, 2025

Copy link
Copy Markdown

@sdghchj could this also be merged to the v2 branch? Thanks

pborgen pushed a commit to pborgen/swag that referenced this pull request Jan 8, 2026
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>
@ant1g

ant1g commented Jan 14, 2026

Copy link
Copy Markdown

Any chance this can get prioritized somehow? I can propose the same PR against the v2 branch? Thanks!

@sdghchj

sdghchj commented Jan 14, 2026

Copy link
Copy Markdown
Member

@ant1g PR is welcome.

Hoffs pushed a commit to Hoffs/swag that referenced this pull request Mar 25, 2026
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>
racciari added a commit to racciari/swag that referenced this pull request Oct 6, 2026
)

With --requiredByDefault, v1 treats a field whose json tag carries
omitempty as optional. The v2 branch forked before swaggo#2041, so both
tagBaseFieldParser.IsRequired and tagBaseFieldParserV3.IsRequired still
return RequiredByDefault for such fields. An explicit binding/validate
required or optional still takes precedence.
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.

3 participants

⌂ 换网址