1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package spec
16
17 import (
18 "encoding/json"
19 "testing"
20
21 "github.com/go-openapi/swag"
22 "github.com/stretchr/testify/assert"
23 "github.com/stretchr/testify/require"
24 )
25
26 var schema = Schema{
27 VendorExtensible: VendorExtensible{Extensions: map[string]interface{}{"x-framework": "go-swagger"}},
28 SchemaProps: SchemaProps{
29 Ref: MustCreateRef("Cat"),
30 Type: []string{"string"},
31 Format: "date",
32 Description: "the description of this schema",
33 Title: "the title",
34 Default: "blah",
35 Maximum: float64Ptr(100),
36 ExclusiveMaximum: true,
37 ExclusiveMinimum: true,
38 Minimum: float64Ptr(5),
39 MaxLength: int64Ptr(100),
40 MinLength: int64Ptr(5),
41 Pattern: "\\w{1,5}\\w+",
42 MaxItems: int64Ptr(100),
43 MinItems: int64Ptr(5),
44 UniqueItems: true,
45 MultipleOf: float64Ptr(5),
46 Enum: []interface{}{"hello", "world"},
47 MaxProperties: int64Ptr(5),
48 MinProperties: int64Ptr(1),
49 Required: []string{"id", "name"},
50 Items: &SchemaOrArray{Schema: &Schema{SchemaProps: SchemaProps{Type: []string{"string"}}}},
51 AllOf: []Schema{{SchemaProps: SchemaProps{Type: []string{"string"}}}},
52 Properties: map[string]Schema{
53 "id": {SchemaProps: SchemaProps{Type: []string{"integer"}, Format: "int64"}},
54 "name": {SchemaProps: SchemaProps{Type: []string{"string"}}},
55 },
56 AdditionalProperties: &SchemaOrBool{Allows: true, Schema: &Schema{SchemaProps: SchemaProps{
57 Type: []string{"integer"},
58 Format: "int32",
59 }}},
60 },
61 SwaggerSchemaProps: SwaggerSchemaProps{
62 Discriminator: "not this",
63 ReadOnly: true,
64 XML: &XMLObject{Name: "sch", Namespace: "io", Prefix: "sw", Attribute: true, Wrapped: true},
65 ExternalDocs: &ExternalDocumentation{
66 Description: "the documentation etc",
67 URL: "http://readthedocs.org/swagger",
68 },
69 Example: []interface{}{
70 map[string]interface{}{
71 "id": 1,
72 "name": "a book",
73 },
74 map[string]interface{}{
75 "id": 2,
76 "name": "the thing",
77 },
78 },
79 },
80 }
81
82 var schemaJSON = `{
83 "x-framework": "go-swagger",
84 "$ref": "Cat",
85 "description": "the description of this schema",
86 "maximum": 100,
87 "minimum": 5,
88 "exclusiveMaximum": true,
89 "exclusiveMinimum": true,
90 "maxLength": 100,
91 "minLength": 5,
92 "pattern": "\\w{1,5}\\w+",
93 "maxItems": 100,
94 "minItems": 5,
95 "uniqueItems": true,
96 "multipleOf": 5,
97 "enum": ["hello", "world"],
98 "type": "string",
99 "format": "date",
100 "title": "the title",
101 "default": "blah",
102 "maxProperties": 5,
103 "minProperties": 1,
104 "required": ["id", "name"],
105 "items": {
106 "type": "string"
107 },
108 "allOf": [
109 {
110 "type": "string"
111 }
112 ],
113 "properties": {
114 "id": {
115 "type": "integer",
116 "format": "int64"
117 },
118 "name": {
119 "type": "string"
120 }
121 },
122 "discriminator": "not this",
123 "readOnly": true,
124 "xml": {
125 "name": "sch",
126 "namespace": "io",
127 "prefix": "sw",
128 "wrapped": true,
129 "attribute": true
130 },
131 "externalDocs": {
132 "description": "the documentation etc",
133 "url": "http://readthedocs.org/swagger"
134 },
135 "example": [
136 {
137 "id": 1,
138 "name": "a book"
139 },
140 {
141 "id": 2,
142 "name": "the thing"
143 }
144 ],
145 "additionalProperties": {
146 "type": "integer",
147 "format": "int32"
148 }
149 }
150 `
151
152 func TestSchema(t *testing.T) {
153
154 expected := map[string]interface{}{}
155 _ = json.Unmarshal([]byte(schemaJSON), &expected)
156 b, err := json.Marshal(schema)
157 require.NoError(t, err)
158
159 var actual map[string]interface{}
160 require.NoError(t, json.Unmarshal(b, &actual))
161 assert.Equal(t, expected, actual)
162
163 actual2 := Schema{}
164 require.NoError(t, json.Unmarshal([]byte(schemaJSON), &actual2))
165
166 assert.Equal(t, schema.Ref, actual2.Ref)
167 assert.Equal(t, schema.Description, actual2.Description)
168 assert.Equal(t, schema.Maximum, actual2.Maximum)
169 assert.Equal(t, schema.Minimum, actual2.Minimum)
170 assert.Equal(t, schema.ExclusiveMinimum, actual2.ExclusiveMinimum)
171 assert.Equal(t, schema.ExclusiveMaximum, actual2.ExclusiveMaximum)
172 assert.Equal(t, schema.MaxLength, actual2.MaxLength)
173 assert.Equal(t, schema.MinLength, actual2.MinLength)
174 assert.Equal(t, schema.Pattern, actual2.Pattern)
175 assert.Equal(t, schema.MaxItems, actual2.MaxItems)
176 assert.Equal(t, schema.MinItems, actual2.MinItems)
177 assert.True(t, actual2.UniqueItems)
178 assert.Equal(t, schema.MultipleOf, actual2.MultipleOf)
179 assert.Equal(t, schema.Enum, actual2.Enum)
180 assert.Equal(t, schema.Type, actual2.Type)
181 assert.Equal(t, schema.Format, actual2.Format)
182 assert.Equal(t, schema.Title, actual2.Title)
183 assert.Equal(t, schema.MaxProperties, actual2.MaxProperties)
184 assert.Equal(t, schema.MinProperties, actual2.MinProperties)
185 assert.Equal(t, schema.Required, actual2.Required)
186 assert.Equal(t, schema.Items, actual2.Items)
187 assert.Equal(t, schema.AllOf, actual2.AllOf)
188 assert.Equal(t, schema.Properties, actual2.Properties)
189 assert.Equal(t, schema.Discriminator, actual2.Discriminator)
190 assert.Equal(t, schema.ReadOnly, actual2.ReadOnly)
191 assert.Equal(t, schema.XML, actual2.XML)
192 assert.Equal(t, schema.ExternalDocs, actual2.ExternalDocs)
193 assert.Equal(t, schema.AdditionalProperties, actual2.AdditionalProperties)
194 assert.Equal(t, schema.Extensions, actual2.Extensions)
195
196 examples := actual2.Example.([]interface{})
197 expEx := schema.Example.([]interface{})
198 ex1 := examples[0].(map[string]interface{})
199 ex2 := examples[1].(map[string]interface{})
200 exp1 := expEx[0].(map[string]interface{})
201 exp2 := expEx[1].(map[string]interface{})
202
203 assert.EqualValues(t, exp1["id"], ex1["id"])
204 assert.Equal(t, exp1["name"], ex1["name"])
205 assert.EqualValues(t, exp2["id"], ex2["id"])
206 assert.Equal(t, exp2["name"], ex2["name"])
207 }
208
209 func BenchmarkSchemaUnmarshal(b *testing.B) {
210 for i := 0; i < b.N; i++ {
211 sch := &Schema{}
212 _ = sch.UnmarshalJSON([]byte(schemaJSON))
213 }
214 }
215
216 func TestSchemaWithValidation(t *testing.T) {
217 s := new(Schema).WithValidations(SchemaValidations{CommonValidations: CommonValidations{MaxLength: swag.Int64(15)}})
218 assert.EqualValues(t, swag.Int64(15), s.MaxLength)
219
220 val := mkVal()
221 s = new(Schema).WithValidations(val)
222
223 assert.EqualValues(t, val, s.Validations())
224 }
225
View as plain text