mirror of
https://github.com/LukeHagar/libopenapi.git
synced 2025-12-07 04:20:14 +00:00
This is a large update, I realized that extensions are not being hashed correctly, and because I have the same code everywhere, it means running back through the stack and cleaning up the invalid code that will break if multiple extensions are used in different positions in the raw spec. At the same time, I realized that the v2 model has the same primitive/enum issues that are part cleaned up in v3. This is a breaking changhe because enums are now []any and not []string, as well as primitives for bool, int etc are all pointers now instead of the copied values. This will break any consumers.
68 lines
1.9 KiB
Go
68 lines
1.9 KiB
Go
package base
|
|
|
|
import (
|
|
"crypto/sha256"
|
|
"fmt"
|
|
"github.com/pb33f/libopenapi/datamodel/low"
|
|
"github.com/pb33f/libopenapi/index"
|
|
"gopkg.in/yaml.v3"
|
|
"sort"
|
|
"strings"
|
|
)
|
|
|
|
// XML represents a low-level representation of an XML object defined by all versions of OpenAPI.
|
|
//
|
|
// A metadata object that allows for more fine-tuned XML model definitions.
|
|
//
|
|
// When using arrays, XML element names are not inferred (for singular/plural forms) and the name property SHOULD be
|
|
// used to add that information. See examples for expected behavior.
|
|
// v2 - https://swagger.io/specification/v2/#xmlObject
|
|
// v3 - https://swagger.io/specification/#xml-object
|
|
type XML struct {
|
|
Name low.NodeReference[string]
|
|
Namespace low.NodeReference[string]
|
|
Prefix low.NodeReference[string]
|
|
Attribute low.NodeReference[bool]
|
|
Wrapped low.NodeReference[bool]
|
|
Extensions map[low.KeyReference[string]]low.ValueReference[any]
|
|
}
|
|
|
|
// Build will extract extensions from the XML instance.
|
|
func (x *XML) Build(root *yaml.Node, _ *index.SpecIndex) error {
|
|
x.Extensions = low.ExtractExtensions(root)
|
|
return nil
|
|
}
|
|
|
|
func (x *XML) GetExtensions() map[low.KeyReference[string]]low.ValueReference[any] {
|
|
return x.Extensions
|
|
}
|
|
|
|
// Hash generates a SHA256 hash of the XML object using properties
|
|
func (x *XML) Hash() [32]byte {
|
|
var f []string
|
|
if !x.Name.IsEmpty() {
|
|
f = append(f, x.Name.Value)
|
|
}
|
|
if !x.Namespace.IsEmpty() {
|
|
f = append(f, x.Namespace.Value)
|
|
}
|
|
if !x.Prefix.IsEmpty() {
|
|
f = append(f, x.Prefix.Value)
|
|
}
|
|
if !x.Attribute.IsEmpty() {
|
|
f = append(f, fmt.Sprint(x.Attribute.Value))
|
|
}
|
|
if !x.Wrapped.IsEmpty() {
|
|
f = append(f, fmt.Sprint(x.Wrapped.Value))
|
|
}
|
|
keys := make([]string, len(x.Extensions))
|
|
z := 0
|
|
for k := range x.Extensions {
|
|
keys[z] = fmt.Sprintf("%s-%x", k.Value, sha256.Sum256([]byte(fmt.Sprint(x.Extensions[k].Value))))
|
|
z++
|
|
}
|
|
sort.Strings(keys)
|
|
f = append(f, keys...)
|
|
return sha256.Sum256([]byte(strings.Join(f, "|")))
|
|
}
|