mirror of
https://github.com/LukeHagar/libopenapi.git
synced 2025-12-07 04:20:14 +00:00
Working through test coverage of schema proxy
Main logic is now in place, ready for fleshing out. Pattern feels right.
This commit is contained in:
@@ -6,6 +6,7 @@ package base
|
||||
import (
|
||||
"github.com/pb33f/libopenapi/datamodel/low"
|
||||
"github.com/pb33f/libopenapi/index"
|
||||
"github.com/pb33f/libopenapi/utils"
|
||||
"gopkg.in/yaml.v3"
|
||||
)
|
||||
|
||||
@@ -55,6 +56,10 @@ type SchemaProxy struct {
|
||||
func (sp *SchemaProxy) Build(root *yaml.Node, idx *index.SpecIndex) error {
|
||||
sp.vn = root
|
||||
sp.idx = idx
|
||||
if rf, _, r := utils.IsNodeRefValue(root); rf {
|
||||
sp.isReference = true
|
||||
sp.referenceLookup = r
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
@@ -9,6 +9,7 @@ import (
|
||||
"github.com/pb33f/libopenapi/datamodel/low/base"
|
||||
v3 "github.com/pb33f/libopenapi/datamodel/low/v3"
|
||||
"sort"
|
||||
"sync"
|
||||
)
|
||||
|
||||
type SchemaChanges struct {
|
||||
@@ -146,24 +147,25 @@ func CompareSchemas(l, r *base.SchemaProxy) *SchemaChanges {
|
||||
} else {
|
||||
// references are different, that's all we care to know.
|
||||
CreateChange[*base.Schema](&changes, Modified, v3.RefLabel,
|
||||
nil, nil, true, l.GetSchemaReference(), r.GetSchemaReference())
|
||||
l.GetValueNode().Content[1], r.GetValueNode().Content[1], true, l.GetSchemaReference(),
|
||||
r.GetSchemaReference())
|
||||
sc.Changes = changes
|
||||
return sc
|
||||
}
|
||||
}
|
||||
|
||||
// changed from ref to inline
|
||||
// changed from inline to ref
|
||||
if !l.IsSchemaReference() && r.IsSchemaReference() {
|
||||
CreateChange[*base.Schema](&changes, Modified, v3.RefLabel,
|
||||
nil, nil, false, "", r.GetSchemaReference())
|
||||
l.GetValueNode(), r.GetValueNode().Content[1], true, l, r.GetSchemaReference())
|
||||
sc.Changes = changes
|
||||
return sc // we're done here
|
||||
}
|
||||
|
||||
// changed from inline to ref
|
||||
// changed from ref to inline
|
||||
if l.IsSchemaReference() && !r.IsSchemaReference() {
|
||||
CreateChange[*base.Schema](&changes, Modified, v3.RefLabel,
|
||||
nil, nil, false, l.GetSchemaReference(), "")
|
||||
l.GetValueNode().Content[1], r.GetValueNode(), true, l.GetSchemaReference(), r)
|
||||
sc.Changes = changes
|
||||
return sc // done, nothing else to do.
|
||||
}
|
||||
@@ -332,17 +334,6 @@ func CompareSchemas(l, r *base.SchemaProxy) *SchemaChanges {
|
||||
New: rSchema,
|
||||
})
|
||||
|
||||
// UniqueItems
|
||||
props = append(props, &PropertyCheck[*base.Schema]{
|
||||
LeftNode: lSchema.UniqueItems.ValueNode,
|
||||
RightNode: rSchema.UniqueItems.ValueNode,
|
||||
Label: v3.MinLengthLabel,
|
||||
Changes: &changes,
|
||||
Breaking: true,
|
||||
Original: lSchema,
|
||||
New: rSchema,
|
||||
})
|
||||
|
||||
// MaxProperties
|
||||
props = append(props, &PropertyCheck[*base.Schema]{
|
||||
LeftNode: lSchema.MaxProperties.ValueNode,
|
||||
@@ -365,54 +356,6 @@ func CompareSchemas(l, r *base.SchemaProxy) *SchemaChanges {
|
||||
New: rSchema,
|
||||
})
|
||||
|
||||
// Required
|
||||
j := make(map[string]int)
|
||||
k := make(map[string]int)
|
||||
for i := range lSchema.Required.Value {
|
||||
j[lSchema.Required.Value[i].Value] = i
|
||||
}
|
||||
for i := range rSchema.Required.Value {
|
||||
k[rSchema.Required.Value[i].Value] = i
|
||||
}
|
||||
for g := range k {
|
||||
if _, ok := j[g]; !ok {
|
||||
CreateChange[*base.Schema](&changes, PropertyAdded, v3.RequiredLabel,
|
||||
nil, rSchema.Required.Value[k[g]].GetValueNode(), true, nil,
|
||||
rSchema.Required.Value[k[g]].GetValue)
|
||||
}
|
||||
}
|
||||
for g := range j {
|
||||
if _, ok := k[g]; !ok {
|
||||
CreateChange[*base.Schema](&changes, PropertyRemoved, v3.RequiredLabel,
|
||||
lSchema.Required.Value[j[g]].GetValueNode(), nil, true, lSchema.Required.Value[j[g]].GetValue,
|
||||
nil)
|
||||
}
|
||||
}
|
||||
|
||||
// Enums
|
||||
j = make(map[string]int)
|
||||
k = make(map[string]int)
|
||||
for i := range lSchema.Enum.Value {
|
||||
j[lSchema.Enum.Value[i].Value] = i
|
||||
}
|
||||
for i := range rSchema.Enum.Value {
|
||||
k[rSchema.Enum.Value[i].Value] = i
|
||||
}
|
||||
for g := range k {
|
||||
if _, ok := j[g]; !ok {
|
||||
CreateChange[*base.Schema](&changes, PropertyAdded, v3.EnumLabel,
|
||||
nil, rSchema.Enum.Value[k[g]].GetValueNode(), false, nil,
|
||||
rSchema.Enum.Value[k[g]].GetValue)
|
||||
}
|
||||
}
|
||||
for g := range j {
|
||||
if _, ok := k[g]; !ok {
|
||||
CreateChange[*base.Schema](&changes, PropertyRemoved, v3.EnumLabel,
|
||||
lSchema.Enum.Value[j[g]].GetValueNode(), nil, true, lSchema.Enum.Value[j[g]].GetValue,
|
||||
nil)
|
||||
}
|
||||
}
|
||||
|
||||
// UniqueItems
|
||||
props = append(props, &PropertyCheck[*base.Schema]{
|
||||
LeftNode: lSchema.UniqueItems.ValueNode,
|
||||
@@ -439,7 +382,7 @@ func CompareSchemas(l, r *base.SchemaProxy) *SchemaChanges {
|
||||
props = append(props, &PropertyCheck[*base.Schema]{
|
||||
LeftNode: lSchema.Description.ValueNode,
|
||||
RightNode: rSchema.Description.ValueNode,
|
||||
Label: v3.MinLengthLabel,
|
||||
Label: v3.DescriptionLabel,
|
||||
Changes: &changes,
|
||||
Breaking: false,
|
||||
Original: lSchema,
|
||||
@@ -534,15 +477,58 @@ func CompareSchemas(l, r *base.SchemaProxy) *SchemaChanges {
|
||||
New: rSchema,
|
||||
})
|
||||
|
||||
// Required
|
||||
j := make(map[string]int)
|
||||
k := make(map[string]int)
|
||||
for i := range lSchema.Required.Value {
|
||||
j[lSchema.Required.Value[i].Value] = i
|
||||
}
|
||||
for i := range rSchema.Required.Value {
|
||||
k[rSchema.Required.Value[i].Value] = i
|
||||
}
|
||||
for g := range k {
|
||||
if _, ok := j[g]; !ok {
|
||||
CreateChange[*base.Schema](&changes, PropertyAdded, v3.RequiredLabel,
|
||||
nil, rSchema.Required.Value[k[g]].GetValueNode(), true, nil,
|
||||
rSchema.Required.Value[k[g]].GetValue)
|
||||
}
|
||||
}
|
||||
for g := range j {
|
||||
if _, ok := k[g]; !ok {
|
||||
CreateChange[*base.Schema](&changes, PropertyRemoved, v3.RequiredLabel,
|
||||
lSchema.Required.Value[j[g]].GetValueNode(), nil, true, lSchema.Required.Value[j[g]].GetValue,
|
||||
nil)
|
||||
}
|
||||
}
|
||||
|
||||
// Enums
|
||||
j = make(map[string]int)
|
||||
k = make(map[string]int)
|
||||
for i := range lSchema.Enum.Value {
|
||||
j[lSchema.Enum.Value[i].Value] = i
|
||||
}
|
||||
for i := range rSchema.Enum.Value {
|
||||
k[rSchema.Enum.Value[i].Value] = i
|
||||
}
|
||||
for g := range k {
|
||||
if _, ok := j[g]; !ok {
|
||||
CreateChange[*base.Schema](&changes, PropertyAdded, v3.EnumLabel,
|
||||
nil, rSchema.Enum.Value[k[g]].GetValueNode(), false, nil,
|
||||
rSchema.Enum.Value[k[g]].GetValue)
|
||||
}
|
||||
}
|
||||
for g := range j {
|
||||
if _, ok := k[g]; !ok {
|
||||
CreateChange[*base.Schema](&changes, PropertyRemoved, v3.EnumLabel,
|
||||
lSchema.Enum.Value[j[g]].GetValueNode(), nil, true, lSchema.Enum.Value[j[g]].GetValue,
|
||||
nil)
|
||||
}
|
||||
}
|
||||
|
||||
// check core properties
|
||||
CheckProperties(props)
|
||||
|
||||
//propChanges := make(map[string]*SchemaChanges)
|
||||
|
||||
// extract left and right prop names
|
||||
if len(lSchema.Properties.Value) == 0 && len(lSchema.Properties.Value) == 0 {
|
||||
// do something here when extracting.
|
||||
}
|
||||
propChanges := make(map[string]*SchemaChanges)
|
||||
|
||||
lProps := make([]string, len(lSchema.Properties.Value))
|
||||
lEntities := make(map[string]*base.SchemaProxy)
|
||||
@@ -550,72 +536,112 @@ func CompareSchemas(l, r *base.SchemaProxy) *SchemaChanges {
|
||||
rEntities := make(map[string]*base.SchemaProxy)
|
||||
|
||||
for w := range lSchema.Properties.Value {
|
||||
if !lSchema.Properties.Value[w].Value.IsSchemaReference() {
|
||||
lProps = append(lProps, w.Value)
|
||||
|
||||
// TODO: check for ref
|
||||
lEntities[w.Value] = lSchema.Properties.Value[w].Value
|
||||
}
|
||||
}
|
||||
for w := range rSchema.Properties.Value {
|
||||
// TODO: check for ref.
|
||||
if !rSchema.Properties.Value[w].Value.IsSchemaReference() {
|
||||
rProps = append(rProps, w.Value)
|
||||
rEntities[w.Value] = rSchema.Properties.Value[w].Value
|
||||
}
|
||||
}
|
||||
sort.Strings(lProps)
|
||||
sort.Strings(rProps)
|
||||
|
||||
if len(lProps) == len(rProps) {
|
||||
for w := range lProps {
|
||||
lp := lEntities[lProps[w]]
|
||||
rp := rEntities[rProps[w]]
|
||||
|
||||
var propLock sync.Mutex
|
||||
checkProperty := func(key string, lp, rp *base.SchemaProxy, propChanges map[string]*SchemaChanges, done chan bool) {
|
||||
if lp != nil && rp != nil {
|
||||
ls := lp.Schema()
|
||||
rs := rp.Schema()
|
||||
if low.AreEqual(ls, rs) {
|
||||
continue
|
||||
done <- true
|
||||
return
|
||||
}
|
||||
s := CompareSchemas(lp, rp)
|
||||
propLock.Lock()
|
||||
propChanges[key] = s
|
||||
propLock.Unlock()
|
||||
done <- true
|
||||
}
|
||||
}
|
||||
//propChanges[w] =
|
||||
|
||||
doneChan := make(chan bool)
|
||||
totalProperties := 0
|
||||
if len(lProps) == len(rProps) {
|
||||
for w := range lProps {
|
||||
lp := lEntities[lProps[w]]
|
||||
rp := rEntities[rProps[w]]
|
||||
if lProps[w] == rProps[w] && lp != nil && rp != nil {
|
||||
totalProperties++
|
||||
go checkProperty(lProps[w], lp, rp, propChanges, doneChan)
|
||||
}
|
||||
|
||||
// keys do not match, even after sorting, means a like for like replacement.
|
||||
if lProps[w] != rProps[w] {
|
||||
|
||||
// old removed, new added.
|
||||
CreateChange[*base.Schema](&changes, ObjectAdded, v3.PropertiesLabel,
|
||||
nil, rEntities[rProps[w]].GetValueNode(), false, nil, rEntities[rProps[w]])
|
||||
CreateChange[*base.Schema](&changes, ObjectRemoved, v3.PropertiesLabel,
|
||||
lEntities[lProps[w]].GetValueNode(), nil, true, lEntities[lProps[w]], nil)
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
//for q := range lSchema.Properties.Value {
|
||||
// if _, ok := rSchema.Properties.Value[q]; ok {
|
||||
// // hash the schemas, if they match, skip, if not... then compare.
|
||||
// ls := lSchema.Properties.Value[q].Value.Schema()
|
||||
// rs := rSchema.Properties.Value[q].Value.Schema()
|
||||
// if low.AreEqual(ls, rs) {
|
||||
// continue
|
||||
// }
|
||||
// prop
|
||||
//
|
||||
// }
|
||||
//}
|
||||
// something removed
|
||||
if len(lProps) > len(rProps) {
|
||||
for w := range lProps {
|
||||
if w < len(rProps) {
|
||||
totalProperties++
|
||||
go checkProperty(lProps[w], lEntities[lProps[w]], rEntities[rProps[w]], propChanges, doneChan)
|
||||
}
|
||||
if w >= len(rProps) {
|
||||
CreateChange[*base.Schema](&changes, ObjectRemoved, v3.PropertiesLabel,
|
||||
lEntities[lProps[w]].GetValueNode(), nil, true, lEntities[lProps[w]], nil)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// something added
|
||||
if len(rProps) > len(lProps) {
|
||||
for w := range rProps {
|
||||
if w < len(lProps) {
|
||||
totalProperties++
|
||||
go checkProperty(rProps[w], lEntities[lProps[w]], rEntities[rProps[w]], propChanges, doneChan)
|
||||
}
|
||||
if w >= len(rProps) {
|
||||
CreateChange[*base.Schema](&changes, ObjectAdded, v3.PropertiesLabel,
|
||||
nil, rEntities[rProps[w]].GetValueNode(), false, nil, rEntities[rProps[w]])
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
sc.SchemaPropertyChanges = propChanges
|
||||
|
||||
// check polymorphic and multi-values async for speed.
|
||||
done := make(chan bool)
|
||||
go extractSchemaChanges(lSchema.OneOf.Value, rSchema.OneOf.Value, v3.OneOfLabel,
|
||||
&sc.OneOfChanges, &changes, done)
|
||||
&sc.OneOfChanges, &changes, doneChan)
|
||||
|
||||
go extractSchemaChanges(lSchema.AllOf.Value, rSchema.AllOf.Value, v3.AllOfLabel,
|
||||
&sc.AllOfChanges, &changes, done)
|
||||
&sc.AllOfChanges, &changes, doneChan)
|
||||
|
||||
go extractSchemaChanges(lSchema.AnyOf.Value, rSchema.AnyOf.Value, v3.AnyOfLabel,
|
||||
&sc.AnyOfChanges, &changes, done)
|
||||
&sc.AnyOfChanges, &changes, doneChan)
|
||||
|
||||
go extractSchemaChanges(lSchema.Items.Value, rSchema.Items.Value, v3.ItemsLabel,
|
||||
&sc.ItemsChanges, &changes, done)
|
||||
&sc.ItemsChanges, &changes, doneChan)
|
||||
|
||||
go extractSchemaChanges(lSchema.Not.Value, rSchema.Not.Value, v3.ItemsLabel,
|
||||
&sc.ItemsChanges, &changes, done)
|
||||
&sc.ItemsChanges, &changes, doneChan)
|
||||
|
||||
totalChecks := 5
|
||||
totalChecks := totalProperties + 5
|
||||
completedChecks := 0
|
||||
for completedChecks < totalChecks {
|
||||
select {
|
||||
case <-done:
|
||||
case <-doneChan:
|
||||
completedChecks++
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@ package what_changed
|
||||
|
||||
import (
|
||||
"github.com/pb33f/libopenapi/datamodel"
|
||||
"github.com/pb33f/libopenapi/datamodel/low/base"
|
||||
v3 "github.com/pb33f/libopenapi/datamodel/low/v3"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"testing"
|
||||
@@ -12,180 +13,276 @@ import (
|
||||
|
||||
// These tests require full documents to be tested properly. schemas are perhaps the most complex
|
||||
// of all the things in OpenAPI, to ensure correctness, we must test the whole document structure.
|
||||
func TestCompareSchemas(t *testing.T) {
|
||||
//
|
||||
// To test this correctly, we need a simulated document with inline schemas for recursive
|
||||
// checking, as well as a couple of references, so we can avoid that disaster.
|
||||
// in our model, components/definitions will be checked independently for changes
|
||||
// and references will be checked only for value changes (points to a different reference)
|
||||
func TestCompareSchemas_PropertyModification(t *testing.T) {
|
||||
left := `components:
|
||||
schemas:
|
||||
OK:
|
||||
title: an OK message`
|
||||
|
||||
// to test this correctly, we need a simulated document with inline schemas for recursive
|
||||
// checking, as well as a couple of references, so we can avoid that disaster.
|
||||
// in our model, components/definitions will be checked independently for changes
|
||||
// and references will be checked only for value changes (points to a different reference)
|
||||
// left := `openapi: 3.1.0
|
||||
//paths:
|
||||
// /chicken/nuggets:
|
||||
// get:
|
||||
// responses:
|
||||
// "200":
|
||||
// content:
|
||||
// application/json:
|
||||
// schema:
|
||||
// $ref: '#/components/schemas/OK'
|
||||
// /chicken/soup:
|
||||
// get:
|
||||
// responses:
|
||||
// "200":
|
||||
// content:
|
||||
// application/json:
|
||||
// schema:
|
||||
// title: an OK message
|
||||
// allOf:
|
||||
// - type: int
|
||||
// properties:
|
||||
// propA:
|
||||
// title: a proxy property
|
||||
// type: string
|
||||
//components:
|
||||
// schemas:
|
||||
// OK:
|
||||
// title: an OK message
|
||||
// allOf:
|
||||
// - type: string
|
||||
// properties:
|
||||
// propA:
|
||||
// title: a proxy property
|
||||
// type: string`
|
||||
//
|
||||
// right := `openapi: 3.1.0
|
||||
//paths:
|
||||
// /chicken/nuggets:
|
||||
// get:
|
||||
// responses:
|
||||
// "200":
|
||||
// content:
|
||||
// application/json:
|
||||
// schema:
|
||||
// $ref: '#/components/schemas/OK'
|
||||
// /chicken/soup:
|
||||
// get:
|
||||
// responses:
|
||||
// "200":
|
||||
// content:
|
||||
// application/json:
|
||||
// schema:
|
||||
// title: an OK message that is different
|
||||
// allOf:
|
||||
// - type: int
|
||||
// description: oh my stars
|
||||
// - $ref: '#/components/schemas/NoWay'
|
||||
// properties:
|
||||
// propA:
|
||||
// title: a proxy property
|
||||
// type: string
|
||||
//components:
|
||||
// schemas:
|
||||
// NoWay:
|
||||
// type: string
|
||||
// OK:
|
||||
// title: an OK message that has now changed.
|
||||
// allOf:
|
||||
// - type: string
|
||||
// properties:
|
||||
// propA:
|
||||
// title: a proxy property
|
||||
// type: string`
|
||||
right := `components:
|
||||
schemas:
|
||||
OK:
|
||||
title: an OK message Changed`
|
||||
|
||||
left := `openapi: 3.1.0
|
||||
paths:
|
||||
/chicken/nuggets:
|
||||
get:
|
||||
responses:
|
||||
"200":
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
$ref: '#/components/schemas/OK'
|
||||
/chicken/soup:
|
||||
get:
|
||||
responses:
|
||||
"200":
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
title: an OK message
|
||||
allOf:
|
||||
- type: bool
|
||||
oneOf:
|
||||
- type: int
|
||||
- type: string
|
||||
properties:
|
||||
propA:
|
||||
title: a proxy property
|
||||
type: string
|
||||
components:
|
||||
leftDoc, rightDoc := test_BuildDoc(left, right)
|
||||
|
||||
// extract left reference schema and non reference schema.
|
||||
lSchemaProxy := leftDoc.Components.Value.FindSchema("OK").Value
|
||||
rSchemaProxy := rightDoc.Components.Value.FindSchema("OK").Value
|
||||
|
||||
changes := CompareSchemas(lSchemaProxy, rSchemaProxy)
|
||||
assert.NotNil(t, changes)
|
||||
assert.Equal(t, 1, changes.TotalChanges())
|
||||
assert.Equal(t, 0, changes.TotalBreakingChanges())
|
||||
assert.Equal(t, Modified, changes.Changes[0].ChangeType)
|
||||
assert.Equal(t, "an OK message Changed", changes.Changes[0].New)
|
||||
}
|
||||
|
||||
func TestCompareSchemas_PropertyAdd(t *testing.T) {
|
||||
left := `components:
|
||||
schemas:
|
||||
OK:
|
||||
title: an OK message`
|
||||
|
||||
right := `components:
|
||||
schemas:
|
||||
OK:
|
||||
title: an OK message
|
||||
allOf:
|
||||
- type: string
|
||||
properties:
|
||||
propA:
|
||||
title: a proxy property
|
||||
type: string`
|
||||
description: a thing`
|
||||
|
||||
right := `openapi: 3.1.0
|
||||
paths:
|
||||
/chicken/nuggets:
|
||||
get:
|
||||
responses:
|
||||
"200":
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
$ref: '#/components/schemas/OK'
|
||||
/chicken/soup:
|
||||
get:
|
||||
responses:
|
||||
"200":
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
title: an OK message that is different
|
||||
allOf:
|
||||
- type: string
|
||||
oneOf:
|
||||
- type: string
|
||||
properties:
|
||||
propA:
|
||||
title: a proxy property
|
||||
type: string
|
||||
components:
|
||||
leftDoc, rightDoc := test_BuildDoc(left, right)
|
||||
|
||||
// extract left reference schema and non reference schema.
|
||||
lSchemaProxy := leftDoc.Components.Value.FindSchema("OK").Value
|
||||
rSchemaProxy := rightDoc.Components.Value.FindSchema("OK").Value
|
||||
|
||||
changes := CompareSchemas(lSchemaProxy, rSchemaProxy)
|
||||
assert.NotNil(t, changes)
|
||||
assert.Len(t, changes.Changes, 1)
|
||||
assert.Equal(t, PropertyAdded, changes.Changes[0].ChangeType)
|
||||
assert.Equal(t, "a thing", changes.Changes[0].New)
|
||||
assert.Equal(t, v3.DescriptionLabel, changes.Changes[0].Property)
|
||||
}
|
||||
|
||||
func TestCompareSchemas_PropertyRemove(t *testing.T) {
|
||||
left := `components:
|
||||
schemas:
|
||||
NoWay:
|
||||
type: string
|
||||
OK:
|
||||
title: an OK message that has now changed.
|
||||
allOf:
|
||||
- type: string
|
||||
properties:
|
||||
propA:
|
||||
title: a proxy property
|
||||
type: string`
|
||||
title: an OK message
|
||||
description: a thing`
|
||||
|
||||
leftInfo, _ := datamodel.ExtractSpecInfo([]byte(left))
|
||||
rightInfo, _ := datamodel.ExtractSpecInfo([]byte(right))
|
||||
right := `components:
|
||||
schemas:
|
||||
OK:
|
||||
title: an OK message`
|
||||
|
||||
leftDoc, rightDoc := test_BuildDoc(left, right)
|
||||
|
||||
// extract left reference schema and non reference schema.
|
||||
lSchemaProxy := leftDoc.Components.Value.FindSchema("OK").Value
|
||||
rSchemaProxy := rightDoc.Components.Value.FindSchema("OK").Value
|
||||
|
||||
changes := CompareSchemas(lSchemaProxy, rSchemaProxy)
|
||||
assert.NotNil(t, changes)
|
||||
assert.Len(t, changes.Changes, 1)
|
||||
assert.Equal(t, PropertyRemoved, changes.Changes[0].ChangeType)
|
||||
assert.Equal(t, "a thing", changes.Changes[0].Original)
|
||||
assert.Equal(t, v3.DescriptionLabel, changes.Changes[0].Property)
|
||||
}
|
||||
|
||||
func TestCompareSchemas_Removed(t *testing.T) {
|
||||
left := `components:
|
||||
schemas:
|
||||
OK:
|
||||
title: an OK message
|
||||
description: a thing`
|
||||
|
||||
right := `components:
|
||||
schemas:`
|
||||
|
||||
leftDoc, _ := test_BuildDoc(left, right)
|
||||
|
||||
// extract left reference schema and non reference schema.
|
||||
lSchemaProxy := leftDoc.Components.Value.FindSchema("OK").Value
|
||||
var rSchemaProxy *base.SchemaProxy
|
||||
|
||||
changes := CompareSchemas(lSchemaProxy, rSchemaProxy)
|
||||
assert.NotNil(t, changes)
|
||||
assert.Len(t, changes.Changes, 1)
|
||||
assert.Equal(t, ObjectRemoved, changes.Changes[0].ChangeType)
|
||||
}
|
||||
|
||||
func TestCompareSchemas_Added(t *testing.T) {
|
||||
left := `components:
|
||||
schemas:
|
||||
OK:
|
||||
title: an OK message
|
||||
description: a thing`
|
||||
|
||||
right := `components:
|
||||
schemas:`
|
||||
|
||||
leftDoc, _ := test_BuildDoc(left, right)
|
||||
|
||||
// extract left reference schema and non reference schema.
|
||||
lSchemaProxy := leftDoc.Components.Value.FindSchema("OK").Value
|
||||
var rSchemaProxy *base.SchemaProxy
|
||||
|
||||
changes := CompareSchemas(rSchemaProxy, lSchemaProxy)
|
||||
assert.NotNil(t, changes)
|
||||
assert.Len(t, changes.Changes, 1)
|
||||
assert.Equal(t, ObjectAdded, changes.Changes[0].ChangeType)
|
||||
}
|
||||
|
||||
func test_BuildDoc(l, r string) (*v3.Document, *v3.Document) {
|
||||
|
||||
leftInfo, _ := datamodel.ExtractSpecInfo([]byte(l))
|
||||
rightInfo, _ := datamodel.ExtractSpecInfo([]byte(r))
|
||||
|
||||
leftDoc, _ := v3.CreateDocument(leftInfo)
|
||||
rightDoc, _ := v3.CreateDocument(rightInfo)
|
||||
return leftDoc, rightDoc
|
||||
}
|
||||
|
||||
func TestCompareSchemas_RefIgnore(t *testing.T) {
|
||||
left := `components:
|
||||
schemas:
|
||||
Yo:
|
||||
type: int
|
||||
OK:
|
||||
$ref: '#/components/schemas/Yo'`
|
||||
|
||||
right := `components:
|
||||
schemas:
|
||||
Yo:
|
||||
type: int
|
||||
OK:
|
||||
$ref: '#/components/schemas/Yo'`
|
||||
|
||||
leftDoc, rightDoc := test_BuildDoc(left, right)
|
||||
|
||||
// extract left reference schema and non reference schema.
|
||||
lSchemaProxy := leftDoc.Paths.Value.FindPath("/chicken/soup").Value.Get.
|
||||
Value.Responses.Value.FindResponseByCode("200").Value.
|
||||
FindContent("application/json").Value.Schema
|
||||
lSchemaProxy := leftDoc.Components.Value.FindSchema("OK").Value
|
||||
rSchemaProxy := rightDoc.Components.Value.FindSchema("OK").Value
|
||||
|
||||
// extract right reference schema and non reference schema.
|
||||
rSchemaProxy := rightDoc.Paths.Value.FindPath("/chicken/soup").Value.Get.
|
||||
Value.Responses.Value.FindResponseByCode("200").Value.
|
||||
FindContent("application/json").Value.Schema
|
||||
changes := CompareSchemas(lSchemaProxy, rSchemaProxy)
|
||||
assert.Nil(t, changes)
|
||||
}
|
||||
|
||||
changes := CompareSchemas(lSchemaProxy.Value, rSchemaProxy.Value)
|
||||
func TestCompareSchemas_RefChanged(t *testing.T) {
|
||||
left := `components:
|
||||
schemas:
|
||||
Yo:
|
||||
type: int
|
||||
OK:
|
||||
$ref: '#/components/schemas/No'`
|
||||
|
||||
right := `components:
|
||||
schemas:
|
||||
Yo:
|
||||
type: int
|
||||
OK:
|
||||
$ref: '#/components/schemas/Woah'`
|
||||
|
||||
leftDoc, rightDoc := test_BuildDoc(left, right)
|
||||
|
||||
// extract left reference schema and non reference schema.
|
||||
lSchemaProxy := leftDoc.Components.Value.FindSchema("OK").Value
|
||||
rSchemaProxy := rightDoc.Components.Value.FindSchema("OK").Value
|
||||
|
||||
changes := CompareSchemas(lSchemaProxy, rSchemaProxy)
|
||||
assert.NotNil(t, changes)
|
||||
assert.Len(t, changes.Changes, 1)
|
||||
assert.Equal(t, Modified, changes.Changes[0].ChangeType)
|
||||
assert.Equal(t, "#/components/schemas/Woah", changes.Changes[0].New)
|
||||
}
|
||||
|
||||
func TestCompareSchemas_RefToInline(t *testing.T) {
|
||||
left := `components:
|
||||
schemas:
|
||||
Yo:
|
||||
type: int
|
||||
OK:
|
||||
$ref: '#/components/schemas/No'`
|
||||
|
||||
right := `components:
|
||||
schemas:
|
||||
Yo:
|
||||
type: int
|
||||
OK:
|
||||
type: string`
|
||||
|
||||
leftDoc, rightDoc := test_BuildDoc(left, right)
|
||||
|
||||
// extract left reference schema and non reference schema.
|
||||
lSchemaProxy := leftDoc.Components.Value.FindSchema("OK").Value
|
||||
rSchemaProxy := rightDoc.Components.Value.FindSchema("OK").Value
|
||||
|
||||
changes := CompareSchemas(lSchemaProxy, rSchemaProxy)
|
||||
assert.NotNil(t, changes)
|
||||
assert.Len(t, changes.Changes, 1)
|
||||
assert.Equal(t, Modified, changes.Changes[0].ChangeType)
|
||||
assert.Equal(t, v3.RefLabel, changes.Changes[0].Property)
|
||||
assert.Equal(t, "#/components/schemas/No", changes.Changes[0].Original)
|
||||
|
||||
}
|
||||
|
||||
func TestCompareSchemas_InlineToRef(t *testing.T) {
|
||||
left := `components:
|
||||
schemas:
|
||||
Yo:
|
||||
type: int
|
||||
OK:
|
||||
$ref: '#/components/schemas/No'`
|
||||
|
||||
right := `components:
|
||||
schemas:
|
||||
Yo:
|
||||
type: int
|
||||
OK:
|
||||
type: string`
|
||||
|
||||
leftDoc, rightDoc := test_BuildDoc(left, right)
|
||||
|
||||
// extract left reference schema and non reference schema.
|
||||
lSchemaProxy := leftDoc.Components.Value.FindSchema("OK").Value
|
||||
rSchemaProxy := rightDoc.Components.Value.FindSchema("OK").Value
|
||||
|
||||
changes := CompareSchemas(rSchemaProxy, lSchemaProxy)
|
||||
assert.NotNil(t, changes)
|
||||
assert.Len(t, changes.Changes, 1)
|
||||
assert.Equal(t, Modified, changes.Changes[0].ChangeType)
|
||||
assert.Equal(t, v3.RefLabel, changes.Changes[0].Property)
|
||||
assert.Equal(t, "#/components/schemas/No", changes.Changes[0].New)
|
||||
|
||||
}
|
||||
|
||||
func TestCompareSchemas_Identical(t *testing.T) {
|
||||
left := `components:
|
||||
schemas:
|
||||
Yo:
|
||||
type: int
|
||||
OK:
|
||||
type: string`
|
||||
|
||||
right := `components:
|
||||
schemas:
|
||||
Yo:
|
||||
type: int
|
||||
OK:
|
||||
type: string`
|
||||
|
||||
leftDoc, rightDoc := test_BuildDoc(left, right)
|
||||
|
||||
// extract left reference schema and non reference schema.
|
||||
lSchemaProxy := leftDoc.Components.Value.FindSchema("OK").Value
|
||||
rSchemaProxy := rightDoc.Components.Value.FindSchema("OK").Value
|
||||
|
||||
changes := CompareSchemas(rSchemaProxy, lSchemaProxy)
|
||||
assert.Nil(t, changes)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user