Fix left right comparison

This commit is contained in:
Hugo Stijns
2022-12-29 11:40:57 +01:00
committed by Dave Shanley
parent b395518a5e
commit 1e1ec6694a
6 changed files with 145 additions and 31 deletions

View File

@@ -122,17 +122,17 @@ func CompareDocuments(l, r any) *DocumentChanges {
lDoc.BasePath.Value, rDoc.BasePath.Value, &changes, v3.BasePathLabel, true)
// schemes
if len(lDoc.Schemes.Value) > 0 || len(lDoc.Schemes.Value) > 0 {
if len(lDoc.Schemes.Value) > 0 || len(rDoc.Schemes.Value) > 0 {
ExtractStringValueSliceChanges(lDoc.Schemes.Value, rDoc.Schemes.Value,
&changes, v3.SchemesLabel, true)
}
// consumes
if len(lDoc.Consumes.Value) > 0 || len(lDoc.Consumes.Value) > 0 {
if len(lDoc.Consumes.Value) > 0 || len(rDoc.Consumes.Value) > 0 {
ExtractStringValueSliceChanges(lDoc.Consumes.Value, rDoc.Consumes.Value,
&changes, v3.ConsumesLabel, true)
}
// produces
if len(lDoc.Produces.Value) > 0 || len(lDoc.Produces.Value) > 0 {
if len(lDoc.Produces.Value) > 0 || len(rDoc.Produces.Value) > 0 {
ExtractStringValueSliceChanges(lDoc.Produces.Value, rDoc.Produces.Value,
&changes, v3.ProducesLabel, true)
}

View File

@@ -4,16 +4,16 @@
package model
import (
"testing"
"github.com/pb33f/libopenapi/datamodel"
"github.com/pb33f/libopenapi/datamodel/low"
v2 "github.com/pb33f/libopenapi/datamodel/low/v2"
v3 "github.com/pb33f/libopenapi/datamodel/low/v3"
"github.com/stretchr/testify/assert"
"gopkg.in/yaml.v3"
"testing"
)
//
func TestCompareDocuments_Swagger_BaseProperties_Identical(t *testing.T) {
left := `swagger: 2.0
x-diet: tough
@@ -88,6 +88,62 @@ produces:
assert.Equal(t, 6, extChanges.TotalBreakingChanges())
}
func TestCompareDocuments_Swagger_BaseProperties_Added(t *testing.T) {
left := `swagger: 2.0
host: https://pb33f.io
basePath: /api`
right := `swagger: 2.0
host: https://pb33f.io
basePath: /api
schemes:
- https
consumes:
- application/json
produces:
- application/json`
// have to build docs fully to get access to objects
siLeft, _ := datamodel.ExtractSpecInfo([]byte(left))
siRight, _ := datamodel.ExtractSpecInfo([]byte(right))
lDoc, _ := v2.CreateDocument(siLeft)
rDoc, _ := v2.CreateDocument(siRight)
// compare.
extChanges := CompareDocuments(lDoc, rDoc)
assert.Equal(t, 3, extChanges.TotalChanges())
assert.Equal(t, 0, extChanges.TotalBreakingChanges())
}
func TestCompareDocuments_Swagger_BaseProperties_Removed(t *testing.T) {
left := `swagger: 2.0
host: https://pb33f.io
basePath: /api
schemes:
- https
consumes:
- application/json
produces:
- application/json`
right := `swagger: 2.0
host: https://pb33f.io
basePath: /api`
// have to build docs fully to get access to objects
siLeft, _ := datamodel.ExtractSpecInfo([]byte(left))
siRight, _ := datamodel.ExtractSpecInfo([]byte(right))
lDoc, _ := v2.CreateDocument(siLeft)
rDoc, _ := v2.CreateDocument(siRight)
// compare.
extChanges := CompareDocuments(lDoc, rDoc)
assert.Equal(t, 3, extChanges.TotalChanges())
assert.Equal(t, 3, extChanges.TotalBreakingChanges())
}
func TestCompareDocuments_Swagger_Info_Modified(t *testing.T) {
left := `swagger: 2.0
info:

View File

@@ -333,7 +333,7 @@ func CompareOperations(l, r any) *OperationChanges {
}
// security
if !lOperation.Security.IsEmpty() && !lOperation.Security.IsEmpty() {
if !lOperation.Security.IsEmpty() || !rOperation.Security.IsEmpty() {
checkSecurity(lOperation.Security, rOperation.Security, &changes, oc)
}

View File

@@ -4,12 +4,13 @@
package model
import (
"testing"
"github.com/pb33f/libopenapi/datamodel/low"
v2 "github.com/pb33f/libopenapi/datamodel/low/v2"
v3 "github.com/pb33f/libopenapi/datamodel/low/v3"
"github.com/stretchr/testify/assert"
"gopkg.in/yaml.v3"
"testing"
)
func TestCompareOperations_V2(t *testing.T) {
@@ -1310,6 +1311,62 @@ security:
assert.Equal(t, ObjectAdded, extChanges.SecurityRequirementChanges[0].Changes[0].ChangeType)
}
func TestCompareOperations_V3_AddSecurity(t *testing.T) {
left := `operationId: coldSecurity
security: []`
right := `operationId: coldSecurity
security:
- winter:
- cold`
var lNode, rNode yaml.Node
_ = yaml.Unmarshal([]byte(left), &lNode)
_ = yaml.Unmarshal([]byte(right), &rNode)
// create low level objects
var lDoc v3.Operation
var rDoc v3.Operation
_ = 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 := CompareOperations(&lDoc, &rDoc)
assert.Equal(t, 1, extChanges.TotalChanges())
assert.Equal(t, 0, extChanges.TotalBreakingChanges())
assert.Empty(t, extChanges.SecurityRequirementChanges)
}
func TestCompareOperations_V3_RemoveSecurity(t *testing.T) {
left := `operationId: coldSecurity
security:
- winter:
- cold`
right := `operationId: coldSecurity
security: []`
var lNode, rNode yaml.Node
_ = yaml.Unmarshal([]byte(left), &lNode)
_ = yaml.Unmarshal([]byte(right), &rNode)
// create low level objects
var lDoc v3.Operation
var rDoc v3.Operation
_ = 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 := CompareOperations(&lDoc, &rDoc)
assert.Equal(t, 1, extChanges.TotalChanges())
assert.Equal(t, 1, extChanges.TotalBreakingChanges())
assert.Empty(t, extChanges.SecurityRequirementChanges)
}
func TestCompareOperations_V3_ModifyRequestBody(t *testing.T) {
left := `requestBody:

View File

@@ -283,7 +283,7 @@ func compareSwaggerPathItem(lPath, rPath *v2.PathItem, changes *[]*Change, pc *P
}
// parameters
if !lPath.Parameters.IsEmpty() && !lPath.Parameters.IsEmpty() {
if !lPath.Parameters.IsEmpty() && !rPath.Parameters.IsEmpty() {
lParams := lPath.Parameters.Value
rParams := rPath.Parameters.Value
lp, rp := extractV2ParametersIntoInterface(lParams, rParams)

View File

@@ -4,12 +4,13 @@
package model
import (
"testing"
"github.com/pb33f/libopenapi/datamodel/low"
v2 "github.com/pb33f/libopenapi/datamodel/low/v2"
v3 "github.com/pb33f/libopenapi/datamodel/low/v3"
"github.com/stretchr/testify/assert"
"gopkg.in/yaml.v3"
"testing"
)
func TestComparePathItem_V2(t *testing.T) {
@@ -262,9 +263,6 @@ parameters:
func TestComparePathItem_V2_RemoveParametersToPath(t *testing.T) {
left := `get:
description: get me`
right := `get:
description: get me
parameters:
- name: cake
@@ -274,6 +272,9 @@ parameters:
- in: tune
name: melody`
right := `get:
description: get me`
var lNode, rNode yaml.Node
_ = yaml.Unmarshal([]byte(left), &lNode)
_ = yaml.Unmarshal([]byte(right), &rNode)
@@ -287,9 +288,9 @@ parameters:
_ = rDoc.Build(rNode.Content[0], nil)
// compare.
extChanges := ComparePathItems(&rDoc, &lDoc)
assert.Equal(t, 4, extChanges.TotalChanges())
assert.Equal(t, 4, extChanges.TotalBreakingChanges())
extChanges := ComparePathItems(&lDoc, &rDoc)
assert.Equal(t, 1, extChanges.TotalChanges())
assert.Equal(t, 1, extChanges.TotalBreakingChanges())
}