From 93fc2a79fe356635dc539a72c77a58bc624f3595 Mon Sep 17 00:00:00 2001 From: Dave Shanley Date: Mon, 15 Aug 2022 21:34:28 -0400 Subject: [PATCH] added tests for v3 request_body after a few drinks, not so easy. --- datamodel/low/3.0/path_test.go | 2 +- datamodel/low/3.0/request_body.go | 7 ++++ datamodel/low/3.0/request_body_test.go | 55 ++++++++++++++++++++++++++ 3 files changed, 63 insertions(+), 1 deletion(-) create mode 100644 datamodel/low/3.0/request_body_test.go diff --git a/datamodel/low/3.0/path_test.go b/datamodel/low/3.0/path_test.go index f992118..d811e54 100644 --- a/datamodel/low/3.0/path_test.go +++ b/datamodel/low/3.0/path_test.go @@ -223,7 +223,7 @@ func TestPaths_Build_BadRef(t *testing.T) { func TestPathItem_Build_GoodRef(t *testing.T) { - // this is kinda nuts, it's also not illegal, however the mechanics still need to work. + // this is kinda nuts, it's also not illegal, however the mechanics still need to work.bfes yml := `"/some/path": description: this is some path get: diff --git a/datamodel/low/3.0/request_body.go b/datamodel/low/3.0/request_body.go index eba05d5..f3ea848 100644 --- a/datamodel/low/3.0/request_body.go +++ b/datamodel/low/3.0/request_body.go @@ -1,3 +1,6 @@ +// Copyright 2022 Princess B33f Heavy Industries / Dave Shanley +// SPDX-License-Identifier: MIT + package v3 import ( @@ -13,6 +16,10 @@ type RequestBody struct { Extensions map[low.KeyReference[string]]low.ValueReference[any] } +func (rb *RequestBody) FindExtension(ext string) *low.ValueReference[any] { + return low.FindItemInMap[any](ext, rb.Extensions) +} + func (rb *RequestBody) FindContent(cType string) *low.ValueReference[*MediaType] { return low.FindItemInMap[*MediaType](cType, rb.Content.Value) } diff --git a/datamodel/low/3.0/request_body_test.go b/datamodel/low/3.0/request_body_test.go new file mode 100644 index 0000000..72b38f9 --- /dev/null +++ b/datamodel/low/3.0/request_body_test.go @@ -0,0 +1,55 @@ +// Copyright 2022 Princess B33f Heavy Industries / Dave Shanley +// SPDX-License-Identifier: MIT + +package v3 + +import ( + "github.com/pb33f/libopenapi/datamodel/low" + "github.com/pb33f/libopenapi/index" + "github.com/stretchr/testify/assert" + "gopkg.in/yaml.v3" + "testing" +) + +func TestRequestBody_Build(t *testing.T) { + + yml := `description: a nice request +required: true +content: + fresh/fish: + example: nice. +x-requesto: presto` + + var idxNode yaml.Node + _ = yaml.Unmarshal([]byte(yml), &idxNode) + idx := index.NewSpecIndex(&idxNode) + + var n RequestBody + err := low.BuildModel(&idxNode, &n) + assert.NoError(t, err) + + err = n.Build(idxNode.Content[0], idx) + assert.NoError(t, err) + assert.Equal(t, "a nice request", n.Description.Value) + assert.True(t, n.Required.Value) + assert.Equal(t, "nice.", n.FindContent("fresh/fish").Value.Example.Value) + assert.Equal(t, "presto", n.FindExtension("x-requesto").Value) + +} + +func TestRequestBody_Fail(t *testing.T) { + + yml := `content: + $ref: #illegal` + + var idxNode yaml.Node + _ = yaml.Unmarshal([]byte(yml), &idxNode) + idx := index.NewSpecIndex(&idxNode) + + var n RequestBody + err := low.BuildModel(&idxNode, &n) + assert.NoError(t, err) + + err = n.Build(idxNode.Content[0], idx) + assert.Error(t, err) +}