Files
libopenapi/datamodel/low/base/info.go
Dave Shanley 61f99b8fd6 Replacing extensions hash code **breaking change**
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.
2022-11-18 11:00:34 -05:00

84 lines
2.4 KiB
Go

// Copyright 2022 Princess B33f Heavy Industries / Dave Shanley
// SPDX-License-Identifier: MIT
package base
import (
"crypto/sha256"
"fmt"
"github.com/pb33f/libopenapi/datamodel/low"
"github.com/pb33f/libopenapi/index"
"gopkg.in/yaml.v3"
"sort"
"strings"
)
// Info represents a low-level Info object as defined by both OpenAPI 2 and OpenAPI 3.
//
// The object provides metadata about the API. The metadata MAY be used by the clients if needed, and MAY be presented
// in editing or documentation generation tools for convenience.
//
// v2 - https://swagger.io/specification/v2/#infoObject
// v3 - https://spec.openapis.org/oas/v3.1.0#info-object
type Info struct {
Title low.NodeReference[string]
Description low.NodeReference[string]
TermsOfService low.NodeReference[string]
Contact low.NodeReference[*Contact]
License low.NodeReference[*License]
Version low.NodeReference[string]
Extensions map[low.KeyReference[string]]low.ValueReference[any]
}
// FindExtension attempts to locate an extension with the supplied key
func (i *Info) FindExtension(ext string) *low.ValueReference[any] {
return low.FindItemInMap(ext, i.Extensions)
}
// Build will extract out the Contact and Info objects from the supplied root node.
func (i *Info) Build(root *yaml.Node, idx *index.SpecIndex) error {
i.Extensions = low.ExtractExtensions(root)
// extract contact
contact, _ := low.ExtractObject[*Contact](ContactLabel, root, idx)
i.Contact = contact
// extract license
lic, _ := low.ExtractObject[*License](LicenseLabel, root, idx)
i.License = lic
return nil
}
// Hash will return a consistent SHA256 Hash of the Info object
func (i *Info) Hash() [32]byte {
var f []string
if !i.Title.IsEmpty() {
f = append(f, i.Title.Value)
}
if !i.Description.IsEmpty() {
f = append(f, i.Description.Value)
}
if !i.TermsOfService.IsEmpty() {
f = append(f, i.TermsOfService.Value)
}
if !i.Contact.IsEmpty() {
f = append(f, low.GenerateHashString(i.Contact.Value))
}
if !i.License.IsEmpty() {
f = append(f, low.GenerateHashString(i.License.Value))
}
if !i.Version.IsEmpty() {
f = append(f, i.Version.Value)
}
keys := make([]string, len(i.Extensions))
z := 0
for k := range i.Extensions {
keys[z] = fmt.Sprintf("%s-%x", k.Value, sha256.Sum256([]byte(fmt.Sprint(i.Extensions[k].Value))))
z++
}
sort.Strings(keys)
f = append(f, keys...)
return sha256.Sum256([]byte(strings.Join(f, "|")))
}