forked from ogen-go/ogen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
spec_test.go
116 lines (92 loc) · 2.36 KB
/
spec_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
108
109
110
111
112
113
114
115
116
package ogen_test
import (
"encoding/json"
"reflect"
"testing"
"github.com/go-faster/yaml"
"github.com/stretchr/testify/require"
"github.com/ogen-go/ogen"
)
func TestExtensionParsing(t *testing.T) {
a := require.New(t)
{
var (
input = `{"url": "/api/v1", "x-ogen-name": "foo"}`
s ogen.Server
)
a.NoError(yaml.Unmarshal([]byte(input), &s))
a.Equal("foo", s.Common.Extensions["x-ogen-name"].Value)
// FIXME(tdakkota): encodeDecode doesn't work for this type
}
{
var (
input = `{"description": "foo", "x-ogen-extension": "bar"}`
s ogen.Response
)
a.NoError(yaml.Unmarshal([]byte(input), &s))
a.Equal("bar", s.Common.Extensions["x-ogen-extension"].Value)
// FIXME(tdakkota): encodeDecode doesn't work for this type
}
}
func TestComponents_Init(t *testing.T) {
a := require.New(t)
c := ogen.Components{}
c.Init()
val := reflect.ValueOf(c)
a.Equalf(10+1, val.NumField(), "update this test if you add new fields to Components")
for i := 0; i < val.NumField(); i++ {
f := val.Field(i)
// Init doesn't set Common and it's ok.
if val.Type().Field(i).Name == "Common" {
continue
}
a.NotNil(f.Interface())
}
}
func TestSwaggerExtraction(t *testing.T) {
a := require.New(t)
{
var (
input = `{"swagger": "2.0.0"}`
s ogen.Spec
)
a.NoError(yaml.Unmarshal([]byte(input), &s))
a.Equal("2.0.0", s.Swagger)
}
}
func TestExtensionsMarshal(t *testing.T) {
a := require.New(t)
extensionValueFunc := func(key, value string) ogen.OpenAPICommon {
return ogen.OpenAPICommon{
Extensions: ogen.Extensions{
key: yaml.Node{Kind: yaml.ScalarNode, Value: value},
},
}
}
extentionKey := "x-ogen-extension"
extenstionValue := "handler"
{
pathItem := ogen.NewPathItem()
pathItem.Common = extensionValueFunc(extentionKey, extenstionValue)
pathItemJSON, err := json.Marshal(pathItem)
a.NoError(err)
var output map[string]interface{}
err = json.Unmarshal(pathItemJSON, &output)
a.NoError(err)
v, ok := output[extentionKey]
a.True(ok)
a.Equal(extenstionValue, v)
}
{
op := ogen.NewOperation()
op.Common = extensionValueFunc(extentionKey, extenstionValue)
pathItemJSON, err := json.Marshal(op)
a.NoError(err)
var output map[string]interface{}
err = json.Unmarshal(pathItemJSON, &output)
a.NoError(err)
v, ok := output[extentionKey]
a.True(ok)
a.Equal(extenstionValue, v)
}
}