mirror of
https://github.com/LukeHagar/libopenapi.git
synced 2025-12-11 04:20:24 +00:00
Added support for empty security requirements.
A new property named `ContainsEmptyRuleRequirement` will exist when a requirement contains an empty object. This acts as a ‘none’ concept that can be used by applications downstream. Signed-off-by: quobix <dave@quobix.com>
This commit is contained in:
@@ -22,6 +22,7 @@ import (
|
||||
// - https://swagger.io/specification/v2/#securityDefinitionsObject
|
||||
type SecurityRequirement struct {
|
||||
Requirements *orderedmap.Map[string, []string] `json:"-" yaml:"-"`
|
||||
ContainsEmptyRequirement bool // if a requirement is empty (this means it's optional)
|
||||
low *base.SecurityRequirement
|
||||
}
|
||||
|
||||
@@ -39,6 +40,7 @@ func NewSecurityRequirement(req *base.SecurityRequirement) *SecurityRequirement
|
||||
values.Set(pair.Key().Value, vals)
|
||||
}
|
||||
r.Requirements = values
|
||||
r.ContainsEmptyRequirement = req.ContainsEmptyRequirement
|
||||
return r
|
||||
}
|
||||
|
||||
|
||||
@@ -26,9 +26,10 @@ import (
|
||||
// - https://swagger.io/specification/v2/#securityDefinitionsObject
|
||||
// - https://swagger.io/specification/#security-requirement-object
|
||||
type SecurityRequirement struct {
|
||||
Requirements low.ValueReference[*orderedmap.Map[low.KeyReference[string], low.ValueReference[[]low.ValueReference[string]]]]
|
||||
KeyNode *yaml.Node
|
||||
RootNode *yaml.Node
|
||||
Requirements low.ValueReference[*orderedmap.Map[low.KeyReference[string], low.ValueReference[[]low.ValueReference[string]]]]
|
||||
KeyNode *yaml.Node
|
||||
RootNode *yaml.Node
|
||||
ContainsEmptyRequirement bool // if a requirement is empty (this means it's optional)
|
||||
*low.Reference
|
||||
}
|
||||
|
||||
@@ -49,6 +50,9 @@ func (s *SecurityRequirement) Build(_ context.Context, keyNode, root *yaml.Node,
|
||||
continue
|
||||
}
|
||||
for j := range root.Content[i].Content {
|
||||
if root.Content[i].Content[j].Value == "" {
|
||||
s.ContainsEmptyRequirement = true
|
||||
}
|
||||
arr = append(arr, low.ValueReference[string]{
|
||||
Value: root.Content[i].Content[j].Value,
|
||||
ValueNode: root.Content[i].Content[j],
|
||||
@@ -65,6 +69,9 @@ func (s *SecurityRequirement) Build(_ context.Context, keyNode, root *yaml.Node,
|
||||
},
|
||||
)
|
||||
}
|
||||
if len(root.Content) == 0 {
|
||||
s.ContainsEmptyRequirement = true
|
||||
}
|
||||
s.Requirements = low.ValueReference[*orderedmap.Map[low.KeyReference[string], low.ValueReference[[]low.ValueReference[string]]]]{
|
||||
Value: valueMap,
|
||||
ValueNode: root,
|
||||
|
||||
@@ -45,3 +45,21 @@ one:
|
||||
assert.Equal(t, sr.Hash(), sr2.Hash())
|
||||
assert.Nil(t, sr.FindRequirement("i-do-not-exist"))
|
||||
}
|
||||
|
||||
func TestSecurityRequirement_TestEmptyReq(t *testing.T) {
|
||||
|
||||
yml := `one:
|
||||
- two
|
||||
- {}`
|
||||
|
||||
var sr SecurityRequirement
|
||||
var idxNode yaml.Node
|
||||
_ = yaml.Unmarshal([]byte(yml), &idxNode)
|
||||
|
||||
_ = sr.Build(context.Background(), nil, idxNode.Content[0], nil)
|
||||
|
||||
assert.Equal(t, 1, orderedmap.Len(sr.Requirements.Value))
|
||||
assert.Len(t, sr.GetKeys(), 1)
|
||||
assert.True(t, sr.ContainsEmptyRequirement)
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user