forked from lazada/swgen
-
Notifications
You must be signed in to change notification settings - Fork 1
/
adapter_test.go
66 lines (53 loc) · 1.55 KB
/
adapter_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
package swgen_test
import (
"testing"
"github.com/stretchr/testify/require"
"github.com/swaggest/assertjson"
"github.com/swaggest/jsonschema-go"
"github.com/swaggest/swgen"
)
// ISOWeek is a week identifier.
type ISOWeek string
// SwaggerDef returns swagger definition.
func (ISOWeek) SwaggerDef() swgen.SwaggerData {
s := swgen.SwaggerData{}
s.Description = "ISO Week"
s.Example = "2006-W43"
s.Type = "string"
s.Pattern = `^[0-9]{4}-W(0[1-9]|[1-4][0-9]|5[0-2])$`
return s
}
type UUID [16]byte
func TestInterceptType(t *testing.T) {
reflector := jsonschema.Reflector{}
reflector.DefaultOptions = append(reflector.DefaultOptions, jsonschema.InterceptType(swgen.JSONSchemaInterceptType))
// Add custom type mappings
uuidDef := swgen.SwaggerData{}
uuidDef.Type = "string"
uuidDef.Format = "uuid"
uuidDef.Example = "248df4b7-aa70-47b8-a036-33ac447e668d"
reflector.AddTypeMapping(UUID{}, uuidDef)
type MyStruct struct {
UUID UUID `json:"uuid"`
ISOWeek ISOWeek `json:"iso_week"`
}
schema, err := reflector.Reflect(MyStruct{})
require.NoError(t, err)
assertjson.EqualMarshal(t, []byte(`{
"definitions":{
"SwgenTestISOWeek":{
"description":"ISO Week","examples":["2006-W43"],
"pattern":"^[0-9]{4}-W(0[1-9]|[1-4][0-9]|5[0-2])$","type":"string"
},
"SwgenTestUUID":{
"examples":["248df4b7-aa70-47b8-a036-33ac447e668d"],"type":"string",
"format":"uuid"
}
},
"properties":{
"iso_week":{"$ref":"#/definitions/SwgenTestISOWeek"},
"uuid":{"$ref":"#/definitions/SwgenTestUUID"}
},
"type":"object"
}`), schema)
}