-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathparams.go
More file actions
535 lines (468 loc) · 14.4 KB
/
Copy pathparams.go
File metadata and controls
535 lines (468 loc) · 14.4 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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
package httpvalidator
import (
"net/url"
"strconv"
"strings"
"github.com/erraggy/oastools/parser"
)
// Parameter serialization style constants.
const (
styleLabel = "label"
styleMatrix = "matrix"
)
// ParamDeserializer handles deserialization of HTTP parameters according to
// OpenAPI serialization styles. Each parameter location has default styles:
//
// | Location | Default Style | Default Explode |
// |----------|---------------|-----------------|
// | path | simple | false |
// | query | form | true |
// | header | simple | false |
// | cookie | form | false |
type ParamDeserializer struct{}
// NewParamDeserializer creates a new parameter deserializer.
func NewParamDeserializer() *ParamDeserializer {
return &ParamDeserializer{}
}
// DeserializePathParam deserializes a path parameter value according to its style.
// Path parameters default to style "simple" with explode=false.
//
// Styles supported:
// - simple (default): comma-separated values, e.g., "a,b,c"
// - label: dot-prefixed values, e.g., ".a.b.c"
// - matrix: semicolon-prefixed key=value, e.g., ";id=5"
func (d *ParamDeserializer) DeserializePathParam(value string, param *parser.Parameter) any {
style := param.Style
if style == "" {
style = "simple"
}
// Default explode is false for path params
explode := false
if param.Explode != nil {
explode = *param.Explode
}
schema := param.Schema
switch style {
case "simple":
return d.deserializeSimple(value, schema, explode)
case styleLabel:
return d.deserializeLabel(value, schema, explode)
case styleMatrix:
return d.deserializeMatrix(value, param.Name, schema, explode)
default:
// Unknown style, return raw value
return value
}
}
// DeserializeQueryParam deserializes query parameter values according to their style.
// Query parameters default to style "form" with explode=true.
//
// Styles supported:
// - form (default): standard query string format
// - spaceDelimited: space-separated values
// - pipeDelimited: pipe-separated values
// - deepObject: nested object notation, e.g., "filter[status]=active"
func (d *ParamDeserializer) DeserializeQueryParam(values []string, param *parser.Parameter) any {
style := param.Style
if style == "" {
style = "form"
}
// Default explode is true for query params with form style
explode := true
if param.Explode != nil {
explode = *param.Explode
}
schema := param.Schema
switch style {
case "form":
return d.deserializeForm(values, schema, explode)
case "spaceDelimited":
return d.deserializeDelimited(values, " ", schema)
case "pipeDelimited":
return d.deserializeDelimited(values, "|", schema)
case "deepObject":
// deepObject is handled at a higher level with the full query string
// Here we just return the values as-is
if len(values) == 1 {
return values[0]
}
return values
default:
if len(values) == 1 {
return values[0]
}
return values
}
}
// DeserializeQueryParamsDeepObject deserializes query parameters using deepObject style.
// This handles nested object notation like "filter[status]=active&filter[type]=user".
//
// Returns a map representing the nested object structure.
func (d *ParamDeserializer) DeserializeQueryParamsDeepObject(queryValues url.Values, paramName string, schema *parser.Schema) map[string]any {
prefix := paramName + "["
result := make(map[string]any)
for key, values := range queryValues {
if !strings.HasPrefix(key, prefix) {
continue
}
// Extract property name from filter[property]
propEnd := strings.Index(key[len(prefix):], "]")
if propEnd == -1 {
continue
}
propName := key[len(prefix) : len(prefix)+propEnd]
if len(values) == 1 {
result[propName] = d.coerceValue(values[0], d.getPropertySchema(schema, propName))
} else {
result[propName] = values
}
}
return result
}
// DeserializeHeaderParam deserializes a header parameter value.
// Header parameters default to style "simple" with explode=false.
func (d *ParamDeserializer) DeserializeHeaderParam(value string, param *parser.Parameter) any {
// Header params use simple style by default
// Currently only simple style is implemented for headers
// Default explode is false for header params
explode := false
if param.Explode != nil {
explode = *param.Explode
}
return d.deserializeSimple(value, param.Schema, explode)
}
// DeserializeCookieParam deserializes a cookie parameter value.
// Cookie parameters default to style "form" with explode=false.
func (d *ParamDeserializer) DeserializeCookieParam(value string, param *parser.Parameter) any {
// Cookie params use form style by default
// Currently only form style is implemented for cookies
schema := param.Schema
// For cookies, we only get a single value string
// Form style without explode is comma-separated for arrays
if isArraySchema(schema) {
return d.deserializeSimple(value, schema, false)
}
return d.coerceValue(value, schema)
}
// deserializeSimple handles the "simple" style (comma-separated).
// Used by path and header parameters by default.
func (d *ParamDeserializer) deserializeSimple(value string, schema *parser.Schema, explode bool) any {
if schema == nil {
return value
}
if isArraySchema(schema) {
// Split by comma
parts := strings.Split(value, ",")
return d.coerceArray(parts, getItemsSchema(schema))
}
if isObjectSchema(schema) {
return d.deserializeSimpleObject(value, schema, explode)
}
return d.coerceValue(value, schema)
}
// deserializeSimpleObject handles object deserialization in simple style.
func (d *ParamDeserializer) deserializeSimpleObject(value string, schema *parser.Schema, explode bool) map[string]any {
result := make(map[string]any)
parts := strings.Split(value, ",")
if explode {
// explode=true: key=value,key2=value2
for _, part := range parts {
if idx := strings.Index(part, "="); idx > 0 {
key := part[:idx]
val := part[idx+1:]
result[key] = d.coerceValue(val, d.getPropertySchema(schema, key))
}
}
} else {
// explode=false: key,value,key2,value2
for i := 0; i+1 < len(parts); i += 2 {
key := parts[i]
val := parts[i+1]
result[key] = d.coerceValue(val, d.getPropertySchema(schema, key))
}
}
return result
}
// deserializeLabel handles the "label" style (dot-prefixed).
func (d *ParamDeserializer) deserializeLabel(value string, schema *parser.Schema, explode bool) any {
// Label style starts with a dot
if !strings.HasPrefix(value, ".") {
return value
}
value = value[1:] // Remove leading dot
if schema == nil {
return value
}
if isArraySchema(schema) {
var parts []string
if explode {
// explode=true: .a.b.c
parts = strings.Split(value, ".")
} else {
// explode=false: .a,b,c
parts = strings.Split(value, ",")
}
return d.coerceArray(parts, getItemsSchema(schema))
}
if isObjectSchema(schema) {
return d.deserializeLabelObject(value, schema, explode)
}
return d.coerceValue(value, schema)
}
// deserializeLabelObject handles object deserialization in label style.
func (d *ParamDeserializer) deserializeLabelObject(value string, schema *parser.Schema, explode bool) map[string]any {
result := make(map[string]any)
if explode {
// explode=true: .key=value.key2=value2
parts := strings.Split(value, ".")
for _, part := range parts {
if part == "" {
continue
}
if idx := strings.Index(part, "="); idx > 0 {
key := part[:idx]
val := part[idx+1:]
result[key] = d.coerceValue(val, d.getPropertySchema(schema, key))
}
}
} else {
// explode=false: .key,value,key2,value2
parts := strings.Split(value, ",")
for i := 0; i+1 < len(parts); i += 2 {
key := parts[i]
val := parts[i+1]
result[key] = d.coerceValue(val, d.getPropertySchema(schema, key))
}
}
return result
}
// deserializeMatrix handles the "matrix" style (semicolon-prefixed).
func (d *ParamDeserializer) deserializeMatrix(value, paramName string, schema *parser.Schema, explode bool) any {
// Matrix style starts with semicolon
if !strings.HasPrefix(value, ";") {
return value
}
value = value[1:] // Remove leading semicolon
if schema == nil {
// Try to extract value from ;name=value
if strings.HasPrefix(value, paramName+"=") {
return value[len(paramName)+1:]
}
return value
}
if isArraySchema(schema) {
return d.deserializeMatrixArray(value, paramName, schema, explode)
}
if isObjectSchema(schema) {
return d.deserializeMatrixObject(value, paramName, schema, explode)
}
// Primitive: ;name=value
if strings.HasPrefix(value, paramName+"=") {
return d.coerceValue(value[len(paramName)+1:], schema)
}
return d.coerceValue(value, schema)
}
// deserializeMatrixArray handles array deserialization in matrix style.
func (d *ParamDeserializer) deserializeMatrixArray(value, paramName string, schema *parser.Schema, explode bool) []any {
if explode {
// explode=true: ;id=3;id=4;id=5
var values []string
parts := strings.Split(value, ";")
prefix := paramName + "="
for _, part := range parts {
if strings.HasPrefix(part, prefix) {
values = append(values, part[len(prefix):])
}
}
return d.coerceArray(values, getItemsSchema(schema))
}
// explode=false: ;id=3,4,5
prefix := paramName + "="
if strings.HasPrefix(value, prefix) {
parts := strings.Split(value[len(prefix):], ",")
return d.coerceArray(parts, getItemsSchema(schema))
}
return nil
}
// deserializeMatrixObject handles object deserialization in matrix style.
func (d *ParamDeserializer) deserializeMatrixObject(value, paramName string, schema *parser.Schema, explode bool) map[string]any {
result := make(map[string]any)
if explode {
// explode=true: ;role=admin;firstName=Alex
parts := strings.Split(value, ";")
for _, part := range parts {
if part == "" {
continue
}
if idx := strings.Index(part, "="); idx > 0 {
key := part[:idx]
val := part[idx+1:]
result[key] = d.coerceValue(val, d.getPropertySchema(schema, key))
}
}
} else {
// explode=false: ;id=role,admin,firstName,Alex
prefix := paramName + "="
if strings.HasPrefix(value, prefix) {
parts := strings.Split(value[len(prefix):], ",")
for i := 0; i+1 < len(parts); i += 2 {
key := parts[i]
val := parts[i+1]
result[key] = d.coerceValue(val, d.getPropertySchema(schema, key))
}
}
}
return result
}
// deserializeForm handles the "form" style (standard query string format).
func (d *ParamDeserializer) deserializeForm(values []string, schema *parser.Schema, explode bool) any {
if schema == nil {
if len(values) == 1 {
return values[0]
}
return values
}
if isArraySchema(schema) {
if explode {
// explode=true: multiple values (id=3&id=4&id=5)
return d.coerceArray(values, getItemsSchema(schema))
}
// explode=false: comma-separated in single value (id=3,4,5)
if len(values) == 1 {
parts := strings.Split(values[0], ",")
return d.coerceArray(parts, getItemsSchema(schema))
}
return d.coerceArray(values, getItemsSchema(schema))
}
if isObjectSchema(schema) {
if explode {
// explode=true: separate keys (role=admin&firstName=Alex)
// This is handled at a higher level with the full query string
if len(values) == 1 {
return values[0]
}
return values
}
// explode=false: comma-separated key,value pairs (id=role,admin,firstName,Alex)
if len(values) == 1 {
parts := strings.Split(values[0], ",")
result := make(map[string]any)
for i := 0; i+1 < len(parts); i += 2 {
key := parts[i]
val := parts[i+1]
result[key] = d.coerceValue(val, d.getPropertySchema(schema, key))
}
return result
}
}
// Primitive type
if len(values) == 1 {
return d.coerceValue(values[0], schema)
}
return values
}
// deserializeDelimited handles space and pipe delimited styles.
func (d *ParamDeserializer) deserializeDelimited(values []string, delimiter string, schema *parser.Schema) any {
// Join all values and split by delimiter
joined := strings.Join(values, delimiter)
parts := strings.Split(joined, delimiter)
if isArraySchema(schema) {
return d.coerceArray(parts, getItemsSchema(schema))
}
if len(parts) == 1 {
return d.coerceValue(parts[0], schema)
}
return parts
}
// coerceValue converts a string value to the appropriate Go type based on the schema.
func (d *ParamDeserializer) coerceValue(value string, schema *parser.Schema) any {
if schema == nil {
return value
}
schemaType := getSchemaType(schema)
switch schemaType {
case "integer":
if i, err := strconv.ParseInt(value, 10, 64); err == nil {
return i
}
return value
case "number":
if f, err := strconv.ParseFloat(value, 64); err == nil {
return f
}
return value
case "boolean":
if b, err := strconv.ParseBool(value); err == nil {
return b
}
return value
default:
return value
}
}
// coerceArray converts string values to a slice of appropriately typed values.
func (d *ParamDeserializer) coerceArray(values []string, itemSchema *parser.Schema) []any {
result := make([]any, len(values))
for i, v := range values {
result[i] = d.coerceValue(v, itemSchema)
}
return result
}
// getPropertySchema returns the schema for a property of an object schema.
func (d *ParamDeserializer) getPropertySchema(schema *parser.Schema, propName string) *parser.Schema {
if schema == nil || schema.Properties == nil {
return nil
}
return schema.Properties[propName]
}
// getSchemaType extracts the type from a schema, handling both string and []string types.
func getSchemaType(schema *parser.Schema) string {
if schema == nil {
return ""
}
switch t := schema.Type.(type) {
case string:
return t
case []string:
// For type arrays, use the first non-null type
for _, typ := range t {
if typ != "null" {
return typ
}
}
if len(t) > 0 {
return t[0]
}
case []any:
for _, typ := range t {
if s, ok := typ.(string); ok && s != "null" {
return s
}
}
if len(t) > 0 {
if s, ok := t[0].(string); ok {
return s
}
}
}
return ""
}
// isArraySchema checks if the schema type is "array".
func isArraySchema(schema *parser.Schema) bool {
return getSchemaType(schema) == "array"
}
// isObjectSchema checks if the schema type is "object".
func isObjectSchema(schema *parser.Schema) bool {
return getSchemaType(schema) == "object"
}
// getItemsSchema returns the items schema for an array schema.
// Schema.Items is `any` in OAS 3.1+ (can be *Schema or bool).
func getItemsSchema(schema *parser.Schema) *parser.Schema {
if schema == nil {
return nil
}
if items, ok := schema.Items.(*parser.Schema); ok {
return items
}
return nil
}