| 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 | |
| 15 | package spec |
| 16 | |
| 17 | import ( |
| 18 | "encoding/json" |
| 19 | "reflect" |
| 20 | "testing" |
| 21 | |
| 22 | "github.com/stretchr/testify/assert" |
| 23 | ) |
| 24 | |
| 25 | func assertSerializeJSON(t testing.TB, actual interface{}, expected string) bool { |
| 26 | ser, err := json.Marshal(actual) |
| 27 | if err != nil { |
| 28 | return assert.Fail(t, "unable to marshal to json (%s): %#v", err, actual) |
| 29 | } |
| 30 | return assert.Equal(t, string(ser), expected) |
| 31 | } |
| 32 | |
| 33 | func derefTypeOf(expected interface{}) (tpe reflect.Type) { |
| 34 | tpe = reflect.TypeOf(expected) |
| 35 | if tpe.Kind() == reflect.Ptr { |
| 36 | tpe = tpe.Elem() |
| 37 | } |
| 38 | return |
| 39 | } |
| 40 | |
| 41 | func isPointed(expected interface{}) (pointed bool) { |
| 42 | tpe := reflect.TypeOf(expected) |
| 43 | if tpe.Kind() == reflect.Ptr { |
| 44 | pointed = true |
| 45 | } |
| 46 | return |
| 47 | } |
| 48 | |
| 49 | func assertParsesJSON(t testing.TB, actual string, expected interface{}) bool { |
| 50 | parsed := reflect.New(derefTypeOf(expected)) |
| 51 | err := json.Unmarshal([]byte(actual), parsed.Interface()) |
| 52 | if err != nil { |
| 53 | return assert.Fail(t, "Failed to unmarshal JSON", "unable to unmarshal from json (%v): %s", err, actual) |
| 54 | } |
| 55 | act := parsed.Interface() |
| 56 | if !isPointed(expected) { |
| 57 | act = reflect.Indirect(parsed).Interface() |
| 58 | } |
| 59 | return assert.Equal(t, act, expected) |
| 60 | } |
| 61 | |
| 62 | func TestSerialization_SerializeJSON(t *testing.T) { |
| 63 | assertSerializeJSON(t, []string{"hello"}, "[\"hello\"]") |
| 64 | assertSerializeJSON(t, []string{"hello", "world", "and", "stuff"}, "[\"hello\",\"world\",\"and\",\"stuff\"]") |
| 65 | assertSerializeJSON(t, StringOrArray(nil), "null") |
| 66 | assertSerializeJSON(t, SchemaOrArray{ |
| 67 | Schemas: []Schema{ |
| 68 | {SchemaProps: SchemaProps{Type: []string{"string"}}}}, |
| 69 | }, "[{\"type\":\"string\"}]") |
| 70 | assertSerializeJSON(t, SchemaOrArray{ |
| 71 | Schemas: []Schema{ |
| 72 | {SchemaProps: SchemaProps{Type: []string{"string"}}}, |
| 73 | {SchemaProps: SchemaProps{Type: []string{"string"}}}, |
| 74 | }}, "[{\"type\":\"string\"},{\"type\":\"string\"}]") |
| 75 | assertSerializeJSON(t, SchemaOrArray{}, "null") |
| 76 | } |
| 77 | |
| 78 | func TestSerialization_DeserializeJSON(t *testing.T) { |
| 79 | // String |
| 80 | assertParsesJSON(t, "\"hello\"", StringOrArray([]string{"hello"})) |
| 81 | assertParsesJSON(t, "[\"hello\",\"world\",\"and\",\"stuff\"]", |
| 82 | StringOrArray([]string{"hello", "world", "and", "stuff"})) |
| 83 | assertParsesJSON(t, "[\"hello\",\"world\",null,\"stuff\"]", StringOrArray([]string{"hello", "world", "", "stuff"})) |
| 84 | assertParsesJSON(t, "null", StringOrArray(nil)) |
| 85 | |
| 86 | // Schema |
| 87 | assertParsesJSON(t, "{\"type\":\"string\"}", SchemaOrArray{Schema: &Schema{ |
| 88 | SchemaProps: SchemaProps{Type: []string{"string"}}}, |
| 89 | }) |
| 90 | assertParsesJSON(t, "[{\"type\":\"string\"},{\"type\":\"string\"}]", &SchemaOrArray{ |
| 91 | Schemas: []Schema{ |
| 92 | {SchemaProps: SchemaProps{Type: []string{"string"}}}, |
| 93 | {SchemaProps: SchemaProps{Type: []string{"string"}}}, |
| 94 | }, |
| 95 | }) |
| 96 | assertParsesJSON(t, "null", SchemaOrArray{}) |
| 97 | } |