Wiring up responses into what-changed feature

Building tests and hashing functions.
This commit is contained in:
Dave Shanley
2022-10-28 15:43:57 -04:00
parent 5bd0d8de50
commit b06ef4bcc9
8 changed files with 475 additions and 66 deletions

View File

@@ -5,6 +5,7 @@ package v3
import (
"fmt"
"github.com/pb33f/libopenapi/datamodel/high"
low "github.com/pb33f/libopenapi/datamodel/low/v3"
)
@@ -23,18 +24,20 @@ import (
// be the response for a successful operation call.
// - https://spec.openapis.org/oas/v3.1.0#responses-object
type Responses struct {
Codes map[string]*Response
Default *Response
low *low.Responses
Codes map[string]*Response
Default *Response
Extensions map[string]any
low *low.Responses
}
// NewResponses will create a new high-level Responses instance from a low-level one. It operates asynchronously
// internally, as each response may be considerable in complexity.
func NewResponses(response *low.Responses) *Responses {
func NewResponses(responses *low.Responses) *Responses {
r := new(Responses)
r.low = response
if !response.Default.IsEmpty() {
r.Default = NewResponse(response.Default.Value)
r.low = responses
r.Extensions = high.ExtractExtensions(responses.Extensions)
if !responses.Default.IsEmpty() {
r.Default = NewResponse(responses.Default.Value)
}
codes := make(map[string]*Response)
@@ -49,10 +52,10 @@ func NewResponses(response *low.Responses) *Responses {
var buildResponse = func(code string, resp *low.Response, c chan respRes) {
c <- respRes{code: code, resp: NewResponse(resp)}
}
for k, v := range response.Codes {
for k, v := range responses.Codes {
go buildResponse(k.Value, v.Value, rChan)
}
totalCodes := len(response.Codes)
totalCodes := len(responses.Codes)
codesParsed := 0
for codesParsed < totalCodes {
select {

View File

@@ -4,11 +4,13 @@
package v2
import (
"crypto/sha256"
"fmt"
"github.com/pb33f/libopenapi/datamodel/low"
"github.com/pb33f/libopenapi/index"
"github.com/pb33f/libopenapi/utils"
"gopkg.in/yaml.v3"
"strings"
)
// Responses is a low-level representation of a Swagger / OpenAPI 2 Responses object.
@@ -30,13 +32,13 @@ func (r *Responses) Build(root *yaml.Node, idx *index.SpecIndex) error {
if codes != nil {
r.Codes = codes
}
def, derr := low.ExtractObject[*Response](DefaultLabel, root, idx)
if derr != nil {
return derr
}
if def.Value != nil {
r.Default = def
if re := r.FindResponseByCode(DefaultLabel); re != nil {
r.Default = low.NodeReference[*Response]{
Value: re.Value,
ValueNode: re.ValueNode,
KeyNode: re.ValueNode,
}
r.deleteCode(DefaultLabel)
}
} else {
return fmt.Errorf("responses build failed: vn node is not a map! line %d, col %d", root.Line, root.Column)
@@ -44,7 +46,37 @@ func (r *Responses) Build(root *yaml.Node, idx *index.SpecIndex) error {
return nil
}
func (r *Responses) deleteCode(code string) {
var key *low.KeyReference[string]
if r.Codes != nil {
for k := range r.Codes {
if k.Value == code {
key = &k
}
}
}
if key != nil {
delete(r.Codes, *key)
}
}
// FindResponseByCode will attempt to locate a Response instance using an HTTP response code string.
func (r *Responses) FindResponseByCode(code string) *low.ValueReference[*Response] {
return low.FindItemInMap[*Response](code, r.Codes)
}
// Hash will return a consistent SHA256 Hash of the Examples object
func (r *Responses) Hash() [32]byte {
var f []string
for k := range r.Codes {
f = append(f, low.GenerateHashString(r.Codes[k].Value))
}
if !r.Default.IsEmpty() {
f = append(f, low.GenerateHashString(r.Default.Value))
}
for k := range r.Extensions {
f = append(f, fmt.Sprintf("%s-%x", k.Value,
sha256.Sum256([]byte(fmt.Sprint(r.Extensions[k].Value)))))
}
return sha256.Sum256([]byte(strings.Join(f, "|")))
}

View File

@@ -112,4 +112,5 @@ const (
ScopesLabel = "scopes"
OperationRefLabel = "operationRef"
OperationIdLabel = "operationId"
CodesLabel = "codes"
)

View File

@@ -8,60 +8,10 @@ import (
"fmt"
"github.com/pb33f/libopenapi/datamodel/low"
"github.com/pb33f/libopenapi/index"
"github.com/pb33f/libopenapi/utils"
"gopkg.in/yaml.v3"
"strings"
)
// Responses represents a low-level OpenAPI 3+ Responses object.
//
// It's a container for the expected responses of an operation. The container maps a HTTP response code to the
// expected response.
//
// The specification is not necessarily expected to cover all possible HTTP response codes because they may not be
// known in advance. However, documentation is expected to cover a successful operation response and any known errors.
//
// The default MAY be used as a default response object for all HTTP codes that are not covered individually by
// the Responses Object.
//
// The Responses Object MUST contain at least one response code, and if only one response code is provided it SHOULD
// be the response for a successful operation call.
// - https://spec.openapis.org/oas/v3.1.0#responses-object
type Responses struct {
Codes map[low.KeyReference[string]]low.ValueReference[*Response]
Default low.NodeReference[*Response]
}
// Build will extract default response and all Response objects for each code
func (r *Responses) Build(root *yaml.Node, idx *index.SpecIndex) error {
if utils.IsNodeMap(root) {
codes, err := low.ExtractMapNoLookup[*Response](root, idx)
if err != nil {
return err
}
if codes != nil {
r.Codes = codes
}
def, derr := low.ExtractObject[*Response](DefaultLabel, root, idx)
if derr != nil {
return derr
}
if def.Value != nil {
r.Default = def
}
} else {
return fmt.Errorf("responses build failed: vn node is not a map! line %d, col %d",
root.Line, root.Column)
}
return nil
}
// FindResponseByCode will attempt to locate a Response using an HTTP response code.
func (r *Responses) FindResponseByCode(code string) *low.ValueReference[*Response] {
return low.FindItemInMap[*Response](code, r.Codes)
}
// Response represents a high-level OpenAPI 3+ Response object that is backed by a low-level one.
//
// Describes a single response from an API Operation, including design-time, static links to

View File

@@ -0,0 +1,86 @@
// Copyright 2022 Princess B33f Heavy Industries / Dave Shanley
// SPDX-License-Identifier: MIT
package v3
import (
"crypto/sha256"
"fmt"
"github.com/pb33f/libopenapi/datamodel/low"
"github.com/pb33f/libopenapi/index"
"github.com/pb33f/libopenapi/utils"
"gopkg.in/yaml.v3"
"strings"
)
// Responses represents a low-level OpenAPI 3+ Responses object.
//
// It's a container for the expected responses of an operation. The container maps a HTTP response code to the
// expected response.
//
// The specification is not necessarily expected to cover all possible HTTP response codes because they may not be
// known in advance. However, documentation is expected to cover a successful operation response and any known errors.
//
// The default MAY be used as a default response object for all HTTP codes that are not covered individually by
// the Responses Object.
//
// The Responses Object MUST contain at least one response code, and if only one response code is provided it SHOULD
// be the response for a successful operation call.
// - https://spec.openapis.org/oas/v3.1.0#responses-object
//
// This structure is identical to the v2 version, however they use different response types, hence
// the duplication. Perhaps in the future we could use generics here, but for now to keep things
// simple, they are broken out into individual versions.
type Responses struct {
Codes map[low.KeyReference[string]]low.ValueReference[*Response]
Default low.NodeReference[*Response]
Extensions map[low.KeyReference[string]]low.ValueReference[any]
}
// Build will extract default response and all Response objects for each code
func (r *Responses) Build(root *yaml.Node, idx *index.SpecIndex) error {
r.Extensions = low.ExtractExtensions(root)
if utils.IsNodeMap(root) {
codes, err := low.ExtractMapNoLookup[*Response](root, idx)
if err != nil {
return err
}
if codes != nil {
r.Codes = codes
}
def, derr := low.ExtractObject[*Response](DefaultLabel, root, idx)
if derr != nil {
return derr
}
if def.Value != nil {
r.Default = def
}
} else {
return fmt.Errorf("responses build failed: vn node is not a map! line %d, col %d",
root.Line, root.Column)
}
return nil
}
// FindResponseByCode will attempt to locate a Response using an HTTP response code.
func (r *Responses) FindResponseByCode(code string) *low.ValueReference[*Response] {
return low.FindItemInMap[*Response](code, r.Codes)
}
// Hash will return a consistent SHA256 Hash of the Examples object
func (r *Responses) Hash() [32]byte {
var f []string
for k := range r.Codes {
f = append(f, low.GenerateHashString(r.Codes[k].Value))
}
if !r.Default.IsEmpty() {
f = append(f, low.GenerateHashString(r.Default.Value))
}
for k := range r.Extensions {
f = append(f, fmt.Sprintf("%s-%x", k.Value,
sha256.Sum256([]byte(fmt.Sprint(r.Extensions[k].Value)))))
}
return sha256.Sum256([]byte(strings.Join(f, "|")))
}