1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251
|
// Copyright 2015 go-swagger maintainers
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package swag
import (
"reflect"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
type testNameStruct struct {
Name string `json:"name"`
NotTheSame int64 `json:"plain"`
Ignored string `json:"-"`
}
func TestNameProvider(t *testing.T) {
provider := NewNameProvider()
var obj = testNameStruct{}
nm, ok := provider.GetGoName(obj, "name")
assert.True(t, ok)
assert.Equal(t, "Name", nm)
nm, ok = provider.GetGoName(obj, "plain")
assert.True(t, ok)
assert.Equal(t, "NotTheSame", nm)
nm, ok = provider.GetGoName(obj, "doesNotExist")
assert.False(t, ok)
assert.Empty(t, nm)
nm, ok = provider.GetGoName(obj, "ignored")
assert.False(t, ok)
assert.Empty(t, nm)
tpe := reflect.TypeOf(obj)
nm, ok = provider.GetGoNameForType(tpe, "name")
assert.True(t, ok)
assert.Equal(t, "Name", nm)
nm, ok = provider.GetGoNameForType(tpe, "plain")
assert.True(t, ok)
assert.Equal(t, "NotTheSame", nm)
nm, ok = provider.GetGoNameForType(tpe, "doesNotExist")
assert.False(t, ok)
assert.Empty(t, nm)
nm, ok = provider.GetGoNameForType(tpe, "ignored")
assert.False(t, ok)
assert.Empty(t, nm)
ptr := &obj
nm, ok = provider.GetGoName(ptr, "name")
assert.True(t, ok)
assert.Equal(t, "Name", nm)
nm, ok = provider.GetGoName(ptr, "plain")
assert.True(t, ok)
assert.Equal(t, "NotTheSame", nm)
nm, ok = provider.GetGoName(ptr, "doesNotExist")
assert.False(t, ok)
assert.Empty(t, nm)
nm, ok = provider.GetGoName(ptr, "ignored")
assert.False(t, ok)
assert.Empty(t, nm)
nm, ok = provider.GetJSONName(obj, "Name")
assert.True(t, ok)
assert.Equal(t, "name", nm)
nm, ok = provider.GetJSONName(obj, "NotTheSame")
assert.True(t, ok)
assert.Equal(t, "plain", nm)
nm, ok = provider.GetJSONName(obj, "DoesNotExist")
assert.False(t, ok)
assert.Empty(t, nm)
nm, ok = provider.GetJSONName(obj, "Ignored")
assert.False(t, ok)
assert.Empty(t, nm)
nm, ok = provider.GetJSONNameForType(tpe, "Name")
assert.True(t, ok)
assert.Equal(t, "name", nm)
nm, ok = provider.GetJSONNameForType(tpe, "NotTheSame")
assert.True(t, ok)
assert.Equal(t, "plain", nm)
nm, ok = provider.GetJSONNameForType(tpe, "doesNotExist")
assert.False(t, ok)
assert.Empty(t, nm)
nm, ok = provider.GetJSONNameForType(tpe, "Ignored")
assert.False(t, ok)
assert.Empty(t, nm)
nm, ok = provider.GetJSONName(ptr, "Name")
assert.True(t, ok)
assert.Equal(t, "name", nm)
nm, ok = provider.GetJSONName(ptr, "NotTheSame")
assert.True(t, ok)
assert.Equal(t, "plain", nm)
nm, ok = provider.GetJSONName(ptr, "doesNotExist")
assert.False(t, ok)
assert.Empty(t, nm)
nm, ok = provider.GetJSONName(ptr, "Ignored")
assert.False(t, ok)
assert.Empty(t, nm)
nms := provider.GetJSONNames(ptr)
assert.Len(t, nms, 2)
assert.Len(t, provider.index, 1)
}
//nolint:testifylint
func TestJSONConcatenation(t *testing.T) {
// we require an exact assertion (with ordering), not just JSON equivalence. Hence: testifylint disabled.
assert.Nil(t, ConcatJSON())
assert.Equal(t, ConcatJSON([]byte(`{"id":1}`)), []byte(`{"id":1}`))
assert.Equal(t, ConcatJSON([]byte(`{}`), []byte(`{}`)), []byte(`{}`))
assert.Equal(t, ConcatJSON([]byte(`[]`), []byte(`[]`)), []byte(`[]`))
assert.Equal(t, ConcatJSON([]byte(`{"id":1}`), []byte(`{"name":"Rachel"}`)), []byte(`{"id":1,"name":"Rachel"}`))
assert.Equal(t, ConcatJSON([]byte(`[{"id":1}]`), []byte(`[{"name":"Rachel"}]`)), []byte(`[{"id":1},{"name":"Rachel"}]`))
assert.Equal(t, ConcatJSON([]byte(`{}`), []byte(`{"name":"Rachel"}`)), []byte(`{"name":"Rachel"}`))
assert.Equal(t, ConcatJSON([]byte(`[]`), []byte(`[{"name":"Rachel"}]`)), []byte(`[{"name":"Rachel"}]`))
assert.Equal(t, ConcatJSON([]byte(`{"id":1}`), []byte(`{}`)), []byte(`{"id":1}`))
assert.Equal(t, ConcatJSON([]byte(`[{"id":1}]`), []byte(`[]`)), []byte(`[{"id":1}]`))
assert.Equal(t, ConcatJSON([]byte(`{}`), []byte(`{}`), []byte(`{}`)), []byte(`{}`))
assert.Equal(t, ConcatJSON([]byte(`[]`), []byte(`[]`), []byte(`[]`)), []byte(`[]`))
assert.Equal(t, ConcatJSON([]byte(`{"id":1}`), []byte(`{"name":"Rachel"}`), []byte(`{"age":32}`)), []byte(`{"id":1,"name":"Rachel","age":32}`))
assert.Equal(t, ConcatJSON([]byte(`[{"id":1}]`), []byte(`[{"name":"Rachel"}]`), []byte(`[{"age":32}]`)), []byte(`[{"id":1},{"name":"Rachel"},{"age":32}]`))
assert.Equal(t, ConcatJSON([]byte(`{}`), []byte(`{"name":"Rachel"}`), []byte(`{"age":32}`)), []byte(`{"name":"Rachel","age":32}`))
assert.Equal(t, ConcatJSON([]byte(`[]`), []byte(`[{"name":"Rachel"}]`), []byte(`[{"age":32}]`)), []byte(`[{"name":"Rachel"},{"age":32}]`))
assert.Equal(t, ConcatJSON([]byte(`{"id":1}`), []byte(`{}`), []byte(`{"age":32}`)), []byte(`{"id":1,"age":32}`))
assert.Equal(t, ConcatJSON([]byte(`[{"id":1}]`), []byte(`[]`), []byte(`[{"age":32}]`)), []byte(`[{"id":1},{"age":32}]`))
assert.Equal(t, ConcatJSON([]byte(`{"id":1}`), []byte(`{"name":"Rachel"}`), []byte(`{}`)), []byte(`{"id":1,"name":"Rachel"}`))
assert.Equal(t, ConcatJSON([]byte(`[{"id":1}]`), []byte(`[{"name":"Rachel"}]`), []byte(`[]`)), []byte(`[{"id":1},{"name":"Rachel"}]`))
// add test on null
assert.Equal(t, ConcatJSON([]byte(nil)), []byte(nil))
assert.Equal(t, ConcatJSON([]byte(`null`)), []byte(nil))
assert.Equal(t, ConcatJSON([]byte(nil), []byte(`null`)), []byte(nil))
assert.Equal(t, ConcatJSON([]byte(`{"id":null}`), []byte(`null`)), []byte(`{"id":null}`))
assert.Equal(t, ConcatJSON([]byte(`{"id":null}`), []byte(`null`), []byte(`{"name":"Rachel"}`)), []byte(`{"id":null,"name":"Rachel"}`))
}
type SharedCounters struct {
Counter1 int64 `json:"counter1,omitempty"`
Counter2 int64 `json:"counter2:,omitempty"`
}
type AggregationObject struct {
SharedCounters
Count int64 `json:"count,omitempty"`
}
func (m *AggregationObject) UnmarshalJSON(raw []byte) error {
// AO0
var aO0 SharedCounters
if err := ReadJSON(raw, &aO0); err != nil {
return err
}
m.SharedCounters = aO0
// now for regular properties
var propsAggregationObject struct {
Count int64 `json:"count,omitempty"`
}
if err := ReadJSON(raw, &propsAggregationObject); err != nil {
return err
}
m.Count = propsAggregationObject.Count
return nil
}
// MarshalJSON marshals this object to a JSON structure
func (m AggregationObject) MarshalJSON() ([]byte, error) {
_parts := make([][]byte, 0, 1)
aO0, err := WriteJSON(m.SharedCounters)
if err != nil {
return nil, err
}
_parts = append(_parts, aO0)
// now for regular properties
var propsAggregationObject struct {
Count int64 `json:"count,omitempty"`
}
propsAggregationObject.Count = m.Count
jsonDataPropsAggregationObject, errAggregationObject := WriteJSON(propsAggregationObject)
if errAggregationObject != nil {
return nil, errAggregationObject
}
_parts = append(_parts, jsonDataPropsAggregationObject)
return ConcatJSON(_parts...), nil
}
func TestIssue2350(t *testing.T) {
obj := AggregationObject{Count: 290, SharedCounters: SharedCounters{Counter1: 304, Counter2: 948}}
rtjson, err := WriteJSON(obj)
require.NoError(t, err)
otjson, err := obj.MarshalJSON()
require.NoError(t, err)
require.JSONEq(t, string(rtjson), string(otjson))
var obj1 AggregationObject
require.NoError(t, ReadJSON(rtjson, &obj1))
require.Equal(t, obj, obj1)
var obj11 AggregationObject
require.NoError(t, obj11.UnmarshalJSON(rtjson))
require.Equal(t, obj, obj11)
jsons := `{"counter1":123,"counter2:":456,"count":999}`
var obj2 AggregationObject
require.NoError(t, ReadJSON([]byte(jsons), &obj2))
require.Equal(t, AggregationObject{SharedCounters: SharedCounters{Counter1: 123, Counter2: 456}, Count: 999}, obj2)
}
|