1
16
17 package v1beta1
18
19 import (
20 "encoding/json"
21 "reflect"
22 "testing"
23 )
24
25 type JSONSchemaPropsOrBoolHolder struct {
26 JSPoB JSONSchemaPropsOrBool `json:"val1"`
27 JSPoBOmitEmpty *JSONSchemaPropsOrBool `json:"val2,omitempty"`
28 }
29
30 func TestJSONSchemaPropsOrBoolUnmarshalJSON(t *testing.T) {
31 cases := []struct {
32 input string
33 result JSONSchemaPropsOrBoolHolder
34 }{
35 {`{}`, JSONSchemaPropsOrBoolHolder{}},
36
37 {`{"val1": {}}`, JSONSchemaPropsOrBoolHolder{JSPoB: JSONSchemaPropsOrBool{Allows: true, Schema: &JSONSchemaProps{}}}},
38 {`{"val1": {"type":"string"}}`, JSONSchemaPropsOrBoolHolder{JSPoB: JSONSchemaPropsOrBool{Allows: true, Schema: &JSONSchemaProps{Type: "string"}}}},
39 {`{"val1": false}`, JSONSchemaPropsOrBoolHolder{JSPoB: JSONSchemaPropsOrBool{}}},
40 {`{"val1": true}`, JSONSchemaPropsOrBoolHolder{JSPoB: JSONSchemaPropsOrBool{Allows: true}}},
41
42 {`{"val2": {}}`, JSONSchemaPropsOrBoolHolder{JSPoBOmitEmpty: &JSONSchemaPropsOrBool{Allows: true, Schema: &JSONSchemaProps{}}}},
43 {`{"val2": {"type":"string"}}`, JSONSchemaPropsOrBoolHolder{JSPoBOmitEmpty: &JSONSchemaPropsOrBool{Allows: true, Schema: &JSONSchemaProps{Type: "string"}}}},
44 {`{"val2": false}`, JSONSchemaPropsOrBoolHolder{JSPoBOmitEmpty: &JSONSchemaPropsOrBool{}}},
45 {`{"val2": true}`, JSONSchemaPropsOrBoolHolder{JSPoBOmitEmpty: &JSONSchemaPropsOrBool{Allows: true}}},
46 }
47
48 for _, c := range cases {
49 var result JSONSchemaPropsOrBoolHolder
50 if err := json.Unmarshal([]byte(c.input), &result); err != nil {
51 t.Errorf("Failed to unmarshal input '%v': %v", c.input, err)
52 }
53 if !reflect.DeepEqual(result, c.result) {
54 t.Errorf("Failed to unmarshal input '%v': expected %+v, got %+v", c.input, c.result, result)
55 }
56 }
57 }
58
59 func TestStringArrayOrStringMarshalJSON(t *testing.T) {
60 cases := []struct {
61 input JSONSchemaPropsOrBoolHolder
62 result string
63 }{
64 {JSONSchemaPropsOrBoolHolder{}, `{"val1":false}`},
65
66 {JSONSchemaPropsOrBoolHolder{JSPoB: JSONSchemaPropsOrBool{Schema: &JSONSchemaProps{}}}, `{"val1":{}}`},
67 {JSONSchemaPropsOrBoolHolder{JSPoB: JSONSchemaPropsOrBool{Schema: &JSONSchemaProps{Type: "string"}}}, `{"val1":{"type":"string"}}`},
68 {JSONSchemaPropsOrBoolHolder{JSPoB: JSONSchemaPropsOrBool{}}, `{"val1":false}`},
69 {JSONSchemaPropsOrBoolHolder{JSPoB: JSONSchemaPropsOrBool{Allows: true}}, `{"val1":true}`},
70
71 {JSONSchemaPropsOrBoolHolder{JSPoBOmitEmpty: &JSONSchemaPropsOrBool{Schema: &JSONSchemaProps{}}}, `{"val1":false,"val2":{}}`},
72 {JSONSchemaPropsOrBoolHolder{JSPoBOmitEmpty: &JSONSchemaPropsOrBool{Schema: &JSONSchemaProps{Type: "string"}}}, `{"val1":false,"val2":{"type":"string"}}`},
73 {JSONSchemaPropsOrBoolHolder{JSPoBOmitEmpty: &JSONSchemaPropsOrBool{}}, `{"val1":false,"val2":false}`},
74 {JSONSchemaPropsOrBoolHolder{JSPoBOmitEmpty: &JSONSchemaPropsOrBool{Allows: true}}, `{"val1":false,"val2":true}`},
75 }
76
77 for _, c := range cases {
78 result, err := json.Marshal(&c.input)
79 if err != nil {
80 t.Errorf("Unexpected error marshaling input '%v': %v", c.input, err)
81 }
82 if string(result) != c.result {
83 t.Errorf("Failed to marshal input '%v': expected: %q, got %q", c.input, c.result, string(result))
84 }
85 }
86 }
87
88 type JSONSchemaPropsOrArrayHolder struct {
89 JSPoA JSONSchemaPropsOrArray `json:"val1"`
90 JSPoAOmitEmpty *JSONSchemaPropsOrArray `json:"val2,omitempty"`
91 }
92
93 func TestJSONSchemaPropsOrArrayUnmarshalJSON(t *testing.T) {
94 cases := []struct {
95 input string
96 result JSONSchemaPropsOrArrayHolder
97 }{
98 {`{}`, JSONSchemaPropsOrArrayHolder{}},
99
100 {`{"val1": {}}`, JSONSchemaPropsOrArrayHolder{JSPoA: JSONSchemaPropsOrArray{Schema: &JSONSchemaProps{}}}},
101 {`{"val1": {"type":"string"}}`, JSONSchemaPropsOrArrayHolder{JSPoA: JSONSchemaPropsOrArray{Schema: &JSONSchemaProps{Type: "string"}}}},
102 {`{"val1": [{}]}`, JSONSchemaPropsOrArrayHolder{JSPoA: JSONSchemaPropsOrArray{JSONSchemas: []JSONSchemaProps{{}}}}},
103 {`{"val1": [{},{"type":"string"}]}`, JSONSchemaPropsOrArrayHolder{JSPoA: JSONSchemaPropsOrArray{JSONSchemas: []JSONSchemaProps{{}, {Type: "string"}}}}},
104
105 {`{"val2": {}}`, JSONSchemaPropsOrArrayHolder{JSPoAOmitEmpty: &JSONSchemaPropsOrArray{Schema: &JSONSchemaProps{}}}},
106 {`{"val2": {"type":"string"}}`, JSONSchemaPropsOrArrayHolder{JSPoAOmitEmpty: &JSONSchemaPropsOrArray{Schema: &JSONSchemaProps{Type: "string"}}}},
107 {`{"val2": [{}]}`, JSONSchemaPropsOrArrayHolder{JSPoAOmitEmpty: &JSONSchemaPropsOrArray{JSONSchemas: []JSONSchemaProps{{}}}}},
108 {`{"val2": [{},{"type":"string"}]}`, JSONSchemaPropsOrArrayHolder{JSPoAOmitEmpty: &JSONSchemaPropsOrArray{JSONSchemas: []JSONSchemaProps{{}, {Type: "string"}}}}},
109 }
110
111 for _, c := range cases {
112 var result JSONSchemaPropsOrArrayHolder
113 if err := json.Unmarshal([]byte(c.input), &result); err != nil {
114 t.Errorf("Failed to unmarshal input '%v': %v", c.input, err)
115 }
116 if !reflect.DeepEqual(result, c.result) {
117 t.Errorf("Failed to unmarshal input '%v': expected %+v, got %+v", c.input, c.result, result)
118 }
119 }
120 }
121
122 func TestJSONSchemaPropsOrArrayMarshalJSON(t *testing.T) {
123 cases := []struct {
124 input JSONSchemaPropsOrArrayHolder
125 result string
126 }{
127 {JSONSchemaPropsOrArrayHolder{}, `{"val1":null}`},
128
129 {JSONSchemaPropsOrArrayHolder{JSPoA: JSONSchemaPropsOrArray{Schema: &JSONSchemaProps{}}}, `{"val1":{}}`},
130 {JSONSchemaPropsOrArrayHolder{JSPoA: JSONSchemaPropsOrArray{Schema: &JSONSchemaProps{Type: "string"}}}, `{"val1":{"type":"string"}}`},
131 {JSONSchemaPropsOrArrayHolder{JSPoA: JSONSchemaPropsOrArray{JSONSchemas: []JSONSchemaProps{{}}}}, `{"val1":[{}]}`},
132 {JSONSchemaPropsOrArrayHolder{JSPoA: JSONSchemaPropsOrArray{JSONSchemas: []JSONSchemaProps{{}, {Type: "string"}}}}, `{"val1":[{},{"type":"string"}]}`},
133
134 {JSONSchemaPropsOrArrayHolder{JSPoAOmitEmpty: &JSONSchemaPropsOrArray{}}, `{"val1":null,"val2":null}`},
135 {JSONSchemaPropsOrArrayHolder{JSPoAOmitEmpty: &JSONSchemaPropsOrArray{Schema: &JSONSchemaProps{}}}, `{"val1":null,"val2":{}}`},
136 {JSONSchemaPropsOrArrayHolder{JSPoAOmitEmpty: &JSONSchemaPropsOrArray{Schema: &JSONSchemaProps{Type: "string"}}}, `{"val1":null,"val2":{"type":"string"}}`},
137 {JSONSchemaPropsOrArrayHolder{JSPoAOmitEmpty: &JSONSchemaPropsOrArray{JSONSchemas: []JSONSchemaProps{{}}}}, `{"val1":null,"val2":[{}]}`},
138 {JSONSchemaPropsOrArrayHolder{JSPoAOmitEmpty: &JSONSchemaPropsOrArray{JSONSchemas: []JSONSchemaProps{{}, {Type: "string"}}}}, `{"val1":null,"val2":[{},{"type":"string"}]}`},
139 }
140
141 for i, c := range cases {
142 result, err := json.Marshal(&c.input)
143 if err != nil {
144 t.Errorf("%d: Unexpected error marshaling input '%v': %v", i, c.input, err)
145 }
146 if string(result) != c.result {
147 t.Errorf("%d: Failed to marshal input '%v': expected: %q, got %q", i, c.input, c.result, string(result))
148 }
149 }
150 }
151
View as plain text