Swagger v2 high model at 100% coverage.

This commit is contained in:
Dave Shanley
2022-09-11 21:09:16 -04:00
parent 02633ad333
commit d2b974829d
6 changed files with 421 additions and 12 deletions

View File

@@ -5,7 +5,7 @@ package base
import (
"github.com/pb33f/libopenapi/datamodel/low"
v3 "github.com/pb33f/libopenapi/datamodel/low/base"
lowbase "github.com/pb33f/libopenapi/datamodel/low/base"
"github.com/pb33f/libopenapi/index"
"github.com/stretchr/testify/assert"
"gopkg.in/yaml.v3"
@@ -39,11 +39,11 @@ func TestNewSchemaProxy(t *testing.T) {
_ = yaml.Unmarshal([]byte(yml), &compNode)
sp := new(v3.SchemaProxy)
sp := new(lowbase.SchemaProxy)
err := sp.Build(compNode.Content[0], idx)
assert.NoError(t, err)
lowproxy := low.NodeReference[*v3.SchemaProxy]{
lowproxy := low.NodeReference[*lowbase.SchemaProxy]{
Value: sp,
ValueNode: idxNode.Content[0],
}
@@ -51,5 +51,264 @@ func TestNewSchemaProxy(t *testing.T) {
sch1 := SchemaProxy{schema: &lowproxy}
assert.Nil(t, sch1.Schema())
assert.Error(t, sch1.GetBuildError())
}
func TestNewSchemaProxy_WithObject(t *testing.T) {
testSpec := `type: object
description: something object
discriminator:
propertyName: athing
mapping:
log: cat
pizza: party
allOf:
- type: object
description: an allof thing
properties:
allOfA:
type: string
description: allOfA description
example: 'allOfAExp'
allOfB:
type: string
description: allOfB description
example: 'allOfBExp'
oneOf:
type: object
description: a oneof thing
properties:
oneOfA:
type: string
description: oneOfA description
example: 'oneOfAExp'
oneOfB:
type: string
description: oneOfB description
example: 'oneOfBExp'
anyOf:
type: object
description: an anyOf thing
properties:
anyOfA:
type: string
description: anyOfA description
example: 'anyOfAExp'
anyOfB:
type: string
description: anyOfB description
example: 'anyOfBExp'
not:
type: object
description: a not thing
properties:
notA:
type: string
description: notA description
example: 'notAExp'
notB:
type: string
description: notB description
example: 'notBExp'
items:
type: object
description: an items thing
properties:
itemsA:
type: string
description: itemsA description
example: 'itemsAExp'
itemsB:
type: string
description: itemsB description
example: 'itemsBExp'
properties:
somethingBee:
type: number
somethingThree:
type: number
somethingTwo:
type: number
somethingOne:
type: number
somethingA:
type: number
description: a number
example: 2
somethingB:
type: object
description: an object
externalDocs:
description: the best docs
url: https://pb33f.io
properties:
somethingBProp:
type: string
description: something b subprop
example: picnics are nice.
xml:
name: an xml thing
namespace: an xml namespace
prefix: a prefix
attribute: true
wrapped: false
x-pizza: love
additionalProperties:
why: yes
thatIs: true
additionalProperties: true
xml:
name: XML Thing
externalDocs:
url: https://pb33f.io/docs
enum: [fish, cake]
required: [cake, fish]`
var compNode yaml.Node
_ = yaml.Unmarshal([]byte(testSpec), &compNode)
sp := new(lowbase.SchemaProxy)
err := sp.Build(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.NotNil(t, compiled)
assert.Nil(t, schemaProxy.GetBuildError())
wentLow := compiled.GoLow()
assert.Equal(t, 102, wentLow.AdditionalProperties.ValueNode.Line)
}
func TestNewSchemaProxy_WithObject_FinishPoly(t *testing.T) {
testSpec := `type: object
description: something object
discriminator:
propertyName: athing
mapping:
log: cat
pizza: party
allOf:
- type: object
description: an allof thing
properties:
allOfA:
type: string
description: allOfA description
example: 'allOfAExp'
allOfB:
type: string
description: allOfB description
example: 'allOfBExp'
oneOf:
type: object
description: a oneof thing
properties:
oneOfA:
type: string
description: oneOfA description
example: 'oneOfAExp'
oneOfB:
type: string
description: oneOfB description
example: 'oneOfBExp'
anyOf:
type: object
description: an anyOf thing
properties:
anyOfA:
type: string
description: anyOfA description
example: 'anyOfAExp'
anyOfB:
type: string
description: anyOfB description
example: 'anyOfBExp'
not:
type: object
description: a not thing
properties:
notA:
type: string
description: notA description
example: 'notAExp'
notB:
type: string
description: notB description
example: 'notBExp'
items:
type: object
description: an items thing
properties:
itemsA:
type: string
description: itemsA description
example: 'itemsAExp'
itemsB:
type: string
description: itemsB description
example: 'itemsBExp'
properties:
somethingB:
type: object
description: an object
externalDocs:
description: the best docs
url: https://pb33f.io
properties:
somethingBProp:
type: string
description: something b subprop
example: picnics are nice.
xml:
name: an xml thing
namespace: an xml namespace
prefix: a prefix
attribute: true
wrapped: false
x-pizza: love
additionalProperties:
why: yes
thatIs: true
additionalProperties: true
xml:
name: XML Thing
externalDocs:
url: https://pb33f.io/docs
enum: [fish, cake]
required: [cake, fish]`
var compNode yaml.Node
_ = yaml.Unmarshal([]byte(testSpec), &compNode)
sp := new(lowbase.SchemaProxy)
err := sp.Build(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.NotNil(t, compiled)
assert.Nil(t, schemaProxy.GetBuildError())
wentLow := compiled.GoLow()
assert.Equal(t, 90, wentLow.AdditionalProperties.ValueNode.Line)
assert.Equal(t, 92, wentLow.XML.ValueNode.Line)
wentLower := compiled.XML.GoLow()
assert.Equal(t, 92, wentLower.Name.ValueNode.Line)
}