From 59995a2bfa8f294e7ca90e338ead2409fbd0b7d8 Mon Sep 17 00:00:00 2001 From: quobix Date: Sat, 14 Nov 2026 17:43:42 -0500 Subject: [PATCH 1/2] Preserve the source file for schema required changes --- what-changed/model/schema.go | 5 ++- what-changed/model/schema_test.go | 55 +++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+), 1 deletion(-) diff --git a/what-changed/model/schema.go b/what-changed/model/schema.go index c3559b17..1f5cea0a 100644 --- a/what-changed/model/schema.go +++ b/what-changed/model/schema.go @@ -1152,7 +1152,8 @@ func (c *schemaComparison) checkSchemaPropertyChanges( // Deprecated addCheck(lnv, rnv, v3.DeprecatedLabel, PropDeprecated) - // Required + // Required entries carry scalar getters. Their parent proxy retains the source index + // even when the comparison uses a normalized schema view. j := make(map[string]int) k := make(map[string]int) if lSchema != nil { @@ -1170,6 +1171,7 @@ func (c *schemaComparison) checkSchemaPropertyChanges( CreateChange(changes, PropertyAdded, v3.RequiredLabel, nil, rSchema.Required.Value[k[g]].GetValueNode(), BreakingAdded(CompSchema, PropRequired), nil, rSchema.Required.Value[k[g]].GetValue) + checkLocation((*changes)[len(*changes)-1].Context, rSchema.ParentProxy.Schema()) } } for g := range j { @@ -1177,6 +1179,7 @@ func (c *schemaComparison) checkSchemaPropertyChanges( CreateChange(changes, PropertyRemoved, v3.RequiredLabel, lSchema.Required.Value[j[g]].GetValueNode(), nil, BreakingRemoved(CompSchema, PropRequired), lSchema.Required.Value[j[g]].GetValue, nil) + checkLocation((*changes)[len(*changes)-1].Context, lSchema.ParentProxy.Schema()) } } diff --git a/what-changed/model/schema_test.go b/what-changed/model/schema_test.go index 19f4b88c..f4499fbd 100644 --- a/what-changed/model/schema_test.go +++ b/what-changed/model/schema_test.go @@ -6,6 +6,8 @@ package model import ( "context" "fmt" + "os" + "path/filepath" "reflect" "sort" "strings" @@ -546,6 +548,59 @@ components: assert.Equal(t, v3.RequiredLabel, changes.Changes[0].Property) } +func TestCompareSchemas_ExternalRequiredLocation(t *testing.T) { + const spec = `openapi: 3.1.0 +info: {title: Orders, version: 1.0.0} +paths: {} +components: + schemas: + Order: +` + const ref = " $ref: './order.yaml'\n" + build := func(schema, required string) (*base.SchemaProxy, string) { + t.Helper() + dir := t.TempDir() + filename := filepath.Join(dir, "order.yaml") + require.NoError(t, os.WriteFile(filename, []byte("type: object\nproperties: {id: {type: string}, currency: {type: string}}\nrequired: ["+required+"]\n"), 0600)) + config := datamodel.NewDocumentConfiguration() + config.BasePath = dir + config.AllowFileReferences = true + info, err := datamodel.ExtractSpecInfo([]byte(spec + schema)) + require.NoError(t, err) + doc, err := v3.CreateDocumentFromConfig(info, config) + require.NoError(t, err) + return doc.Components.Value.FindSchema("Order").Value, filename + } + + left, _ := build(ref, "id") + right, file := build(ref, "id, currency") + + added := CompareSchemas(left, right) + require.NotNil(t, added) + require.Len(t, added.Changes, 1) + assert.Equal(t, PropertyAdded, added.Changes[0].ChangeType) + assert.Equal(t, file, added.Changes[0].Context.DocumentLocation) + assert.Equal(t, "currency", added.Changes[0].NewObject.(func() string)()) + + removed := CompareSchemas(right, left) + require.NotNil(t, removed) + require.Len(t, removed.Changes, 1) + assert.Equal(t, PropertyRemoved, removed.Changes[0].ChangeType) + assert.Equal(t, file, removed.Changes[0].Context.DocumentLocation) + assert.Equal(t, "currency", removed.Changes[0].OriginalObject.(func() string)()) + // A preserved object branch has a different index from its inline wrapper. + wrapped, branchFile := build(" anyOf:\n - $ref: './order.yaml'\n - type: 'null'\n", "id, currency") + widened := CompareSchemas(left, wrapped) + require.NotNil(t, widened) + for _, change := range widened.GetAllChanges() { + if change.Property == v3.RequiredLabel && change.New == "currency" { + assert.Equal(t, branchFile, change.Context.DocumentLocation) + return + } + } + t.Fatal("missing required currency addition in external branch") +} + func TestCompareSchemas_EnumSimilar(t *testing.T) { // Clear hash cache to ensure deterministic results in concurrent test environments low.ClearHashCache() From db27aae96380883740653c3658a79b4110ea35ff Mon Sep 17 00:00:00 2001 From: quobix Date: Sat, 14 Nov 2026 17:47:05 -0500 Subject: [PATCH 2/2] Handle source indexes without a registered root --- what-changed/model/comparison_functions.go | 2 +- what-changed/model/comparison_functions_test.go | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/what-changed/model/comparison_functions.go b/what-changed/model/comparison_functions.go index 1de6d4d2..a415f76a 100644 --- a/what-changed/model/comparison_functions.go +++ b/what-changed/model/comparison_functions.go @@ -109,7 +109,7 @@ func checkLocation(ctx *ChangeContext, hs base.HasIndex) bool { if idx.GetRolodex() != nil { r := idx.GetRolodex() rIdx := r.GetRootIndex() - if rIdx.GetSpecAbsolutePath() != idx.GetSpecAbsolutePath() { + if rIdx != nil && rIdx.GetSpecAbsolutePath() != idx.GetSpecAbsolutePath() { ctx.DocumentLocation = idx.GetSpecAbsolutePath() return true } diff --git a/what-changed/model/comparison_functions_test.go b/what-changed/model/comparison_functions_test.go index d2ec9bf0..c9492eee 100644 --- a/what-changed/model/comparison_functions_test.go +++ b/what-changed/model/comparison_functions_test.go @@ -326,7 +326,6 @@ func Test_checkLocation(t *testing.T) { rolodex := index.NewRolodex(&index.SpecIndexConfig{}) idx.SetRolodex(rolodex) idxB.SetRolodex(rolodex) - rolodex.SetRootIndex(idx) testHasIndex := &test_hasIndex{ index: idxB, @@ -334,6 +333,8 @@ func Test_checkLocation(t *testing.T) { // https://suno.com/s/FtPAc2SaXEw5vTsH idxB.SetAbsolutePath("milly-milk-bottle") + assert.False(t, checkLocation(&ChangeContext{}, testHasIndex)) + rolodex.SetRootIndex(idx) assert.True(t, checkLocation(&ChangeContext{DocumentLocation: "sunny-spain"}, testHasIndex)) }