Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions bundler/bundler_composer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3196,3 +3196,49 @@ func TestBuildSchema_UnresolvedReferenceReturnsError(t *testing.T) {
require.ErrorContains(t, err, "#/missing")
require.Nil(t, schema)
}

// TestBundleBytesComposed_AnchorRefsIntoPrefixItems checks that plain-name $anchor refs
// into a tuple are rewritten to pointers at each prefixItems element, not at the array.
func TestBundleBytesComposed_AnchorRefsIntoPrefixItems(t *testing.T) {
spec := []byte(`openapi: 3.1.0
info:
title: t
version: '1'
paths: {}
components:
schemas:
Tuple:
type: array
prefixItems:
- $anchor: first
type: boolean
- $anchor: second
type: number
T:
type: object
properties:
first:
$ref: '#first'
second:
$ref: '#second'
`)

schemaAt := func(t *testing.T, bundled []byte, prop string) map[string]any {
t.Helper()
var doc map[string]any
require.NoError(t, yaml.Unmarshal(bundled, &doc))
schemas := doc["components"].(map[string]any)["schemas"].(map[string]any)
props := schemas["T"].(map[string]any)["properties"].(map[string]any)
return props[prop].(map[string]any)
}

composed, err := BundleBytesComposed(spec, &datamodel.DocumentConfiguration{}, nil)
require.NoError(t, err)
assert.Equal(t, "#/components/schemas/Tuple/prefixItems/0", schemaAt(t, composed, "first")["$ref"])
assert.Equal(t, "#/components/schemas/Tuple/prefixItems/1", schemaAt(t, composed, "second")["$ref"])

inlined, err := BundleBytes(spec, &datamodel.DocumentConfiguration{})
require.NoError(t, err)
assert.Equal(t, "boolean", schemaAt(t, inlined, "first")["type"])
assert.Equal(t, "number", schemaAt(t, inlined, "second")["type"])
}
319 changes: 319 additions & 0 deletions document_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2600,3 +2600,322 @@ func TestDocument_BuildV3Model_NoVersionNode(t *testing.T) {
require.Error(t, buildErr)
assert.Contains(t, buildErr.Error(), "no openapi version/tag found")
}

// A plain-name fragment such as "#foo" names an $anchor in the schema resource
// it appears in, not a JSON Pointer.
func TestDocument_BuildV3Model_SchemaAnchorRefs(t *testing.T) {
spec := `openapi: 3.1.0
info: {title: anchors, version: "1"}
paths: {}
components:
schemas:
A:
$anchor: foo
type: string
B:
type: object
properties:
local: {$ref: '#foo'}
encoded: {$ref: '#f%6Fo'}
R:
$id: https://example.com/r
type: object
$defs:
X: {$anchor: bar, type: integer}
properties:
scoped: {$ref: '#bar'}
absolute: {$ref: 'https://example.com/r#bar'}
encoded: {$ref: '#b%61r'}
U:
type: object
properties:
outside: {$ref: 'https://example.com/r#bar'}
encodedOutside: {$ref: 'https://example.com/r#b%61r'}
Tuple:
type: array
prefixItems:
- {$anchor: first, type: boolean}
- {$anchor: second, type: number}
T:
type: object
properties:
first: {$ref: '#first'}
second: {$ref: '#second'}
`
doc, err := NewDocument([]byte(spec))
require.NoError(t, err)
m, err := doc.BuildV3Model()
require.NoError(t, err)
assert.Empty(t, m.Index.GetReferenceIndexErrors())

schemas := m.Model.Components.Schemas
for _, tc := range []struct{ schema, property, kind string }{
{"B", "local", "string"},
{"B", "encoded", "string"},
{"R", "scoped", "integer"},
{"R", "absolute", "integer"},
{"R", "encoded", "integer"},
{"U", "outside", "integer"},
{"U", "encodedOutside", "integer"},
{"T", "first", "boolean"},
{"T", "second", "number"},
} {
t.Run(tc.schema+"."+tc.property, func(t *testing.T) {
prop := schemas.GetOrZero(tc.schema).Schema().Properties.GetOrZero(tc.property)
require.NotNil(t, prop)
s := prop.Schema()
require.NotNil(t, s, prop.GetBuildError())
assert.Equal(t, []string{tc.kind}, s.Type)
})
}

mapped := m.Index.GetMappedReferences()
assert.Contains(t, mapped, "#/components/schemas/Tuple/prefixItems/0")
assert.Contains(t, mapped, "#/components/schemas/Tuple/prefixItems/1")
}

