mirror of
https://github.com/LukeHagar/libopenapi.git
synced 2025-12-09 12:37:49 +00:00
614 lines
15 KiB
Go
614 lines
15 KiB
Go
// Copyright 2022 Princess B33f Heavy Industries / Dave Shanley
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package model
|
|
|
|
import (
|
|
"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
|
|
host: https://pb33f.io
|
|
basePath: /api
|
|
schemes:
|
|
- http
|
|
- https
|
|
consumes:
|
|
- application/json
|
|
- apple/pie
|
|
produces:
|
|
- application/json
|
|
- fat/belly`
|
|
|
|
right := left
|
|
|
|
var lNode, rNode yaml.Node
|
|
_ = yaml.Unmarshal([]byte(left), &lNode)
|
|
_ = yaml.Unmarshal([]byte(right), &rNode)
|
|
|
|
// create low level objects
|
|
var lDoc v2.Swagger
|
|
var rDoc v2.Swagger
|
|
_ = low.BuildModel(lNode.Content[0], &lDoc)
|
|
_ = low.BuildModel(rNode.Content[0], &rDoc)
|
|
|
|
// compare.
|
|
extChanges := CompareDocuments(&lDoc, &rDoc)
|
|
assert.Nil(t, extChanges)
|
|
}
|
|
|
|
func TestCompareDocuments_Swagger_BaseProperties_Modified(t *testing.T) {
|
|
left := `swagger: 2.0
|
|
host: https://pb33f.io
|
|
basePath: /api
|
|
schemes:
|
|
- http
|
|
- https
|
|
consumes:
|
|
- application/json
|
|
- apple/pie
|
|
produces:
|
|
- application/json
|
|
- fat/belly`
|
|
|
|
right := `swagger: 2.2
|
|
host: https://quobix.com
|
|
basePath: /new-api
|
|
schemes:
|
|
- ws
|
|
- https
|
|
consumes:
|
|
- application/json
|
|
- apple/ice-cream
|
|
produces:
|
|
- application/json
|
|
- very-fat/belly`
|
|
|
|
var lNode, rNode yaml.Node
|
|
_ = yaml.Unmarshal([]byte(left), &lNode)
|
|
_ = yaml.Unmarshal([]byte(right), &rNode)
|
|
|
|
// create low level objects
|
|
var lDoc v2.Swagger
|
|
var rDoc v2.Swagger
|
|
_ = low.BuildModel(lNode.Content[0], &lDoc)
|
|
_ = low.BuildModel(rNode.Content[0], &rDoc)
|
|
|
|
// compare.
|
|
extChanges := CompareDocuments(&lDoc, &rDoc)
|
|
assert.Equal(t, 9, extChanges.TotalChanges())
|
|
assert.Equal(t, 6, extChanges.TotalBreakingChanges())
|
|
}
|
|
|
|
func TestCompareDocuments_Swagger_Info_Modified(t *testing.T) {
|
|
left := `swagger: 2.0
|
|
info:
|
|
title: a doc
|
|
contact:
|
|
name: buckaroo
|
|
license:
|
|
url: https://pb33f.io`
|
|
|
|
right := `swagger: 2.0
|
|
info:
|
|
title: a doc that changed
|
|
contact:
|
|
name: chief buckaroo
|
|
license:
|
|
url: https://pb33f.io/license`
|
|
|
|
// 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())
|
|
assert.Equal(t, 3, extChanges.InfoChanges.TotalChanges())
|
|
}
|
|
|
|
func TestCompareDocuments_Swagger_Info_Added(t *testing.T) {
|
|
left := `swagger: 2.0`
|
|
|
|
right := `swagger: 2.0
|
|
info:
|
|
title: a doc that changed
|
|
contact:
|
|
name: chief buckaroo
|
|
license:
|
|
url: https://pb33f.io/license`
|
|
|
|
// 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, 1, extChanges.TotalChanges())
|
|
assert.Equal(t, 0, extChanges.TotalBreakingChanges())
|
|
assert.Equal(t, v3.InfoLabel, extChanges.Changes[0].Property)
|
|
assert.Equal(t, PropertyAdded, extChanges.Changes[0].ChangeType)
|
|
}
|
|
|
|
func TestCompareDocuments_Swagger_Info_Removed(t *testing.T) {
|
|
left := `swagger: 2.0`
|
|
|
|
right := `swagger: 2.0
|
|
info:
|
|
title: a doc that changed
|
|
contact:
|
|
name: chief buckaroo
|
|
license:
|
|
url: https://pb33f.io/license`
|
|
|
|
// 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(rDoc, lDoc)
|
|
assert.Equal(t, 1, extChanges.TotalChanges())
|
|
assert.Equal(t, 0, extChanges.TotalBreakingChanges())
|
|
assert.Equal(t, v3.InfoLabel, extChanges.Changes[0].Property)
|
|
assert.Equal(t, PropertyRemoved, extChanges.Changes[0].ChangeType)
|
|
}
|
|
|
|
func TestCompareDocuments_Swagger_ExternalDocs_Modified(t *testing.T) {
|
|
left := `swagger: 2.0
|
|
externalDocs:
|
|
url: https://pb33f.io
|
|
description: the ranch`
|
|
|
|
right := `swagger: 2.0
|
|
externalDocs:
|
|
url: https://pb33f.io/new
|
|
description: the bunkhouse`
|
|
|
|
// 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, 2, extChanges.TotalChanges())
|
|
assert.Equal(t, 0, extChanges.TotalBreakingChanges())
|
|
assert.Equal(t, 2, extChanges.ExternalDocChanges.TotalChanges())
|
|
}
|
|
|
|
func TestCompareDocuments_Swagger_ExternalDocs_Added(t *testing.T) {
|
|
left := `swagger: 2.0`
|
|
|
|
right := `swagger: 2.0
|
|
externalDocs:
|
|
url: https://pb33f.io
|
|
description: the ranch`
|
|
|
|
// 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, 1, extChanges.TotalChanges())
|
|
assert.Equal(t, 0, extChanges.TotalBreakingChanges())
|
|
assert.Equal(t, v3.ExternalDocsLabel, extChanges.Changes[0].Property)
|
|
assert.Equal(t, PropertyAdded, extChanges.Changes[0].ChangeType)
|
|
}
|
|
|
|
func TestCompareDocuments_Swagger_ExternalDocs_Removed(t *testing.T) {
|
|
left := `swagger: 2.0`
|
|
|
|
right := `swagger: 2.0
|
|
externalDocs:
|
|
url: https://pb33f.io
|
|
description: the ranch`
|
|
|
|
// 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(rDoc, lDoc)
|
|
assert.Equal(t, 1, extChanges.TotalChanges())
|
|
assert.Equal(t, 0, extChanges.TotalBreakingChanges())
|
|
assert.Equal(t, v3.ExternalDocsLabel, extChanges.Changes[0].Property)
|
|
assert.Equal(t, PropertyRemoved, extChanges.Changes[0].ChangeType)
|
|
}
|
|
|
|
func TestCompareDocuments_Swagger_Security_Identical(t *testing.T) {
|
|
left := `swagger: 2.0
|
|
security:
|
|
- nice:
|
|
- rice
|
|
- spice
|
|
- fries
|
|
- bad:
|
|
- glad
|
|
- sad
|
|
- bag`
|
|
|
|
right := `swagger: 2.0
|
|
security:
|
|
- bad:
|
|
- sad
|
|
- bag
|
|
- glad
|
|
- nice:
|
|
- spice
|
|
- rice
|
|
- fries`
|
|
|
|
// 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.Nil(t, extChanges)
|
|
}
|
|
|
|
func TestCompareDocuments_Swagger_Security_Changed(t *testing.T) {
|
|
left := `swagger: 2.0
|
|
security:
|
|
- nice:
|
|
- rice
|
|
- spice
|
|
- fries
|
|
- bad:
|
|
- glad
|
|
- sad
|
|
- bag`
|
|
|
|
right := `swagger: 2.0
|
|
security:
|
|
- bad:
|
|
- sad
|
|
- bag
|
|
- odd
|
|
- nice:
|
|
- spice
|
|
- lego
|
|
- fries`
|
|
|
|
// 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, 4, extChanges.TotalChanges())
|
|
assert.Equal(t, 2, extChanges.TotalBreakingChanges())
|
|
}
|
|
|
|
func TestCompareDocuments_Swagger_Components_Schemas(t *testing.T) {
|
|
left := `swagger: 2.0
|
|
definitions:
|
|
burgers:
|
|
type: int
|
|
description: num burgers
|
|
cakes:
|
|
type: string
|
|
description: your favorite cake`
|
|
|
|
right := `swagger: 2.0
|
|
definitions:
|
|
burgers:
|
|
type: bool
|
|
description: allow burgers?
|
|
cakes:
|
|
type: int
|
|
description: how many cakes?`
|
|
|
|
// 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, 4, extChanges.TotalChanges())
|
|
assert.Equal(t, 2, extChanges.TotalBreakingChanges())
|
|
}
|
|
|
|
func TestCompareDocuments_Swagger_Components_SecurityDefinitions_Identical(t *testing.T) {
|
|
left := `swagger: 2.0
|
|
securityDefinitions:
|
|
letMeIn:
|
|
type: password
|
|
description: a password that lets you in
|
|
letMeOut:
|
|
type: 2fa
|
|
description: are you a robot?`
|
|
|
|
right := `swagger: 2.0
|
|
securityDefinitions:
|
|
letMeIn:
|
|
type: password
|
|
description: a password that lets you in
|
|
letMeOut:
|
|
type: 2fa
|
|
description: are you a robot?`
|
|
|
|
// 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.Nil(t, extChanges)
|
|
}
|
|
|
|
func TestCompareDocuments_Swagger_Components_SecurityDefinitions_Changed(t *testing.T) {
|
|
left := `swagger: 2.0
|
|
securityDefinitions:
|
|
letMeIn:
|
|
type: password
|
|
description: a password that lets you in
|
|
letMeOut:
|
|
type: 2fa
|
|
description: are you a robot?`
|
|
|
|
right := `swagger: 2.0
|
|
securityDefinitions:
|
|
letMeIn:
|
|
type: secret
|
|
description: what is a secret?
|
|
letMeOut:
|
|
type: 2fa
|
|
description: are you a robot?`
|
|
|
|
// 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, 2, extChanges.TotalChanges())
|
|
assert.Equal(t, 1, extChanges.TotalBreakingChanges())
|
|
}
|
|
|
|
func TestCompareDocuments_Swagger_Components_Parameters_Identical(t *testing.T) {
|
|
left := `swagger: 2.0
|
|
parameters:
|
|
letMeIn:
|
|
name: letsGo
|
|
type: int
|
|
description: lets go my friends.
|
|
letMeOut:
|
|
name: whyNow
|
|
type: string
|
|
description: why?`
|
|
|
|
right := `swagger: 2.0
|
|
parameters:
|
|
letMeIn:
|
|
name: letsGo
|
|
type: int
|
|
description: lets go my friends.
|
|
letMeOut:
|
|
name: whyNow
|
|
type: string
|
|
description: why?`
|
|
|
|
// 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.Nil(t, extChanges)
|
|
}
|
|
|
|
func TestCompareDocuments_Swagger_Components_Parameters_Changed(t *testing.T) {
|
|
left := `swagger: 2.0
|
|
parameters:
|
|
letMeIn:
|
|
name: letsGo
|
|
type: int
|
|
description: lets go my friends. now.
|
|
letMeOut:
|
|
name: whyNow
|
|
type: string
|
|
description: why?`
|
|
|
|
right := `swagger: 2.0
|
|
parameters:
|
|
letMeIn:
|
|
name: letsGoNow
|
|
type: string
|
|
description: lets go my friends.
|
|
letMeOut:
|
|
name: whyNowPlease
|
|
type: int
|
|
description: why should we do it?`
|
|
|
|
// 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, 6, extChanges.TotalChanges())
|
|
assert.Equal(t, 4, extChanges.TotalBreakingChanges())
|
|
}
|
|
|
|
func TestCompareDocuments_Swagger_Components_Responses_Identical(t *testing.T) {
|
|
left := `swagger: 2.0
|
|
responses:
|
|
tacos:
|
|
description: who wants a taco?
|
|
schema:
|
|
type: int
|
|
pizza:
|
|
description: who wants a pizza?
|
|
schema:
|
|
type: string`
|
|
|
|
right := `swagger: 2.0
|
|
responses:
|
|
tacos:
|
|
description: who wants a taco?
|
|
schema:
|
|
type: int
|
|
pizza:
|
|
description: who wants a pizza?
|
|
schema:
|
|
type: string`
|
|
|
|
// 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.Nil(t, extChanges)
|
|
}
|
|
|
|
func TestCompareDocuments_Swagger_Components_Responses_Modified(t *testing.T) {
|
|
left := `swagger: 2.0
|
|
responses:
|
|
tacos:
|
|
description: who wants a taco?
|
|
schema:
|
|
type: int
|
|
pizza:
|
|
description: who wants a pizza?
|
|
schema:
|
|
type: string`
|
|
|
|
right := `swagger: 2.0
|
|
responses:
|
|
tacos:
|
|
description: I want a taco
|
|
schema:
|
|
type: bool
|
|
pizza:
|
|
description: I need a pizza
|
|
schema:
|
|
type: int`
|
|
|
|
// 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, 4, extChanges.TotalChanges())
|
|
assert.Equal(t, 2, extChanges.TotalBreakingChanges())
|
|
}
|
|
|
|
func TestCompareDocuments_Swagger_Components_Paths_Identical(t *testing.T) {
|
|
left := `swagger: 2.0
|
|
paths:
|
|
/nice/rice:
|
|
get:
|
|
description: nice rice?
|
|
/lovely/horse:
|
|
post:
|
|
description: what a lovely horse.`
|
|
|
|
right := `swagger: 2.0
|
|
paths:
|
|
/lovely/horse:
|
|
post:
|
|
description: what a lovely horse.
|
|
/nice/rice:
|
|
get:
|
|
description: nice rice?`
|
|
|
|
// 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.Nil(t, extChanges)
|
|
}
|
|
|
|
//func TestCompareDocuments_Swagger_Components_Paths_Changed(t *testing.T) {
|
|
// left := `swagger: 2.0
|
|
//paths:
|
|
// /nice/rice:
|
|
// get:
|
|
// description: wow!
|
|
// /lovely/horse:
|
|
// put:
|
|
// description: what a lovely horse.`
|
|
//
|
|
// right := `swagger: 2.0
|
|
//paths:
|
|
// /lovely/horse:
|
|
// post:
|
|
// description: what a lovely horse.
|
|
// /nice/rice:
|
|
// get:
|
|
// description: nice rice?`
|
|
//
|
|
// // 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, 1, extChanges.TotalBreakingChanges())
|
|
//}
|