-
Notifications
You must be signed in to change notification settings - Fork 105
Expand file tree
/
Copy pathstructs_test.go
More file actions
69 lines (60 loc) · 2.25 KB
/
Copy pathstructs_test.go
File metadata and controls
69 lines (60 loc) · 2.25 KB
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
// SPDX-FileCopyrightText: Copyright 2015-2025 go-swagger maintainers
// SPDX-License-Identifier: Apache-2.0
package spec
import (
"encoding/json"
"testing"
"github.com/go-openapi/testify/v2/assert"
"github.com/go-openapi/testify/v2/require"
)
func TestSerialization_SerializeJSON(t *testing.T) {
assert.JSONMarshalAsT(t, `["hello"]`, []string{"hello"})
assert.JSONMarshalAsT(t, `["hello","world","and","stuff"]`, []string{"hello", "world", "and", "stuff"})
assert.JSONMarshalAsT(t, `null`, StringOrArray(nil))
assert.JSONMarshalAsT(t, `[{"type":"string"}]`, SchemaOrArray{
Schemas: []Schema{
{SchemaProps: SchemaProps{Type: []string{"string"}}},
},
})
assert.JSONMarshalAsT(t, `[{"type":"string"},{"type":"string"}]`, SchemaOrArray{
Schemas: []Schema{
{SchemaProps: SchemaProps{Type: []string{"string"}}},
{SchemaProps: SchemaProps{Type: []string{"string"}}},
},
})
// an empty tuple is still a tuple: "null" would not be a schema
assert.JSONMarshalAsT(t, `[]`, SchemaOrArray{Schemas: []Schema{}})
assert.JSONMarshalAsT(t, `null`, SchemaOrArray{})
}
func TestSerialization_RoundTripEmptyItems(t *testing.T) {
const document = `{"definitions":{"A":{"items":[]}}}`
var doc Swagger
require.NoError(t, json.Unmarshal([]byte(document), &doc))
out, err := json.Marshal(doc)
require.NoError(t, err)
assert.JSONEqT(t, `{"paths":null,"definitions":{"A":{"items":[]}}}`, string(out))
}
func TestSerialization_DeserializeJSON(t *testing.T) {
// String
assert.JSONUnmarshalAsT(t, StringOrArray([]string{"hello"}), `"hello"`)
assert.JSONUnmarshalAsT(t,
StringOrArray([]string{"hello", "world", "and", "stuff"}),
`["hello","world","and","stuff"]`)
assert.JSONUnmarshalAsT(t,
StringOrArray([]string{"hello", "world", "", "stuff"}),
`["hello","world",null,"stuff"]`)
assert.JSONUnmarshalAsT(t, StringOrArray(nil), `null`)
// Schema
assert.JSONUnmarshalAsT(t, SchemaOrArray{
Schema: &Schema{
SchemaProps: SchemaProps{Type: []string{"string"}},
},
}, `{"type":"string"}`)
assert.JSONUnmarshalAsT(t, &SchemaOrArray{
Schemas: []Schema{
{SchemaProps: SchemaProps{Type: []string{"string"}}},
{SchemaProps: SchemaProps{Type: []string{"string"}}},
},
}, `[{"type":"string"},{"type":"string"}]`)
assert.JSONUnmarshalAsT(t, SchemaOrArray{}, `null`)
}