diff --git a/datamodel/high/base/licence_test.go b/datamodel/high/base/licence_test.go index 80dd70b..a34f8ba 100644 --- a/datamodel/high/base/licence_test.go +++ b/datamodel/high/base/licence_test.go @@ -57,3 +57,42 @@ url: https://pb33f.io/not-real assert.Equal(t, yml, string(bytes)) } + +func TestLicense_Render_Identifier(t *testing.T) { + + highL := &License{Name: "MIT", Identifier: "MIT"} + dat, _ := highL.Render() + + // unmarshal yaml into a *yaml.Node instance + var cNode yaml.Node + _ = yaml.Unmarshal(dat, &cNode) + + // build low + var lowLicense lowbase.License + _ = lowmodel.BuildModel(cNode.Content[0], &lowLicense) + + // build high + highLicense := NewLicense(&lowLicense) + + assert.Equal(t, "MIT", highLicense.Name) + assert.Equal(t, "MIT", highLicense.Identifier) + +} + +func TestLicense_Render_IdentifierAndURL_Error(t *testing.T) { + + // this should fail because you can't have both an identifier and a URL + highL := &License{Name: "MIT", Identifier: "MIT", URL: "https://pb33f.io"} + dat, _ := highL.Render() + + // unmarshal yaml into a *yaml.Node instance + var cNode yaml.Node + _ = yaml.Unmarshal(dat, &cNode) + + // build low + var lowLicense lowbase.License + _ = lowmodel.BuildModel(cNode.Content[0], &lowLicense) + err := lowLicense.Build(cNode.Content[0], nil) + + assert.Error(t, err) +} diff --git a/datamodel/low/base/schema_test.go b/datamodel/low/base/schema_test.go index e679cb2..ab1382d 100644 --- a/datamodel/low/base/schema_test.go +++ b/datamodel/low/base/schema_test.go @@ -1597,6 +1597,9 @@ func TestSchema_UnevaluatedPropertiesAsBool_DefinedAsTrue(t *testing.T) { assert.True(t, res.Value.Schema().UnevaluatedProperties.Value.IsB()) assert.True(t, *res.Value.Schema().UnevaluatedProperties.Value.B) + assert.Equal(t, "571bd1853c22393131e2dcadce86894da714ec14968895c8b7ed18154b2be8cd", + low.GenerateHashString(res.Value.Schema().UnevaluatedProperties.Value)) + } func TestSchema_UnevaluatedPropertiesAsBool_DefinedAsFalse(t *testing.T) { diff --git a/datamodel/low/extraction_functions.go b/datamodel/low/extraction_functions.go index 6411e87..366f87a 100644 --- a/datamodel/low/extraction_functions.go +++ b/datamodel/low/extraction_functions.go @@ -6,6 +6,7 @@ package low import ( "crypto/sha256" "fmt" + "reflect" "strconv" "strings" @@ -685,5 +686,9 @@ func GenerateHashString(v any) string { return fmt.Sprintf(HASH, h.Hash()) } } + // if we get here, we're a primitive, check if we're a pointer and de-point + if reflect.TypeOf(v).Kind() == reflect.Ptr { + v = reflect.ValueOf(v).Elem().Interface() + } return fmt.Sprintf(HASH, sha256.Sum256([]byte(fmt.Sprint(v)))) } diff --git a/datamodel/low/extraction_functions_test.go b/datamodel/low/extraction_functions_test.go index a58c187..53e8580 100644 --- a/datamodel/low/extraction_functions_test.go +++ b/datamodel/low/extraction_functions_test.go @@ -8,6 +8,7 @@ import ( "fmt" "io/ioutil" "os" + "strings" "testing" "github.com/pb33f/libopenapi/index" @@ -1615,11 +1616,19 @@ x-tacos: [1,2,3]` } type test_fresh struct { - val string + val string + thang *bool } func (f test_fresh) Hash() [32]byte { - return sha256.Sum256([]byte(f.val)) + var data []string + if f.val != "" { + data = append(data, f.val) + } + if f.thang != nil { + data = append(data, fmt.Sprintf("%v", *f.thang)) + } + return sha256.Sum256([]byte(strings.Join(data, "|"))) } func TestAreEqual(t *testing.T) { assert.True(t, AreEqual(test_fresh{val: "hello"}, test_fresh{val: "hello"})) @@ -1637,6 +1646,17 @@ func TestGenerateHashString(t *testing.T) { } +func TestGenerateHashString_Pointer(t *testing.T) { + + val := true + assert.Equal(t, "b5bea41b6c623f7c09f1bf24dcae58ebab3c0cdd90ad966bc43a45b44867e12b", + GenerateHashString(test_fresh{thang: &val})) + + assert.Equal(t, "b5bea41b6c623f7c09f1bf24dcae58ebab3c0cdd90ad966bc43a45b44867e12b", + GenerateHashString(&val)) + +} + func TestSetReference(t *testing.T) { type testObj struct { diff --git a/utils/utils_test.go b/utils/utils_test.go index e848b58..e070d2f 100644 --- a/utils/utils_test.go +++ b/utils/utils_test.go @@ -797,3 +797,9 @@ func TestCheckEnumForDuplicates_FailMultiple(t *testing.T) { yaml.Unmarshal([]byte(yml), &rootNode) assert.Len(t, CheckEnumForDuplicates(rootNode.Content[0].Content), 3) } + +func TestConvertComponentIdIntoFriendlyPathSearch_Brackets(t *testing.T) { + segment, path := ConvertComponentIdIntoFriendlyPathSearch("#/components/schemas/OhNoWhy[HaveYouDoneThis]") + assert.Equal(t, "$.components.schemas['OhNoWhy[HaveYouDoneThis]']", path) + assert.Equal(t, "OhNoWhy[HaveYouDoneThis]", segment) +}