1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
|
package openapi3
import (
"context"
"fmt"
"testing"
"github.com/stretchr/testify/require"
)
func TestIssue376(t *testing.T) {
spec := []byte(`
openapi: 3.0.0
components:
schemas:
schema1:
type: object
additionalProperties:
type: string
schema2:
type: object
properties:
prop:
$ref: '#/components/schemas/schema1/additionalProperties'
paths: {}
info:
title: An API
version: 1.2.3.4
`)
loader := NewLoader()
doc, err := loader.LoadFromData(spec)
require.NoError(t, err)
err = doc.Validate(loader.Context)
require.NoError(t, err)
require.Equal(t, "An API", doc.Info.Title)
require.Equal(t, 2, len(doc.Components.Schemas))
require.Equal(t, 0, doc.Paths.Len())
require.Equal(t, &Types{"string"}, doc.Components.Schemas["schema2"].Value.Properties["prop"].Value.Type)
}
func TestExclusiveValuesOfValuesAdditionalProperties(t *testing.T) {
schema := &Schema{
AdditionalProperties: AdditionalProperties{
Has: BoolPtr(false),
Schema: NewSchemaRef("", &Schema{}),
},
}
err := schema.Validate(context.Background())
require.ErrorContains(t, err, ` to both `)
schema = &Schema{
AdditionalProperties: AdditionalProperties{
Has: BoolPtr(false),
},
}
err = schema.Validate(context.Background())
require.NoError(t, err)
schema = &Schema{
AdditionalProperties: AdditionalProperties{
Schema: NewSchemaRef("", &Schema{}),
},
}
err = schema.Validate(context.Background())
require.NoError(t, err)
}
func TestMultijsonTagSerialization(t *testing.T) {
specYAML := []byte(`
openapi: 3.0.0
components:
schemas:
unset:
type: number
empty-object:
additionalProperties: {}
object:
additionalProperties: {type: string}
boolean:
additionalProperties: false
paths: {}
info:
title: An API
version: 1.2.3.4
`)
specJSON := []byte(`{
"openapi": "3.0.0",
"components": {
"schemas": {
"unset": {
"type": "number"
},
"empty-object": {
"additionalProperties": {
}
},
"object": {
"additionalProperties": {
"type": "string"
}
},
"boolean": {
"additionalProperties": false
}
}
},
"paths": {
},
"info": {
"title": "An API",
"version": "1.2.3.4"
}
}`)
for i, spec := range [][]byte{specJSON, specYAML} {
t.Run(fmt.Sprintf("spec%02d", i), func(t *testing.T) {
loader := NewLoader()
doc, err := loader.LoadFromData(spec)
require.NoError(t, err)
err = doc.Validate(loader.Context)
require.NoError(t, err)
for propName, propSchema := range doc.Components.Schemas {
t.Run(propName, func(t *testing.T) {
ap := propSchema.Value.AdditionalProperties.Schema
apa := propSchema.Value.AdditionalProperties.Has
apStr := ""
if ap != nil {
apStr = fmt.Sprintf("{Ref:%s Value.Type:%v}", (*ap).Ref, (*ap).Value.Type)
}
apaStr := ""
if apa != nil {
apaStr = fmt.Sprintf("%v", *apa)
}
encoded, err := propSchema.MarshalJSON()
require.NoError(t, err)
require.Equal(t, map[string]string{
"unset": `{"type":"number"}`,
"empty-object": `{"additionalProperties":{}}`,
"object": `{"additionalProperties":{"type":"string"}}`,
"boolean": `{"additionalProperties":false}`,
}[propName], string(encoded))
if propName == "unset" {
require.True(t, ap == nil && apa == nil)
return
}
require.Truef(t, (ap != nil && apa == nil) || (ap == nil && apa != nil),
"%s: isnil(%s) xor isnil(%s)", propName, apaStr, apStr)
})
}
})
}
}
|