Optional: infer required fields from Go types#112
Merged
Conversation
When enabled, struct fields used in response/body schemas are added to
the parent's `required` array unless the field is a pointer, has
`omitempty` in its struct tag, or carries an explicit `{optional}` doc
tag. Path/query/form parameters are unaffected — they have their own
required handling.
Off by default; opt in via `infer-required true` in the config.
Coverage Report for CI Build 25836839371Coverage increased (+0.4%) to 53.429%Details
Uncovered Changes
Coverage RegressionsNo coverage regressions found. Coverage Stats
💛 - Coveralls |
rafaeljusto
approved these changes
May 14, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Adds an opt-in
infer-requiredconfig flag that marks response/body struct fields asrequiredin the generated schema when the Go type implies presence:omitemptyin the struct tag{optional}doc tagPath, query, and form parameters keep their existing required handling (untouched).
Default is off — fully backwards compatible. Existing
{required}doc tags continue to work and stack with inference.Motivation
In
projectsapigo/swagger.yamltoday, every field on every response view is optional, because kommentaar only emitsrequiredfrom explicit{required}doc tags. For a typical view struct like:…all five fields show up optional in the spec, even though
id,name, andcodeare always populated. Consumers can't trust the spec, and we'd have to hand-annotate thousands of fields to fix it.This flag lets us flip that on per-project (or per-section, eventually) and have the Go type system do the work.
Behaviour
With
infer-required true:F stringF string \json:"f"``F *stringF string \json:"f,omitempty"``F stringwith{optional}docF *stringwith{required}docTests
testdata/openapi2/src/infer-required/— end-to-end fixture covering every branch (pointer, omitempty,{optional},{required}on a pointer, plain field, query-param struct that must not be affected).TestIsInferredRequired— unit test for the helper across 9 cases.go test ./...) green.Discussion points for the API team
infer-requiredvsrequired-by-defaultvs something else.Marked as draft pending that discussion.