// a default payload is an instance, not a schema, so its $anchor does not take the name from the
// schema declared after it.
func TestDocument_BuildV3Model_SchemaAnchorAfterDefaultPayload(t *testing.T) {
spec := `openapi: 3.1.0
info: {title: anchors, version: "1"}
paths: {}
components:
schemas:
Choice:
type: object
default: {$anchor: selected, type: integer}
Selected:
$anchor: selected
type: string
Uses:
type: object
properties:
pick: {$ref: '#selected'}
`
doc, err := NewDocument([]byte(spec))
require.NoError(t, err)
m, err := doc.BuildV3Model()
require.NoError(t, err)
assert.Empty(t, m.Index.GetReferenceIndexErrors())

prop := m.Model.Components.Schemas.GetOrZero("Uses").Schema().Properties.GetOrZero("pick")
require.NotNil(t, prop)
s := prop.Schema()
require.NotNil(t, s, prop.GetBuildError())
assert.Equal(t, []string{"string"}, s.Type)
}

// an $id under an extension or a default payload still sets the base URI for the refs inside it.
func TestDocument_BuildV3Model_SchemaIdInExtensionAndDefault(t *testing.T) {
const head = "openapi: 3.1.0\ninfo: {title: ids, version: \"1\"}\npaths: {}\n"
build := func(t *testing.T, spec string) {
doc, err := NewDocument([]byte(head + spec))
require.NoError(t, err)
m, err := doc.BuildV3Model()
require.NoError(t, err)
assert.Empty(t, m.Index.GetReferenceIndexErrors())
}

t.Run("ref inside an $id resource under an extension", func(t *testing.T) {
build(t, `x-vendor:
Resource:
$id: 'https://example.com/vendor'
type: object
$defs:
S: {type: string}
properties:
s: {$ref: '#/$defs/S'}
`)
})

t.Run("ref inside an $id resource under a default", func(t *testing.T) {
build(t, `components:
schemas:
Holder:
type: object
default:
$id: 'https://example.com/default'
type: object
$defs:
S: {type: string}
properties:
s: {$ref: '#/$defs/S'}
`)
})
}

// a Link Object's parameter values and request body, and an extension on an operation's Responses
// Object, are data, so an $anchor there does not shadow the schema that declares it.
func TestDocument_BuildV3Model_SchemaAnchorInLinkAndResponsesExtension(t *testing.T) {
const link = `
links:
L:
operationId: getP
`
for name, c := range map[string]struct{ paths, links string }{
"link parameter value": {paths: link + ` parameters:
p: {$anchor: thing, type: integer}
`},
"link request body": {paths: link + ` requestBody: {$anchor: thing, type: integer}
`},
"responses extension": {paths: `
x-shadow:
content:
application/json:
schema: {$anchor: thing, type: integer}
`},
"component link parameter value": {links: ` links:
L:
operationId: getP
parameters:
p: {$anchor: thing, type: integer}
`},
} {
t.Run(name, func(t *testing.T) {
spec := `openapi: 3.1.0
info: {title: anchors, version: "1"}
paths:
/p:
get:
operationId: getP
responses:
"200":
description: ok` + c.paths + `
components:
` + c.links + ` schemas:
Thing: {$anchor: thing, type: string}
Uses:
type: object
properties:
thing: {$ref: '#thing'}
`
doc, err := NewDocument([]byte(spec))
require.NoError(t, err)
m, err := doc.BuildV3Model()
require.NoError(t, err)
assert.Empty(t, m.Index.GetReferenceIndexErrors())

prop := m.Model.Components.Schemas.GetOrZero("Uses").Schema().Properties.GetOrZero("thing")
require.NotNil(t, prop)
s := prop.Schema()
require.NotNil(t, s, prop.GetBuildError())
assert.Equal(t, []string{"string"}, s.Type)
})
}
}

