Skip to content

Commit b4fd758

Browse files
openapi3: address review feedback on WalkSchemas
- Visit maps in sorted key order so the traversal is deterministic. - Rename escapeJSONPointerToken to escapeRefString, pairing it with the existing unescapeRefString. - Rename the callback parameter location to jsonPointer and document that it is an RFC 6901 pointer within the resolved document (file-agnostic), not a source location. - Add ExampleT_WalkSchemas demonstrating a real use case (mapping every schema to its location).
1 parent b980f31 commit b4fd758

3 files changed

Lines changed: 150 additions & 90 deletions

File tree

.github/docs/openapi3.txt

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3344,9 +3344,10 @@ func (doc *T) ValidateSchemaJSON(schema *Schema, value any, opts ...SchemaValida
33443344

33453345
func (doc *T) WalkSchemas(fn WalkSchemasFunc) error
33463346
WalkSchemas visits every schema reachable from the document exactly once,
3347-
in document order, invoking fn for each. It follows resolved $ref targets
3348-
(schema.Value) and guards against reference cycles, so each distinct *Schema
3349-
is visited a single time regardless of how many references point at it.
3347+
invoking fn for each. It follows resolved $ref targets (schema.Value) and
3348+
guards against reference cycles, so each distinct *Schema is visited a
3349+
single time regardless of how many references point at it. Maps are visited
3350+
in sorted key order, so the traversal is deterministic.
33503351

33513352
It covers schemas under components (schemas, parameters, headers, request
33523353
bodies, responses, callbacks), the paths and their operations (parameters,
@@ -3686,14 +3687,17 @@ type ValidationOptions struct {
36863687
}
36873688
ValidationOptions provides configuration for validating OpenAPI documents.
36883689

3689-
type WalkSchemasFunc func(location string, schema *SchemaRef) error
3690+
type WalkSchemasFunc func(jsonPointer string, schema *SchemaRef) error
36903691
WalkSchemasFunc is called once for each schema visited by WalkSchemas.
36913692

3692-
location is the JSON Pointer (RFC 6901) of the schema within
3693+
jsonPointer is the RFC 6901 JSON Pointer of the schema within
36933694
the document, e.g. "/components/schemas/Pet/properties/tag" or
3694-
"/paths/~1pets/get/responses/200/content/application~1json/schema". sr is
3695-
never nil and sr.Value is never nil; the callback may modify sr.Value in
3696-
place, so WalkSchemas serves transformers and not only read-only inspection.
3695+
"/paths/~1pets/get/responses/200/content/application~1json/schema". It is
3696+
file-agnostic: once the loader has resolved external $refs the document is
3697+
a single tree, so the pointer addresses a position in that tree and never
3698+
encodes a source file. schema is non-nil and schema.Value is non-nil;
3699+
the callback may modify schema.Value in place, so WalkSchemas serves
3700+
transformers and not only read-only inspection.
36973701

36983702
Returning SkipSubtree skips this schema's sub-schemas; returning any other
36993703
error aborts the walk and is returned by WalkSchemas.

0 commit comments

Comments
 (0)