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