From 81e6fb6c4931fdb615fc58fbe83dd93bd4f5341d Mon Sep 17 00:00:00 2001 From: quobix Date: Wed, 25 Nov 2026 10:49:35 -0500 Subject: [PATCH 1/4] fix(what-changed): report an example value modification once --- what-changed/model/example.go | 65 +-------------- what-changed/model/example_test.go | 125 +++++++++-------------------- 2 files changed, 39 insertions(+), 151 deletions(-) diff --git a/what-changed/model/example.go b/what-changed/model/example.go index 35a8cf22..56c56957 100644 --- a/what-changed/model/example.go +++ b/what-changed/model/example.go @@ -4,10 +4,6 @@ package model import ( - "fmt" - "sort" - - "github.com/pb33f/libopenapi/datamodel/low" "github.com/pb33f/libopenapi/datamodel/low/base" v3 "github.com/pb33f/libopenapi/datamodel/low/v3" "github.com/pb33f/libopenapi/utils" @@ -84,65 +80,8 @@ func CompareExamples(l, r *base.Example) *ExampleChanges { // Value if utils.IsNodeMap(l.Value.ValueNode) && utils.IsNodeMap(r.Value.ValueNode) { - if !compareYAMLNodesForChanges(l.Value.ValueNode, r.Value.ValueNode) { - lKeys := make([]string, len(l.Value.ValueNode.Content)/2) - rKeys := make([]string, len(r.Value.ValueNode.Content)/2) - z := 0 - for k := range l.Value.ValueNode.Content { - if k%2 == 0 { - // if there is no value (value is another map or something else), render the node into yaml and hash it. - // https://github.com/pb33f/libopenapi/issues/61 - val := l.Value.ValueNode.Content[k+1].Value - if val == "" { - val = low.HashYAMLNodeSlice(l.Value.ValueNode.Content[k+1].Content) - } - lKeys[z] = fmt.Sprintf("%v-%v-%v", - l.Value.ValueNode.Content[k].Value, - l.Value.ValueNode.Content[k+1].Tag, - fmt.Sprintf("%x", val)) - z++ - } else { - continue - } - } - z = 0 - for k := range r.Value.ValueNode.Content { - if k%2 == 0 { - // if there is no value (value is another map or something else), render the node into yaml and hash it. - // https://github.com/pb33f/libopenapi/issues/61 - val := r.Value.ValueNode.Content[k+1].Value - if val == "" { - val = low.HashYAMLNodeSlice(r.Value.ValueNode.Content[k+1].Content) - } - rKeys[z] = fmt.Sprintf("%v-%v-%v", - r.Value.ValueNode.Content[k].Value, - r.Value.ValueNode.Content[k+1].Tag, - fmt.Sprintf("%x", val)) - z++ - } else { - continue - } - } - sort.Strings(lKeys) - sort.Strings(rKeys) - for k := range lKeys { - if k < len(rKeys) && lKeys[k] != rKeys[k] { - CreateChangeWithEncoding(&changes, Modified, v3.ValueLabel, - l.Value.GetValueNode(), r.Value.GetValueNode(), BreakingModified(CompExample, PropValue), l.Value.GetValue(), r.Value.GetValue()) - continue - } - if k >= len(rKeys) { - CreateChangeWithEncoding(&changes, PropertyRemoved, v3.ValueLabel, - l.Value.ValueNode, r.Value.ValueNode, BreakingRemoved(CompExample, PropValue), l.Value.Value, r.Value.Value) - } - } - for k := range rKeys { - if k >= len(lKeys) { - CreateChangeWithEncoding(&changes, PropertyAdded, v3.ValueLabel, - l.Value.ValueNode, r.Value.ValueNode, BreakingAdded(CompExample, PropValue), l.Value.Value, r.Value.Value) - } - } - } + CheckForModificationWithEncoding(l.Value.ValueNode, r.Value.ValueNode, v3.ValueLabel, + &changes, BreakingModified(CompExample, PropValue), l.Value.Value, r.Value.Value) } else { props = append(props, NewPropertyCheck(CompExample, PropValue, l.Value.ValueNode, r.Value.ValueNode, diff --git a/what-changed/model/example_test.go b/what-changed/model/example_test.go index b60d1209..14b85dbe 100644 --- a/what-changed/model/example_test.go +++ b/what-changed/model/example_test.go @@ -9,6 +9,7 @@ import ( "github.com/pb33f/go-yaml" "github.com/pb33f/testify/assert" + "github.com/pb33f/testify/require" "github.com/pb33f/libopenapi/datamodel/low" "github.com/pb33f/libopenapi/datamodel/low/base" @@ -44,94 +45,42 @@ func TestCompareExamples_SummaryModified(t *testing.T) { assert.Equal(t, "cure all", extChanges.Changes[0].New) } -func TestCompareExamples_Map(t *testing.T) { - // Clear hash cache to ensure deterministic results in concurrent test environments - low.ClearHashCache() - left := `value: - cheesy: bread - pasta: sauce` - - right := `value: - cheesy: cakes - pasta: spicy` - - var lNode, rNode yaml.Node - _ = yaml.Unmarshal([]byte(left), &lNode) - _ = yaml.Unmarshal([]byte(right), &rNode) - - // create low level objects - var lDoc base.Example - var rDoc base.Example - _ = low.BuildModel(lNode.Content[0], &lDoc) - _ = low.BuildModel(rNode.Content[0], &rDoc) - _ = lDoc.Build(context.Background(), nil, lNode.Content[0], nil) - _ = rDoc.Build(context.Background(), nil, rNode.Content[0], nil) - - extChanges := CompareExamples(&lDoc, &rDoc) - - assert.Equal(t, extChanges.TotalChanges(), 2) - assert.Len(t, extChanges.GetAllChanges(), 2) - assert.Equal(t, 0, extChanges.TotalBreakingChanges()) - assert.Equal(t, Modified, extChanges.Changes[0].ChangeType) -} - -func TestCompareExamples_MapAdded(t *testing.T) { - // Clear hash cache to ensure deterministic results in concurrent test environments - low.ClearHashCache() - left := `value: - cheesy: bread` - - right := `value: - cheesy: bread - pasta: sauce` - - var lNode, rNode yaml.Node - _ = yaml.Unmarshal([]byte(left), &lNode) - _ = yaml.Unmarshal([]byte(right), &rNode) - - // create low level objects - var lDoc base.Example - var rDoc base.Example - _ = low.BuildModel(lNode.Content[0], &lDoc) - _ = low.BuildModel(rNode.Content[0], &rDoc) - _ = lDoc.Build(context.Background(), nil, lNode.Content[0], nil) - _ = rDoc.Build(context.Background(), nil, rNode.Content[0], nil) - - extChanges := CompareExamples(&lDoc, &rDoc) - - assert.Equal(t, extChanges.TotalChanges(), 1) - assert.Len(t, extChanges.GetAllChanges(), 1) - assert.Equal(t, 0, extChanges.TotalBreakingChanges()) - assert.Equal(t, PropertyAdded, extChanges.Changes[0].ChangeType) -} - -func TestCompareExamples_MapRemoved(t *testing.T) { - // Clear hash cache to ensure deterministic results in concurrent test environments - low.ClearHashCache() - left := `value: - cheesy: bread` - - right := `value: - cheesy: bread - pasta: sauce` - - var lNode, rNode yaml.Node - _ = yaml.Unmarshal([]byte(left), &lNode) - _ = yaml.Unmarshal([]byte(right), &rNode) - - // create low level objects - var lDoc base.Example - var rDoc base.Example - _ = low.BuildModel(lNode.Content[0], &lDoc) - _ = low.BuildModel(rNode.Content[0], &rDoc) - _ = lDoc.Build(context.Background(), nil, lNode.Content[0], nil) - _ = rDoc.Build(context.Background(), nil, rNode.Content[0], nil) - - extChanges := CompareExamples(&rDoc, &lDoc) - - assert.Equal(t, extChanges.TotalChanges(), 1) - assert.Equal(t, 0, extChanges.TotalBreakingChanges()) - assert.Equal(t, PropertyRemoved, extChanges.Changes[0].ChangeType) +func TestCompareExamples_ObjectValue(t *testing.T) { + for _, tc := range []struct { + name, left, right string + }{ + {"two members modified", "{id_1: 11, id_2: 21}", "{id_1: 10, id_2: 20}"}, + {"member added", "{}", "{id: 1}"}, + {"member removed", "{id: 1}", "{}"}, + {"sequence reordered inside object", "{items: [1, 2]}", "{items: [2, 1]}"}, + } { + t.Run(tc.name, func(t *testing.T) { + left := buildTestExample(t, "value: "+tc.left) + right := buildTestExample(t, "value: "+tc.right) + changes := CompareExamples(left, right) + require.NotNil(t, changes) + require.Len(t, changes.GetAllChanges(), 1) + change := changes.Changes[0] + assert.Equal(t, v3.ValueLabel, change.Property) + assert.Equal(t, Modified, change.ChangeType) + assert.False(t, change.Breaking) + assert.Equal(t, tc.left+"\n", change.OriginalEncoded) + assert.Equal(t, tc.right+"\n", change.NewEncoded) + }) + } + + left := buildTestExample(t, "value: {a: 1, b: 2}") + reordered := buildTestExample(t, "value: {b: 2, a: 1}") + assert.Nil(t, CompareExamples(left, reordered)) + + absent := buildTestExample(t, "summary: example") + present := buildTestExample(t, "summary: example\nvalue: {}") + added := CompareExamples(absent, present).GetAllChanges() + require.Len(t, added, 1) + assert.Equal(t, PropertyAdded, added[0].ChangeType) + removed := CompareExamples(present, absent).GetAllChanges() + require.Len(t, removed, 1) + assert.Equal(t, PropertyRemoved, removed[0].ChangeType) } func TestCompareExamples_SummaryAdded(t *testing.T) { From c826deb2ad9ae25a02a140cf306f4fff880a3455 Mon Sep 17 00:00:00 2001 From: quobix Date: Wed, 25 Nov 2026 10:49:37 -0500 Subject: [PATCH 2/4] fix(what-changed): ignore schema type array order --- what-changed/model/schema.go | 32 +++++++++++++++++++++++++++++-- what-changed/model/schema_test.go | 30 +++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+), 2 deletions(-) diff --git a/what-changed/model/schema.go b/what-changed/model/schema.go index c3dc57cd..b55710df 100644 --- a/what-changed/model/schema.go +++ b/what-changed/model/schema.go @@ -857,8 +857,9 @@ func (c *schemaComparison) checkSchemaPropertyChanges( if rSchema != nil && rSchema.Type.ValueNode != nil { rnv = rSchema.Type.ValueNode } - // Type - addCheck(lnv, rnv, v3.TypeLabel, PropType) + if !equalSchemaTypeArrays(lnv, rnv) { + addCheck(lnv, rnv, v3.TypeLabel, PropType) + } lnv = nil rnv = nil } @@ -1537,6 +1538,33 @@ func (c *schemaComparison) checkSchemaPropertyChanges( } } +// JSON Schema type arrays list the allowed types in any order. +func equalSchemaTypeArrays(left, right *yaml.Node) bool { + left, right = utils.NodeAlias(left), utils.NodeAlias(right) + if left == nil || right == nil || left.Kind != yaml.SequenceNode || right.Kind != yaml.SequenceNode { + return false + } + if len(left.Content) != len(right.Content) { + return false + } + counts := make(map[string]int, len(left.Content)) + for _, item := range left.Content { + item = utils.NodeAlias(item) + if item == nil || item.Kind != yaml.ScalarNode || item.Tag != "!!str" { + return false + } + counts[item.Value]++ + } + for _, item := range right.Content { + item = utils.NodeAlias(item) + if item == nil || item.Kind != yaml.ScalarNode || item.Tag != "!!str" || counts[item.Value] == 0 { + return false + } + counts[item.Value]-- + } + return true +} + func schemasUseEquivalentSimpleScalarUnion(l, r *base.SchemaProxy) bool { return schemaPairUsesEquivalentSimpleScalarUnion(l, r) || schemaPairUsesEquivalentSimpleScalarUnion(r, l) diff --git a/what-changed/model/schema_test.go b/what-changed/model/schema_test.go index f4499fbd..7824717e 100644 --- a/what-changed/model/schema_test.go +++ b/what-changed/model/schema_test.go @@ -2041,6 +2041,36 @@ components: assert.Equal(t, "bool", changes.AnyOfChanges[0].Changes[0].Original) } +func TestCompareSchemas_TypeArrayWithDescriptionChange(t *testing.T) { + const prefix = "x-string-type: &text string\n" + left := buildSchemaProxyFromYAML(t, prefix+"type: ['null', string, number]") + for _, tc := range []struct { + name, types string + typeChanged bool + }{ + {"reordered", "[string, number, 'null']", false}, + {"reordered with alias", "[*text, number, 'null']", false}, + {"narrowed", "[string, number]", true}, + {"replaced", "[string, boolean, 'null']", true}, + } { + t.Run(tc.name, func(t *testing.T) { + right := buildSchemaProxyFromYAML(t, prefix+"type: "+tc.types+"\ndescription: Filter operator") + changes := CompareSchemas(left, right) + require.NotNil(t, changes) + properties := make(map[string]bool) + for _, change := range changes.GetAllChanges() { + properties[change.Property] = change.Breaking + } + want := map[string]bool{v3.DescriptionLabel: false} + if tc.typeChanged { + want[v3.TypeLabel] = true + } + assert.Equal(t, want, properties) + assert.Equal(t, len(want), changes.TotalChanges()) + }) + } +} + // https://github.com/pb33f/openapi-changes/issues/105 func TestCompareSchemas_AnyOfSimpleScalarUnionMatchesTypeArray(t *testing.T) { // Clear hash cache to ensure deterministic results in concurrent test environments From 34afec91e6c04d942d822d40057d48d8d0dab3fe Mon Sep 17 00:00:00 2001 From: quobix Date: Wed, 25 Nov 2026 11:39:02 -0500 Subject: [PATCH 3/4] test(what-changed): cover non-string members on both sides of type comparisons --- what-changed/model/schema_test.go | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/what-changed/model/schema_test.go b/what-changed/model/schema_test.go index 7824717e..4e48f7cc 100644 --- a/what-changed/model/schema_test.go +++ b/what-changed/model/schema_test.go @@ -2043,18 +2043,22 @@ components: func TestCompareSchemas_TypeArrayWithDescriptionChange(t *testing.T) { const prefix = "x-string-type: &text string\n" - left := buildSchemaProxyFromYAML(t, prefix+"type: ['null', string, number]") + const original = "['null', string, number]" for _, tc := range []struct { - name, types string - typeChanged bool + name, left, right string + typeChanged bool }{ - {"reordered", "[string, number, 'null']", false}, - {"reordered with alias", "[*text, number, 'null']", false}, - {"narrowed", "[string, number]", true}, - {"replaced", "[string, boolean, 'null']", true}, + {"reordered", original, "[string, number, 'null']", false}, + {"reordered with alias", original, "[*text, number, 'null']", false}, + {"narrowed", original, "[string, number]", true}, + {"replaced", original, "[string, boolean, 'null']", true}, + // Bare YAML null is distinct from the JSON Schema type name 'null'. + {"correct unquoted null", "[null, string, number]", original, true}, + {"introduce unquoted null", original, "[null, string, number]", true}, } { t.Run(tc.name, func(t *testing.T) { - right := buildSchemaProxyFromYAML(t, prefix+"type: "+tc.types+"\ndescription: Filter operator") + left := buildSchemaProxyFromYAML(t, prefix+"type: "+tc.left) + right := buildSchemaProxyFromYAML(t, prefix+"type: "+tc.right+"\ndescription: Filter operator") changes := CompareSchemas(left, right) require.NotNil(t, changes) properties := make(map[string]bool) From 03be841da13c2c711c6a3ec6cfd19d8c217d7ec4 Mon Sep 17 00:00:00 2001 From: quobix Date: Thu, 26 Nov 2026 11:26:58 -0500 Subject: [PATCH 4/4] fix(what-changed): compare schema types as the hash reads them and encode all example values --- what-changed/model/example.go | 23 ++++----- what-changed/model/example_test.go | 83 ++++++++++++++++++------------ what-changed/model/schema.go | 43 +++++++++------- what-changed/model/schema_test.go | 15 ++++-- 4 files changed, 95 insertions(+), 69 deletions(-) diff --git a/what-changed/model/example.go b/what-changed/model/example.go index 56c56957..5ac081f4 100644 --- a/what-changed/model/example.go +++ b/what-changed/model/example.go @@ -6,7 +6,6 @@ package model import ( "github.com/pb33f/libopenapi/datamodel/low/base" v3 "github.com/pb33f/libopenapi/datamodel/low/v3" - "github.com/pb33f/libopenapi/utils" ) // ExampleChanges represent changes to an Example object, part of an OpenAPI specification. @@ -78,25 +77,21 @@ func CompareExamples(l, r *base.Example) *ExampleChanges { v3.DescriptionLabel, &changes, l, r), ) - // Value - if utils.IsNodeMap(l.Value.ValueNode) && utils.IsNodeMap(r.Value.ValueNode) { - CheckForModificationWithEncoding(l.Value.ValueNode, r.Value.ValueNode, v3.ValueLabel, - &changes, BreakingModified(CompExample, PropValue), l.Value.Value, r.Value.Value) - } else { - props = append(props, NewPropertyCheck(CompExample, PropValue, + // Value and DataValue hold any YAML shape, so they are checked with encoding. + CheckPropertiesWithEncoding([]*PropertyCheck{ + NewPropertyCheck(CompExample, PropValue, l.Value.ValueNode, r.Value.ValueNode, - v3.ValueLabel, &changes, l, r)) - } + v3.ValueLabel, &changes, l, r), + NewPropertyCheck(CompExample, PropDataValue, + l.DataValue.ValueNode, r.DataValue.ValueNode, + base.DataValueLabel, &changes, l, r), + }) + // ExternalValue props = append(props, NewPropertyCheck(CompExample, PropExternalValue, l.ExternalValue.ValueNode, r.ExternalValue.ValueNode, v3.ExternalValue, &changes, l, r)) - // DataValue (OpenAPI 3.2+) - props = append(props, NewPropertyCheck(CompExample, PropDataValue, - l.DataValue.ValueNode, r.DataValue.ValueNode, - base.DataValueLabel, &changes, l, r)) - // SerializedValue (OpenAPI 3.2+) props = append(props, NewPropertyCheck(CompExample, PropSerializedValue, l.SerializedValue.ValueNode, r.SerializedValue.ValueNode, diff --git a/what-changed/model/example_test.go b/what-changed/model/example_test.go index 14b85dbe..5f2e08d8 100644 --- a/what-changed/model/example_test.go +++ b/what-changed/model/example_test.go @@ -45,42 +45,57 @@ func TestCompareExamples_SummaryModified(t *testing.T) { assert.Equal(t, "cure all", extChanges.Changes[0].New) } -func TestCompareExamples_ObjectValue(t *testing.T) { - for _, tc := range []struct { - name, left, right string - }{ - {"two members modified", "{id_1: 11, id_2: 21}", "{id_1: 10, id_2: 20}"}, - {"member added", "{}", "{id: 1}"}, - {"member removed", "{id: 1}", "{}"}, - {"sequence reordered inside object", "{items: [1, 2]}", "{items: [2, 1]}"}, - } { - t.Run(tc.name, func(t *testing.T) { - left := buildTestExample(t, "value: "+tc.left) - right := buildTestExample(t, "value: "+tc.right) - changes := CompareExamples(left, right) - require.NotNil(t, changes) - require.Len(t, changes.GetAllChanges(), 1) - change := changes.Changes[0] - assert.Equal(t, v3.ValueLabel, change.Property) - assert.Equal(t, Modified, change.ChangeType) - assert.False(t, change.Breaking) - assert.Equal(t, tc.left+"\n", change.OriginalEncoded) - assert.Equal(t, tc.right+"\n", change.NewEncoded) +func TestCompareExamples_StructuredValue(t *testing.T) { + for _, field := range []string{v3.ValueLabel, base.DataValueLabel} { + for _, tc := range []struct { + name, left, right string + }{ + {"two members modified", "{id_1: 11, id_2: 21}", "{id_1: 10, id_2: 20}"}, + {"member added", "{}", "{id: 1}"}, + {"member removed", "{id: 1}", "{}"}, + {"sequence reordered inside object", "{items: [1, 2]}", "{items: [2, 1]}"}, + {"sequence item modified", "[1, 2]", "[1, 3]"}, + {"sequence reordered", "[1, 2]", "[2, 1]"}, + {"sequence item added", "[1, 2]", "[1, 2, 3]"}, + {"object replaced by sequence", "{a: 1}", "[1]"}, + {"sequence replaced by object", "[1]", "{a: 1}"}, + } { + t.Run(field+" "+tc.name, func(t *testing.T) { + left := buildTestExample(t, field+": "+tc.left) + right := buildTestExample(t, field+": "+tc.right) + changes := CompareExamples(left, right) + require.NotNil(t, changes) + require.Len(t, changes.GetAllChanges(), 1) + change := changes.Changes[0] + assert.Equal(t, field, change.Property) + assert.Equal(t, Modified, change.ChangeType) + assert.False(t, change.Breaking) + assert.Equal(t, tc.left+"\n", change.OriginalEncoded) + assert.Equal(t, tc.right+"\n", change.NewEncoded) + }) + } + + t.Run(field+" members reordered", func(t *testing.T) { + left := buildTestExample(t, field+": {a: 1, b: 2}") + reordered := buildTestExample(t, field+": {b: 2, a: 1}") + assert.Nil(t, CompareExamples(left, reordered)) }) - } - left := buildTestExample(t, "value: {a: 1, b: 2}") - reordered := buildTestExample(t, "value: {b: 2, a: 1}") - assert.Nil(t, CompareExamples(left, reordered)) - - absent := buildTestExample(t, "summary: example") - present := buildTestExample(t, "summary: example\nvalue: {}") - added := CompareExamples(absent, present).GetAllChanges() - require.Len(t, added, 1) - assert.Equal(t, PropertyAdded, added[0].ChangeType) - removed := CompareExamples(present, absent).GetAllChanges() - require.Len(t, removed, 1) - assert.Equal(t, PropertyRemoved, removed[0].ChangeType) + t.Run(field+" added and removed", func(t *testing.T) { + absent := buildTestExample(t, "summary: example") + present := buildTestExample(t, "summary: example\n"+field+": {}") + added := CompareExamples(absent, present).GetAllChanges() + require.Len(t, added, 1) + assert.Equal(t, field, added[0].Property) + assert.Equal(t, PropertyAdded, added[0].ChangeType) + assert.Equal(t, "{}\n", added[0].NewEncoded) + removed := CompareExamples(present, absent).GetAllChanges() + require.Len(t, removed, 1) + assert.Equal(t, field, removed[0].Property) + assert.Equal(t, PropertyRemoved, removed[0].ChangeType) + assert.Equal(t, "{}\n", removed[0].OriginalEncoded) + }) + } } func TestCompareExamples_SummaryAdded(t *testing.T) { diff --git a/what-changed/model/schema.go b/what-changed/model/schema.go index b55710df..1acc2446 100644 --- a/what-changed/model/schema.go +++ b/what-changed/model/schema.go @@ -857,7 +857,7 @@ func (c *schemaComparison) checkSchemaPropertyChanges( if rSchema != nil && rSchema.Type.ValueNode != nil { rnv = rSchema.Type.ValueNode } - if !equalSchemaTypeArrays(lnv, rnv) { + if !equalSchemaTypes(lnv, rnv) { addCheck(lnv, rnv, v3.TypeLabel, PropType) } lnv = nil @@ -1538,31 +1538,38 @@ func (c *schemaComparison) checkSchemaPropertyChanges( } } -// JSON Schema type arrays list the allowed types in any order. -func equalSchemaTypeArrays(left, right *yaml.Node) bool { +// equalSchemaTypes reports whether two type nodes allow the same types, the way Schema.Hash reads them: +// order is ignored, a bare YAML null is the type name 'null', and a single type equals a one entry array. +func equalSchemaTypes(left, right *yaml.Node) bool { left, right = utils.NodeAlias(left), utils.NodeAlias(right) - if left == nil || right == nil || left.Kind != yaml.SequenceNode || right.Kind != yaml.SequenceNode { + if left == nil || right == nil || (left.Kind != yaml.SequenceNode && right.Kind != yaml.SequenceNode) { return false } - if len(left.Content) != len(right.Content) { + lt, lok := schemaTypeNames(left) + rt, rok := schemaTypeNames(right) + if !lok || !rok || len(lt) != len(rt) { return false } - counts := make(map[string]int, len(left.Content)) - for _, item := range left.Content { - item = utils.NodeAlias(item) - if item == nil || item.Kind != yaml.ScalarNode || item.Tag != "!!str" { - return false - } - counts[item.Value]++ + slices.Sort(lt) + slices.Sort(rt) + return slices.Equal(lt, rt) +} + +// schemaTypeNames returns the type names held by a type node, which is a single type or an array of them. +func schemaTypeNames(node *yaml.Node) ([]string, bool) { + items := []*yaml.Node{node} + if node.Kind == yaml.SequenceNode { + items = node.Content } - for _, item := range right.Content { - item = utils.NodeAlias(item) - if item == nil || item.Kind != yaml.ScalarNode || item.Tag != "!!str" || counts[item.Value] == 0 { - return false + names := make([]string, 0, len(items)) + for _, item := range items { + name, ok := extractScalarTypeName(utils.NodeAlias(item)) + if !ok { + return nil, false } - counts[item.Value]-- + names = append(names, name) } - return true + return names, true } func schemasUseEquivalentSimpleScalarUnion(l, r *base.SchemaProxy) bool { diff --git a/what-changed/model/schema_test.go b/what-changed/model/schema_test.go index 4e48f7cc..c6eb5773 100644 --- a/what-changed/model/schema_test.go +++ b/what-changed/model/schema_test.go @@ -2052,9 +2052,18 @@ func TestCompareSchemas_TypeArrayWithDescriptionChange(t *testing.T) { {"reordered with alias", original, "[*text, number, 'null']", false}, {"narrowed", original, "[string, number]", true}, {"replaced", original, "[string, boolean, 'null']", true}, - // Bare YAML null is distinct from the JSON Schema type name 'null'. - {"correct unquoted null", "[null, string, number]", original, true}, - {"introduce unquoted null", original, "[null, string, number]", true}, + // A bare YAML null is the type name 'null', the same way Schema.Hash reads it. + {"correct unquoted null", "[null, string, number]", original, false}, + {"introduce unquoted null", original, "[null, string, number]", false}, + {"reordered unquoted null", "[null, string, number]", "[string, number, null]", false}, + {"reordered tilde null", "[~, string, number]", "[string, number, ~]", false}, + // A single type means the same as a type array holding only that type. + {"single type to array", "string", "[string]", false}, + {"array to single type", "[string]", "string", false}, + {"array narrowed to single type", original, "string", true}, + {"single type replaced", "string", "number", true}, + {"duplicate member swapped", "[string, string, 'null']", "[string, 'null', 'null']", true}, + {"member without a type name", "['', string]", "[string, '']", true}, } { t.Run(tc.name, func(t *testing.T) { left := buildSchemaProxyFromYAML(t, prefix+"type: "+tc.left)