mirror of
https://github.com/LukeHagar/libopenapi.git
synced 2025-12-10 12:37:48 +00:00
@@ -5,6 +5,7 @@ package base
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"github.com/pb33f/libopenapi/datamodel"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
@@ -1191,3 +1192,97 @@ properties:
|
|||||||
assert.Equal(t, true, compiled.Properties["additionalPropertiesBool"].Schema().AdditionalProperties.B)
|
assert.Equal(t, true, compiled.Properties["additionalPropertiesBool"].Schema().AdditionalProperties.B)
|
||||||
assert.Equal(t, []string{"string"}, compiled.Properties["additionalPropertiesAnyOf"].Schema().AdditionalProperties.A.Schema().AnyOf[0].Schema().Type)
|
assert.Equal(t, []string{"string"}, compiled.Properties["additionalPropertiesAnyOf"].Schema().AdditionalProperties.A.Schema().AnyOf[0].Schema().Type)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestSchema_RenderProxyWithConfig_3(t *testing.T) {
|
||||||
|
testSpec := `exclusiveMinimum: true`
|
||||||
|
|
||||||
|
var compNode yaml.Node
|
||||||
|
_ = yaml.Unmarshal([]byte(testSpec), &compNode)
|
||||||
|
|
||||||
|
sp := new(lowbase.SchemaProxy)
|
||||||
|
err := sp.Build(nil, compNode.Content[0], nil)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
|
||||||
|
config := index.CreateOpenAPIIndexConfig()
|
||||||
|
config.SpecInfo = &datamodel.SpecInfo{
|
||||||
|
VersionNumeric: 3.0,
|
||||||
|
}
|
||||||
|
lowproxy := low.NodeReference[*lowbase.SchemaProxy]{
|
||||||
|
Value: sp,
|
||||||
|
ValueNode: compNode.Content[0],
|
||||||
|
}
|
||||||
|
|
||||||
|
schemaProxy := NewSchemaProxy(&lowproxy)
|
||||||
|
compiled := schemaProxy.Schema()
|
||||||
|
|
||||||
|
// now render it out, it should be identical.
|
||||||
|
schemaBytes, _ := compiled.Render()
|
||||||
|
assert.Equal(t, testSpec, strings.TrimSpace(string(schemaBytes)))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestSchema_RenderProxyWithConfig_Corrected_31(t *testing.T) {
|
||||||
|
testSpec := `exclusiveMinimum: true`
|
||||||
|
testSpecCorrect := `exclusiveMinimum: 0`
|
||||||
|
|
||||||
|
var compNode yaml.Node
|
||||||
|
_ = yaml.Unmarshal([]byte(testSpec), &compNode)
|
||||||
|
|
||||||
|
sp := new(lowbase.SchemaProxy)
|
||||||
|
config := index.CreateOpenAPIIndexConfig()
|
||||||
|
config.SpecInfo = &datamodel.SpecInfo{
|
||||||
|
VersionNumeric: 3.1,
|
||||||
|
}
|
||||||
|
idx := index.NewSpecIndexWithConfig(compNode.Content[0], config)
|
||||||
|
|
||||||
|
err := sp.Build(nil, compNode.Content[0], idx)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
|
||||||
|
lowproxy := low.NodeReference[*lowbase.SchemaProxy]{
|
||||||
|
Value: sp,
|
||||||
|
ValueNode: compNode.Content[0],
|
||||||
|
}
|
||||||
|
|
||||||
|
schemaProxy := NewSchemaProxy(&lowproxy)
|
||||||
|
compiled := schemaProxy.Schema()
|
||||||
|
|
||||||
|
// now render it out, it should be identical.
|
||||||
|
schemaBytes, _ := compiled.Render()
|
||||||
|
assert.Equal(t, testSpecCorrect, strings.TrimSpace(string(schemaBytes)))
|
||||||
|
|
||||||
|
schemaBytes, _ = compiled.RenderInline()
|
||||||
|
assert.Equal(t, testSpecCorrect, strings.TrimSpace(string(schemaBytes)))
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestSchema_RenderProxyWithConfig_Corrected_3(t *testing.T) {
|
||||||
|
testSpec := `exclusiveMinimum: 0`
|
||||||
|
testSpecCorrect := `exclusiveMinimum: false`
|
||||||
|
|
||||||
|
var compNode yaml.Node
|
||||||
|
_ = yaml.Unmarshal([]byte(testSpec), &compNode)
|
||||||
|
|
||||||
|
sp := new(lowbase.SchemaProxy)
|
||||||
|
config := index.CreateOpenAPIIndexConfig()
|
||||||
|
config.SpecInfo = &datamodel.SpecInfo{
|
||||||
|
VersionNumeric: 3.0,
|
||||||
|
}
|
||||||
|
idx := index.NewSpecIndexWithConfig(compNode.Content[0], config)
|
||||||
|
|
||||||
|
err := sp.Build(nil, compNode.Content[0], idx)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
|
||||||
|
lowproxy := low.NodeReference[*lowbase.SchemaProxy]{
|
||||||
|
Value: sp,
|
||||||
|
ValueNode: compNode.Content[0],
|
||||||
|
}
|
||||||
|
|
||||||
|
schemaProxy := NewSchemaProxy(&lowproxy)
|
||||||
|
compiled := schemaProxy.Schema()
|
||||||
|
|
||||||
|
// now render it out, it should be identical.
|
||||||
|
schemaBytes, _ := compiled.Render()
|
||||||
|
assert.Equal(t, testSpecCorrect, strings.TrimSpace(string(schemaBytes)))
|
||||||
|
|
||||||
|
schemaBytes, _ = compiled.RenderInline()
|
||||||
|
assert.Equal(t, testSpecCorrect, strings.TrimSpace(string(schemaBytes)))
|
||||||
|
}
|
||||||
|
|||||||
@@ -90,6 +90,7 @@ type test1 struct {
|
|||||||
Thugg *bool `yaml:"thugg,renderZero"`
|
Thugg *bool `yaml:"thugg,renderZero"`
|
||||||
Thurr *int64 `yaml:"thurr,omitempty"`
|
Thurr *int64 `yaml:"thurr,omitempty"`
|
||||||
Thral *float64 `yaml:"thral,omitempty"`
|
Thral *float64 `yaml:"thral,omitempty"`
|
||||||
|
Throo *float64 `yaml:"throo,renderZero,omitempty"`
|
||||||
Tharg []string `yaml:"tharg,omitempty"`
|
Tharg []string `yaml:"tharg,omitempty"`
|
||||||
Type []string `yaml:"type,omitempty"`
|
Type []string `yaml:"type,omitempty"`
|
||||||
Throg []*key `yaml:"throg,omitempty"`
|
Throg []*key `yaml:"throg,omitempty"`
|
||||||
@@ -922,6 +923,40 @@ func TestNewNodeBuilder_TestRenderZero(t *testing.T) {
|
|||||||
assert.Equal(t, desired, strings.TrimSpace(string(data)))
|
assert.Equal(t, desired, strings.TrimSpace(string(data)))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestNewNodeBuilder_TestRenderZero_Float(t *testing.T) {
|
||||||
|
|
||||||
|
f := 0.0
|
||||||
|
t1 := test1{
|
||||||
|
Throo: &f,
|
||||||
|
}
|
||||||
|
|
||||||
|
nb := NewNodeBuilder(&t1, &t1)
|
||||||
|
node := nb.Render()
|
||||||
|
|
||||||
|
data, _ := yaml.Marshal(node)
|
||||||
|
|
||||||
|
desired := `throo: 0`
|
||||||
|
|
||||||
|
assert.Equal(t, desired, strings.TrimSpace(string(data)))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestNewNodeBuilder_TestRenderZero_Float_NotZero(t *testing.T) {
|
||||||
|
|
||||||
|
f := 0.12
|
||||||
|
t1 := test1{
|
||||||
|
Throo: &f,
|
||||||
|
}
|
||||||
|
|
||||||
|
nb := NewNodeBuilder(&t1, &t1)
|
||||||
|
node := nb.Render()
|
||||||
|
|
||||||
|
data, _ := yaml.Marshal(node)
|
||||||
|
|
||||||
|
desired := `throo: 0.12`
|
||||||
|
|
||||||
|
assert.Equal(t, desired, strings.TrimSpace(string(data)))
|
||||||
|
}
|
||||||
|
|
||||||
func TestNewNodeBuilder_TestRenderServerVariableSimulation(t *testing.T) {
|
func TestNewNodeBuilder_TestRenderServerVariableSimulation(t *testing.T) {
|
||||||
|
|
||||||
t1 := test1{
|
t1 := test1{
|
||||||
|
|||||||
@@ -1,14 +1,14 @@
|
|||||||
package base
|
package base
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"testing"
|
"github.com/pb33f/libopenapi/datamodel"
|
||||||
|
|
||||||
"github.com/pb33f/libopenapi/datamodel/low"
|
"github.com/pb33f/libopenapi/datamodel/low"
|
||||||
"github.com/pb33f/libopenapi/index"
|
"github.com/pb33f/libopenapi/index"
|
||||||
"github.com/pb33f/libopenapi/resolver"
|
"github.com/pb33f/libopenapi/resolver"
|
||||||
"github.com/pb33f/libopenapi/utils"
|
"github.com/pb33f/libopenapi/utils"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"gopkg.in/yaml.v3"
|
"gopkg.in/yaml.v3"
|
||||||
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
func test_get_schema_blob() string {
|
func test_get_schema_blob() string {
|
||||||
@@ -1636,3 +1636,123 @@ func TestSchema_UnevaluatedPropertiesAsBool_Undefined(t *testing.T) {
|
|||||||
|
|
||||||
assert.Nil(t, res.Value.Schema().UnevaluatedProperties.Value)
|
assert.Nil(t, res.Value.Schema().UnevaluatedProperties.Value)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestSchema_ExclusiveMinimum_3_with_Config(t *testing.T) {
|
||||||
|
yml := `openapi: 3.0.3
|
||||||
|
components:
|
||||||
|
schemas:
|
||||||
|
Something:
|
||||||
|
type: integer
|
||||||
|
minimum: 3
|
||||||
|
exclusiveMinimum: true`
|
||||||
|
|
||||||
|
var iNode yaml.Node
|
||||||
|
mErr := yaml.Unmarshal([]byte(yml), &iNode)
|
||||||
|
assert.NoError(t, mErr)
|
||||||
|
|
||||||
|
config := index.CreateOpenAPIIndexConfig()
|
||||||
|
config.SpecInfo = &datamodel.SpecInfo{
|
||||||
|
VersionNumeric: 3.0,
|
||||||
|
}
|
||||||
|
|
||||||
|
idx := index.NewSpecIndexWithConfig(&iNode, config)
|
||||||
|
|
||||||
|
yml = `$ref: '#/components/schemas/Something'`
|
||||||
|
|
||||||
|
var idxNode yaml.Node
|
||||||
|
_ = yaml.Unmarshal([]byte(yml), &idxNode)
|
||||||
|
|
||||||
|
res, _ := ExtractSchema(idxNode.Content[0], idx)
|
||||||
|
|
||||||
|
assert.True(t, res.Value.Schema().ExclusiveMinimum.Value.A)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestSchema_ExclusiveMinimum_31_with_Config(t *testing.T) {
|
||||||
|
yml := `openapi: 3.1
|
||||||
|
components:
|
||||||
|
schemas:
|
||||||
|
Something:
|
||||||
|
type: integer
|
||||||
|
minimum: 3
|
||||||
|
exclusiveMinimum: 3`
|
||||||
|
|
||||||
|
var iNode yaml.Node
|
||||||
|
mErr := yaml.Unmarshal([]byte(yml), &iNode)
|
||||||
|
assert.NoError(t, mErr)
|
||||||
|
|
||||||
|
config := index.CreateOpenAPIIndexConfig()
|
||||||
|
config.SpecInfo = &datamodel.SpecInfo{
|
||||||
|
VersionNumeric: 3.1,
|
||||||
|
}
|
||||||
|
|
||||||
|
idx := index.NewSpecIndexWithConfig(&iNode, config)
|
||||||
|
|
||||||
|
yml = `$ref: '#/components/schemas/Something'`
|
||||||
|
|
||||||
|
var idxNode yaml.Node
|
||||||
|
_ = yaml.Unmarshal([]byte(yml), &idxNode)
|
||||||
|
|
||||||
|
res, _ := ExtractSchema(idxNode.Content[0], idx)
|
||||||
|
|
||||||
|
assert.Equal(t, 3.0, res.Value.Schema().ExclusiveMinimum.Value.B)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestSchema_ExclusiveMaximum_3_with_Config(t *testing.T) {
|
||||||
|
yml := `openapi: 3.0.3
|
||||||
|
components:
|
||||||
|
schemas:
|
||||||
|
Something:
|
||||||
|
type: integer
|
||||||
|
maximum: 3
|
||||||
|
exclusiveMaximum: true`
|
||||||
|
|
||||||
|
var iNode yaml.Node
|
||||||
|
mErr := yaml.Unmarshal([]byte(yml), &iNode)
|
||||||
|
assert.NoError(t, mErr)
|
||||||
|
|
||||||
|
config := index.CreateOpenAPIIndexConfig()
|
||||||
|
config.SpecInfo = &datamodel.SpecInfo{
|
||||||
|
VersionNumeric: 3.0,
|
||||||
|
}
|
||||||
|
|
||||||
|
idx := index.NewSpecIndexWithConfig(&iNode, config)
|
||||||
|
|
||||||
|
yml = `$ref: '#/components/schemas/Something'`
|
||||||
|
|
||||||
|
var idxNode yaml.Node
|
||||||
|
_ = yaml.Unmarshal([]byte(yml), &idxNode)
|
||||||
|
|
||||||
|
res, _ := ExtractSchema(idxNode.Content[0], idx)
|
||||||
|
|
||||||
|
assert.True(t, res.Value.Schema().ExclusiveMaximum.Value.A)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestSchema_ExclusiveMaximum_31_with_Config(t *testing.T) {
|
||||||
|
yml := `openapi: 3.1
|
||||||
|
components:
|
||||||
|
schemas:
|
||||||
|
Something:
|
||||||
|
type: integer
|
||||||
|
maximum: 3
|
||||||
|
exclusiveMaximum: 3`
|
||||||
|
|
||||||
|
var iNode yaml.Node
|
||||||
|
mErr := yaml.Unmarshal([]byte(yml), &iNode)
|
||||||
|
assert.NoError(t, mErr)
|
||||||
|
|
||||||
|
config := index.CreateOpenAPIIndexConfig()
|
||||||
|
config.SpecInfo = &datamodel.SpecInfo{
|
||||||
|
VersionNumeric: 3.1,
|
||||||
|
}
|
||||||
|
|
||||||
|
idx := index.NewSpecIndexWithConfig(&iNode, config)
|
||||||
|
|
||||||
|
yml = `$ref: '#/components/schemas/Something'`
|
||||||
|
|
||||||
|
var idxNode yaml.Node
|
||||||
|
_ = yaml.Unmarshal([]byte(yml), &idxNode)
|
||||||
|
|
||||||
|
res, _ := ExtractSchema(idxNode.Content[0], idx)
|
||||||
|
|
||||||
|
assert.Equal(t, 3.0, res.Value.Schema().ExclusiveMaximum.Value.B)
|
||||||
|
}
|
||||||
|
|||||||
@@ -23,3 +23,10 @@ func TestSpecIndex_Children(t *testing.T) {
|
|||||||
assert.Equal(t, 1, len(idx4.GetChildren()))
|
assert.Equal(t, 1, len(idx4.GetChildren()))
|
||||||
assert.Equal(t, 0, len(idx5.GetChildren()))
|
assert.Equal(t, 0, len(idx5.GetChildren()))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestSpecIndex_GetConfig(t *testing.T) {
|
||||||
|
idx1 := new(SpecIndex)
|
||||||
|
c := SpecIndexConfig{}
|
||||||
|
idx1.config = &c
|
||||||
|
assert.Equal(t, &c, idx1.GetConfig())
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user