mirror of
https://github.com/LukeHagar/libopenapi.git
synced 2025-12-09 04:20:17 +00:00
The v3 model is wrong and out of sync with the spec. It's been corrected, so the v2 and v2 model for SecurityRequirement have been collapsed down into a base model., they are the same data structures. This has allowed me to delete the complexity of sharing two different models for the same structure, by unifying the model correctly. I am not sure why I decided to change the v3 model, oh well, its been corrected. Long live swagger!
140 lines
3.7 KiB
Go
140 lines
3.7 KiB
Go
// Copyright 2022 Princess B33f Heavy Industries / Dave Shanley
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package model
|
|
|
|
import (
|
|
"github.com/pb33f/libopenapi/datamodel/low"
|
|
"github.com/pb33f/libopenapi/datamodel/low/base"
|
|
"github.com/pb33f/libopenapi/datamodel/low/v3"
|
|
"gopkg.in/yaml.v3"
|
|
)
|
|
|
|
type SecurityRequirementChanges struct {
|
|
PropertyChanges
|
|
}
|
|
|
|
func (s *SecurityRequirementChanges) TotalChanges() int {
|
|
return s.PropertyChanges.TotalChanges()
|
|
}
|
|
|
|
func (s *SecurityRequirementChanges) TotalBreakingChanges() int {
|
|
return s.PropertyChanges.TotalBreakingChanges()
|
|
}
|
|
|
|
func removedSecurityRequirement(vn *yaml.Node, name string, changes *[]*Change) {
|
|
CreateChange(changes, ObjectRemoved, v3.SecurityLabel,
|
|
vn, nil, true, name, nil)
|
|
}
|
|
|
|
func addedSecurityRequirement(vn *yaml.Node, name string, changes *[]*Change) {
|
|
CreateChange(changes, ObjectAdded, v3.SecurityLabel,
|
|
nil, vn, false, nil, name)
|
|
}
|
|
|
|
func CompareSecurityRequirement(l, r *base.SecurityRequirement) *SecurityRequirementChanges {
|
|
|
|
var changes []*Change
|
|
sc := new(SecurityRequirementChanges)
|
|
|
|
if low.AreEqual(l, r) {
|
|
return nil
|
|
}
|
|
checkSecurityRequirement(l.Requirements.Value, r.Requirements.Value, &changes)
|
|
sc.Changes = changes
|
|
return sc
|
|
}
|
|
|
|
func checkSecurityRequirement(lSec, rSec map[low.KeyReference[string]]low.ValueReference[[]low.ValueReference[string]],
|
|
changes *[]*Change) {
|
|
|
|
lKeys := make([]string, len(lSec))
|
|
rKeys := make([]string, len(rSec))
|
|
lValues := make(map[string]low.ValueReference[[]low.ValueReference[string]])
|
|
rValues := make(map[string]low.ValueReference[[]low.ValueReference[string]])
|
|
var n, z int
|
|
for i := range lSec {
|
|
lKeys[n] = i.Value
|
|
lValues[i.Value] = lSec[i]
|
|
n++
|
|
}
|
|
for i := range rSec {
|
|
rKeys[z] = i.Value
|
|
rValues[i.Value] = rSec[i]
|
|
z++
|
|
}
|
|
|
|
for z = range lKeys {
|
|
if z < len(rKeys) {
|
|
if _, ok := rValues[lKeys[z]]; !ok {
|
|
removedSecurityRequirement(lValues[lKeys[z]].ValueNode, lKeys[z], changes)
|
|
continue
|
|
}
|
|
|
|
lValue := lValues[lKeys[z]].Value
|
|
rValue := rValues[lKeys[z]].Value
|
|
|
|
// check if actual values match up
|
|
lRoleKeys := make([]string, len(lValue))
|
|
rRoleKeys := make([]string, len(rValue))
|
|
lRoleValues := make(map[string]low.ValueReference[string])
|
|
rRoleValues := make(map[string]low.ValueReference[string])
|
|
var t, k int
|
|
for i := range lValue {
|
|
lRoleKeys[t] = lValue[i].Value
|
|
lRoleValues[lValue[i].Value] = lValue[i]
|
|
t++
|
|
}
|
|
for i := range rValue {
|
|
rRoleKeys[k] = rValue[i].Value
|
|
rRoleValues[rValue[i].Value] = rValue[i]
|
|
k++
|
|
}
|
|
|
|
for t = range lRoleKeys {
|
|
if t < len(rRoleKeys) {
|
|
if _, ok := rRoleValues[lRoleKeys[t]]; !ok {
|
|
removedSecurityRequirement(lRoleValues[lRoleKeys[t]].ValueNode, lRoleKeys[t], changes)
|
|
continue
|
|
}
|
|
}
|
|
if t >= len(rRoleKeys) {
|
|
if _, ok := rRoleValues[lRoleKeys[t]]; !ok {
|
|
removedSecurityRequirement(lRoleValues[lRoleKeys[t]].ValueNode, lRoleKeys[t], changes)
|
|
}
|
|
}
|
|
}
|
|
for t = range rRoleKeys {
|
|
if t < len(lRoleKeys) {
|
|
if _, ok := lRoleValues[rRoleKeys[t]]; !ok {
|
|
addedSecurityRequirement(rRoleValues[rRoleKeys[t]].ValueNode, rRoleKeys[t], changes)
|
|
continue
|
|
}
|
|
}
|
|
if t >= len(lRoleKeys) {
|
|
addedSecurityRequirement(rRoleValues[rRoleKeys[t]].ValueNode, rRoleKeys[t], changes)
|
|
}
|
|
}
|
|
|
|
}
|
|
if z >= len(rKeys) {
|
|
if _, ok := rValues[lKeys[z]]; !ok {
|
|
removedSecurityRequirement(lValues[lKeys[z]].ValueNode, lKeys[z], changes)
|
|
}
|
|
}
|
|
}
|
|
for z = range rKeys {
|
|
if z < len(lKeys) {
|
|
if _, ok := lValues[rKeys[z]]; !ok {
|
|
addedSecurityRequirement(rValues[rKeys[z]].ValueNode, rKeys[z], changes)
|
|
continue
|
|
}
|
|
}
|
|
if z >= len(lKeys) {
|
|
if _, ok := lValues[rKeys[z]]; !ok {
|
|
addedSecurityRequirement(rValues[rKeys[z]].ValueNode, rKeys[z], changes)
|
|
}
|
|
}
|
|
}
|
|
}
|