-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathschema_go118_test.go
More file actions
152 lines (127 loc) · 3.47 KB
/
schema_go118_test.go
File metadata and controls
152 lines (127 loc) · 3.47 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
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
// SPDX-FileCopyrightText: Copyright 2015-2025 go-swagger maintainers
// SPDX-License-Identifier: Apache-2.0
package codescan
import (
"testing"
"github.com/go-openapi/testify/v2/assert"
"github.com/go-openapi/testify/v2/require"
"github.com/go-openapi/spec"
)
var go118ClassificationCtx *scanCtx
func loadGo118ClassificationPkgsCtx(t *testing.T, extra ...string) *scanCtx {
t.Helper()
if go118ClassificationCtx != nil {
return go118ClassificationCtx
}
sctx, err := newScanCtx(&Options{
Packages: append([]string{
"./goparsing/go118",
}, extra...),
WorkDir: "fixtures",
})
require.NoError(t, err)
go118ClassificationCtx = sctx
return go118ClassificationCtx
}
func getGo118ClassificationModel(sctx *scanCtx, nm string) *entityDecl {
decl, ok := sctx.FindDecl(fixturesModule+"/goparsing/go118", nm)
if !ok {
return nil
}
return decl
}
func TestGo118SwaggerTypeNamed(t *testing.T) {
sctx := loadGo118ClassificationPkgsCtx(t)
decl := getGo118ClassificationModel(sctx, "NamedWithType")
require.NotNil(t, decl)
prs := &schemaBuilder{
ctx: sctx,
decl: decl,
}
models := make(map[string]spec.Schema)
require.NoError(t, prs.Build(models))
schema := models["namedWithType"]
assertProperty(t, &schema, "object", "some_map", "", "SomeMap")
}
func TestGo118AliasedModels(t *testing.T) {
sctx := loadGo118ClassificationPkgsCtx(t)
names := []string{
"SomeObject",
}
defs := make(map[string]spec.Schema)
for _, nm := range names {
decl := getGo118ClassificationModel(sctx, nm)
require.NotNil(t, decl)
prs := &schemaBuilder{
decl: decl,
ctx: sctx,
}
require.NoError(t, prs.Build(defs))
}
for k := range defs {
for i, b := range names {
if b == k {
// remove the entry from the collection
names = append(names[:i], names[i+1:]...)
}
}
}
if assert.Empty(t, names) {
// map types
assertMapDefinition(t, defs, "SomeObject", "object", "", "")
}
}
func TestGo118InterfaceField(t *testing.T) {
sctx := loadGo118ClassificationPkgsCtx(t)
decl := getGo118ClassificationModel(sctx, "Interfaced")
require.NotNil(t, decl)
prs := &schemaBuilder{
ctx: sctx,
decl: decl,
}
models := make(map[string]spec.Schema)
require.NoError(t, prs.Build(models))
schema := models["Interfaced"]
assertProperty(t, &schema, "", "custom_data", "", "CustomData")
}
func TestGo118ParameterParser_Issue2011(t *testing.T) {
sctx := loadGo118ClassificationPkgsCtx(t)
operations := make(map[string]*spec.Operation)
td := getParameter(sctx, "NumPlates")
prs := ¶meterBuilder{
ctx: sctx,
decl: td,
}
require.NoError(t, prs.Build(operations))
op := operations["putNumPlate"]
require.NotNil(t, op)
require.Len(t, op.Parameters, 1)
sch := op.Parameters[0].Schema
require.NotNil(t, sch)
}
func TestGo118ParseResponses_Issue2011(t *testing.T) {
sctx := loadGo118ClassificationPkgsCtx(t)
responses := make(map[string]spec.Response)
td := getResponse(sctx, "NumPlatesResp")
prs := &responseBuilder{
ctx: sctx,
decl: td,
}
require.NoError(t, prs.Build(responses))
resp := responses["NumPlatesResp"]
require.Empty(t, resp.Headers)
require.NotNil(t, resp.Schema)
}
func TestGo118_Issue2809(t *testing.T) {
sctx := loadGo118ClassificationPkgsCtx(t)
decl := getGo118ClassificationModel(sctx, "transportErr")
require.NotNil(t, decl)
prs := &schemaBuilder{
ctx: sctx,
decl: decl,
}
models := make(map[string]spec.Schema)
require.NoError(t, prs.Build(models))
schema := models["transportErr"]
assertProperty(t, &schema, "", "data", "", "Data")
}