mirror of
https://github.com/LukeHagar/libopenapi.git
synced 2025-12-06 20:47:49 +00:00
3.0 and 2.0 do not work, there are multiple versions and anything with a period in it sucks from my point of view, v2 and v3 feel much better from a DX perspective.
56 lines
1.2 KiB
Go
56 lines
1.2 KiB
Go
// 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)
|
|
}
|