// a key holding "~" or "/" is escaped in the definition path, and a ref to an anchor beneath it
// still resolves.
func TestDocument_BuildV3Model_SchemaAnchorUnderTildeKey(t *testing.T) {
spec := `openapi: 3.1.0
info: {title: anchors, version: "1"}
paths: {}
components:
schemas:
A:
type: object
properties:
a~b: {$anchor: tilde, type: string}
c/d~1e: {$anchor: slash, type: integer}
Uses:
type: object
properties:
t: {$ref: '#tilde'}
s: {$ref: '#slash'}
`
doc, err := NewDocument([]byte(spec))
require.NoError(t, err)
m, err := doc.BuildV3Model()
require.NoError(t, err)
assert.Empty(t, m.Index.GetReferenceIndexErrors())

for ref, want := range map[string]struct{ path, prop, kind string }{
"#tilde": {path: "#/components/schemas/A/properties/a~0b", prop: "t", kind: "string"},
"#slash": {path: "#/components/schemas/A/properties/c~1d~01e", prop: "s", kind: "integer"},
} {
resolved := m.Index.ResolveRefViaSchemaId(ref)
require.NotNil(t, resolved, ref)
assert.Equal(t, want.path, resolved.Definition, ref)

prop := m.Model.Components.Schemas.GetOrZero("Uses").Schema().Properties.GetOrZero(want.prop)
require.NotNil(t, prop, ref)
s := prop.Schema()
require.NotNil(t, s, prop.GetBuildError())
assert.Equal(t, []string{want.kind}, s.Type, ref)
}
props := m.Model.Components.Schemas.GetOrZero("A").Schema().Properties
assert.Equal(t, []string{"string"}, props.GetOrZero("a~b").Schema().Type)
assert.Equal(t, []string{"integer"}, props.GetOrZero("c/d~1e").Schema().Type)
}

// an $anchor inside an $id resource is classified by its path from the resource's root, so a
// resource under an extension or a default keeps its anchors, while data inside the resource still
// declares none.
func TestDocument_BuildV3Model_SchemaAnchorInsideIdResource(t *testing.T) {
const head = "openapi: 3.1.0\ninfo: {title: anchors, version: \"1\"}\npaths: {}\n"
build := func(t *testing.T, spec string) *DocumentModel[v3high.Document] {
doc, err := NewDocument([]byte(head + spec))
require.NoError(t, err)
m, err := doc.BuildV3Model()
require.NoError(t, err)
assert.Empty(t, m.Index.GetReferenceIndexErrors())
return m
}
kind := func(t *testing.T, m *DocumentModel[v3high.Document], property string) []string {
prop := m.Model.Components.Schemas.GetOrZero("Uses").Schema().Properties.GetOrZero(property)
require.NotNil(t, prop, property)
s := prop.Schema()
require.NotNil(t, s, prop.GetBuildError())
return s.Type
}

t.Run("resource under an extension", func(t *testing.T) {
m := build(t, `x-vendor:
Thing: {$id: 'https://example.com/thing', type: object, $defs: {S: {$anchor: s, type: string}}}
components:
schemas:
Uses:
type: object
properties:
byId: {$ref: 'https://example.com/thing'}
byPointer: {$ref: 'https://example.com/thing#/$defs/S'}
byAnchor: {$ref: 'https://example.com/thing#s'}
`)
assert.Equal(t, []string{"object"}, kind(t, m, "byId"))
assert.Equal(t, []string{"string"}, kind(t, m, "byPointer"))
assert.Equal(t, []string{"string"}, kind(t, m, "byAnchor"))
})

t.Run("default inside a resource under an extension", func(t *testing.T) {
m := build(t, `x-vendor:
Thing:
$id: 'https://example.com/thing'
default: {$anchor: s, type: integer}
$defs:
S: {$anchor: s, type: string}
components:
schemas:
Uses:
type: object
properties:
byAnchor: {$ref: 'https://example.com/thing#s'}
`)
assert.Equal(t, []string{"string"}, kind(t, m, "byAnchor"))
})

t.Run("resource in a default payload", func(t *testing.T) {
m := build(t, `components:
schemas:
Holder:
type: object
default: {$id: 'https://example.com/payload', $anchor: thing, type: integer}
Thing: {$anchor: thing, type: string}
Uses:
type: object
properties:
thing: {$ref: '#thing'}
`)
assert.Equal(t, []string{"string"}, kind(t, m, "thing"))
})
}
Loading
Loading