mirror of
https://github.com/LukeHagar/libopenapi.git
synced 2025-12-09 12:37:49 +00:00
Added example object to what-changed feature.
Ready to tackle the main event now. The Schema! Signed-off-by: Dave Shanley <dave@quobix.com>
This commit is contained in:
88
what-changed/example.go
Normal file
88
what-changed/example.go
Normal file
@@ -0,0 +1,88 @@
|
||||
// Copyright 2022 Princess B33f Heavy Industries / Dave Shanley
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
package what_changed
|
||||
|
||||
import (
|
||||
"github.com/pb33f/libopenapi/datamodel/low/base"
|
||||
v3 "github.com/pb33f/libopenapi/datamodel/low/v3"
|
||||
)
|
||||
|
||||
// ExampleChanges represent changes to an Example object, part of an OpenAPI specification.
|
||||
type ExampleChanges struct {
|
||||
PropertyChanges[*base.Example]
|
||||
ExtensionChanges *ExtensionChanges
|
||||
}
|
||||
|
||||
// TotalChanges returns the total number of changes made to Example
|
||||
func (e *ExampleChanges) TotalChanges() int {
|
||||
l := e.PropertyChanges.TotalChanges()
|
||||
if e.ExtensionChanges != nil {
|
||||
l += e.ExtensionChanges.PropertyChanges.TotalChanges()
|
||||
}
|
||||
return l
|
||||
}
|
||||
|
||||
// TotalChanges
|
||||
|
||||
func CompareExamples(l, r *base.Example) *ExampleChanges {
|
||||
|
||||
ec := new(ExampleChanges)
|
||||
var changes []*Change[*base.Example]
|
||||
var props []*PropertyCheck[*base.Example]
|
||||
|
||||
// Summary
|
||||
props = append(props, &PropertyCheck[*base.Example]{
|
||||
LeftNode: l.Summary.ValueNode,
|
||||
RightNode: r.Summary.ValueNode,
|
||||
Label: v3.SummaryLabel,
|
||||
Changes: &changes,
|
||||
Breaking: false,
|
||||
Original: l,
|
||||
New: r,
|
||||
})
|
||||
|
||||
// Description
|
||||
props = append(props, &PropertyCheck[*base.Example]{
|
||||
LeftNode: l.Description.ValueNode,
|
||||
RightNode: r.Description.ValueNode,
|
||||
Label: v3.DescriptionLabel,
|
||||
Changes: &changes,
|
||||
Breaking: false,
|
||||
Original: l,
|
||||
New: r,
|
||||
})
|
||||
|
||||
// Value
|
||||
props = append(props, &PropertyCheck[*base.Example]{
|
||||
LeftNode: l.Value.ValueNode,
|
||||
RightNode: r.Value.ValueNode,
|
||||
Label: v3.ValueLabel,
|
||||
Changes: &changes,
|
||||
Breaking: false,
|
||||
Original: l,
|
||||
New: r,
|
||||
})
|
||||
|
||||
// ExternalValue
|
||||
props = append(props, &PropertyCheck[*base.Example]{
|
||||
LeftNode: l.ExternalValue.ValueNode,
|
||||
RightNode: r.ExternalValue.ValueNode,
|
||||
Label: v3.ExternalValue,
|
||||
Changes: &changes,
|
||||
Breaking: false,
|
||||
Original: l,
|
||||
New: r,
|
||||
})
|
||||
|
||||
// check properties
|
||||
CheckProperties(props)
|
||||
|
||||
// check extensions
|
||||
ec.ExtensionChanges = CheckExtensions(l, r)
|
||||
ec.Changes = changes
|
||||
if ec.TotalChanges() <= 0 {
|
||||
return nil
|
||||
}
|
||||
return ec
|
||||
}
|
||||
112
what-changed/example_test.go
Normal file
112
what-changed/example_test.go
Normal file
@@ -0,0 +1,112 @@
|
||||
// Copyright 2022 Princess B33f Heavy Industries / Dave Shanley
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
package what_changed
|
||||
|
||||
import (
|
||||
"github.com/pb33f/libopenapi/datamodel/low"
|
||||
"github.com/pb33f/libopenapi/datamodel/low/base"
|
||||
v3 "github.com/pb33f/libopenapi/datamodel/low/v3"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"gopkg.in/yaml.v3"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestCompareExamples_SummaryModified(t *testing.T) {
|
||||
|
||||
left := `summary: magic herbs`
|
||||
right := `summary: cure all`
|
||||
|
||||
var lNode, rNode yaml.Node
|
||||
_ = yaml.Unmarshal([]byte(left), &lNode)
|
||||
_ = yaml.Unmarshal([]byte(right), &rNode)
|
||||
|
||||
// create low level objects
|
||||
var lDoc base.Example
|
||||
var rDoc base.Example
|
||||
_ = low.BuildModel(&lNode, &lDoc)
|
||||
_ = low.BuildModel(&rNode, &rDoc)
|
||||
_ = lDoc.Build(lNode.Content[0], nil)
|
||||
_ = rDoc.Build(rNode.Content[0], nil)
|
||||
|
||||
extChanges := CompareExamples(&lDoc, &rDoc)
|
||||
|
||||
assert.Equal(t, extChanges.TotalChanges(), 1)
|
||||
assert.Equal(t, Modified, extChanges.Changes[0].ChangeType)
|
||||
assert.Equal(t, v3.SummaryLabel, extChanges.Changes[0].Property)
|
||||
assert.Equal(t, "magic herbs", extChanges.Changes[0].Original)
|
||||
assert.Equal(t, "cure all", extChanges.Changes[0].New)
|
||||
}
|
||||
|
||||
func TestCompareExamples_SummaryAdded(t *testing.T) {
|
||||
|
||||
left := `summary: magic herbs`
|
||||
right := `summary: magic herbs
|
||||
description: cure all`
|
||||
|
||||
var lNode, rNode yaml.Node
|
||||
_ = yaml.Unmarshal([]byte(left), &lNode)
|
||||
_ = yaml.Unmarshal([]byte(right), &rNode)
|
||||
|
||||
// create low level objects
|
||||
var lDoc base.Example
|
||||
var rDoc base.Example
|
||||
_ = low.BuildModel(&lNode, &lDoc)
|
||||
_ = low.BuildModel(&rNode, &rDoc)
|
||||
_ = lDoc.Build(lNode.Content[0], nil)
|
||||
_ = rDoc.Build(rNode.Content[0], nil)
|
||||
|
||||
extChanges := CompareExamples(&lDoc, &rDoc)
|
||||
|
||||
assert.Equal(t, extChanges.TotalChanges(), 1)
|
||||
assert.Equal(t, PropertyAdded, extChanges.Changes[0].ChangeType)
|
||||
assert.Equal(t, v3.DescriptionLabel, extChanges.Changes[0].Property)
|
||||
assert.Equal(t, "cure all", extChanges.Changes[0].New)
|
||||
}
|
||||
|
||||
func TestCompareExamples_ExtensionAdded(t *testing.T) {
|
||||
|
||||
left := `summary: magic herbs`
|
||||
right := `summary: magic herbs
|
||||
x-herbs: cure all`
|
||||
|
||||
var lNode, rNode yaml.Node
|
||||
_ = yaml.Unmarshal([]byte(left), &lNode)
|
||||
_ = yaml.Unmarshal([]byte(right), &rNode)
|
||||
|
||||
// create low level objects
|
||||
var lDoc base.Example
|
||||
var rDoc base.Example
|
||||
_ = low.BuildModel(&lNode, &lDoc)
|
||||
_ = low.BuildModel(&rNode, &rDoc)
|
||||
_ = lDoc.Build(lNode.Content[0], nil)
|
||||
_ = rDoc.Build(rNode.Content[0], nil)
|
||||
|
||||
extChanges := CompareExamples(&lDoc, &rDoc)
|
||||
|
||||
assert.Equal(t, extChanges.TotalChanges(), 1)
|
||||
assert.Equal(t, ObjectAdded, extChanges.ExtensionChanges.Changes[0].ChangeType)
|
||||
assert.Equal(t, "x-herbs", extChanges.ExtensionChanges.Changes[0].Property)
|
||||
assert.Equal(t, "cure all", extChanges.ExtensionChanges.Changes[0].New)
|
||||
}
|
||||
|
||||
func TestCompareExamples_Identical(t *testing.T) {
|
||||
|
||||
left := `summary: magic herbs`
|
||||
right := `summary: magic herbs`
|
||||
|
||||
var lNode, rNode yaml.Node
|
||||
_ = yaml.Unmarshal([]byte(left), &lNode)
|
||||
_ = yaml.Unmarshal([]byte(right), &rNode)
|
||||
|
||||
// create low level objects
|
||||
var lDoc base.Example
|
||||
var rDoc base.Example
|
||||
_ = low.BuildModel(&lNode, &lDoc)
|
||||
_ = low.BuildModel(&rNode, &rDoc)
|
||||
_ = lDoc.Build(lNode.Content[0], nil)
|
||||
_ = rDoc.Build(rNode.Content[0], nil)
|
||||
|
||||
extChanges := CompareExamples(&lDoc, &rDoc)
|
||||
assert.Nil(t, extChanges)
|
||||
}
|
||||
Reference in New Issue
Block a user