Skip to content

Commit

Permalink
Added support for identifier to License pb33f#124
Browse files Browse the repository at this point in the history
An error is thrown if both URL and Identifier are used together as they are mutually exclusive.

Signed-off-by: Dave Shanley <[email protected]>
  • Loading branch information
daveshanley committed Jun 17, 2023
1 parent c3cf5f1 commit d3a34b5
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 3 deletions.
14 changes: 11 additions & 3 deletions datamodel/low/base/license.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package base

import (
"crypto/sha256"
"fmt"
"github.com/pb33f/libopenapi/datamodel/low"
"github.com/pb33f/libopenapi/index"
"gopkg.in/yaml.v3"
Expand All @@ -16,14 +17,18 @@ import (
// v2 - https://swagger.io/specification/v2/#licenseObject
// v3 - https://spec.openapis.org/oas/v3.1.0#license-object
type License struct {
Name low.NodeReference[string]
URL low.NodeReference[string]
Name low.NodeReference[string]
URL low.NodeReference[string]
Identifier low.NodeReference[string]
*low.Reference
}

// Build is not implemented for License (there is nothing to build)
// Build out a license, complain if both a URL and identifier are present as they are mutually exclusive
func (l *License) Build(root *yaml.Node, idx *index.SpecIndex) error {
l.Reference = new(low.Reference)
if l.URL.Value != "" && l.Identifier.Value != "" {
return fmt.Errorf("license cannot have both a URL and an identifier, they are mutually exclusive")
}
return nil
}

Expand All @@ -36,5 +41,8 @@ func (l *License) Hash() [32]byte {
if !l.URL.IsEmpty() {
f = append(f, l.URL.Value)
}
if !l.Identifier.IsEmpty() {
f = append(f, l.Identifier.Value)
}
return sha256.Sum256([]byte(strings.Join(f, "|")))
}
45 changes: 45 additions & 0 deletions datamodel/low/base/license_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,48 @@ description: the ranch`
assert.Equal(t, lDoc.Hash(), rDoc.Hash())

}

func TestLicense_WithIdentifier_Hash(t *testing.T) {

left := `identifier: MIT
description: the ranch`

right := `identifier: MIT
description: the ranch`

var lNode, rNode yaml.Node
_ = yaml.Unmarshal([]byte(left), &lNode)
_ = yaml.Unmarshal([]byte(right), &rNode)

// create low level objects
var lDoc License
var rDoc License
err := low.BuildModel(lNode.Content[0], &lDoc)
assert.NoError(t, err)

err = low.BuildModel(rNode.Content[0], &rDoc)
assert.NoError(t, err)

assert.Equal(t, lDoc.Hash(), rDoc.Hash())

}

func TestLicense_WithIdentifierAndURL_Error(t *testing.T) {

left := `identifier: MIT
url: https://pb33f.io
description: the ranch`

var lNode yaml.Node
_ = yaml.Unmarshal([]byte(left), &lNode)

// create low level objects
var lDoc License
err := low.BuildModel(lNode.Content[0], &lDoc)

err = lDoc.Build(lNode.Content[0], nil)

assert.Error(t, err)
assert.Equal(t, "license cannot have both a URL and an identifier, they are mutually exclusive", err.Error())

}

0 comments on commit d3a34b5

Please sign in to comment.