...

Source file src/github.com/go-openapi/swag/json_test.go

Documentation: github.com/go-openapi/swag

     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 swag
    16  
    17  import (
    18  	"reflect"
    19  	"testing"
    20  
    21  	"github.com/stretchr/testify/assert"
    22  	"github.com/stretchr/testify/require"
    23  )
    24  
    25  type testNameStruct struct {
    26  	Name       string `json:"name"`
    27  	NotTheSame int64  `json:"plain"`
    28  	Ignored    string `json:"-"`
    29  }
    30  
    31  func TestNameProvider(t *testing.T) {
    32  
    33  	provider := NewNameProvider()
    34  
    35  	var obj = testNameStruct{}
    36  
    37  	nm, ok := provider.GetGoName(obj, "name")
    38  	assert.True(t, ok)
    39  	assert.Equal(t, "Name", nm)
    40  
    41  	nm, ok = provider.GetGoName(obj, "plain")
    42  	assert.True(t, ok)
    43  	assert.Equal(t, "NotTheSame", nm)
    44  
    45  	nm, ok = provider.GetGoName(obj, "doesNotExist")
    46  	assert.False(t, ok)
    47  	assert.Empty(t, nm)
    48  
    49  	nm, ok = provider.GetGoName(obj, "ignored")
    50  	assert.False(t, ok)
    51  	assert.Empty(t, nm)
    52  
    53  	tpe := reflect.TypeOf(obj)
    54  	nm, ok = provider.GetGoNameForType(tpe, "name")
    55  	assert.True(t, ok)
    56  	assert.Equal(t, "Name", nm)
    57  
    58  	nm, ok = provider.GetGoNameForType(tpe, "plain")
    59  	assert.True(t, ok)
    60  	assert.Equal(t, "NotTheSame", nm)
    61  
    62  	nm, ok = provider.GetGoNameForType(tpe, "doesNotExist")
    63  	assert.False(t, ok)
    64  	assert.Empty(t, nm)
    65  
    66  	nm, ok = provider.GetGoNameForType(tpe, "ignored")
    67  	assert.False(t, ok)
    68  	assert.Empty(t, nm)
    69  
    70  	ptr := &obj
    71  	nm, ok = provider.GetGoName(ptr, "name")
    72  	assert.True(t, ok)
    73  	assert.Equal(t, "Name", nm)
    74  
    75  	nm, ok = provider.GetGoName(ptr, "plain")
    76  	assert.True(t, ok)
    77  	assert.Equal(t, "NotTheSame", nm)
    78  
    79  	nm, ok = provider.GetGoName(ptr, "doesNotExist")
    80  	assert.False(t, ok)
    81  	assert.Empty(t, nm)
    82  
    83  	nm, ok = provider.GetGoName(ptr, "ignored")
    84  	assert.False(t, ok)
    85  	assert.Empty(t, nm)
    86  
    87  	nm, ok = provider.GetJSONName(obj, "Name")
    88  	assert.True(t, ok)
    89  	assert.Equal(t, "name", nm)
    90  
    91  	nm, ok = provider.GetJSONName(obj, "NotTheSame")
    92  	assert.True(t, ok)
    93  	assert.Equal(t, "plain", nm)
    94  
    95  	nm, ok = provider.GetJSONName(obj, "DoesNotExist")
    96  	assert.False(t, ok)
    97  	assert.Empty(t, nm)
    98  
    99  	nm, ok = provider.GetJSONName(obj, "Ignored")
   100  	assert.False(t, ok)
   101  	assert.Empty(t, nm)
   102  
   103  	nm, ok = provider.GetJSONNameForType(tpe, "Name")
   104  	assert.True(t, ok)
   105  	assert.Equal(t, "name", nm)
   106  
   107  	nm, ok = provider.GetJSONNameForType(tpe, "NotTheSame")
   108  	assert.True(t, ok)
   109  	assert.Equal(t, "plain", nm)
   110  
   111  	nm, ok = provider.GetJSONNameForType(tpe, "doesNotExist")
   112  	assert.False(t, ok)
   113  	assert.Empty(t, nm)
   114  
   115  	nm, ok = provider.GetJSONNameForType(tpe, "Ignored")
   116  	assert.False(t, ok)
   117  	assert.Empty(t, nm)
   118  
   119  	nm, ok = provider.GetJSONName(ptr, "Name")
   120  	assert.True(t, ok)
   121  	assert.Equal(t, "name", nm)
   122  
   123  	nm, ok = provider.GetJSONName(ptr, "NotTheSame")
   124  	assert.True(t, ok)
   125  	assert.Equal(t, "plain", nm)
   126  
   127  	nm, ok = provider.GetJSONName(ptr, "doesNotExist")
   128  	assert.False(t, ok)
   129  	assert.Empty(t, nm)
   130  
   131  	nm, ok = provider.GetJSONName(ptr, "Ignored")
   132  	assert.False(t, ok)
   133  	assert.Empty(t, nm)
   134  
   135  	nms := provider.GetJSONNames(ptr)
   136  	assert.Len(t, nms, 2)
   137  
   138  	assert.Len(t, provider.index, 1)
   139  
   140  }
   141  
   142  func TestJSONConcatenation(t *testing.T) {
   143  	assert.Nil(t, ConcatJSON())
   144  	assert.Equal(t, ConcatJSON([]byte(`{"id":1}`)), []byte(`{"id":1}`))
   145  	assert.Equal(t, ConcatJSON([]byte(`{}`), []byte(`{}`)), []byte(`{}`))
   146  	assert.Equal(t, ConcatJSON([]byte(`[]`), []byte(`[]`)), []byte(`[]`))
   147  	assert.Equal(t, ConcatJSON([]byte(`{"id":1}`), []byte(`{"name":"Rachel"}`)), []byte(`{"id":1,"name":"Rachel"}`))
   148  	assert.Equal(t, ConcatJSON([]byte(`[{"id":1}]`), []byte(`[{"name":"Rachel"}]`)), []byte(`[{"id":1},{"name":"Rachel"}]`))
   149  	assert.Equal(t, ConcatJSON([]byte(`{}`), []byte(`{"name":"Rachel"}`)), []byte(`{"name":"Rachel"}`))
   150  	assert.Equal(t, ConcatJSON([]byte(`[]`), []byte(`[{"name":"Rachel"}]`)), []byte(`[{"name":"Rachel"}]`))
   151  	assert.Equal(t, ConcatJSON([]byte(`{"id":1}`), []byte(`{}`)), []byte(`{"id":1}`))
   152  	assert.Equal(t, ConcatJSON([]byte(`[{"id":1}]`), []byte(`[]`)), []byte(`[{"id":1}]`))
   153  	assert.Equal(t, ConcatJSON([]byte(`{}`), []byte(`{}`), []byte(`{}`)), []byte(`{}`))
   154  	assert.Equal(t, ConcatJSON([]byte(`[]`), []byte(`[]`), []byte(`[]`)), []byte(`[]`))
   155  	assert.Equal(t, ConcatJSON([]byte(`{"id":1}`), []byte(`{"name":"Rachel"}`), []byte(`{"age":32}`)), []byte(`{"id":1,"name":"Rachel","age":32}`))
   156  	assert.Equal(t, ConcatJSON([]byte(`[{"id":1}]`), []byte(`[{"name":"Rachel"}]`), []byte(`[{"age":32}]`)), []byte(`[{"id":1},{"name":"Rachel"},{"age":32}]`))
   157  	assert.Equal(t, ConcatJSON([]byte(`{}`), []byte(`{"name":"Rachel"}`), []byte(`{"age":32}`)), []byte(`{"name":"Rachel","age":32}`))
   158  	assert.Equal(t, ConcatJSON([]byte(`[]`), []byte(`[{"name":"Rachel"}]`), []byte(`[{"age":32}]`)), []byte(`[{"name":"Rachel"},{"age":32}]`))
   159  	assert.Equal(t, ConcatJSON([]byte(`{"id":1}`), []byte(`{}`), []byte(`{"age":32}`)), []byte(`{"id":1,"age":32}`))
   160  	assert.Equal(t, ConcatJSON([]byte(`[{"id":1}]`), []byte(`[]`), []byte(`[{"age":32}]`)), []byte(`[{"id":1},{"age":32}]`))
   161  	assert.Equal(t, ConcatJSON([]byte(`{"id":1}`), []byte(`{"name":"Rachel"}`), []byte(`{}`)), []byte(`{"id":1,"name":"Rachel"}`))
   162  	assert.Equal(t, ConcatJSON([]byte(`[{"id":1}]`), []byte(`[{"name":"Rachel"}]`), []byte(`[]`)), []byte(`[{"id":1},{"name":"Rachel"}]`))
   163  
   164  	// add test on null
   165  	assert.Equal(t, ConcatJSON([]byte(nil)), []byte(nil))
   166  	assert.Equal(t, ConcatJSON([]byte(`null`)), []byte(nil))
   167  	assert.Equal(t, ConcatJSON([]byte(nil), []byte(`null`)), []byte(nil))
   168  	assert.Equal(t, ConcatJSON([]byte(`{"id":null}`), []byte(`null`)), []byte(`{"id":null}`))
   169  	assert.Equal(t, ConcatJSON([]byte(`{"id":null}`), []byte(`null`), []byte(`{"name":"Rachel"}`)), []byte(`{"id":null,"name":"Rachel"}`))
   170  }
   171  
   172  type SharedCounters struct {
   173  	Counter1 int64 `json:"counter1,omitempty"`
   174  	Counter2 int64 `json:"counter2:,omitempty"`
   175  }
   176  
   177  type AggregationObject struct {
   178  	SharedCounters
   179  	Count int64 `json:"count,omitempty"`
   180  }
   181  
   182  func (m *AggregationObject) UnmarshalJSON(raw []byte) error {
   183  	// AO0
   184  	var aO0 SharedCounters
   185  	if err := ReadJSON(raw, &aO0); err != nil {
   186  		return err
   187  	}
   188  	m.SharedCounters = aO0
   189  
   190  	// now for regular properties
   191  	var propsAggregationObject struct {
   192  		Count int64 `json:"count,omitempty"`
   193  	}
   194  	if err := ReadJSON(raw, &propsAggregationObject); err != nil {
   195  		return err
   196  	}
   197  	m.Count = propsAggregationObject.Count
   198  
   199  	return nil
   200  }
   201  
   202  // MarshalJSON marshals this object to a JSON structure
   203  func (m AggregationObject) MarshalJSON() ([]byte, error) {
   204  	_parts := make([][]byte, 0, 1)
   205  
   206  	aO0, err := WriteJSON(m.SharedCounters)
   207  	if err != nil {
   208  		return nil, err
   209  	}
   210  	_parts = append(_parts, aO0)
   211  
   212  	// now for regular properties
   213  	var propsAggregationObject struct {
   214  		Count int64 `json:"count,omitempty"`
   215  	}
   216  	propsAggregationObject.Count = m.Count
   217  
   218  	jsonDataPropsAggregationObject, errAggregationObject := WriteJSON(propsAggregationObject)
   219  	if errAggregationObject != nil {
   220  		return nil, errAggregationObject
   221  	}
   222  	_parts = append(_parts, jsonDataPropsAggregationObject)
   223  	return ConcatJSON(_parts...), nil
   224  }
   225  
   226  func TestIssue2350(t *testing.T) {
   227  	obj := AggregationObject{Count: 290, SharedCounters: SharedCounters{Counter1: 304, Counter2: 948}}
   228  
   229  	rtjson, err := WriteJSON(obj)
   230  	require.NoError(t, err)
   231  
   232  	otjson, err := obj.MarshalJSON()
   233  	require.NoError(t, err)
   234  	require.JSONEq(t, string(rtjson), string(otjson))
   235  
   236  	var obj1 AggregationObject
   237  	require.NoError(t, ReadJSON(rtjson, &obj1))
   238  	require.Equal(t, obj, obj1)
   239  
   240  	var obj11 AggregationObject
   241  	require.NoError(t, obj11.UnmarshalJSON(rtjson))
   242  	require.Equal(t, obj, obj11)
   243  
   244  	jsons := `{"counter1":123,"counter2:":456,"count":999}`
   245  	var obj2 AggregationObject
   246  	require.NoError(t, ReadJSON([]byte(jsons), &obj2))
   247  	require.Equal(t, AggregationObject{SharedCounters: SharedCounters{Counter1: 123, Counter2: 456}, Count: 999}, obj2)
   248  }
   249  

View as plain text