This repository was archived by the owner on Apr 14, 2026. It is now read-only.
forked from oapi-codegen/oapi-codegen
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathitemschema_test.go
More file actions
305 lines (271 loc) · 8.19 KB
/
itemschema_test.go
File metadata and controls
305 lines (271 loc) · 8.19 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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
package codegen
import (
"strings"
"testing"
"github.com/pb33f/libopenapi"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func schemaPathStrings(schemas []*SchemaDescriptor) []string {
paths := make([]string, len(schemas))
for i, s := range schemas {
paths[i] = s.Path.String()
}
return paths
}
func TestIsSequentialMediaType(t *testing.T) {
tests := []struct {
contentType string
want bool
}{
{"text/event-stream", true},
{"application/jsonl", true},
{"application/x-ndjson", true},
{"application/json-seq", true},
{"multipart/mixed", true},
{"application/json", false},
{"text/plain", false},
{"application/xml", false},
{"multipart/form-data", false},
{"application/x-www-form-urlencoded", false},
}
for _, tt := range tests {
t.Run(tt.contentType, func(t *testing.T) {
got := IsSequentialMediaType(tt.contentType)
assert.Equal(t, tt.want, got)
})
}
}
const itemSchemaResponseSpec = `openapi: "3.1.0"
info:
title: ItemSchema Test API
version: "1.0"
paths:
/events:
get:
operationId: streamEvents
responses:
"200":
description: OK
content:
text/event-stream:
schema:
type: string
description: Raw SSE stream
itemSchema:
type: object
properties:
id:
type: integer
message:
type: string
`
const itemSchemaRequestBodySpec = `openapi: "3.1.0"
info:
title: ItemSchema RequestBody Test API
version: "1.0"
paths:
/ingest:
post:
operationId: ingestEvents
requestBody:
required: true
content:
application/jsonl:
schema:
type: string
description: Raw JSONL stream
itemSchema:
type: object
properties:
event:
type: string
timestamp:
type: integer
responses:
"202":
description: Accepted
`
func TestGatherItemSchema_Response(t *testing.T) {
doc, err := libopenapi.NewDocument([]byte(itemSchemaResponseSpec))
require.NoError(t, err, "Failed to parse spec")
contentTypeMatcher := NewContentTypeMatcher(DefaultContentTypes())
schemas, err := GatherSchemas(doc, contentTypeMatcher, OutputOptions{})
require.NoError(t, err, "Failed to gather schemas")
// Verify the itemSchema was gathered
var foundItemSchema bool
for _, s := range schemas {
pathStr := s.Path.String()
if strings.Contains(pathStr, "itemSchema") {
foundItemSchema = true
assert.NotNil(t, s.Schema)
assert.NotNil(t, s.Schema.Properties)
break
}
}
assert.True(t, foundItemSchema, "expected itemSchema to be gathered from response; gathered paths: %v", schemaPathStrings(schemas))
}
func TestGatherItemSchema_RequestBody(t *testing.T) {
doc, err := libopenapi.NewDocument([]byte(itemSchemaRequestBodySpec))
require.NoError(t, err, "Failed to parse spec")
contentTypeMatcher := NewContentTypeMatcher(DefaultContentTypes())
schemas, err := GatherSchemas(doc, contentTypeMatcher, OutputOptions{})
require.NoError(t, err, "Failed to gather schemas")
// Verify the itemSchema was gathered
var foundItemSchema bool
for _, s := range schemas {
pathStr := s.Path.String()
if strings.Contains(pathStr, "itemSchema") {
foundItemSchema = true
assert.NotNil(t, s.Schema)
assert.NotNil(t, s.Schema.Properties)
break
}
}
assert.True(t, foundItemSchema, "expected itemSchema to be gathered from request body; gathered paths: %v", schemaPathStrings(schemas))
}
func TestGatherOperations_ItemSchema_Response(t *testing.T) {
doc, err := libopenapi.NewDocument([]byte(itemSchemaResponseSpec))
require.NoError(t, err, "Failed to parse spec")
ctx := NewCodegenContext()
contentTypeMatcher := NewContentTypeMatcher(DefaultContentTypes())
ops, err := GatherOperations(doc, ctx, contentTypeMatcher)
require.NoError(t, err, "Failed to gather operations")
require.Len(t, ops, 1, "Expected 1 operation")
op := ops[0]
assert.Equal(t, "streamEvents", op.OperationID)
// Find the SSE response content
require.NotEmpty(t, op.Responses, "Operation responses should not be empty")
resp := op.Responses[0]
require.NotEmpty(t, resp.Contents, "Response content should not be empty")
var sseContent *ResponseContentDescriptor
for _, c := range resp.Contents {
if c.ContentType == "text/event-stream" {
sseContent = c
break
}
}
require.NotNil(t, sseContent, "expected text/event-stream content")
assert.True(t, sseContent.IsSequential)
assert.NotNil(t, sseContent.ItemSchema, "expected ItemSchema to be populated")
assert.NotNil(t, sseContent.ItemSchema.Schema)
assert.NotNil(t, sseContent.ItemSchema.Schema.Properties)
}
func TestGatherOperations_ItemSchema_RequestBody(t *testing.T) {
doc, err := libopenapi.NewDocument([]byte(itemSchemaRequestBodySpec))
require.NoError(t, err, "Failed to parse spec")
ctx := NewCodegenContext()
contentTypeMatcher := NewContentTypeMatcher(DefaultContentTypes())
ops, err := GatherOperations(doc, ctx, contentTypeMatcher)
require.NoError(t, err, "Failed to gather operations")
require.Len(t, ops, 1, "Expected 1 operation")
op := ops[0]
assert.Equal(t, "ingestEvents", op.OperationID)
// Find the JSONL request body
require.NotEmpty(t, op.Bodies, "Operation body should not be empty")
var jsonlBody *RequestBodyDescriptor
for _, b := range op.Bodies {
if b.ContentType == "application/jsonl" {
jsonlBody = b
break
}
}
require.NotNil(t, jsonlBody, "expected application/jsonl body")
assert.True(t, jsonlBody.IsSequential)
assert.NotNil(t, jsonlBody.ItemSchema, "expected ItemSchema to be populated")
assert.NotNil(t, jsonlBody.ItemSchema.Schema)
assert.NotNil(t, jsonlBody.ItemSchema.Schema.Properties)
}
func TestGatherOperations_NonSequential_HasNoItemSchema(t *testing.T) {
const spec = `openapi: "3.1.0"
info:
title: Test
version: "1.0"
paths:
/pets:
get:
operationId: listPets
responses:
"200":
description: OK
content:
application/json:
schema:
type: array
items:
type: object
properties:
name:
type: string
`
doc, err := libopenapi.NewDocument([]byte(spec))
require.NoError(t, err, "Failed to parse spec")
ctx := NewCodegenContext()
contentTypeMatcher := NewContentTypeMatcher(DefaultContentTypes())
ops, err := GatherOperations(doc, ctx, contentTypeMatcher)
require.NoError(t, err, "Failed to gather operations")
require.Len(t, ops, 1, "Expected 1 operation")
resp := ops[0].Responses[0]
require.NotEmpty(t, resp.Contents)
jsonContent := resp.Contents[0]
assert.False(t, jsonContent.IsSequential, "Contents should not be sequential")
assert.Nil(t, jsonContent.ItemSchema, "Item Schema should be empty")
}
func TestGenerate_ItemSchema(t *testing.T) {
const spec = `openapi: "3.1.0"
info:
title: Streaming API
version: "1.0"
paths:
/events:
get:
operationId: streamEvents
responses:
"200":
description: OK
content:
text/event-stream:
schema:
type: string
itemSchema:
type: object
properties:
id:
type: integer
message:
type: string
/ingest:
post:
operationId: ingestData
requestBody:
required: true
content:
application/x-ndjson:
schema:
type: string
itemSchema:
$ref: '#/components/schemas/Event'
responses:
"202":
description: Accepted
components:
schemas:
Event:
type: object
properties:
type:
type: string
data:
type: string
`
doc, err := libopenapi.NewDocument([]byte(spec))
require.NoError(t, err, "Failed to parse spec")
cfg := Configuration{
PackageName: "testpkg",
}
code, err := Generate(doc, []byte(spec), cfg)
require.NoError(t, err)
assert.NotEmpty(t, code)
// Verify the itemSchema types are generated
assert.Contains(t, code, "Event")
}