mirror of
https://github.com/LukeHagar/libopenapi.git
synced 2025-12-07 12:37:48 +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:
@@ -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