mirror of
https://github.com/LukeHagar/libopenapi.git
synced 2025-12-07 20:47:45 +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 (
|
import (
|
||||||
"github.com/pb33f/libopenapi/datamodel/low"
|
"github.com/pb33f/libopenapi/datamodel/low"
|
||||||
"github.com/pb33f/libopenapi/index"
|
"github.com/pb33f/libopenapi/index"
|
||||||
|
"github.com/pb33f/libopenapi/utils"
|
||||||
"gopkg.in/yaml.v3"
|
"gopkg.in/yaml.v3"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -55,6 +56,10 @@ type SchemaProxy struct {
|
|||||||
func (sp *SchemaProxy) Build(root *yaml.Node, idx *index.SpecIndex) error {
|
func (sp *SchemaProxy) Build(root *yaml.Node, idx *index.SpecIndex) error {
|
||||||
sp.vn = root
|
sp.vn = root
|
||||||
sp.idx = idx
|
sp.idx = idx
|
||||||
|
if rf, _, r := utils.IsNodeRefValue(root); rf {
|
||||||
|
sp.isReference = true
|
||||||
|
sp.referenceLookup = r
|
||||||
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ import (
|
|||||||
"github.com/pb33f/libopenapi/datamodel/low/base"
|
"github.com/pb33f/libopenapi/datamodel/low/base"
|
||||||
v3 "github.com/pb33f/libopenapi/datamodel/low/v3"
|
v3 "github.com/pb33f/libopenapi/datamodel/low/v3"
|
||||||
"sort"
|
"sort"
|
||||||
|
"sync"
|
||||||
)
|
)
|
||||||
|
|
||||||
type SchemaChanges struct {
|
type SchemaChanges struct {
|
||||||
@@ -146,24 +147,25 @@ func CompareSchemas(l, r *base.SchemaProxy) *SchemaChanges {
|
|||||||
} else {
|
} else {
|
||||||
// references are different, that's all we care to know.
|
// references are different, that's all we care to know.
|
||||||
CreateChange[*base.Schema](&changes, Modified, v3.RefLabel,
|
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
|
sc.Changes = changes
|
||||||
return sc
|
return sc
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// changed from ref to inline
|
// changed from inline to ref
|
||||||
if !l.IsSchemaReference() && r.IsSchemaReference() {
|
if !l.IsSchemaReference() && r.IsSchemaReference() {
|
||||||
CreateChange[*base.Schema](&changes, Modified, v3.RefLabel,
|
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
|
sc.Changes = changes
|
||||||
return sc // we're done here
|
return sc // we're done here
|
||||||
}
|
}
|
||||||
|
|
||||||
// changed from inline to ref
|
// changed from ref to inline
|
||||||
if l.IsSchemaReference() && !r.IsSchemaReference() {
|
if l.IsSchemaReference() && !r.IsSchemaReference() {
|
||||||
CreateChange[*base.Schema](&changes, Modified, v3.RefLabel,
|
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
|
sc.Changes = changes
|
||||||
return sc // done, nothing else to do.
|
return sc // done, nothing else to do.
|
||||||
}
|
}
|
||||||
@@ -332,17 +334,6 @@ func CompareSchemas(l, r *base.SchemaProxy) *SchemaChanges {
|
|||||||
New: rSchema,
|
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
|
// MaxProperties
|
||||||
props = append(props, &PropertyCheck[*base.Schema]{
|
props = append(props, &PropertyCheck[*base.Schema]{
|
||||||
LeftNode: lSchema.MaxProperties.ValueNode,
|
LeftNode: lSchema.MaxProperties.ValueNode,
|
||||||
@@ -365,54 +356,6 @@ func CompareSchemas(l, r *base.SchemaProxy) *SchemaChanges {
|
|||||||
New: rSchema,
|
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
|
// UniqueItems
|
||||||
props = append(props, &PropertyCheck[*base.Schema]{
|
props = append(props, &PropertyCheck[*base.Schema]{
|
||||||
LeftNode: lSchema.UniqueItems.ValueNode,
|
LeftNode: lSchema.UniqueItems.ValueNode,
|
||||||
@@ -439,7 +382,7 @@ func CompareSchemas(l, r *base.SchemaProxy) *SchemaChanges {
|
|||||||
props = append(props, &PropertyCheck[*base.Schema]{
|
props = append(props, &PropertyCheck[*base.Schema]{
|
||||||
LeftNode: lSchema.Description.ValueNode,
|
LeftNode: lSchema.Description.ValueNode,
|
||||||
RightNode: rSchema.Description.ValueNode,
|
RightNode: rSchema.Description.ValueNode,
|
||||||
Label: v3.MinLengthLabel,
|
Label: v3.DescriptionLabel,
|
||||||
Changes: &changes,
|
Changes: &changes,
|
||||||
Breaking: false,
|
Breaking: false,
|
||||||
Original: lSchema,
|
Original: lSchema,
|
||||||
@@ -534,15 +477,58 @@ func CompareSchemas(l, r *base.SchemaProxy) *SchemaChanges {
|
|||||||
New: rSchema,
|
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
|
// check core properties
|
||||||
CheckProperties(props)
|
CheckProperties(props)
|
||||||
|
|
||||||
//propChanges := make(map[string]*SchemaChanges)
|
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.
|
|
||||||
}
|
|
||||||
|
|
||||||
lProps := make([]string, len(lSchema.Properties.Value))
|
lProps := make([]string, len(lSchema.Properties.Value))
|
||||||
lEntities := make(map[string]*base.SchemaProxy)
|
lEntities := make(map[string]*base.SchemaProxy)
|
||||||
@@ -550,72 +536,112 @@ func CompareSchemas(l, r *base.SchemaProxy) *SchemaChanges {
|
|||||||
rEntities := make(map[string]*base.SchemaProxy)
|
rEntities := make(map[string]*base.SchemaProxy)
|
||||||
|
|
||||||
for w := range lSchema.Properties.Value {
|
for w := range lSchema.Properties.Value {
|
||||||
|
if !lSchema.Properties.Value[w].Value.IsSchemaReference() {
|
||||||
lProps = append(lProps, w.Value)
|
lProps = append(lProps, w.Value)
|
||||||
|
|
||||||
// TODO: check for ref
|
|
||||||
lEntities[w.Value] = lSchema.Properties.Value[w].Value
|
lEntities[w.Value] = lSchema.Properties.Value[w].Value
|
||||||
}
|
}
|
||||||
|
}
|
||||||
for w := range rSchema.Properties.Value {
|
for w := range rSchema.Properties.Value {
|
||||||
// TODO: check for ref.
|
if !rSchema.Properties.Value[w].Value.IsSchemaReference() {
|
||||||
rProps = append(rProps, w.Value)
|
rProps = append(rProps, w.Value)
|
||||||
rEntities[w.Value] = rSchema.Properties.Value[w].Value
|
rEntities[w.Value] = rSchema.Properties.Value[w].Value
|
||||||
}
|
}
|
||||||
|
}
|
||||||
sort.Strings(lProps)
|
sort.Strings(lProps)
|
||||||
sort.Strings(rProps)
|
sort.Strings(rProps)
|
||||||
|
|
||||||
if len(lProps) == len(rProps) {
|
var propLock sync.Mutex
|
||||||
for w := range lProps {
|
checkProperty := func(key string, lp, rp *base.SchemaProxy, propChanges map[string]*SchemaChanges, done chan bool) {
|
||||||
lp := lEntities[lProps[w]]
|
|
||||||
rp := rEntities[rProps[w]]
|
|
||||||
|
|
||||||
if lp != nil && rp != nil {
|
if lp != nil && rp != nil {
|
||||||
ls := lp.Schema()
|
ls := lp.Schema()
|
||||||
rs := rp.Schema()
|
rs := rp.Schema()
|
||||||
if low.AreEqual(ls, rs) {
|
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 {
|
// something removed
|
||||||
// if _, ok := rSchema.Properties.Value[q]; ok {
|
if len(lProps) > len(rProps) {
|
||||||
// // hash the schemas, if they match, skip, if not... then compare.
|
for w := range lProps {
|
||||||
// ls := lSchema.Properties.Value[q].Value.Schema()
|
if w < len(rProps) {
|
||||||
// rs := rSchema.Properties.Value[q].Value.Schema()
|
totalProperties++
|
||||||
// if low.AreEqual(ls, rs) {
|
go checkProperty(lProps[w], lEntities[lProps[w]], rEntities[rProps[w]], propChanges, doneChan)
|
||||||
// continue
|
}
|
||||||
// }
|
if w >= len(rProps) {
|
||||||
// prop
|
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.
|
// check polymorphic and multi-values async for speed.
|
||||||
done := make(chan bool)
|
|
||||||
go extractSchemaChanges(lSchema.OneOf.Value, rSchema.OneOf.Value, v3.OneOfLabel,
|
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,
|
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,
|
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,
|
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,
|
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
|
completedChecks := 0
|
||||||
for completedChecks < totalChecks {
|
for completedChecks < totalChecks {
|
||||||
select {
|
select {
|
||||||
case <-done:
|
case <-doneChan:
|
||||||
completedChecks++
|
completedChecks++
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ package what_changed
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/pb33f/libopenapi/datamodel"
|
"github.com/pb33f/libopenapi/datamodel"
|
||||||
|
"github.com/pb33f/libopenapi/datamodel/low/base"
|
||||||
v3 "github.com/pb33f/libopenapi/datamodel/low/v3"
|
v3 "github.com/pb33f/libopenapi/datamodel/low/v3"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"testing"
|
"testing"
|
||||||
@@ -12,180 +13,276 @@ import (
|
|||||||
|
|
||||||
// These tests require full documents to be tested properly. schemas are perhaps the most complex
|
// 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.
|
// 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
|
right := `components:
|
||||||
// checking, as well as a couple of references, so we can avoid that disaster.
|
schemas:
|
||||||
// in our model, components/definitions will be checked independently for changes
|
OK:
|
||||||
// and references will be checked only for value changes (points to a different reference)
|
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: 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`
|
|
||||||
|
|
||||||
left := `openapi: 3.1.0
|
leftDoc, rightDoc := test_BuildDoc(left, right)
|
||||||
paths:
|
|
||||||
/chicken/nuggets:
|
// extract left reference schema and non reference schema.
|
||||||
get:
|
lSchemaProxy := leftDoc.Components.Value.FindSchema("OK").Value
|
||||||
responses:
|
rSchemaProxy := rightDoc.Components.Value.FindSchema("OK").Value
|
||||||
"200":
|
|
||||||
content:
|
changes := CompareSchemas(lSchemaProxy, rSchemaProxy)
|
||||||
application/json:
|
assert.NotNil(t, changes)
|
||||||
schema:
|
assert.Equal(t, 1, changes.TotalChanges())
|
||||||
$ref: '#/components/schemas/OK'
|
assert.Equal(t, 0, changes.TotalBreakingChanges())
|
||||||
/chicken/soup:
|
assert.Equal(t, Modified, changes.Changes[0].ChangeType)
|
||||||
get:
|
assert.Equal(t, "an OK message Changed", changes.Changes[0].New)
|
||||||
responses:
|
}
|
||||||
"200":
|
|
||||||
content:
|
func TestCompareSchemas_PropertyAdd(t *testing.T) {
|
||||||
application/json:
|
left := `components:
|
||||||
schema:
|
schemas:
|
||||||
title: an OK message
|
OK:
|
||||||
allOf:
|
title: an OK message`
|
||||||
- type: bool
|
|
||||||
oneOf:
|
right := `components:
|
||||||
- type: int
|
|
||||||
- type: string
|
|
||||||
properties:
|
|
||||||
propA:
|
|
||||||
title: a proxy property
|
|
||||||
type: string
|
|
||||||
components:
|
|
||||||
schemas:
|
schemas:
|
||||||
OK:
|
OK:
|
||||||
title: an OK message
|
title: an OK message
|
||||||
allOf:
|
description: a thing`
|
||||||
- type: string
|
|
||||||
properties:
|
|
||||||
propA:
|
|
||||||
title: a proxy property
|
|
||||||
type: string`
|
|
||||||
|
|
||||||
right := `openapi: 3.1.0
|
leftDoc, rightDoc := test_BuildDoc(left, right)
|
||||||
paths:
|
|
||||||
/chicken/nuggets:
|
// extract left reference schema and non reference schema.
|
||||||
get:
|
lSchemaProxy := leftDoc.Components.Value.FindSchema("OK").Value
|
||||||
responses:
|
rSchemaProxy := rightDoc.Components.Value.FindSchema("OK").Value
|
||||||
"200":
|
|
||||||
content:
|
changes := CompareSchemas(lSchemaProxy, rSchemaProxy)
|
||||||
application/json:
|
assert.NotNil(t, changes)
|
||||||
schema:
|
assert.Len(t, changes.Changes, 1)
|
||||||
$ref: '#/components/schemas/OK'
|
assert.Equal(t, PropertyAdded, changes.Changes[0].ChangeType)
|
||||||
/chicken/soup:
|
assert.Equal(t, "a thing", changes.Changes[0].New)
|
||||||
get:
|
assert.Equal(t, v3.DescriptionLabel, changes.Changes[0].Property)
|
||||||
responses:
|
}
|
||||||
"200":
|
|
||||||
content:
|
func TestCompareSchemas_PropertyRemove(t *testing.T) {
|
||||||
application/json:
|
left := `components:
|
||||||
schema:
|
|
||||||
title: an OK message that is different
|
|
||||||
allOf:
|
|
||||||
- type: string
|
|
||||||
oneOf:
|
|
||||||
- type: string
|
|
||||||
properties:
|
|
||||||
propA:
|
|
||||||
title: a proxy property
|
|
||||||
type: string
|
|
||||||
components:
|
|
||||||
schemas:
|
schemas:
|
||||||
NoWay:
|
|
||||||
type: string
|
|
||||||
OK:
|
OK:
|
||||||
title: an OK message that has now changed.
|
title: an OK message
|
||||||
allOf:
|
description: a thing`
|
||||||
- type: string
|
|
||||||
properties:
|
|
||||||
propA:
|
|
||||||
title: a proxy property
|
|
||||||
type: string`
|
|
||||||
|
|
||||||
leftInfo, _ := datamodel.ExtractSpecInfo([]byte(left))
|
right := `components:
|
||||||
rightInfo, _ := datamodel.ExtractSpecInfo([]byte(right))
|
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)
|
leftDoc, _ := v3.CreateDocument(leftInfo)
|
||||||
rightDoc, _ := v3.CreateDocument(rightInfo)
|
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.
|
// extract left reference schema and non reference schema.
|
||||||
lSchemaProxy := leftDoc.Paths.Value.FindPath("/chicken/soup").Value.Get.
|
lSchemaProxy := leftDoc.Components.Value.FindSchema("OK").Value
|
||||||
Value.Responses.Value.FindResponseByCode("200").Value.
|
rSchemaProxy := rightDoc.Components.Value.FindSchema("OK").Value
|
||||||
FindContent("application/json").Value.Schema
|
|
||||||
|
|
||||||
// extract right reference schema and non reference schema.
|
changes := CompareSchemas(lSchemaProxy, rSchemaProxy)
|
||||||
rSchemaProxy := rightDoc.Paths.Value.FindPath("/chicken/soup").Value.Get.
|
assert.Nil(t, changes)
|
||||||
Value.Responses.Value.FindResponseByCode("200").Value.
|
}
|
||||||
FindContent("application/json").Value.Schema
|
|
||||||
|
|
||||||
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.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