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.
81 lines
1.7 KiB
Go
81 lines
1.7 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 TestLink_Build(t *testing.T) {
|
|
|
|
yml := `operationRef: '#/someref'
|
|
operationId: someId
|
|
parameters:
|
|
param1: something
|
|
param2: somethingElse
|
|
requestBody: somebody
|
|
description: this is a link object.
|
|
server:
|
|
url: https://pb33f.io
|
|
x-linky: slinky
|
|
`
|
|
|
|
var idxNode yaml.Node
|
|
_ = yaml.Unmarshal([]byte(yml), &idxNode)
|
|
idx := index.NewSpecIndex(&idxNode)
|
|
|
|
var n Link
|
|
err := low.BuildModel(&idxNode, &n)
|
|
assert.NoError(t, err)
|
|
|
|
err = n.Build(idxNode.Content[0], idx)
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, "#/someref", n.OperationRef.Value)
|
|
assert.Equal(t, "someId", n.OperationId.Value)
|
|
assert.Equal(t, "this is a link object.", n.Description.Value)
|
|
|
|
ext := n.FindExtension("x-linky")
|
|
assert.NotNil(t, ext)
|
|
assert.Equal(t, "slinky", ext.Value)
|
|
|
|
param1 := n.FindParameter("param1")
|
|
assert.Equal(t, "something", param1.Value)
|
|
param2 := n.FindParameter("param2")
|
|
assert.Equal(t, "somethingElse", param2.Value)
|
|
|
|
assert.NotNil(t, n.Server.Value)
|
|
assert.Equal(t, "https://pb33f.io", n.Server.Value.URL.Value)
|
|
|
|
}
|
|
|
|
func TestLink_Build_Fail(t *testing.T) {
|
|
|
|
yml := `operationRef: '#/someref'
|
|
operationId: someId
|
|
parameters:
|
|
param1: something
|
|
param2: somethingElse
|
|
requestBody: somebody
|
|
description: this is a link object.
|
|
server:
|
|
$ref: #bork`
|
|
|
|
var idxNode yaml.Node
|
|
_ = yaml.Unmarshal([]byte(yml), &idxNode)
|
|
idx := index.NewSpecIndex(&idxNode)
|
|
|
|
var n Link
|
|
err := low.BuildModel(&idxNode, &n)
|
|
assert.NoError(t, err)
|
|
|
|
err = n.Build(idxNode.Content[0], idx)
|
|
assert.Error(t, err)
|
|
|
|
}
|