mirror of
https://github.com/LukeHagar/libopenapi.git
synced 2025-12-10 12:37:48 +00:00
Merge branch 'main' into Baliedge/PIP-2552-consistent-ordering
This commit is contained in:
@@ -4,10 +4,13 @@
|
||||
package base
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/pb33f/libopenapi/datamodel"
|
||||
|
||||
"github.com/pb33f/libopenapi/datamodel/low"
|
||||
lowbase "github.com/pb33f/libopenapi/datamodel/low/base"
|
||||
"github.com/pb33f/libopenapi/index"
|
||||
@@ -49,7 +52,7 @@ func TestNewSchemaProxy(t *testing.T) {
|
||||
_ = yaml.Unmarshal([]byte(yml), &compNode)
|
||||
|
||||
sp := new(lowbase.SchemaProxy)
|
||||
err := sp.Build(nil, compNode.Content[0], idx)
|
||||
err := sp.Build(context.Background(), nil, compNode.Content[0], idx)
|
||||
assert.NoError(t, err)
|
||||
|
||||
lowproxy := low.NodeReference[*lowbase.SchemaProxy]{
|
||||
@@ -90,7 +93,7 @@ func TestNewSchemaProxyRender(t *testing.T) {
|
||||
_ = yaml.Unmarshal([]byte(yml), &compNode)
|
||||
|
||||
sp := new(lowbase.SchemaProxy)
|
||||
err := sp.Build(nil, compNode.Content[0], idx)
|
||||
err := sp.Build(context.Background(), nil, compNode.Content[0], idx)
|
||||
assert.NoError(t, err)
|
||||
|
||||
lowproxy := low.NodeReference[*lowbase.SchemaProxy]{
|
||||
@@ -111,7 +114,6 @@ func TestNewSchemaProxyRender(t *testing.T) {
|
||||
rice:
|
||||
$ref: '#/components/schemas/rice'`
|
||||
assert.Equal(t, desired, strings.TrimSpace(string(rend)))
|
||||
|
||||
}
|
||||
|
||||
func TestNewSchemaProxy_WithObject(t *testing.T) {
|
||||
@@ -217,10 +219,7 @@ properties:
|
||||
type: number
|
||||
description: a number
|
||||
example: "2"
|
||||
additionalProperties:
|
||||
- chicken
|
||||
- nugget
|
||||
- soup
|
||||
additionalProperties: false
|
||||
somethingB:
|
||||
type: object
|
||||
exclusiveMinimum: true
|
||||
@@ -241,8 +240,7 @@ properties:
|
||||
attribute: true
|
||||
x-pizza: love
|
||||
additionalProperties:
|
||||
why: yes
|
||||
thatIs: true
|
||||
type: string
|
||||
additionalProperties: true
|
||||
required:
|
||||
- them
|
||||
@@ -274,7 +272,7 @@ $anchor: anchor`
|
||||
_ = yaml.Unmarshal([]byte(testSpec), &compNode)
|
||||
|
||||
sp := new(lowbase.SchemaProxy)
|
||||
err := sp.Build(nil, compNode.Content[0], nil)
|
||||
err := sp.Build(context.Background(), nil, compNode.Content[0], nil)
|
||||
assert.NoError(t, err)
|
||||
|
||||
lowproxy := low.NodeReference[*lowbase.SchemaProxy]{
|
||||
@@ -315,12 +313,12 @@ $anchor: anchor`
|
||||
assert.Equal(t, "anchor", compiled.Anchor)
|
||||
|
||||
wentLow := compiled.GoLow()
|
||||
assert.Equal(t, 129, wentLow.AdditionalProperties.ValueNode.Line)
|
||||
assert.Equal(t, 125, wentLow.AdditionalProperties.ValueNode.Line)
|
||||
assert.NotNil(t, compiled.GoLowUntyped())
|
||||
|
||||
// now render it out!
|
||||
schemaBytes, _ := compiled.Render()
|
||||
assert.Len(t, schemaBytes, 3494)
|
||||
assert.Len(t, schemaBytes, 3417)
|
||||
}
|
||||
|
||||
func TestSchemaObjectWithAllOfSequenceOrder(t *testing.T) {
|
||||
@@ -348,7 +346,7 @@ func TestSchemaObjectWithAllOfSequenceOrder(t *testing.T) {
|
||||
}
|
||||
|
||||
sp := new(lowbase.SchemaProxy)
|
||||
err := sp.Build(nil, compNode.Content[0], nil)
|
||||
err := sp.Build(context.Background(), nil, compNode.Content[0], nil)
|
||||
assert.NoError(t, err)
|
||||
|
||||
lowproxy := low.NodeReference[*lowbase.SchemaProxy]{
|
||||
@@ -485,7 +483,7 @@ required: [cake, fish]`
|
||||
_ = yaml.Unmarshal([]byte(testSpec), &compNode)
|
||||
|
||||
sp := new(lowbase.SchemaProxy)
|
||||
err := sp.Build(nil, compNode.Content[0], nil)
|
||||
err := sp.Build(context.Background(), nil, compNode.Content[0], nil)
|
||||
assert.NoError(t, err)
|
||||
|
||||
lowproxy := low.NodeReference[*lowbase.SchemaProxy]{
|
||||
@@ -504,7 +502,7 @@ required: [cake, fish]`
|
||||
assert.Equal(t, float64(334), compiled.Properties.GetOrZero("somethingB").Schema().ExclusiveMaximum.B)
|
||||
assert.Len(t, compiled.Properties.GetOrZero("somethingB").Schema().Properties.GetOrZero("somethingBProp").Schema().Type, 2)
|
||||
|
||||
assert.Equal(t, "nice", compiled.AdditionalProperties.(*SchemaProxy).Schema().Description)
|
||||
assert.Equal(t, "nice", compiled.AdditionalProperties.A.Schema().Description)
|
||||
|
||||
wentLow := compiled.GoLow()
|
||||
assert.Equal(t, 97, wentLow.AdditionalProperties.ValueNode.Line)
|
||||
@@ -541,7 +539,7 @@ func TestSchemaProxy_GoLow(t *testing.T) {
|
||||
_ = yaml.Unmarshal([]byte(ymlSchema), &node)
|
||||
|
||||
lowProxy := new(lowbase.SchemaProxy)
|
||||
err := lowProxy.Build(nil, node.Content[0], idx)
|
||||
err := lowProxy.Build(context.Background(), nil, node.Content[0], idx)
|
||||
assert.NoError(t, err)
|
||||
|
||||
lowRef := low.NodeReference[*lowbase.SchemaProxy]{
|
||||
@@ -556,7 +554,6 @@ func TestSchemaProxy_GoLow(t *testing.T) {
|
||||
spNil := NewSchemaProxy(nil)
|
||||
assert.Nil(t, spNil.GoLow())
|
||||
assert.Nil(t, spNil.GoLowUntyped())
|
||||
|
||||
}
|
||||
|
||||
func getHighSchema(t *testing.T, yml string) *Schema {
|
||||
@@ -567,7 +564,7 @@ func getHighSchema(t *testing.T, yml string) *Schema {
|
||||
// build out the low-level model
|
||||
var lowSchema lowbase.Schema
|
||||
assert.NoError(t, low.BuildModel(node.Content[0], &lowSchema))
|
||||
assert.NoError(t, lowSchema.Build(node.Content[0], nil))
|
||||
assert.NoError(t, lowSchema.Build(context.Background(), node.Content[0], nil))
|
||||
|
||||
// build the high level model
|
||||
return NewSchema(&lowSchema)
|
||||
@@ -728,7 +725,7 @@ properties:
|
||||
// build out the low-level model
|
||||
var lowSchema lowbase.Schema
|
||||
_ = low.BuildModel(node.Content[0], &lowSchema)
|
||||
_ = lowSchema.Build(node.Content[0], nil)
|
||||
_ = lowSchema.Build(context.Background(), node.Content[0], nil)
|
||||
|
||||
// build the high level model
|
||||
highSchema := NewSchema(&lowSchema)
|
||||
@@ -757,7 +754,7 @@ properties:
|
||||
// build out the low-level model
|
||||
var lowSchema lowbase.SchemaProxy
|
||||
_ = low.BuildModel(node.Content[0], &lowSchema)
|
||||
_ = lowSchema.Build(nil, node.Content[0], nil)
|
||||
_ = lowSchema.Build(context.Background(), nil, node.Content[0], nil)
|
||||
|
||||
// build the high level schema proxy
|
||||
highSchema := NewSchemaProxy(&low.NodeReference[*lowbase.SchemaProxy]{
|
||||
@@ -817,7 +814,7 @@ allOf:
|
||||
_ = yaml.Unmarshal([]byte(testSpec), &compNode)
|
||||
|
||||
sp := new(lowbase.SchemaProxy)
|
||||
err := sp.Build(nil, compNode.Content[0], nil)
|
||||
err := sp.Build(context.Background(), nil, compNode.Content[0], nil)
|
||||
assert.NoError(t, err)
|
||||
|
||||
lowproxy := low.NodeReference[*lowbase.SchemaProxy]{
|
||||
@@ -836,7 +833,6 @@ allOf:
|
||||
// now render it out, it should be identical.
|
||||
schemaBytes, _ := compiled.Render()
|
||||
assert.Equal(t, testSpec, string(schemaBytes))
|
||||
|
||||
}
|
||||
|
||||
func TestNewSchemaProxy_RenderSchemaWithMultipleObjectTypes(t *testing.T) {
|
||||
@@ -881,7 +877,7 @@ items:
|
||||
_ = yaml.Unmarshal([]byte(testSpec), &compNode)
|
||||
|
||||
sp := new(lowbase.SchemaProxy)
|
||||
err := sp.Build(nil, compNode.Content[0], nil)
|
||||
err := sp.Build(context.Background(), nil, compNode.Content[0], nil)
|
||||
assert.NoError(t, err)
|
||||
|
||||
lowproxy := low.NodeReference[*lowbase.SchemaProxy]{
|
||||
@@ -934,8 +930,7 @@ func TestNewSchemaProxy_RenderSchemaEnsurePropertyOrdering(t *testing.T) {
|
||||
attribute: true
|
||||
x-pizza: love
|
||||
additionalProperties:
|
||||
why: yes
|
||||
thatIs: true
|
||||
type: string
|
||||
additionalProperties: true
|
||||
xml:
|
||||
name: XML Thing`
|
||||
@@ -944,7 +939,7 @@ xml:
|
||||
_ = yaml.Unmarshal([]byte(testSpec), &compNode)
|
||||
|
||||
sp := new(lowbase.SchemaProxy)
|
||||
err := sp.Build(nil, compNode.Content[0], nil)
|
||||
err := sp.Build(context.Background(), nil, compNode.Content[0], nil)
|
||||
assert.NoError(t, err)
|
||||
|
||||
lowproxy := low.NodeReference[*lowbase.SchemaProxy]{
|
||||
@@ -973,7 +968,7 @@ func TestNewSchemaProxy_RenderSchemaCheckDiscriminatorMappingOrder(t *testing.T)
|
||||
_ = yaml.Unmarshal([]byte(testSpec), &compNode)
|
||||
|
||||
sp := new(lowbase.SchemaProxy)
|
||||
err := sp.Build(nil, compNode.Content[0], nil)
|
||||
err := sp.Build(context.Background(), nil, compNode.Content[0], nil)
|
||||
assert.NoError(t, err)
|
||||
|
||||
lowproxy := low.NodeReference[*lowbase.SchemaProxy]{
|
||||
@@ -989,60 +984,6 @@ func TestNewSchemaProxy_RenderSchemaCheckDiscriminatorMappingOrder(t *testing.T)
|
||||
assert.Equal(t, testSpec, strings.TrimSpace(string(schemaBytes)))
|
||||
}
|
||||
|
||||
func TestNewSchemaProxy_RenderSchemaCheckAdditionalPropertiesSlice(t *testing.T) {
|
||||
testSpec := `additionalProperties:
|
||||
- one
|
||||
- two
|
||||
- miss a few
|
||||
- ninety nine
|
||||
- hundred`
|
||||
|
||||
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)
|
||||
|
||||
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.Len(t, schemaBytes, 91)
|
||||
}
|
||||
|
||||
func TestNewSchemaProxy_RenderSchemaCheckAdditionalPropertiesSliceMap(t *testing.T) {
|
||||
testSpec := `additionalProperties:
|
||||
- nice: cake
|
||||
- yummy: beer
|
||||
- hot: coffee`
|
||||
|
||||
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)
|
||||
|
||||
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.Len(t, schemaBytes, 75)
|
||||
}
|
||||
|
||||
func TestNewSchemaProxy_CheckDefaultBooleanFalse(t *testing.T) {
|
||||
testSpec := `default: false`
|
||||
|
||||
@@ -1050,7 +991,7 @@ func TestNewSchemaProxy_CheckDefaultBooleanFalse(t *testing.T) {
|
||||
_ = yaml.Unmarshal([]byte(testSpec), &compNode)
|
||||
|
||||
sp := new(lowbase.SchemaProxy)
|
||||
err := sp.Build(nil, compNode.Content[0], nil)
|
||||
err := sp.Build(context.Background(), nil, compNode.Content[0], nil)
|
||||
assert.NoError(t, err)
|
||||
|
||||
lowproxy := low.NodeReference[*lowbase.SchemaProxy]{
|
||||
@@ -1073,7 +1014,7 @@ func TestNewSchemaProxy_RenderAdditionalPropertiesFalse(t *testing.T) {
|
||||
_ = yaml.Unmarshal([]byte(testSpec), &compNode)
|
||||
|
||||
sp := new(lowbase.SchemaProxy)
|
||||
err := sp.Build(nil, compNode.Content[0], nil)
|
||||
err := sp.Build(context.Background(), nil, compNode.Content[0], nil)
|
||||
assert.NoError(t, err)
|
||||
|
||||
lowproxy := low.NodeReference[*lowbase.SchemaProxy]{
|
||||
@@ -1117,7 +1058,7 @@ components:
|
||||
|
||||
sp := new(lowbase.SchemaProxy)
|
||||
|
||||
err := sp.Build(nil, compNode.Content[0], idx)
|
||||
err := sp.Build(context.Background(), nil, compNode.Content[0], idx)
|
||||
assert.NoError(t, err)
|
||||
|
||||
lowproxy := low.NodeReference[*lowbase.SchemaProxy]{
|
||||
@@ -1169,7 +1110,7 @@ components:
|
||||
|
||||
sp := new(lowbase.SchemaProxy)
|
||||
|
||||
err := sp.Build(nil, compNode.Content[0], idx)
|
||||
err := sp.Build(context.Background(), nil, compNode.Content[0], idx)
|
||||
assert.NoError(t, err)
|
||||
|
||||
lowproxy := low.NodeReference[*lowbase.SchemaProxy]{
|
||||
@@ -1192,8 +1133,7 @@ unevaluatedProperties: true
|
||||
`
|
||||
highSchema := getHighSchema(t, yml)
|
||||
|
||||
value := true
|
||||
assert.EqualValues(t, &value, highSchema.UnevaluatedProperties.B)
|
||||
assert.True(t, highSchema.UnevaluatedProperties.B)
|
||||
}
|
||||
|
||||
func TestUnevaluatedPropertiesBoolean_False(t *testing.T) {
|
||||
@@ -1203,6 +1143,147 @@ unevaluatedProperties: false
|
||||
`
|
||||
highSchema := getHighSchema(t, yml)
|
||||
|
||||
value := false
|
||||
assert.EqualValues(t, &value, highSchema.UnevaluatedProperties.B)
|
||||
assert.False(t, highSchema.UnevaluatedProperties.B)
|
||||
}
|
||||
|
||||
func TestUnevaluatedPropertiesBoolean_Unset(t *testing.T) {
|
||||
yml := `
|
||||
type: number
|
||||
`
|
||||
highSchema := getHighSchema(t, yml)
|
||||
|
||||
assert.Nil(t, highSchema.UnevaluatedProperties)
|
||||
}
|
||||
|
||||
func TestAdditionalProperties(t *testing.T) {
|
||||
testSpec := `type: object
|
||||
properties:
|
||||
additionalPropertiesSimpleSchema:
|
||||
type: object
|
||||
additionalProperties:
|
||||
type: string
|
||||
additionalPropertiesBool:
|
||||
type: object
|
||||
additionalProperties: true
|
||||
additionalPropertiesAnyOf:
|
||||
type: object
|
||||
additionalProperties:
|
||||
anyOf:
|
||||
- type: string
|
||||
- type: array
|
||||
items:
|
||||
type: string
|
||||
`
|
||||
|
||||
var compNode yaml.Node
|
||||
_ = yaml.Unmarshal([]byte(testSpec), &compNode)
|
||||
|
||||
sp := new(lowbase.SchemaProxy)
|
||||
err := sp.Build(context.Background(), nil, compNode.Content[0], nil)
|
||||
assert.NoError(t, err)
|
||||
|
||||
lowproxy := low.NodeReference[*lowbase.SchemaProxy]{
|
||||
Value: sp,
|
||||
ValueNode: compNode.Content[0],
|
||||
}
|
||||
|
||||
schemaProxy := NewSchemaProxy(&lowproxy)
|
||||
compiled := schemaProxy.Schema()
|
||||
|
||||
assert.Equal(t, []string{"string"}, compiled.Properties.GetOrZero("additionalPropertiesSimpleSchema").Schema().AdditionalProperties.A.Schema().Type)
|
||||
assert.Equal(t, true, compiled.Properties.GetOrZero("additionalPropertiesBool").Schema().AdditionalProperties.B)
|
||||
assert.Equal(t, []string{"string"}, compiled.Properties.GetOrZero("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(context.Background(), 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(context.Background(), 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(context.Background(), 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)))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user