-
Notifications
You must be signed in to change notification settings - Fork 14
/
reflect_go1.18_test.go
107 lines (94 loc) · 2.78 KB
/
reflect_go1.18_test.go
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
//go:build go1.18
// +build go1.18
package jsonschema_test
import (
"net/netip"
"testing"
"time"
"github.com/stretchr/testify/require"
"github.com/swaggest/assertjson"
"github.com/swaggest/jsonschema-go"
)
func TestReflector_Reflect_generic(t *testing.T) {
type helloOutput struct {
Now time.Time `header:"X-Now" json:"-"`
Message string `json:"message"`
}
type helloOutput2 struct {
Bar string `json:"bar"`
}
type APIResponse[T any] struct {
Data *T `json:"data"`
}
var ar struct {
Foo APIResponse[helloOutput] `json:"foo"`
Bar APIResponse[helloOutput2] `json:"bar"`
}
r := jsonschema.Reflector{}
s, err := r.Reflect(ar, jsonschema.StripDefinitionNamePrefix("JsonschemaGoTest"))
require.NoError(t, err)
assertjson.EqualMarshal(t, []byte(`{
"definitions":{
"APIResponse[HelloOutput2]":{
"properties":{"data":{"$ref":"#/definitions/HelloOutput2"}},
"type":"object"
},
"APIResponse[HelloOutput]":{
"properties":{"data":{"$ref":"#/definitions/HelloOutput"}},
"type":"object"
},
"HelloOutput":{"properties":{"message":{"type":"string"}},"type":"object"},
"HelloOutput2":{"properties":{"bar":{"type":"string"}},"type":"object"}
},
"properties":{
"bar":{"$ref":"#/definitions/APIResponse[HelloOutput2]"},
"foo":{"$ref":"#/definitions/APIResponse[HelloOutput]"}
},
"type":"object"
}`), s)
r = jsonschema.Reflector{}
s, err = r.Reflect(ar)
require.NoError(t, err)
assertjson.EqualMarshal(t, []byte(`{
"definitions":{
"JsonschemaGoTestAPIResponse[JsonschemaGoTestHelloOutput2]":{
"properties":{"data":{"$ref":"#/definitions/JsonschemaGoTestHelloOutput2"}},
"type":"object"
},
"JsonschemaGoTestAPIResponse[JsonschemaGoTestHelloOutput]":{
"properties":{"data":{"$ref":"#/definitions/JsonschemaGoTestHelloOutput"}},
"type":"object"
},
"JsonschemaGoTestHelloOutput":{"properties":{"message":{"type":"string"}},"type":"object"},
"JsonschemaGoTestHelloOutput2":{"properties":{"bar":{"type":"string"}},"type":"object"}
},
"properties":{
"bar":{
"$ref":"#/definitions/JsonschemaGoTestAPIResponse[JsonschemaGoTestHelloOutput2]"
},
"foo":{
"$ref":"#/definitions/JsonschemaGoTestAPIResponse[JsonschemaGoTestHelloOutput]"
}
},
"type":"object"
}`), s)
}
func TestReflector_Reflect_fieldTags(t *testing.T) {
type My struct {
Prefix netip.Prefix `json:"prefix" required:"true" example:"192.168.0.0/24" description:"Prefix in CIDR notation" format:"cidr"`
}
reflector := jsonschema.Reflector{}
s, err := reflector.Reflect(My{})
require.NoError(t, err)
assertjson.EqualMarshal(t, []byte(`{
"required":["prefix"],
"properties":{
"prefix":{
"type":"string",
"description":"Prefix in CIDR notation","examples":["192.168.0.0/24"],
"format":"cidr"
}
},
"type":"object"
}`), s)
}