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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion what-changed/model/comparison_functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
3 changes: 2 additions & 1 deletion what-changed/model/comparison_functions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -326,14 +326,15 @@ 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,
}

// 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))

}
Expand Down
5 changes: 4 additions & 1 deletion what-changed/model/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -1170,13 +1171,15 @@ 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 {
if _, ok := k[g]; !ok {
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())
}
}

Expand Down
55 changes: 55 additions & 0 deletions what-changed/model/schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ package model
import (
"context"
"fmt"
"os"
"path/filepath"
"reflect"
"sort"
"strings"
Expand Down Expand Up @@ -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()
Expand Down
Loading