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