Paths added to what-changed feature

bubbled right up to the top and all the hard bits are done! components/defs are up next, however before that - we have to clean up some of the test coverage carnage appearing in our model after so much feature work.
This commit is contained in:
Dave Shanley
2022-11-04 10:09:20 -04:00
parent 55291d42c0
commit a522ec0f91
3 changed files with 538 additions and 0 deletions

View File

@@ -35,6 +35,16 @@ func (p *Paths) FindPath(path string) *low.ValueReference[*PathItem] {
return nil
}
// FindPathAndKey attempts to locate a PathItem instance, given a path key.
func (p *Paths) FindPathAndKey(path string) (*low.KeyReference[string], *low.ValueReference[*PathItem]) {
for k, j := range p.PathItems {
if k.Value == path {
return &k, &j
}
}
return nil, nil
}
// FindExtension will attempt to locate an extension using the specified string.
func (p *Paths) FindExtension(ext string) *low.ValueReference[any] {
return low.FindItemInMap[any](ext, p.Extensions)

188
what-changed/model/paths.go Normal file
View File

@@ -0,0 +1,188 @@
// Copyright 2022 Princess B33f Heavy Industries / Dave Shanley
// SPDX-License-Identifier: MIT
package model
import (
"github.com/pb33f/libopenapi/datamodel/low"
"github.com/pb33f/libopenapi/datamodel/low/v2"
v3 "github.com/pb33f/libopenapi/datamodel/low/v3"
"reflect"
"sync"
)
type PathsChanges struct {
PropertyChanges
PathItemsChanges map[string]*PathItemChanges
ExtensionChanges *ExtensionChanges
}
func (p *PathsChanges) TotalChanges() int {
c := p.PropertyChanges.TotalChanges()
for k := range p.PathItemsChanges {
c += p.PathItemsChanges[k].TotalChanges()
}
if p.ExtensionChanges != nil {
c += p.ExtensionChanges.TotalChanges()
}
return c
}
func (p *PathsChanges) TotalBreakingChanges() int {
c := p.PropertyChanges.TotalBreakingChanges()
for k := range p.PathItemsChanges {
c += p.PathItemsChanges[k].TotalBreakingChanges()
}
return c
}
func ComparePaths(l, r any) *PathsChanges {
var changes []*Change
pc := new(PathsChanges)
pathChanges := make(map[string]*PathItemChanges)
// Swagger
if reflect.TypeOf(&v2.Paths{}) == reflect.TypeOf(l) &&
reflect.TypeOf(&v2.Paths{}) == reflect.TypeOf(r) {
lPath := l.(*v2.Paths)
rPath := r.(*v2.Paths)
// perform hash check to avoid further processing
if low.AreEqual(lPath, rPath) {
return nil
}
lKeys := make(map[string]low.ValueReference[*v2.PathItem])
rKeys := make(map[string]low.ValueReference[*v2.PathItem])
for k := range lPath.PathItems {
lKeys[k.Value] = lPath.PathItems[k]
}
for k := range rPath.PathItems {
rKeys[k.Value] = rPath.PathItems[k]
}
// run every comparison in a thread.
var mLock sync.Mutex
compare := func(path string, pChanges map[string]*PathItemChanges, l, r *v2.PathItem, doneChan chan bool) {
if !low.AreEqual(l, r) {
mLock.Lock()
pathChanges[path] = ComparePathItems(l, r)
mLock.Unlock()
}
doneChan <- true
}
doneChan := make(chan bool)
pathsChecked := 0
for k := range lKeys {
if _, ok := rKeys[k]; ok {
go compare(k, pathChanges, lKeys[k].Value, rKeys[k].Value, doneChan)
pathsChecked++
continue
}
g, p := lPath.FindPathAndKey(k)
CreateChange(&changes, ObjectRemoved, v3.PathLabel,
g.KeyNode, nil, true,
p.Value, nil)
}
for k := range rKeys {
if _, ok := lKeys[k]; !ok {
g, p := rPath.FindPathAndKey(k)
CreateChange(&changes, ObjectAdded, v3.PathLabel,
nil, g.KeyNode, false,
nil, p.Value)
}
}
// wait for the things to be done.
completedChecks := 0
for completedChecks < pathsChecked {
select {
case <-doneChan:
completedChecks++
}
}
if len(pathChanges) > 0 {
pc.PathItemsChanges = pathChanges
}
pc.ExtensionChanges = CompareExtensions(lPath.Extensions, rPath.Extensions)
}
// OpenAPI
if reflect.TypeOf(&v3.Paths{}) == reflect.TypeOf(l) &&
reflect.TypeOf(&v3.Paths{}) == reflect.TypeOf(r) {
lPath := l.(*v3.Paths)
rPath := r.(*v3.Paths)
// perform hash check to avoid further processing
if low.AreEqual(lPath, rPath) {
return nil
}
lKeys := make(map[string]low.ValueReference[*v3.PathItem])
rKeys := make(map[string]low.ValueReference[*v3.PathItem])
for k := range lPath.PathItems {
lKeys[k.Value] = lPath.PathItems[k]
}
for k := range rPath.PathItems {
rKeys[k.Value] = rPath.PathItems[k]
}
// run every comparison in a thread.
var mLock sync.Mutex
compare := func(path string, pChanges map[string]*PathItemChanges, l, r *v3.PathItem, doneChan chan bool) {
if !low.AreEqual(l, r) {
mLock.Lock()
pathChanges[path] = ComparePathItems(l, r)
mLock.Unlock()
}
doneChan <- true
}
doneChan := make(chan bool)
pathsChecked := 0
for k := range lKeys {
if _, ok := rKeys[k]; ok {
go compare(k, pathChanges, lKeys[k].Value, rKeys[k].Value, doneChan)
pathsChecked++
continue
}
g, p := lPath.FindPathAndKey(k)
CreateChange(&changes, ObjectRemoved, v3.PathLabel,
g.KeyNode, nil, true,
p.Value, nil)
}
for k := range rKeys {
if _, ok := lKeys[k]; !ok {
g, p := rPath.FindPathAndKey(k)
CreateChange(&changes, ObjectAdded, v3.PathLabel,
nil, g.KeyNode, false,
nil, p.Value)
}
}
// wait for the things to be done.
completedChecks := 0
for completedChecks < pathsChecked {
select {
case <-doneChan:
completedChecks++
}
}
if len(pathChanges) > 0 {
pc.PathItemsChanges = pathChanges
}
pc.ExtensionChanges = CompareExtensions(lPath.Extensions, rPath.Extensions)
}
pc.Changes = changes
return pc
}

View File

@@ -0,0 +1,340 @@
// Copyright 2022 Princess B33f Heavy Industries / Dave Shanley
// SPDX-License-Identifier: MIT
package model
import (
"github.com/pb33f/libopenapi/datamodel/low"
"github.com/pb33f/libopenapi/datamodel/low/v2"
"github.com/pb33f/libopenapi/datamodel/low/v3"
"github.com/stretchr/testify/assert"
"gopkg.in/yaml.v3"
"testing"
)
func TestComparePaths_v2(t *testing.T) {
left := `
/fresh/cake:
get:
description: a thing?
/battered/fish:
post:
description: a thong?
/crispy/chips:
head:
description: a thang?`
right := left
var lNode, rNode yaml.Node
_ = yaml.Unmarshal([]byte(left), &lNode)
_ = yaml.Unmarshal([]byte(right), &rNode)
// create low level objects
var lDoc v2.Paths
var rDoc v2.Paths
_ = low.BuildModel(lNode.Content[0], &lDoc)
_ = low.BuildModel(rNode.Content[0], &rDoc)
_ = lDoc.Build(lNode.Content[0], nil)
_ = rDoc.Build(rNode.Content[0], nil)
// compare.
extChanges := ComparePaths(&rDoc, &lDoc)
assert.Nil(t, extChanges)
}
func TestComparePaths_v2_ModifyOp(t *testing.T) {
left := `/fresh/cake:
get:
description: a thing?
x-windows: dirty
/battered/fish:
post:
description: a thong?
/crispy/chips:
head:
description: a thang?`
right := `/fresh/cake:
get:
description: well, it's nice to be updated
/battered/fish:
post:
description: love being edited
x-windows: washed
/crispy/chips:
head:
description: any one for tennis?`
var lNode, rNode yaml.Node
_ = yaml.Unmarshal([]byte(left), &lNode)
_ = yaml.Unmarshal([]byte(right), &rNode)
// create low level objects
var lDoc v2.Paths
var rDoc v2.Paths
_ = low.BuildModel(lNode.Content[0], &lDoc)
_ = low.BuildModel(rNode.Content[0], &rDoc)
_ = lDoc.Build(lNode.Content[0], nil)
_ = rDoc.Build(rNode.Content[0], nil)
// compare.
extChanges := ComparePaths(&lDoc, &rDoc)
assert.Equal(t, 4, extChanges.TotalChanges())
assert.Equal(t, 0, extChanges.TotalBreakingChanges())
}
func TestComparePaths_v2_AddPath(t *testing.T) {
left := `/fresh/cake:
get:
description: a thing?
x-windows: dirty
/battered/fish:
post:
description: a thong?`
right := `/fresh/cake:
get:
description: a thing?
x-windows: dirty
/battered/fish:
post:
description: a thong?
/crispy/chips:
head:
description: a thang?`
var lNode, rNode yaml.Node
_ = yaml.Unmarshal([]byte(left), &lNode)
_ = yaml.Unmarshal([]byte(right), &rNode)
// create low level objects
var lDoc v2.Paths
var rDoc v2.Paths
_ = low.BuildModel(lNode.Content[0], &lDoc)
_ = low.BuildModel(rNode.Content[0], &rDoc)
_ = lDoc.Build(lNode.Content[0], nil)
_ = rDoc.Build(rNode.Content[0], nil)
// compare.
extChanges := ComparePaths(&lDoc, &rDoc)
assert.Equal(t, 1, extChanges.TotalChanges())
assert.Equal(t, 0, extChanges.TotalBreakingChanges())
assert.Equal(t, ObjectAdded, extChanges.Changes[0].ChangeType)
assert.Equal(t, "/crispy/chips", extChanges.Changes[0].New)
}
func TestComparePaths_v2_RemovePath(t *testing.T) {
left := `/fresh/cake:
get:
description: a thing?
x-windows: dirty
/battered/fish:
post:
description: a thong?`
right := `/fresh/cake:
get:
description: a thing?
x-windows: dirty
/battered/fish:
post:
description: a thong?
/crispy/chips:
head:
description: a thang?`
var lNode, rNode yaml.Node
_ = yaml.Unmarshal([]byte(left), &lNode)
_ = yaml.Unmarshal([]byte(right), &rNode)
// create low level objects
var lDoc v2.Paths
var rDoc v2.Paths
_ = low.BuildModel(lNode.Content[0], &lDoc)
_ = low.BuildModel(rNode.Content[0], &rDoc)
_ = lDoc.Build(lNode.Content[0], nil)
_ = rDoc.Build(rNode.Content[0], nil)
// compare.
extChanges := ComparePaths(&rDoc, &lDoc)
assert.Equal(t, 1, extChanges.TotalChanges())
assert.Equal(t, 1, extChanges.TotalBreakingChanges())
assert.Equal(t, ObjectRemoved, extChanges.Changes[0].ChangeType)
assert.Equal(t, "/crispy/chips", extChanges.Changes[0].Original)
}
func TestComparePaths_v3(t *testing.T) {
left := `/fresh/cake:
get:
description: a thing?
/battered/fish:
post:
description: a thong?
/crispy/chips:
head:
description: a thang?`
right := left
var lNode, rNode yaml.Node
_ = yaml.Unmarshal([]byte(left), &lNode)
_ = yaml.Unmarshal([]byte(right), &rNode)
// create low level objects
var lDoc v3.Paths
var rDoc v3.Paths
_ = low.BuildModel(lNode.Content[0], &lDoc)
_ = low.BuildModel(rNode.Content[0], &rDoc)
_ = lDoc.Build(lNode.Content[0], nil)
_ = rDoc.Build(rNode.Content[0], nil)
// compare.
extChanges := ComparePaths(&rDoc, &lDoc)
assert.Nil(t, extChanges)
}
func TestComparePaths_v3_ModifyOp(t *testing.T) {
left := `/fresh/cake:
get:
description: a thing?
x-windows: dirty
/battered/fish:
post:
description: a thong?
/crispy/chips:
head:
description: a thang?`
right := `/fresh/cake:
get:
description: well, it's nice to be updated
/battered/fish:
post:
description: love being edited
x-windows: washed
/crispy/chips:
head:
description: any one for tennis?`
var lNode, rNode yaml.Node
_ = yaml.Unmarshal([]byte(left), &lNode)
_ = yaml.Unmarshal([]byte(right), &rNode)
// create low level objects
var lDoc v3.Paths
var rDoc v3.Paths
_ = low.BuildModel(lNode.Content[0], &lDoc)
_ = low.BuildModel(rNode.Content[0], &rDoc)
_ = lDoc.Build(lNode.Content[0], nil)
_ = rDoc.Build(rNode.Content[0], nil)
// compare.
extChanges := ComparePaths(&lDoc, &rDoc)
assert.Equal(t, 4, extChanges.TotalChanges())
assert.Equal(t, 0, extChanges.TotalBreakingChanges())
}
func TestComparePaths_v3_AddPath(t *testing.T) {
left := `/fresh/cake:
get:
description: a thing?
x-windows: dirty
/battered/fish:
post:
description: a thong?
/crispy/chips:
head:
description: a thang?`
right := `/fresh/cake:
get:
description: a thing?
x-windows: dirty
/battered/fish:
post:
description: a thong?
/crispy/chips:
head:
description: a thang?
/mushy/peas:
post:
description: love being edited
x-windows: dirty`
var lNode, rNode yaml.Node
_ = yaml.Unmarshal([]byte(left), &lNode)
_ = yaml.Unmarshal([]byte(right), &rNode)
// create low level objects
var lDoc v3.Paths
var rDoc v3.Paths
_ = low.BuildModel(lNode.Content[0], &lDoc)
_ = low.BuildModel(rNode.Content[0], &rDoc)
_ = lDoc.Build(lNode.Content[0], nil)
_ = rDoc.Build(rNode.Content[0], nil)
// compare.
extChanges := ComparePaths(&lDoc, &rDoc)
assert.Equal(t, 1, extChanges.TotalChanges())
assert.Equal(t, 0, extChanges.TotalBreakingChanges())
assert.Equal(t, ObjectAdded, extChanges.Changes[0].ChangeType)
assert.Equal(t, "/mushy/peas", extChanges.Changes[0].New)
}
func TestComparePaths_v3_RemovePath(t *testing.T) {
left := `/fresh/cake:
get:
description: a thing?
x-windows: dirty
/battered/fish:
post:
description: a thong?
/crispy/chips:
head:
description: a thang?`
right := `/fresh/cake:
get:
description: a thing?
x-windows: dirty
/battered/fish:
post:
description: a thong?
/crispy/chips:
head:
description: a thang?
/mushy/peas:
post:
description: love being edited
x-windows: dirty`
var lNode, rNode yaml.Node
_ = yaml.Unmarshal([]byte(left), &lNode)
_ = yaml.Unmarshal([]byte(right), &rNode)
// create low level objects
var lDoc v3.Paths
var rDoc v3.Paths
_ = low.BuildModel(lNode.Content[0], &lDoc)
_ = low.BuildModel(rNode.Content[0], &rDoc)
_ = lDoc.Build(lNode.Content[0], nil)
_ = rDoc.Build(rNode.Content[0], nil)
// compare.
extChanges := ComparePaths(&rDoc, &lDoc)
assert.Equal(t, 1, extChanges.TotalChanges())
assert.Equal(t, 1, extChanges.TotalBreakingChanges())
assert.Equal(t, ObjectRemoved, extChanges.Changes[0].ChangeType)
assert.Equal(t, "/mushy/peas", extChanges.Changes[0].Original)
}