From 665238242914ee210966fcb6e3cb69006e99b0be Mon Sep 17 00:00:00 2001 From: Thomas Date: Fri, 13 Nov 2026 00:39:40 +1100 Subject: [PATCH 1/3] fix(base): keep unevaluated keywords beside allOf for sibling refs The sibling-ref transformer moved every sibling of a $ref into allOf member 0. unevaluatedProperties and unevaluatedItems only see what adjacent keywords evaluated, so inside that member they could no longer see the properties and items the $ref target evaluated. A schema like {$ref: Base, unevaluatedProperties: false} then rejected every property Base declares. Keep both keywords on the wrapper, beside allOf. The other siblings still go into member 0, and when unevaluated* are the only siblings, allOf holds just the $ref. The high-level semantic view does the same, and renderTransformedRefWithSiblings still collapses the wrapper back to $ref plus siblings, so rendered documents keep their authored shape. --- datamodel/high/base/schema_proxy.go | 64 +++++--- datamodel/high/base/schema_proxy_test.go | 137 +++++++++++++++++- datamodel/low/base/schema_proxy.go | 3 +- datamodel/low/base/sibling_ref_transformer.go | 32 +++- .../low/base/sibling_ref_transformer_test.go | 69 +++++++++ document_test.go | 62 ++++++++ renderer/schema_renderer_test.go | 43 ++++++ 7 files changed, 389 insertions(+), 21 deletions(-) diff --git a/datamodel/high/base/schema_proxy.go b/datamodel/high/base/schema_proxy.go index fe4a2461..f8893a75 100644 --- a/datamodel/high/base/schema_proxy.go +++ b/datamodel/high/base/schema_proxy.go @@ -672,6 +672,8 @@ func (sp *SchemaProxy) buildSiblingOnlySchemaView() *Schema { // BuildTransformedRefSemanticSchema returns the internal semantic allOf view for // an authored $ref-with-siblings proxy, using current high-level sibling values. +// unevaluatedProperties and unevaluatedItems sit beside allOf, so when they are the +// only siblings, allOf holds just the $ref. func (sp *SchemaProxy) BuildTransformedRefSemanticSchema(currentSibling *Schema) (*Schema, error) { return sp.buildSemanticAllOfSchemaView(currentSibling) } @@ -689,10 +691,25 @@ func (sp *SchemaProxy) buildSemanticAllOfSchemaView(currentSibling *Schema) (*Sc if currentSibling == nil { currentSibling = sp.Schema() } - if currentSibling != nil && len(schema.AllOf) == 2 { + if currentSibling != nil { siblingCopy := *currentSibling siblingCopy.ParentProxy = nil - schema.AllOf[0] = CreateSchemaProxy(&siblingCopy) + + // unevaluated* stay beside allOf so they see what the $ref target evaluated. + schema.UnevaluatedProperties = siblingCopy.UnevaluatedProperties + schema.UnevaluatedItems = siblingCopy.UnevaluatedItems + siblingCopy.UnevaluatedProperties = nil + siblingCopy.UnevaluatedItems = nil + + switch len(schema.AllOf) { + case 2: + schema.AllOf[0] = CreateSchemaProxy(&siblingCopy) + case 1: + // every authored sibling was an unevaluated* keyword, so allOf only holds the $ref. + if len(high.NewNodeBuilder(&siblingCopy, nil).Render().Content) > 0 { + schema.AllOf = []*SchemaProxy{CreateSchemaProxy(&siblingCopy), schema.AllOf[0]} + } + } } return schema, nil } @@ -724,28 +741,41 @@ func (sp *SchemaProxy) renderTransformedRefWithSiblings(s *Schema) (*yaml.Node, ref := sp.schema.Value.GetTransformedRefReference() if !sp.schemaIsTransformedSiblingView(s) { - if len(s.AllOf) != 2 || s.AllOf[0] == nil || s.AllOf[1] == nil || !s.AllOf[1].IsReference() { + refMember := len(s.AllOf) - 1 + if refMember < 0 || refMember > 1 || s.AllOf[0] == nil || !s.AllOf[refMember].IsReference() { return nil, false, nil } - // Only collapse the synthetic allOf created by the sibling-ref transformer. - // If callers add fields to the outer schema or change its composition, keep - // the explicit allOf so no mutations are hidden. + // Only collapse the synthetic allOf created by the sibling-ref transformer, + // which may hold unevaluated* beside allOf. If callers add other fields to the + // outer schema or change its composition, keep the explicit allOf so no + // mutations are hidden. outerNode := high.NewNodeBuilder(s, s.low).Render() - if len(outerNode.Content) != 2 || outerNode.Content[0].Value != "allOf" { - return nil, false, nil + var outerSiblings []*yaml.Node + for i := 0; i+1 < len(outerNode.Content); i += 2 { + switch key := outerNode.Content[i].Value; key { + case base.AllOfLabel: + case base.UnevaluatedPropertiesLabel, base.UnevaluatedItemsLabel: + outerSiblings = append(outerSiblings, outerNode.Content[i], outerNode.Content[i+1]) + default: + return nil, false, nil + } } - siblingRender, err := s.AllOf[0].MarshalYAML() - if err != nil { - return nil, true, err - } - var ok bool - siblingNode, ok = yamlNodeFromRender(siblingRender) - if !ok || !utils.IsNodeMap(siblingNode) { - return nil, false, nil + siblingNode = utils.CreateEmptyMapNode() + if refMember == 1 { + siblingRender, err := s.AllOf[0].MarshalYAML() + if err != nil { + return nil, true, err + } + var ok bool + siblingNode, ok = yamlNodeFromRender(siblingRender) + if !ok || !utils.IsNodeMap(siblingNode) { + return nil, false, nil + } } - ref = s.AllOf[1].GetReference() + siblingNode.Content = append(siblingNode.Content, outerSiblings...) + ref = s.AllOf[refMember].GetReference() } else { siblingNode = high.NewNodeBuilder(s, s.low).Render() } diff --git a/datamodel/high/base/schema_proxy_test.go b/datamodel/high/base/schema_proxy_test.go index c1fb2591..84e6ff30 100644 --- a/datamodel/high/base/schema_proxy_test.go +++ b/datamodel/high/base/schema_proxy_test.go @@ -2046,6 +2046,136 @@ func TestSchemaProxy_ParsedTransformedRefWithSiblingsInlinePreservesSemantics(t assert.Contains(t, string(inlineOut), "type: string") } +func TestSchemaProxy_ParsedTransformedRefWithSiblingsKeepsUnevaluatedBesideAllOf(t *testing.T) { + spec := `openapi: 3.1.0 +paths: {} +components: + schemas: + Thing: + type: object + properties: + a: + type: integer + Pair: + type: array + prefixItems: + - type: integer + Holder: + type: object + properties: + thing: + $ref: '#/components/schemas/Thing' + required: [a] + unevaluatedProperties: false + only: + $ref: '#/components/schemas/Thing' + unevaluatedProperties: false + list: + $ref: '#/components/schemas/Pair' + unevaluatedItems: + type: string +` + keys := func(t *testing.T, rendered any) []string { + t.Helper() + node, ok := rendered.(*yaml.Node) + require.True(t, ok) + var out []string + for i := 0; i+1 < len(node.Content); i += 2 { + out = append(out, node.Content[i].Value) + } + return out + } + + t.Run("with other siblings", func(t *testing.T) { + sp := buildParsedSiblingRefProxyFromSpec(t, spec, 3.1, "thing") + sibling := sp.Schema() + require.NotNil(t, sibling) + require.NotNil(t, sibling.UnevaluatedProperties) + assert.False(t, sibling.UnevaluatedProperties.B) + assert.Equal(t, []string{"a"}, sibling.Required) + + semantic, err := sp.BuildTransformedRefSemanticSchema(sibling) + require.NoError(t, err) + require.NotNil(t, semantic.UnevaluatedProperties) + assert.False(t, semantic.UnevaluatedProperties.B) + require.Len(t, semantic.AllOf, 2) + assert.Nil(t, semantic.AllOf[0].Schema().UnevaluatedProperties) + assert.Equal(t, []string{"a"}, semantic.AllOf[0].Schema().Required) + assert.True(t, semantic.AllOf[1].IsReference()) + + inline, err := sp.MarshalYAMLInline() + require.NoError(t, err) + assert.Equal(t, []string{"allOf", "unevaluatedProperties"}, keys(t, inline)) + + rendered, err := sp.MarshalYAML() + require.NoError(t, err) + assert.Equal(t, []string{"$ref", "required", "unevaluatedProperties"}, keys(t, rendered)) + }) + + t.Run("only unevaluated siblings", func(t *testing.T) { + sp := buildParsedSiblingRefProxyFromSpec(t, spec, 3.1, "only") + sibling := sp.Schema() + require.NotNil(t, sibling) + + semantic, err := sp.BuildTransformedRefSemanticSchema(sibling) + require.NoError(t, err) + require.NotNil(t, semantic.UnevaluatedProperties) + require.Len(t, semantic.AllOf, 1) + assert.True(t, semantic.AllOf[0].IsReference()) + + // the allOf view collapses back to the authored shape. + semantic.ParentProxy = sp + node, ok, err := sp.renderTransformedRefWithSiblings(semantic) + require.NoError(t, err) + require.True(t, ok) + assert.Equal(t, []string{"$ref", "unevaluatedProperties"}, keys(t, node)) + + // a keyword added to the sibling view joins allOf. + sibling.Required = []string{"a"} + semantic, err = sp.BuildTransformedRefSemanticSchema(sibling) + require.NoError(t, err) + require.Len(t, semantic.AllOf, 2) + assert.Equal(t, []string{"a"}, semantic.AllOf[0].Schema().Required) + assert.Nil(t, semantic.AllOf[0].Schema().UnevaluatedProperties) + assert.True(t, semantic.AllOf[1].IsReference()) + + rendered, err := sp.MarshalYAML() + require.NoError(t, err) + assert.Equal(t, []string{"$ref", "unevaluatedProperties", "required"}, keys(t, rendered)) + }) + + t.Run("other outer keywords keep the allOf", func(t *testing.T) { + sp := buildParsedSiblingRefProxyFromSpec(t, spec, 3.1, "only") + semantic, err := sp.BuildTransformedRefSemanticSchema(sp.Schema()) + require.NoError(t, err) + semantic.ParentProxy = sp + semantic.Title = "outer" + + node, ok, err := sp.renderTransformedRefWithSiblings(semantic) + require.NoError(t, err) + assert.False(t, ok) + assert.Nil(t, node) + }) + + t.Run("unevaluatedItems", func(t *testing.T) { + sp := buildParsedSiblingRefProxyFromSpec(t, spec, 3.1, "list") + semantic, err := sp.BuildTransformedRefSemanticSchema(sp.Schema()) + require.NoError(t, err) + require.NotNil(t, semantic.UnevaluatedItems) + assert.Equal(t, []string{"string"}, semantic.UnevaluatedItems.Schema().Type) + require.Len(t, semantic.AllOf, 1) + assert.True(t, semantic.AllOf[0].IsReference()) + + inline, err := sp.MarshalYAMLInline() + require.NoError(t, err) + assert.Equal(t, []string{"allOf", "unevaluatedItems"}, keys(t, inline)) + + rendered, err := sp.MarshalYAML() + require.NoError(t, err) + assert.Equal(t, []string{"$ref", "unevaluatedItems"}, keys(t, rendered)) + }) +} + func TestSchemaProxy_ParsedTransformedRefWithSiblingsJSONPreservesReference(t *testing.T) { sp := buildParsedSiblingRefProxy(t, 3.1) schema := sp.Schema() @@ -2212,6 +2342,11 @@ components: description: original description title: Original title ` + return buildParsedSiblingRefProxyFromSpec(t, spec, version, "thing") +} + +func buildParsedSiblingRefProxyFromSpec(t *testing.T, spec string, version float32, property string) *SchemaProxy { + t.Helper() var root yaml.Node require.NoError(t, yaml.Unmarshal([]byte(spec), &root)) @@ -2222,7 +2357,7 @@ components: cfg.TransformSiblingRefs = true idx := index.NewSpecIndexWithConfig(rootNode, cfg) - refNode := findHighSchemaTestNode(t, rootNode, "components", "schemas", "Holder", "properties", "thing") + refNode := findHighSchemaTestNode(t, rootNode, "components", "schemas", "Holder", "properties", property) lowProxy := new(lowbase.SchemaProxy) require.NoError(t, lowProxy.Build(context.Background(), nil, refNode, idx)) require.NotNil(t, lowProxy.TransformedRef) diff --git a/datamodel/low/base/schema_proxy.go b/datamodel/low/base/schema_proxy.go index 4c2614f6..b75e8f1b 100644 --- a/datamodel/low/base/schema_proxy.go +++ b/datamodel/low/base/schema_proxy.go @@ -171,7 +171,8 @@ func (sp *SchemaProxy) GetTransformedRefReference() string { } // GetTransformedRefAllOfSchema returns the internal allOf schema for an -// authored $ref-with-siblings node. +// authored $ref-with-siblings node. unevaluatedProperties and unevaluatedItems +// sit beside allOf, so when they are the only siblings, allOf holds just the $ref. func (sp *SchemaProxy) GetTransformedRefAllOfSchema() *yaml.Node { if !sp.IsTransformedRefWithSiblings() { return nil diff --git a/datamodel/low/base/sibling_ref_transformer.go b/datamodel/low/base/sibling_ref_transformer.go index 38c9d31d..151d8715 100644 --- a/datamodel/low/base/sibling_ref_transformer.go +++ b/datamodel/low/base/sibling_ref_transformer.go @@ -36,6 +36,12 @@ func NewSiblingRefTransformer(idx *index.SpecIndex) *SiblingRefTransformer { // // Input: {title: "MySchema", $ref: "#/components/schemas/Base"} // Output: {allOf: [{title: "MySchema"}, {$ref: "#/components/schemas/Base"}]} +// +// unevaluatedProperties and unevaluatedItems stay beside allOf rather than moving into it, +// because they only see what adjacent keywords evaluated, and that includes the $ref target: +// +// Input: {$ref: "#/components/schemas/Base", unevaluatedProperties: false} +// Output: {allOf: [{$ref: "#/components/schemas/Base"}], unevaluatedProperties: false} func (srt *SiblingRefTransformer) TransformSiblingRef(node *yaml.Node) (*yaml.Node, error) { transformed := srt.transformSiblingRefWithMetadata(node) if transformed == nil { @@ -93,8 +99,24 @@ func (srt *SiblingRefTransformer) createAllOfStructureWithSiblingNode(refValue s allOfArrayNode := allOfNode.Content[1] - if siblingSchemaNode != nil && len(siblingSchemaNode.Content) > 0 { - allOfArrayNode.Content = append(allOfArrayNode.Content, siblingSchemaNode) + memberNode := siblingSchemaNode + if siblingSchemaNode != nil { + var kept []*yaml.Node + for i := 0; i+1 < len(siblingSchemaNode.Content); i += 2 { + keyNode, valueNode := siblingSchemaNode.Content[i], siblingSchemaNode.Content[i+1] + if isUnevaluatedKeyword(keyNode.Value) { + allOfNode.Content = append(allOfNode.Content, keyNode, valueNode) + } else { + kept = append(kept, keyNode, valueNode) + } + } + if len(kept) != len(siblingSchemaNode.Content) { + memberNode = &yaml.Node{Kind: yaml.MappingNode, Tag: "!!map", Content: kept} + } + } + + if memberNode != nil && len(memberNode.Content) > 0 { + allOfArrayNode.Content = append(allOfArrayNode.Content, memberNode) } refSchemaNode := &yaml.Node{ @@ -110,6 +132,12 @@ func (srt *SiblingRefTransformer) createAllOfStructureWithSiblingNode(refValue s return allOfNode } +// isUnevaluatedKeyword reports whether a sibling keyword has to stay beside allOf to see what +// the $ref target evaluated. +func isUnevaluatedKeyword(key string) bool { + return key == UnevaluatedPropertiesLabel || key == UnevaluatedItemsLabel +} + func (srt *SiblingRefTransformer) createSiblingSchemaNode(node *yaml.Node) *yaml.Node { if !utils.IsNodeMap(node) { return nil diff --git a/datamodel/low/base/sibling_ref_transformer_test.go b/datamodel/low/base/sibling_ref_transformer_test.go index 8fdace3a..a01727ca 100644 --- a/datamodel/low/base/sibling_ref_transformer_test.go +++ b/datamodel/low/base/sibling_ref_transformer_test.go @@ -148,6 +148,26 @@ func TestSiblingRefTransformer_CreateAllOfStructure(t *testing.T) { assert.Equal(t, "#/components/schemas/Base", refSchema.Content[1].Value) }) + t.Run("create allOf with unevaluatedProperties beside it", func(t *testing.T) { + siblings := map[string]*yaml.Node{ + "title": {Kind: yaml.ScalarNode, Value: "Custom Title"}, + "unevaluatedProperties": {Kind: yaml.ScalarNode, Tag: "!!bool", Value: "false"}, + } + + result := transformer.CreateAllOfStructure("#/components/schemas/Base", siblings) + + require.Len(t, result.Content, 4) + assert.Equal(t, "allOf", result.Content[0].Value) + assert.Equal(t, "unevaluatedProperties", result.Content[2].Value) + assert.Equal(t, "false", result.Content[3].Value) + + allOfArray := result.Content[1] + require.Len(t, allOfArray.Content, 2) + require.Len(t, allOfArray.Content[0].Content, 2) + assert.Equal(t, "title", allOfArray.Content[0].Content[0].Value) + assert.Equal(t, "$ref", allOfArray.Content[1].Content[0].Value) + }) + t.Run("create allOf with empty siblings", func(t *testing.T) { siblings := map[string]*yaml.Node{} refValue := "#/components/schemas/Base" @@ -214,6 +234,55 @@ $ref: "#/components/schemas/Base"` assert.Equal(t, "title", partial.Content[0].Value) }) + t.Run("unevaluated keywords stay beside allOf", func(t *testing.T) { + yml := `$ref: "#/components/schemas/Base" +unevaluatedProperties: false +required: [id] +unevaluatedItems: false` + + var node yaml.Node + _ = yaml.Unmarshal([]byte(yml), &node) + actualNode := node.Content[0] + + transformed := transformer.transformSiblingRefWithMetadata(actualNode) + require.NotNil(t, transformed) + result := transformed.allOfNode + + require.Len(t, result.Content, 6) + assert.Equal(t, "allOf", result.Content[0].Value) + assert.Equal(t, "unevaluatedProperties", result.Content[2].Value) + assert.Equal(t, "unevaluatedItems", result.Content[4].Value) + + allOfArray := result.Content[1] + require.Len(t, allOfArray.Content, 2) + require.Len(t, allOfArray.Content[0].Content, 2) + assert.Equal(t, "required", allOfArray.Content[0].Content[0].Value) + assert.Equal(t, "$ref", allOfArray.Content[1].Content[0].Value) + + // the sibling view still holds every authored sibling. + require.Len(t, transformed.siblingNode.Content, 6) + assert.Equal(t, "unevaluatedProperties", transformed.siblingNode.Content[0].Value) + assert.Equal(t, "required", transformed.siblingNode.Content[2].Value) + assert.Equal(t, "unevaluatedItems", transformed.siblingNode.Content[4].Value) + }) + + t.Run("only unevaluated siblings", func(t *testing.T) { + yml := `$ref: "#/components/schemas/Base" +unevaluatedProperties: false` + + var node yaml.Node + _ = yaml.Unmarshal([]byte(yml), &node) + + result, err := transformer.TransformSiblingRef(node.Content[0]) + require.NoError(t, err) + require.Len(t, result.Content, 4) + assert.Equal(t, "allOf", result.Content[0].Value) + require.Len(t, result.Content[1].Content, 1) + assert.Equal(t, "$ref", result.Content[1].Content[0].Content[0].Value) + assert.Equal(t, "unevaluatedProperties", result.Content[2].Value) + assert.Equal(t, "false", result.Content[3].Value) + }) + t.Run("no transformation for ref only", func(t *testing.T) { yml := `$ref: "#/components/schemas/Base"` diff --git a/document_test.go b/document_test.go index f5d7b9f2..7916e645 100644 --- a/document_test.go +++ b/document_test.go @@ -2389,6 +2389,68 @@ components: assert.Contains(t, string(modelBytes), "- $ref: '#/components/schemas/Name'") } +func TestDocument_SiblingRefUnevaluatedKeywordsStayBesideAllOf(t *testing.T) { + spec := `openapi: 3.1.0 +info: + title: Unevaluated sibling refs + version: 1.0.0 +paths: {} +components: + schemas: + Base: + type: object + properties: + a: + type: integer + Closed: + $ref: '#/components/schemas/Base' + unevaluatedProperties: false + Holder: + type: object + properties: + items: + $ref: '#/components/schemas/List' + maxItems: 3 + unevaluatedItems: + type: string + List: + type: array + prefixItems: + - type: integer +` + + doc, err := NewDocument([]byte(spec)) + require.NoError(t, err) + + model, err := doc.BuildV3Model() + require.NoError(t, err) + + closed, err := model.Model.Components.Schemas.GetOrZero("Closed").Schema().RenderInline() + require.NoError(t, err) + assert.Equal(t, `allOf: + - type: object + properties: + a: + type: integer +unevaluatedProperties: false +`, string(closed)) + + items, err := model.Model.Components.Schemas.GetOrZero("Holder").Schema().Properties.GetOrZero("items").Schema().RenderInline() + require.NoError(t, err) + assert.Equal(t, `allOf: + - maxItems: 3 + - type: array + prefixItems: + - type: integer +unevaluatedItems: + type: string +`, string(items)) + + modelBytes, err := model.Model.Render() + require.NoError(t, err) + assert.Equal(t, spec, string(modelBytes)) +} + func TestDocument_Render_OpenAPI30TransformedSiblingRefStaysAllOf(t *testing.T) { spec := `openapi: 3.0.3 info: diff --git a/renderer/schema_renderer_test.go b/renderer/schema_renderer_test.go index 1530bb06..bc736cf1 100644 --- a/renderer/schema_renderer_test.go +++ b/renderer/schema_renderer_test.go @@ -1436,6 +1436,49 @@ components: assert.Equal(t, "from-base", structure["value"]) } +func TestRenderSchema_RefWithUnevaluatedSiblingUsesReferencedShape(t *testing.T) { + spec := `openapi: 3.1.0 +info: + title: Ref Siblings + version: 1.0.0 +paths: {} +components: + schemas: + Base: + type: object + properties: + name: + type: string + example: from-base + Closed: + $ref: '#/components/schemas/Base' + unevaluatedProperties: false + Extended: + $ref: '#/components/schemas/Base' + properties: + tag: + type: string + example: from-sibling + unevaluatedProperties: false +` + + doc, err := libopenapi.NewDocument([]byte(spec)) + assert.NoError(t, err) + model, err := doc.BuildV3Model() + assert.NoError(t, err) + + for name, want := range map[string]map[string]any{ + "Closed": {"name": "from-base"}, + "Extended": {"name": "from-base", "tag": "from-sibling"}, + } { + t.Run(name, func(t *testing.T) { + proxy := model.Model.Components.Schemas.GetOrZero(name) + wr := createSchemaRenderer() + assert.Equal(t, want, wr.RenderSchema(proxy.Schema())) + }) + } +} + func TestRenderSchema_TransformedRefSemanticErrors(t *testing.T) { schema := &highbase.Schema{ParentProxy: malformedRendererTransformedSiblingRefProxy(t)} From a8cafe82472bd9012d960d125f382725e14df3f2 Mon Sep 17 00:00:00 2001 From: Thomas Date: Mon, 16 Nov 2026 01:48:27 +1100 Subject: [PATCH 2/3] fix(base): keep resource keywords beside allOf for sibling refs $id, $schema, $vocabulary, $anchor, $dynamicAnchor, $defs and definitions now stay on the allOf wrapper with unevaluatedProperties and unevaluatedItems, so a relative $ref still resolves against the authored $id, $anchor still names the whole object, and $defs pointers still reach their subschemas. The high-level allOf view moves the same keywords, the collapse back to the $ref form only applies to OpenAPI 3.1 and later, and a boolean sibling renders as allOf: [bool, {$ref}] from every entry point. The member is only copied when a keyword moves. The document test compiles the allOf schema and the inline render with pb33f/jsonschema and checks they validate like the authored schema, which adds github.com/pb33f/jsonschema/v6 to go.mod. --- bundler/bundler_test.go | 97 ++++++ datamodel/high/base/schema.go | 38 +- datamodel/high/base/schema_proxy.go | 129 +++++-- datamodel/high/base/schema_proxy_test.go | 325 +++++++++++++++++- datamodel/low/base/schema_proxy.go | 5 +- datamodel/low/base/sibling_ref_transformer.go | 50 ++- .../low/base/sibling_ref_transformer_test.go | 32 ++ document_test.go | 196 +++++++++++ go.mod | 2 + go.sum | 6 + 10 files changed, 822 insertions(+), 58 deletions(-) diff --git a/bundler/bundler_test.go b/bundler/bundler_test.go index 71be6754..eaaa0aa4 100644 --- a/bundler/bundler_test.go +++ b/bundler/bundler_test.go @@ -3347,3 +3347,100 @@ paths: require.NoError(t, err) require.NotNil(t, bundledBytes) } + +func TestBundleBytes_SiblingRefUnevaluatedKeywordsStayBesideAllOf(t *testing.T) { + tmpDir := t.TempDir() + + rootSpec := `openapi: 3.1.0 +info: {title: bundle, version: '1'} +paths: + /x: + post: + requestBody: + content: + application/json: + schema: + $ref: 'ext.yaml#/components/schemas/Base' + required: [a] + unevaluatedProperties: false + responses: + '200': + description: ok +components: + schemas: + Base: + $id: 'https://example.com/schemas/base' + type: object + Scoped: + $ref: 'base' + $id: 'https://example.com/schemas/scoped' + $defs: + Node: + type: object + properties: + next: + $ref: '#/$defs/Node' + unevaluatedProperties: + $ref: '#/$defs/Node' +` + extSpec := `components: + schemas: + Base: + type: object + properties: + a: + type: integer +` + require.NoError(t, os.WriteFile(filepath.Join(tmpDir, "root.yaml"), []byte(rootSpec), 0o644)) + require.NoError(t, os.WriteFile(filepath.Join(tmpDir, "ext.yaml"), []byte(extSpec), 0o644)) + + cfg := datamodel.NewDocumentConfiguration() + cfg.BasePath = tmpDir + cfg.SpecFilePath = "root.yaml" + cfg.AllowFileReferences = true + bundled, err := BundleBytes([]byte(rootSpec), cfg) + require.NoError(t, err) + + subtree := func(path ...string) string { + t.Helper() + node := lookupMappingNode(t, bundled, path...) + require.NotNil(t, node, strings.Join(path, ".")) + out, err := yaml.Marshal(node) + require.NoError(t, err) + return string(out) + } + + // unevaluatedProperties only sees properties evaluated by adjacent keywords, so it must stay + // beside the allOf; inside an allOf member it would not see what the $ref target evaluated. + assert.Equal(t, `allOf: + - required: + - a + - type: object + properties: + a: + type: integer +unevaluatedProperties: false +`, subtree("paths", "/x", "post", "requestBody", "content", "application/json", "schema")) + + // $id and $defs stay beside it too: 'base' resolves against Scoped's $id to the local Base, + // and '#/$defs/Node' resolves against Scoped's $id. + assert.Equal(t, `allOf: + - $id: 'https://example.com/schemas/base' + type: object +$id: 'https://example.com/schemas/scoped' +$defs: + Node: + type: object + properties: + next: + type: object + properties: + next: + $ref: '#/$defs/Node' +unevaluatedProperties: + type: object + properties: + next: + $ref: '#/$defs/Node' +`, subtree("components", "schemas", "Scoped")) +} diff --git a/datamodel/high/base/schema.go b/datamodel/high/base/schema.go index 78a36448..7df3c28c 100644 --- a/datamodel/high/base/schema.go +++ b/datamodel/high/base/schema.go @@ -540,16 +540,17 @@ func (s *Schema) RenderInline() ([]byte, error) { // MarshalYAML will create a ready to render YAML representation of the Schema object. func (s *Schema) MarshalYAML() (interface{}, error) { - if node := s.booleanSchemaNode(); node != nil { - return node, nil - } - + // the sibling view of a $ref with siblings renders like its proxy, even when it is boolean. if s.ParentProxy != nil { if node, ok, err := s.ParentProxy.renderTransformedRefWithSiblings(s); ok || err != nil { return node, err } } + if node := s.booleanSchemaNode(); node != nil { + return node, nil + } + nb := high.NewNodeBuilder(s, s.low) // determine index version @@ -564,16 +565,19 @@ func (s *Schema) MarshalYAML() (interface{}, error) { // MarshalJSON will create a ready to render JSON representation of the Schema object. func (s *Schema) MarshalJSON() ([]byte, error) { - if s.BooleanSchema != nil { - return json.Marshal(*s.BooleanSchema) - } - - if s.ParentProxy != nil && s.ParentProxy.isParsedRefWithSiblings() { - node, err := s.ParentProxy.referenceYAMLNodeForSchema(s) + // like MarshalYAML, render the full schema when the $ref form cannot hold it. + if s.ParentProxy != nil { + node, ok, err := s.ParentProxy.renderTransformedRefWithSiblings(s) if err != nil { return nil, err } - return marshalYAMLNodeJSON(node) + if ok { + return marshalYAMLNodeJSON(node) + } + } + + if s.BooleanSchema != nil { + return json.Marshal(*s.BooleanSchema) } nb := high.NewNodeBuilder(s, s.low) @@ -596,10 +600,6 @@ func (s *Schema) MarshalJSON() ([]byte, error) { // The ctx parameter should be *InlineRenderContext but is typed as any to satisfy the // high.RenderableInlineWithContext interface without import cycles. func (s *Schema) MarshalYAMLInlineWithContext(ctx any) (interface{}, error) { - if node := s.booleanSchemaNode(); node != nil { - return node, nil - } - // ensure we have a valid render context; create default bundle mode context if nil. // this ensures backward compatibility where nil context = bundle mode behavior. renderCtx, ok := ctx.(*InlineRenderContext) @@ -607,10 +607,15 @@ func (s *Schema) MarshalYAMLInlineWithContext(ctx any) (interface{}, error) { renderCtx = NewInlineRenderContext() ctx = renderCtx } + // the sibling view of a $ref with siblings renders like its proxy, even when it is boolean. if s.ParentProxy != nil && s.ParentProxy.isParsedRefWithSiblings() { return s.ParentProxy.marshalParsedRefWithSiblingsInline(renderCtx, s) } + if node := s.booleanSchemaNode(); node != nil { + return node, nil + } + // determine if we should preserve discriminator refs based on rendering mode. // in validation mode, we need to fully inline all refs for the JSON schema compiler. // in bundle mode (default), we preserve discriminator refs for mapping compatibility. @@ -659,11 +664,12 @@ func (s *Schema) MarshalYAMLInline() (interface{}, error) { // MarshalJSONInline will render out the Schema pointer as JSON, and all refs will be inlined fully func (s *Schema) MarshalJSONInline() ([]byte, error) { - if s.BooleanSchema != nil { + if s.BooleanSchema != nil && !s.ParentProxy.schemaIsTransformedSiblingView(s) { return json.Marshal(*s.BooleanSchema) } if s.ParentProxy != nil && s.ParentProxy.isParsedRefWithSiblings() { + // the sibling view renders like its proxy, even when it is boolean. rendered, err := s.MarshalYAMLInline() if err != nil { return nil, err diff --git a/datamodel/high/base/schema_proxy.go b/datamodel/high/base/schema_proxy.go index f8893a75..401571ef 100644 --- a/datamodel/high/base/schema_proxy.go +++ b/datamodel/high/base/schema_proxy.go @@ -672,8 +672,12 @@ func (sp *SchemaProxy) buildSiblingOnlySchemaView() *Schema { // BuildTransformedRefSemanticSchema returns the internal semantic allOf view for // an authored $ref-with-siblings proxy, using current high-level sibling values. -// unevaluatedProperties and unevaluatedItems sit beside allOf, so when they are the -// only siblings, allOf holds just the $ref. +// $id, $schema, $vocabulary, $anchor, $dynamicAnchor, $defs, unevaluatedProperties and +// unevaluatedItems sit beside allOf, so when they are the only siblings, allOf holds just the +// $ref. When the sibling is a boolean schema, $id through $defs still sit beside allOf, because +// they belong to the object that holds the $ref, but unevaluatedProperties and unevaluatedItems +// are dropped, because a boolean schema replaces every assertion. The low-level allOf node also +// keeps definitions beside allOf, but Schema has no field for it. func (sp *SchemaProxy) BuildTransformedRefSemanticSchema(currentSibling *Schema) (*Schema, error) { return sp.buildSemanticAllOfSchemaView(currentSibling) } @@ -695,18 +699,51 @@ func (sp *SchemaProxy) buildSemanticAllOfSchemaView(currentSibling *Schema) (*Sc siblingCopy := *currentSibling siblingCopy.ParentProxy = nil - // unevaluated* stay beside allOf so they see what the $ref target evaluated. - schema.UnevaluatedProperties = siblingCopy.UnevaluatedProperties - schema.UnevaluatedItems = siblingCopy.UnevaluatedItems - siblingCopy.UnevaluatedProperties = nil - siblingCopy.UnevaluatedItems = nil + // $id through $defs belong to the object that holds the $ref, so they always move beside + // allOf with their current values. unevaluated* are assertions, and a boolean sibling + // replaces every assertion, so they only move when the sibling is not boolean. + schema.Id, siblingCopy.Id = siblingCopy.Id, "" + schema.SchemaTypeRef, siblingCopy.SchemaTypeRef = siblingCopy.SchemaTypeRef, "" + schema.Vocabulary, siblingCopy.Vocabulary = siblingCopy.Vocabulary, nil + schema.Anchor, siblingCopy.Anchor = siblingCopy.Anchor, "" + schema.DynamicAnchor, siblingCopy.DynamicAnchor = siblingCopy.DynamicAnchor, "" + schema.Defs, siblingCopy.Defs = siblingCopy.Defs, nil + schema.UnevaluatedProperties, schema.UnevaluatedItems = nil, nil + if siblingCopy.BooleanSchema == nil { + schema.UnevaluatedProperties, siblingCopy.UnevaluatedProperties = siblingCopy.UnevaluatedProperties, nil + schema.UnevaluatedItems, siblingCopy.UnevaluatedItems = siblingCopy.UnevaluatedItems, nil + } + // The sibling's low schema still holds the keywords that moved, and a cleared high field + // still renders when its low field has a value, so rendering against it would bring them + // back into the member. The member renders against the authored allOf member instead, + // which never holds them, or against an empty low schema when every sibling moved. The + // empty low schema is built again on every call, so its RootNode differs each time. + var lowMember *base.Schema + if len(lowSchema.AllOf.Value) == 2 && lowSchema.AllOf.Value[0].Value != nil { + lowMember = lowSchema.AllOf.Value[0].Value.Schema() + } + if lowMember == nil { + lowMember = new(base.Schema) + if err := lowMember.Build(sp.schema.Value.GetContext(), utils.CreateEmptyMapNode(), lowSchema.Index); err != nil { + return nil, err + } + } + siblingCopy.low = lowMember + + // the sibling goes in front of the $ref when it is a boolean schema or still sets a keyword, + // so allOf holds just the $ref once a caller clears every sibling. + member := siblingCopy.BooleanSchema != nil || len(high.NewNodeBuilder(&siblingCopy, nil).Render().Content) > 0 switch len(schema.AllOf) { case 2: - schema.AllOf[0] = CreateSchemaProxy(&siblingCopy) + if member { + schema.AllOf[0] = CreateSchemaProxy(&siblingCopy) + } else { + schema.AllOf = schema.AllOf[1:] + } case 1: - // every authored sibling was an unevaluated* keyword, so allOf only holds the $ref. - if len(high.NewNodeBuilder(&siblingCopy, nil).Render().Content) > 0 { + // every authored sibling stayed beside allOf, so allOf only holds the $ref. + if member { schema.AllOf = []*SchemaProxy{CreateSchemaProxy(&siblingCopy), schema.AllOf[0]} } } @@ -736,6 +773,22 @@ func (sp *SchemaProxy) renderTransformedRefWithSiblings(s *Schema) (*yaml.Node, if !sp.shouldCollapseTransformedRefWithSiblings() { return nil, false, nil } + if s.BooleanSchema != nil { + if !sp.schemaIsTransformedSiblingView(s) { + return nil, false, nil + } + // the $ref form cannot hold a boolean schema, so a boolean sibling renders the allOf view. + semantic, err := sp.buildSemanticAllOfSchemaView(s) + if err != nil { + return nil, true, err + } + if semantic == nil { + return nil, false, nil + } + rendered, err := semantic.MarshalYAML() + node, _ := yamlNodeFromRender(rendered) + return node, true, err + } var siblingNode *yaml.Node ref := sp.schema.Value.GetTransformedRefReference() @@ -747,15 +800,15 @@ func (sp *SchemaProxy) renderTransformedRefWithSiblings(s *Schema) (*yaml.Node, } // Only collapse the synthetic allOf created by the sibling-ref transformer, - // which may hold unevaluated* beside allOf. If callers add other fields to the - // outer schema or change its composition, keep the explicit allOf so no - // mutations are hidden. + // which may only hold keywords that isBesideAllOfKeyword accepts beside allOf. + // If callers add other fields to the outer schema or change its composition, + // keep the explicit allOf so no mutations are hidden. outerNode := high.NewNodeBuilder(s, s.low).Render() var outerSiblings []*yaml.Node for i := 0; i+1 < len(outerNode.Content); i += 2 { - switch key := outerNode.Content[i].Value; key { - case base.AllOfLabel: - case base.UnevaluatedPropertiesLabel, base.UnevaluatedItemsLabel: + switch key := outerNode.Content[i].Value; { + case key == base.AllOfLabel: + case isBesideAllOfKeyword(key): outerSiblings = append(outerSiblings, outerNode.Content[i], outerNode.Content[i+1]) default: return nil, false, nil @@ -773,6 +826,14 @@ func (sp *SchemaProxy) renderTransformedRefWithSiblings(s *Schema) (*yaml.Node, if !ok || !utils.IsNodeMap(siblingNode) { return nil, false, nil } + // parsing the $ref form would move a member keyword that isBesideAllOfKeyword accepts + // beside allOf, where it means something else, and a member $ref would clash with the + // collapsed $ref, so a member holding either keeps the explicit allOf. + for i := 0; i+1 < len(siblingNode.Content); i += 2 { + if key := siblingNode.Content[i].Value; key == "$ref" || isBesideAllOfKeyword(key) { + return nil, false, nil + } + } } siblingNode.Content = append(siblingNode.Content, outerSiblings...) ref = s.AllOf[refMember].GetReference() @@ -819,6 +880,18 @@ func (sp *SchemaProxy) renderTransformedRefWithSiblings(s *Schema) (*yaml.Node, return result, true, nil } +// isBesideAllOfKeyword reports whether the sibling-ref transformer moves a rendered Schema keyword +// beside allOf instead of into it. The transformer also moves definitions, but Schema has no +// field for it, so it never appears in a rendered Schema. +func isBesideAllOfKeyword(key string) bool { + switch key { + case base.UnevaluatedPropertiesLabel, base.UnevaluatedItemsLabel, base.IdLabel, base.SchemaTypeLabel, + base.VocabularyLabel, base.AnchorLabel, base.DynamicAnchorLabel, base.DefsLabel: + return true + } + return false +} + func (sp *SchemaProxy) schemaIsTransformedSiblingView(s *Schema) bool { if sp == nil || sp.schema == nil || sp.schema.Value == nil || s == nil { return false @@ -868,7 +941,7 @@ func (sp *SchemaProxy) Render() ([]byte, error) { // MarshalYAML will create a ready to render YAML representation of the SchemaProxy object. func (sp *SchemaProxy) MarshalYAML() (interface{}, error) { if sp.isParsedRefWithSiblings() { - return sp.referenceYAMLNodeForSchema(nil) + return sp.referenceYAMLNode() } if !sp.IsReference() { s, err := sp.BuildSchema() @@ -890,10 +963,6 @@ func (sp *SchemaProxy) MarshalYAML() (interface{}, error) { return sp.GetReferenceNode(), nil } -func (sp *SchemaProxy) referenceYAMLNode() (*yaml.Node, error) { - return sp.referenceYAMLNodeForSchema(nil) -} - func (sp *SchemaProxy) sourceIndex() *index.SpecIndex { if sp == nil || sp.schema == nil || sp.schema.Value == nil { return nil @@ -977,17 +1046,14 @@ func (sp *SchemaProxy) circularReferenceResult(ctx *InlineRenderContext, node *y return node, errors.Join(refErr, circularErr) } -func (sp *SchemaProxy) referenceYAMLNodeForSchema(s *Schema) (*yaml.Node, error) { +func (sp *SchemaProxy) referenceYAMLNode() (*yaml.Node, error) { if sp.isRefWithSiblings() { return sp.renderRefWithSiblings(), nil } if sp.isParsedRefWithSiblings() { - if s == nil { - var err error - s, err = sp.BuildSchema() - if err != nil { - return nil, err - } + s, err := sp.BuildSchema() + if err != nil { + return nil, err } if node, ok, renderErr := sp.renderTransformedRefWithSiblings(s); ok || renderErr != nil { return node, renderErr @@ -1265,6 +1331,13 @@ func (sp *SchemaProxy) matchCircularReference(ctx *InlineRenderContext, idx *ind } func (sp *SchemaProxy) marshalParsedRefWithSiblingsInline(ctx *InlineRenderContext, currentSibling *Schema) (interface{}, error) { + if currentSibling != nil && !sp.schemaIsTransformedSiblingView(currentSibling) { + // any other schema, such as the allOf view, already holds its allOf, so it renders as it + // is, without ParentProxy so it does not come back here. + detached := *currentSibling + detached.ParentProxy = nil + return detached.MarshalYAMLInlineWithContext(ctx) + } s, err := sp.buildSemanticAllOfSchemaView(currentSibling) if err != nil { return nil, err diff --git a/datamodel/high/base/schema_proxy_test.go b/datamodel/high/base/schema_proxy_test.go index 84e6ff30..6c7e68ba 100644 --- a/datamodel/high/base/schema_proxy_test.go +++ b/datamodel/high/base/schema_proxy_test.go @@ -9,6 +9,7 @@ import ( "fmt" "os" "path/filepath" + "reflect" "strings" "sync" "sync/atomic" @@ -16,6 +17,7 @@ import ( "github.com/pb33f/go-yaml" "github.com/pb33f/libopenapi/datamodel" + "github.com/pb33f/libopenapi/datamodel/high" "github.com/pb33f/libopenapi/datamodel/low" lowbase "github.com/pb33f/libopenapi/datamodel/low/base" "github.com/pb33f/libopenapi/index" @@ -2074,6 +2076,9 @@ components: $ref: '#/components/schemas/Pair' unevaluatedItems: type: string + anchored: + $ref: '#/components/schemas/Thing' + $anchor: anchored ` keys := func(t *testing.T, rendered any) []string { t.Helper() @@ -2123,11 +2128,12 @@ components: require.Len(t, semantic.AllOf, 1) assert.True(t, semantic.AllOf[0].IsReference()) - // the allOf view collapses back to the authored shape. + // BuildTransformedRefSemanticSchema returns the allOf view without ParentProxy. Setting it + // makes the view render the $ref form where that form can hold the schema, which here is + // the authored shape. Later subtests set ParentProxy for the same reason. semantic.ParentProxy = sp - node, ok, err := sp.renderTransformedRefWithSiblings(semantic) + node, err := semantic.MarshalYAML() require.NoError(t, err) - require.True(t, ok) assert.Equal(t, []string{"$ref", "unevaluatedProperties"}, keys(t, node)) // a keyword added to the sibling view joins allOf. @@ -2144,6 +2150,87 @@ components: assert.Equal(t, []string{"$ref", "unevaluatedProperties", "required"}, keys(t, rendered)) }) + t.Run("a keyword added beside only an $anchor joins allOf alone", func(t *testing.T) { + sp := buildParsedSiblingRefProxyFromSpec(t, spec, 3.1, "anchored") + sibling := sp.Schema() + require.NotNil(t, sibling) + sibling.Required = []string{"a"} + + // allOf only held the $ref, so the added keyword forms a new member, and the $anchor + // that moved beside allOf is not repeated in it. + inline, err := sp.MarshalYAMLInline() + require.NoError(t, err) + assert.Equal(t, []string{"allOf", "$anchor"}, keys(t, inline)) + _, allOf := findYAMLPair(inline.(*yaml.Node), "allOf") + require.NotNil(t, allOf) + require.Len(t, allOf.Content, 2) + assert.Equal(t, []string{"required"}, keys(t, allOf.Content[0])) + }) + + t.Run("clearing every member keyword leaves allOf holding just the $ref", func(t *testing.T) { + sp := buildParsedSiblingRefProxyFromSpec(t, spec, 3.1, "thing") + sibling := sp.Schema() + require.NotNil(t, sibling) + sibling.Required = nil + + semantic, err := sp.BuildTransformedRefSemanticSchema(sibling) + require.NoError(t, err) + require.Len(t, semantic.AllOf, 1) + assert.True(t, semantic.AllOf[0].IsReference()) + + inline, err := sp.MarshalYAMLInline() + require.NoError(t, err) + assert.Equal(t, []string{"allOf", "unevaluatedProperties"}, keys(t, inline)) + _, allOf := findYAMLPair(inline.(*yaml.Node), "allOf") + require.NotNil(t, allOf) + require.Len(t, allOf.Content, 1) + assert.Equal(t, []string{"type", "properties"}, keys(t, allOf.Content[0])) + }) + + // jsonMatchesYAML checks that MarshalJSON renders the same schema as MarshalYAML. + jsonMatchesYAML := func(t *testing.T, s *Schema, rendered any) string { + t.Helper() + fromYAML, err := marshalYAMLRenderJSON(rendered) + require.NoError(t, err) + jsonBytes, err := s.MarshalJSON() + require.NoError(t, err) + assert.JSONEq(t, string(fromYAML), string(jsonBytes)) + return string(jsonBytes) + } + + t.Run("member keywords that move beside allOf keep the allOf", func(t *testing.T) { + for name, tc := range map[string]struct { + edit func(*Schema) + want string + }{ + "anchor": { + edit: func(s *Schema) { s.AllOf[0].Schema().Anchor = "inner" }, + want: `"$anchor":"inner"`, + }, + // the member $ref would clash with the $ref of the collapsed form. + "ref": { + edit: func(s *Schema) { s.AllOf[0] = CreateSchemaProxyRef("#/components/schemas/Pair") }, + want: `{"$ref":"#/components/schemas/Pair"}`, + }, + } { + sp := buildParsedSiblingRefProxyFromSpec(t, spec, 3.1, "thing") + semantic, err := sp.BuildTransformedRefSemanticSchema(sp.Schema()) + require.NoError(t, err) + require.Len(t, semantic.AllOf, 2) + tc.edit(semantic) + + // the $ref form would move the keyword beside allOf, so the explicit allOf stays. + semantic.ParentProxy = sp + _, ok, err := sp.renderTransformedRefWithSiblings(semantic) + require.NoError(t, err) + assert.False(t, ok, name) + rendered, err := semantic.MarshalYAML() + require.NoError(t, err) + assert.Equal(t, []string{"allOf", "unevaluatedProperties"}, keys(t, rendered), name) + assert.Contains(t, jsonMatchesYAML(t, semantic, rendered), tc.want, name) + } + }) + t.Run("other outer keywords keep the allOf", func(t *testing.T) { sp := buildParsedSiblingRefProxyFromSpec(t, spec, 3.1, "only") semantic, err := sp.BuildTransformedRefSemanticSchema(sp.Schema()) @@ -2155,6 +2242,49 @@ components: require.NoError(t, err) assert.False(t, ok) assert.Nil(t, node) + rendered, err := semantic.MarshalYAML() + require.NoError(t, err) + assert.Equal(t, []string{"allOf", "unevaluatedProperties", "title"}, keys(t, rendered)) + assert.Contains(t, jsonMatchesYAML(t, semantic, rendered), `"title":"outer"`) + }) + + t.Run("the allOf view renders inline without wrapping its allOf again", func(t *testing.T) { + sp := buildParsedSiblingRefProxyFromSpec(t, spec, 3.1, "thing") + semantic, err := sp.BuildTransformedRefSemanticSchema(sp.Schema()) + require.NoError(t, err) + semantic.ParentProxy = sp + + inline, err := semantic.MarshalYAMLInline() + require.NoError(t, err) + assert.Equal(t, []string{"allOf", "unevaluatedProperties"}, keys(t, inline)) + _, allOf := findYAMLPair(inline.(*yaml.Node), "allOf") + require.NotNil(t, allOf) + require.Len(t, allOf.Content, 2) + assert.Equal(t, []string{"required"}, keys(t, allOf.Content[0])) + jsonInline, err := semantic.MarshalJSONInline() + require.NoError(t, err) + assert.JSONEq(t, `{"allOf":[{"required":["a"]},{"type":"object","properties":{"a":{"type":"integer"}}}],`+ + `"unevaluatedProperties":false}`, string(jsonInline)) + }) + + t.Run("a boolean schema that is not the sibling view stays a bare boolean", func(t *testing.T) { + sp := buildParsedSiblingRefProxyFromSpec(t, spec, 3.1, "thing") + semantic, err := sp.BuildTransformedRefSemanticSchema(sp.Schema()) + require.NoError(t, err) + value := true + semantic.BooleanSchema = &value + semantic.ParentProxy = sp + + for name, render := range map[string]func() ([]byte, error){ + "yaml": semantic.Render, "inline YAML": semantic.RenderInline, + "json": semantic.MarshalJSON, "inline JSON": semantic.MarshalJSONInline, + } { + b, err := render() + require.NoError(t, err, name) + var actual any + require.NoError(t, yaml.Unmarshal(b, &actual), name) + assert.Equal(t, true, actual, name) + } }) t.Run("unevaluatedItems", func(t *testing.T) { @@ -2174,6 +2304,193 @@ components: require.NoError(t, err) assert.Equal(t, []string{"$ref", "unevaluatedItems"}, keys(t, rendered)) }) + + t.Run("boolean sibling stays in allOf", func(t *testing.T) { + const thing = `{"type":"object","properties":{"a":{"type":"integer"}}}` + for _, tc := range []struct { + property string + value bool + anchor string + want string + wantOuter []string + json string + jsonInline string + }{ + { + property: "only", value: false, want: "false", wantOuter: []string{"allOf"}, + json: `{"allOf":[false,{"$ref":"#/components/schemas/Thing"}]}`, + jsonInline: `{"allOf":[false,` + thing + `]}`, + }, + { + property: "thing", value: true, want: "true", wantOuter: []string{"allOf"}, + json: `{"allOf":[true,{"$ref":"#/components/schemas/Thing"}]}`, + jsonInline: `{"allOf":[true,` + thing + `]}`, + }, + // $anchor names the object rather than asserting anything, so the current $anchor stays + // beside allOf. + { + property: "anchored", value: true, anchor: "current", want: "true", wantOuter: []string{"allOf", "$anchor"}, + json: `{"allOf":[true,{"$ref":"#/components/schemas/Thing"}],"$anchor":"current"}`, + jsonInline: `{"allOf":[true,` + thing + `],"$anchor":"current"}`, + }, + } { + sp := buildParsedSiblingRefProxyFromSpec(t, spec, 3.1, tc.property) + sibling := sp.Schema() + require.NotNil(t, sibling) + sibling.BooleanSchema = &tc.value + if tc.anchor != "" { + sibling.Anchor = tc.anchor + } + + inline, err := sp.MarshalYAMLInline() + require.NoError(t, err) + assert.Equal(t, tc.wantOuter, keys(t, inline), tc.property) + _, allOf := findYAMLPair(inline.(*yaml.Node), "allOf") + require.NotNil(t, allOf, tc.property) + require.Len(t, allOf.Content, 2, tc.property) + assert.Equal(t, tc.want, allOf.Content[0].Value, tc.property) + if tc.anchor != "" { + _, anchor := findYAMLPair(inline.(*yaml.Node), "$anchor") + require.NotNil(t, anchor, tc.property) + assert.Equal(t, tc.anchor, anchor.Value, tc.property) + } + // a boolean sibling replaces every authored assertion, unevaluated* included. + rendered, err := yaml.Marshal(inline) + require.NoError(t, err) + assert.NotContains(t, string(rendered), "unevaluated", tc.property) + + // the proxy and the sibling view render the same schema through every entry point. + proxyRendered, err := sp.MarshalYAML() + require.NoError(t, err) + proxyJSON, err := marshalYAMLRenderJSON(proxyRendered) + require.NoError(t, err) + assert.JSONEq(t, tc.json, string(proxyJSON), tc.property) + siblingRendered, err := sibling.MarshalYAML() + require.NoError(t, err) + siblingJSON, err := marshalYAMLRenderJSON(siblingRendered) + require.NoError(t, err) + assert.JSONEq(t, tc.json, string(siblingJSON), tc.property) + siblingJSON, err = sibling.MarshalJSON() + require.NoError(t, err) + assert.JSONEq(t, tc.json, string(siblingJSON), tc.property) + siblingJSON, err = sibling.MarshalJSONInline() + require.NoError(t, err) + assert.JSONEq(t, tc.jsonInline, string(siblingJSON), tc.property) + siblingYAML, err := sibling.RenderInline() + require.NoError(t, err) + var siblingNode yaml.Node + require.NoError(t, yaml.Unmarshal(siblingYAML, &siblingNode)) + siblingJSON, err = marshalYAMLRenderJSON(siblingNode.Content[0]) + require.NoError(t, err) + assert.JSONEq(t, tc.jsonInline, string(siblingJSON), tc.property) + + // the allOf view keeps its allOf even with ParentProxy set, because the $ref form + // cannot hold a boolean schema. + semantic, err := sp.BuildTransformedRefSemanticSchema(sibling) + require.NoError(t, err) + jsonInline, err := semantic.MarshalJSONInline() + require.NoError(t, err) + assert.JSONEq(t, tc.jsonInline, string(jsonInline), tc.property) + semantic.ParentProxy = sp + jsonBytes, err := semantic.MarshalJSON() + require.NoError(t, err) + assert.JSONEq(t, tc.json, string(jsonBytes), tc.property) + } + }) +} + +// The sibling-ref transformer, isBesideAllOfKeyword and the field moves in the allOf view must +// agree on which keywords sit beside allOf. +func TestSchemaProxy_BesideAllOfKeywordsMatchTransformer(t *testing.T) { + // every keyword Schema renders, plus definitions, which Schema has no field for. + keywords := []string{"definitions"} + schemaType := reflect.TypeOf(Schema{}) + for i := 0; i < schemaType.NumField(); i++ { + name, _, _ := strings.Cut(schemaType.Field(i).Tag.Get("yaml"), ",") + if name != "" && name != "-" { + keywords = append(keywords, name) + } + } + + t.Run("transformer", func(t *testing.T) { + cfg := index.CreateOpenAPIIndexConfig() + cfg.TransformSiblingRefs = true + transformer := lowbase.NewSiblingRefTransformer(index.NewSpecIndexWithConfig(&yaml.Node{}, cfg)) + for _, keyword := range keywords { + node := &yaml.Node{Kind: yaml.MappingNode, Tag: "!!map", Content: []*yaml.Node{ + utils.CreateStringNode("$ref"), utils.CreateStringNode("#/components/schemas/Thing"), + utils.CreateStringNode(keyword), utils.CreateEmptyMapNode(), + }} + transformed, err := transformer.TransformSiblingRef(node) + require.NoError(t, err) + // the first pair is the allOf the transformer builds, and moved keywords follow it. + _, beside := findYAMLPair(&yaml.Node{Kind: yaml.MappingNode, Content: transformed.Content[2:]}, keyword) + assert.Equal(t, isBesideAllOfKeyword(keyword) || keyword == "definitions", beside != nil, keyword) + } + }) + + t.Run("allOf view", func(t *testing.T) { + // authors every keyword isBesideAllOfKeyword accepts except $id, plus title, which joins + // allOf. An authored $id would change the base URI the $ref resolves against, so $id is + // set on the sibling instead. + spec := `openapi: 3.1.0 +paths: {} +components: + schemas: + Thing: + type: object + Holder: + type: object + properties: + all: + $ref: '#/components/schemas/Thing' + title: member + $schema: 'https://json-schema.org/draft/2020-12/schema' + $vocabulary: + 'https://json-schema.org/draft/2020-12/vocab/core': true + $anchor: all + $dynamicAnchor: all + $defs: + D: + type: string + unevaluatedProperties: false + unevaluatedItems: + type: string +` + var want []string + for _, keyword := range keywords { + if isBesideAllOfKeyword(keyword) { + want = append(want, keyword) + } + } + sp := buildParsedSiblingRefProxyFromSpec(t, spec, 3.1, "all") + sibling := sp.Schema() + require.NoError(t, sp.GetBuildError()) + require.NotNil(t, sibling) + sibling.Id = "https://example.com/all" + semantic, err := sp.BuildTransformedRefSemanticSchema(sibling) + require.NoError(t, err) + require.Len(t, semantic.AllOf, 2) + + var outer []string + rendered := high.NewNodeBuilder(semantic, semantic.low).Render() + for i := 0; i+1 < len(rendered.Content); i += 2 { + if key := rendered.Content[i].Value; key != lowbase.AllOfLabel { + outer = append(outer, key) + } + } + assert.ElementsMatch(t, want, outer) + + member, err := semantic.AllOf[0].MarshalYAML() + require.NoError(t, err) + memberNode, ok := yamlNodeFromRender(member) + require.True(t, ok) + var inner []string + for i := 0; i+1 < len(memberNode.Content); i += 2 { + inner = append(inner, memberNode.Content[i].Value) + } + assert.Equal(t, []string{"title"}, inner) + }) } func TestSchemaProxy_ParsedTransformedRefWithSiblingsJSONPreservesReference(t *testing.T) { @@ -2203,6 +2520,8 @@ func TestSchemaProxy_ParsedTransformedRefWithSiblingsJSONReturnsErrors(t *testin require.NoError(t, err) require.NotNil(t, semanticSchema) require.Len(t, semanticSchema.AllOf, 2) + // with ParentProxy set the allOf view renders the $ref form, which renders the member and so + // meets its error. semanticSchema.ParentProxy = sp semanticSchema.AllOf[0] = &SchemaProxy{buildError: errors.New("boom")} diff --git a/datamodel/low/base/schema_proxy.go b/datamodel/low/base/schema_proxy.go index b75e8f1b..1d6ae157 100644 --- a/datamodel/low/base/schema_proxy.go +++ b/datamodel/low/base/schema_proxy.go @@ -171,8 +171,9 @@ func (sp *SchemaProxy) GetTransformedRefReference() string { } // GetTransformedRefAllOfSchema returns the internal allOf schema for an -// authored $ref-with-siblings node. unevaluatedProperties and unevaluatedItems -// sit beside allOf, so when they are the only siblings, allOf holds just the $ref. +// authored $ref-with-siblings node. unevaluatedProperties, unevaluatedItems, $id, +// $schema, $vocabulary, $anchor, $dynamicAnchor, $defs and definitions always sit +// beside allOf, so when they are the only siblings, allOf holds just the $ref. func (sp *SchemaProxy) GetTransformedRefAllOfSchema() *yaml.Node { if !sp.IsTransformedRefWithSiblings() { return nil diff --git a/datamodel/low/base/sibling_ref_transformer.go b/datamodel/low/base/sibling_ref_transformer.go index 151d8715..f06fdeb1 100644 --- a/datamodel/low/base/sibling_ref_transformer.go +++ b/datamodel/low/base/sibling_ref_transformer.go @@ -42,6 +42,17 @@ func NewSiblingRefTransformer(idx *index.SpecIndex) *SiblingRefTransformer { // // Input: {$ref: "#/components/schemas/Base", unevaluatedProperties: false} // Output: {allOf: [{$ref: "#/components/schemas/Base"}], unevaluatedProperties: false} +// +// $id, $schema, $vocabulary, $anchor, $dynamicAnchor, $defs and definitions also stay beside +// allOf, so the wrapper keeps the identity of the authored object: $id sets the base URI the $ref +// resolves against, $schema names the dialect and $vocabulary the vocabularies it uses, $anchor +// and $dynamicAnchor name the whole object, and $defs and definitions stay at the JSON pointers +// they were written at. +// The other siblings move under allOf/0, so a JSON pointer that reached one of them, such as +// ".../thing#/properties/x", no longer does. +// +// Input: {$id: "https://example.com/thing", $ref: "base", title: "Thing"} +// Output: {allOf: [{title: "Thing"}, {$ref: "base"}], $id: "https://example.com/thing"} func (srt *SiblingRefTransformer) TransformSiblingRef(node *yaml.Node) (*yaml.Node, error) { transformed := srt.transformSiblingRefWithMetadata(node) if transformed == nil { @@ -99,18 +110,28 @@ func (srt *SiblingRefTransformer) createAllOfStructureWithSiblingNode(refValue s allOfArrayNode := allOfNode.Content[1] + // keywords that stay beside allOf go on the wrapper and the rest form the allOf member. The + // sibling node itself is the member until a keyword moves, so the member pairs are only copied + // when one does. memberNode := siblingSchemaNode if siblingSchemaNode != nil { var kept []*yaml.Node + moved := false for i := 0; i+1 < len(siblingSchemaNode.Content); i += 2 { keyNode, valueNode := siblingSchemaNode.Content[i], siblingSchemaNode.Content[i+1] - if isUnevaluatedKeyword(keyNode.Value) { - allOfNode.Content = append(allOfNode.Content, keyNode, valueNode) - } else { - kept = append(kept, keyNode, valueNode) + if !staysBesideAllOf(keyNode.Value) { + if moved { + kept = append(kept, keyNode, valueNode) + } + continue } + if !moved { + moved = true + kept = append(make([]*yaml.Node, 0, len(siblingSchemaNode.Content)-2), siblingSchemaNode.Content[:i]...) + } + allOfNode.Content = append(allOfNode.Content, keyNode, valueNode) } - if len(kept) != len(siblingSchemaNode.Content) { + if moved { memberNode = &yaml.Node{Kind: yaml.MappingNode, Tag: "!!map", Content: kept} } } @@ -132,10 +153,21 @@ func (srt *SiblingRefTransformer) createAllOfStructureWithSiblingNode(refValue s return allOfNode } -// isUnevaluatedKeyword reports whether a sibling keyword has to stay beside allOf to see what -// the $ref target evaluated. -func isUnevaluatedKeyword(key string) bool { - return key == UnevaluatedPropertiesLabel || key == UnevaluatedItemsLabel +// staysBesideAllOf reports whether a sibling keyword stays beside allOf instead of moving into it. +// unevaluatedProperties and unevaluatedItems only see what adjacent keywords evaluated, so they +// stay next to the $ref. $id sets the base URI the $ref resolves against, $schema names the +// dialect of the object and $vocabulary the vocabularies that dialect uses, $anchor and +// $dynamicAnchor name the whole object, and $defs and definitions hold subschemas that JSON +// pointers reach through the object. +// +// definitions is written out because the v2 package, which defines its label, imports this one. +func staysBesideAllOf(key string) bool { + switch key { + case UnevaluatedPropertiesLabel, UnevaluatedItemsLabel, IdLabel, SchemaTypeLabel, VocabularyLabel, + AnchorLabel, DynamicAnchorLabel, DefsLabel, "definitions": + return true + } + return false } func (srt *SiblingRefTransformer) createSiblingSchemaNode(node *yaml.Node) *yaml.Node { diff --git a/datamodel/low/base/sibling_ref_transformer_test.go b/datamodel/low/base/sibling_ref_transformer_test.go index a01727ca..edc845a6 100644 --- a/datamodel/low/base/sibling_ref_transformer_test.go +++ b/datamodel/low/base/sibling_ref_transformer_test.go @@ -266,6 +266,38 @@ unevaluatedItems: false` assert.Equal(t, "unevaluatedItems", transformed.siblingNode.Content[4].Value) }) + t.Run("resource keywords stay beside allOf", func(t *testing.T) { + mappingKeys := func(n *yaml.Node) []string { + var keys []string + for i := 0; i+1 < len(n.Content); i += 2 { + keys = append(keys, n.Content[i].Value) + } + return keys + } + yml := `$ref: "https://example.com/base" +$id: "https://example.com/thing" +$schema: "https://json-schema.org/draft/2020-12/schema" +$vocabulary: {"https://json-schema.org/draft/2020-12/vocab/core": true} +$anchor: thing +$dynamicAnchor: node +title: Thing +$defs: {Extra: {type: string}} +definitions: {Legacy: {type: string}} +unevaluatedProperties: {$ref: "#/$defs/Extra"}` + + var node yaml.Node + require.NoError(t, yaml.Unmarshal([]byte(yml), &node)) + + result, err := transformer.TransformSiblingRef(node.Content[0]) + require.NoError(t, err) + assert.Equal(t, []string{"allOf", "$id", "$schema", "$vocabulary", "$anchor", "$dynamicAnchor", + "$defs", "definitions", "unevaluatedProperties"}, mappingKeys(result)) + allOf := result.Content[1] + require.Len(t, allOf.Content, 2) + assert.Equal(t, []string{"title"}, mappingKeys(allOf.Content[0])) + assert.Equal(t, []string{"$ref"}, mappingKeys(allOf.Content[1])) + }) + t.Run("only unevaluated siblings", func(t *testing.T) { yml := `$ref: "#/components/schemas/Base" unevaluatedProperties: false` diff --git a/document_test.go b/document_test.go index 7916e645..1f6ff973 100644 --- a/document_test.go +++ b/document_test.go @@ -5,6 +5,7 @@ package libopenapi import ( "bytes" stdContext "context" + "encoding/json" "fmt" "log/slog" "net/http" @@ -19,6 +20,7 @@ import ( "time" "github.com/pb33f/go-yaml" + "github.com/pb33f/jsonschema/v6" "github.com/pb33f/libopenapi/datamodel" "github.com/pb33f/libopenapi/datamodel/high/base" v3high "github.com/pb33f/libopenapi/datamodel/high/v3" @@ -2451,6 +2453,200 @@ unevaluatedItems: assert.Equal(t, spec, string(modelBytes)) } +func TestDocument_SiblingRefUnevaluatedKeywordsKeepResourceScope(t *testing.T) { + spec := `openapi: 3.1.0 +info: + title: Unevaluated sibling refs + version: 1.0.0 +paths: {} +components: + schemas: + Base: + $id: 'https://example.com/schemas/base' + type: object + properties: + a: + type: integer + Scoped: + $ref: 'https://example.com/schemas/base' + $id: 'https://example.com/schemas/scoped' + $defs: + Extra: + type: string + unevaluatedProperties: + $ref: '#/$defs/Extra' +` + + doc, err := NewDocument([]byte(spec)) + require.NoError(t, err) + + model, err := doc.BuildV3Model() + require.NoError(t, err) + + proxy := model.Model.Components.Schemas.GetOrZero("Scoped") + + // #/$defs/Extra resolves against $id, so $id and $defs stay in the same schema resource as + // unevaluatedProperties, and the allOf schema still resolves the reference on its own. + allOf, err := yaml.Marshal(proxy.GoLow().GetTransformedRefAllOfSchema()) + require.NoError(t, err) + assert.Equal(t, `allOf: + - $ref: https://example.com/schemas/base +$id: 'https://example.com/schemas/scoped' +$defs: + Extra: + type: string +unevaluatedProperties: + $ref: '#/$defs/Extra' +`, string(allOf)) + + inline, err := proxy.Schema().RenderInline() + require.NoError(t, err) + assert.Equal(t, `allOf: + - $id: 'https://example.com/schemas/base' + type: object + properties: + a: + type: integer +$id: 'https://example.com/schemas/scoped' +$defs: + Extra: + type: string +unevaluatedProperties: + type: string +`, string(inline)) + + // a JSON Schema compiler resolves #/$defs/Extra in both, and both validate like the authored + // schema: properties the $ref target evaluated are allowed, and any other must be a string. + baseSchema, err := model.Model.Components.Schemas.GetOrZero("Base").Render() + require.NoError(t, err) + for name, resources := range map[string]map[string][]byte{ + "allOf schema": {"https://example.com/schemas/scoped": allOf, "https://example.com/schemas/base": baseSchema}, + "inline": {"https://example.com/schemas/scoped": inline}, + } { + compiler := jsonschema.NewCompiler() + for location, schemaYAML := range resources { + var value any + require.NoError(t, yaml.Unmarshal(schemaYAML, &value), name) + schemaJSON, err := json.Marshal(value) + require.NoError(t, err, name) + doc, err := jsonschema.UnmarshalJSON(bytes.NewReader(schemaJSON)) + require.NoError(t, err, name) + require.NoError(t, compiler.AddResource(location, doc), name) + } + compiled, err := compiler.Compile("https://example.com/schemas/scoped") + require.NoError(t, err, name) + for instance, valid := range map[string]bool{ + `{"a": 1}`: true, + `{"a": 1, "x": "s"}`: true, + `{"a": 1, "x": 2}`: false, + `{"a": "s"}`: false, + } { + value, err := jsonschema.UnmarshalJSON(strings.NewReader(instance)) + require.NoError(t, err, name) + assert.Equal(t, valid, compiled.Validate(value) == nil, "%s %s", name, instance) + } + } +} + +func TestDocument_SiblingRefResourceKeywordsStayBesideAllOf(t *testing.T) { + spec := `openapi: 3.1.0 +info: + title: Resource sibling refs + version: 1.0.0 +paths: {} +components: + schemas: + Base: + $id: 'https://example.com/schemas/base' + type: object + Named: + $ref: 'https://example.com/schemas/base' + $id: 'https://example.com/schemas/named' + $schema: 'https://json-schema.org/draft/2020-12/schema' + $vocabulary: + https://json-schema.org/draft/2020-12/vocab/core: true + $anchor: named + $dynamicAnchor: node + $defs: + Extra: + type: string + title: Named +` + + doc, err := NewDocument([]byte(spec)) + require.NoError(t, err) + + model, err := doc.BuildV3Model() + require.NoError(t, err) + + proxy := model.Model.Components.Schemas.GetOrZero("Named") + + // $id, $schema, $vocabulary, $anchor, $dynamicAnchor and $defs sit beside allOf without any + // unevaluated* keyword, and the member does not repeat them. + inline, err := proxy.Schema().RenderInline() + require.NoError(t, err) + assert.Equal(t, `allOf: + - title: Named + - $id: 'https://example.com/schemas/base' + type: object +$id: 'https://example.com/schemas/named' +$schema: 'https://json-schema.org/draft/2020-12/schema' +$vocabulary: + https://json-schema.org/draft/2020-12/vocab/core: true +$anchor: named +$dynamicAnchor: node +$defs: + Extra: + type: string +`, string(inline)) + + // the semantic view is built detached, and setting ParentProxy asks for the collapsed $ref + // form, which here is the authored one. + semantic, err := proxy.BuildTransformedRefSemanticSchema(proxy.Schema()) + require.NoError(t, err) + semantic.ParentProxy = proxy + collapsed, err := semantic.Render() + require.NoError(t, err) + assert.Equal(t, `$ref: 'https://example.com/schemas/base' +$id: 'https://example.com/schemas/named' +$schema: 'https://json-schema.org/draft/2020-12/schema' +$vocabulary: + https://json-schema.org/draft/2020-12/vocab/core: true +$anchor: named +$dynamicAnchor: node +$defs: + Extra: + type: string +title: Named +`, string(collapsed)) + + modelBytes, err := model.Model.Render() + require.NoError(t, err) + assert.Equal(t, spec, string(modelBytes)) + + // edited values replace the authored ones beside allOf, and the member does not repeat them. + sibling := proxy.Schema() + sibling.Id = "https://example.com/schemas/renamed" + sibling.Anchor = "renamed" + semantic, err = proxy.BuildTransformedRefSemanticSchema(sibling) + require.NoError(t, err) + semanticBytes, err := semantic.Render() + require.NoError(t, err) + assert.Equal(t, `allOf: + - title: Named + - $ref: https://example.com/schemas/base +$id: 'https://example.com/schemas/renamed' +$schema: 'https://json-schema.org/draft/2020-12/schema' +$vocabulary: + https://json-schema.org/draft/2020-12/vocab/core: true +$anchor: renamed +$dynamicAnchor: node +$defs: + Extra: + type: string +`, string(semanticBytes)) +} + func TestDocument_Render_OpenAPI30TransformedSiblingRefStaysAllOf(t *testing.T) { spec := `openapi: 3.0.3 info: diff --git a/go.mod b/go.mod index 52201a1a..10ba1f71 100644 --- a/go.mod +++ b/go.mod @@ -6,6 +6,7 @@ require ( github.com/lucasjones/reggen v0.0.0-20200904144131-37ba4fa293bb github.com/pb33f/go-yaml v0.1.1 github.com/pb33f/jsonpath v0.8.5 + github.com/pb33f/jsonschema/v6 v6.0.3 github.com/pb33f/ordered-map/v2 v2.3.2 github.com/pb33f/testify v0.1.1 golang.org/x/sync v0.25.0 @@ -14,4 +15,5 @@ require ( require ( github.com/bahlo/generic-list-go v0.2.0 // indirect github.com/buger/jsonparser v1.1.2 // indirect + golang.org/x/text v0.14.0 // indirect ) diff --git a/go.sum b/go.sum index 7a70d1ff..3c1b6c03 100644 --- a/go.sum +++ b/go.sum @@ -4,12 +4,16 @@ github.com/buger/jsonparser v1.1.2 h1:frqHqw7otoVbk5M8LlE/L7HTnIq2v9RX6EJ48i9AxJ github.com/buger/jsonparser v1.1.2/go.mod h1:6RYKKt7H4d4+iWqouImQ9R2FZql3VbhNgx27UK13J/0= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/dlclark/regexp2 v1.11.0 h1:G/nrcoOa7ZXlpoa/91N3X7mM3r8eIlMBBJZvsz/mxKI= +github.com/dlclark/regexp2 v1.11.0/go.mod h1:DHkYz0B9wPfa6wondMfaivmHpzrQ3v9q8cnmRbL6yW8= github.com/lucasjones/reggen v0.0.0-20200904144131-37ba4fa293bb h1:w1g9wNDIE/pHSTmAaUhv4TZQuPBS6GV3mMz5hkgziIU= github.com/lucasjones/reggen v0.0.0-20200904144131-37ba4fa293bb/go.mod h1:5ELEyG+X8f+meRWHuqUOewBOhvHkl7M76pdGEansxW4= github.com/pb33f/go-yaml v0.1.1 h1:yWGmhVdwzionRnSc/hEJwcfq6PaZL2QdYzyReXBbX7Q= github.com/pb33f/go-yaml v0.1.1/go.mod h1:QvWdkHP5VjFYAeHMQUkaPQqREaQnBNOXmWzAjNrnSdo= github.com/pb33f/jsonpath v0.8.5 h1:wsbwJbz6MPUTy5fAtiIlOyQb3oblaa5M7qjtdz6orPw= github.com/pb33f/jsonpath v0.8.5/go.mod h1:eO6mgdhw5RgzCxm/bXTi75VUs0/cCYJzdGjMCCUEI6M= +github.com/pb33f/jsonschema/v6 v6.0.3 h1:TMIkE1xCmnF8Cy+anIBs+r4ASfGtwzfFxYsrrIn0wSU= +github.com/pb33f/jsonschema/v6 v6.0.3/go.mod h1:cYPShw+w1Jrs0YgpE2rk1FLdIAsKfWcotv/54G6ddFo= github.com/pb33f/ordered-map/v2 v2.3.2 h1:wDyaZ2Pv9QLh64X4utCeD5Zoi9nIv2aW3lwv172O5PQ= github.com/pb33f/ordered-map/v2 v2.3.2/go.mod h1:1OhFrXu3OYw3kM+FXF+Ug3ugmmZ9LE8z0JfQMLvtV0w= github.com/pb33f/testify v0.1.1 h1:mnHe7uxKt8dyNYEGUspow72VjD264DB5rOJq4bz5tJw= @@ -20,5 +24,7 @@ github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U= golang.org/x/sync v0.25.0 h1:dJnifr0nMV8/SBnBvdo4eSGGnrc3O4FFx6IqvtLcmRs= golang.org/x/sync v0.25.0/go.mod h1:sUUOizhqBxiL6pEWpqNLUiaJn1ShEbZ6BBqskPbjZm0= +golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ= +golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= From 3eeeedb527328b508aa6377e5b2876d8faa9e1d4 Mon Sep 17 00:00:00 2001 From: Thomas Date: Mon, 16 Nov 2026 16:28:38 +1100 Subject: [PATCH 3/3] test(base): cover the boolean sibling build error and drop an unreachable check --- datamodel/high/base/schema_proxy.go | 13 +++++---- datamodel/high/base/schema_proxy_test.go | 34 ++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 7 deletions(-) diff --git a/datamodel/high/base/schema_proxy.go b/datamodel/high/base/schema_proxy.go index 401571ef..26d4b442 100644 --- a/datamodel/high/base/schema_proxy.go +++ b/datamodel/high/base/schema_proxy.go @@ -724,10 +724,10 @@ func (sp *SchemaProxy) buildSemanticAllOfSchemaView(currentSibling *Schema) (*Sc lowMember = lowSchema.AllOf.Value[0].Value.Schema() } if lowMember == nil { + // Build only fails on a nil node or on a keyword it cannot read, and an empty mapping + // has neither. lowMember = new(base.Schema) - if err := lowMember.Build(sp.schema.Value.GetContext(), utils.CreateEmptyMapNode(), lowSchema.Index); err != nil { - return nil, err - } + _ = lowMember.Build(sp.schema.Value.GetContext(), utils.CreateEmptyMapNode(), lowSchema.Index) } siblingCopy.low = lowMember @@ -778,12 +778,11 @@ func (sp *SchemaProxy) renderTransformedRefWithSiblings(s *Schema) (*yaml.Node, return nil, false, nil } // the $ref form cannot hold a boolean schema, so a boolean sibling renders the allOf view. + // the view is nil only when the low schema is missing, and err then holds the build error + // if there was one. semantic, err := sp.buildSemanticAllOfSchemaView(s) - if err != nil { - return nil, true, err - } if semantic == nil { - return nil, false, nil + return nil, err != nil, err } rendered, err := semantic.MarshalYAML() node, _ := yamlNodeFromRender(rendered) diff --git a/datamodel/high/base/schema_proxy_test.go b/datamodel/high/base/schema_proxy_test.go index 6c7e68ba..197426eb 100644 --- a/datamodel/high/base/schema_proxy_test.go +++ b/datamodel/high/base/schema_proxy_test.go @@ -2644,6 +2644,40 @@ func TestSchemaProxy_RenderTransformedRefWithSiblingsFallbacks(t *testing.T) { assert.Nil(t, node) } +func TestSchemaProxy_RenderTransformedRefWithSiblingsBooleanBuildError(t *testing.T) { + // unevaluatedItems stays beside allOf and builds eagerly, so its missing $ref fails the build of + // the outer schema, not just the sibling. + spec := `openapi: 3.1.0 +paths: {} +components: + schemas: + Thing: + type: string + Holder: + type: object + properties: + thing: + $ref: '#/components/schemas/Thing' + unevaluatedItems: + $ref: '#/components/schemas/Missing' +` + invalid := buildParsedSiblingRefProxyFromSpec(t, spec, 3.1, "thing") + siblingNode := invalid.schema.Value.GetTransformedRefSiblingSchema() + require.NotNil(t, siblingNode) + + // a boolean view of the sibling renders through the allOf view, which needs the outer low + // schema, so the build error comes back instead of a node. + value := false + boolean := &Schema{ + BooleanSchema: &value, + low: &lowbase.Schema{RootNode: siblingNode}, + } + node, ok, err := invalid.renderTransformedRefWithSiblings(boolean) + require.ErrorContains(t, err, "#/components/schemas/Missing") + assert.True(t, ok) + assert.Nil(t, node) +} + func buildParsedSiblingRefProxy(t *testing.T, version float32) *SchemaProxy { t.Helper()