You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit e56c2c7
Browse filesBrowse the repository at this point in the historyBrowse files
openapi3: aggregate independent validation errors via EnableMultiError (#1185)
* openapi3: aggregate independent validation errors via EnableMultiError
Today Validate returns the first error it encounters and stops, so a
spec with several independent defects only reports one at a time: fix,
re-run, see the next, repeat. With the new EnableMultiError
ValidationOption, container validators aggregate independent problems
and return them together as a flat MultiError.
Aggregation happens at the container fan-out points where it is safe
to continue past an error: T, Paths, PathItem, Operation, Components,
Responses. Leaf validators (Schema, Info, Server, etc.) still fail
fast, since continuing past a leaf error can hit a nil deref.
The result shape is flat: each top-level MultiError entry is one
fully-wrapped chain (SectionValidationError -> PathValidationError ->
OperationValidationError -> typed leaf). Consumers iterating the
MultiError get one entry per problem; consumers using errors.As /
errors.Is work unchanged thanks to MultiError.As / MultiError.Is.
By default Validate returns the first error encountered, byte-identical
to today: the existing test suite passes through the same code path it
always did.
* openapi3: keep "return validateExtensions" pattern in container validators
CI greps for "return .*validateExtensions" in openapi3/ and asserts the
count equals the number of Extensions fields. The previous commit moved
those calls to "me.emit(validateExtensions(...))" + "return me.result()",
breaking the count.
Add a finalize helper on errCollector that emits its argument and returns
the accumulated result, so each container's trailing line is a single
"return me.finalize(validateExtensions(ctx, X.Extensions))" that both
reads cleanly and matches the CI fence regex.
* openapi3: address review feedback on EnableMultiError
Top-level:
- cmd/validate: add --multi flag (mirrors --defaults / --examples / --patterns)
- Convert simple leaves to aggregate independent errors: License, Info,
Server (singular + plural), Tag (singular + plural), ExternalDocs.
Each is sequential independent checks with no guard-then-deref pattern,
structurally identical to the existing container validators. Schema,
Parameter, MediaType have internal cross-field dependencies and are left
for follow-up PRs after per-method analysis.
- Rewrite EnableMultiError docstring: drop the explicit fan-out list (would
go stale), name the transition explicitly, explain why some leaves are
not yet converted, and replace the generic errors.As / errors.Is note
with a concrete how-to.
Inline:
- validate_multi_error_test.go: use require.ErrorContains for chain checks;
tighten GreaterOrEqual to Equal where the count is exact.
- response.go: route Responses.Validate's empty-responses error through
me.emit for consistency with the rest of the multi-error path.
- components.go, paths.go, openapi3.go: add comments at the error-path
continue statements explaining why we skip descending past a malformed
parent key (component name, path key, nil webhook).
- error_collector.go (new): move the errCollector type and methods out of
validation_options.go so the options file stays focused on options.
- openapi3.go: revert to the wrap-variable reassignment pattern per
section block; keep wrapSection as the factory.
* test: hoist err.Error() out of require.Equal to satisfy CI grep fence
* openapi3: wrap plural examples Validate errors in newSchemaValueError
Parameter.Validate and MediaType.Validate already use newSchemaValueError
for the singular `example:` field, but the plural `examples:` map loop
returned a bare fmt.Errorf wrapping the underlying *SchemaError. That
meant errors.As(err, &SchemaValueError) didn't match for plural examples,
so downstream consumers fell back to a generic catch-all instead of
recognising the example-violates-schema cluster.
Wrap the plural-loop result in newSchemaValueError("example", ...) too,
preserving the example key inside the wrap so the rendered message still
names the offending example. Aligns the plural path with the singular
path; both now surface as *SchemaValueError to errors.As consumers.
Existing example_validation_test.go assertions and the apis_guru golden
fixtures gain the "invalid example: " prefix the wrap adds.
* openapi3: point Examples Validate failures at the example's value, not the parent
For plural Examples entries on Parameter/MediaType, the Validate failure
used the parent struct's Origin, which deep-links to the parameter or
media-type start (e.g. the `in: query` line) rather than the offending
example value. Build the SchemaValueError's Origin from the Example
struct's per-field origin (`Origin.Fields["value"]`) when available,
falling back to the example's struct origin, then to the parent.
No message-format change: existing string assertions continue to hold,
only the resolved line/column tightens to the example's `value:` field.
* openapi3: keep validating extensions when responses is empty
The empty-responses path used 'return me.result()' which short-circuited
before validateExtensions could run, so extension errors would never
aggregate with the empty-responses finding under multi mode. Drop the
early return and let control fall through to the trailing finalize() call;
the for loop is a no-op on an empty Responses, so the only behaviour
change is that validateExtensions now runs.
Spotted by @fenollp in PR review.
* test: regression pin for the empty-responses + extension aggregation fix
Adds TestResponses_Validate_EmptyAndExtensionAggregate which constructs
an empty Responses with a non-x- sibling on Extensions and asserts both
findings surface under EnableMultiError. Reverting the previous commit's
fall-through edit causes this test to fail (one leaf instead of two).
0 commit comments