Skip to content

Commit 567d5b8

Browse files
authored
fix: convert url variables to colon x (#109)
1 parent 1398493 commit 567d5b8

File tree

3 files changed

+145
-4
lines changed

3 files changed

+145
-4
lines changed

internal/pkg/openapi2apisix/openapi2apisix.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@ var (
2222
)
2323

2424
func convertPathVariables(path string) string {
25-
return _pathVariableRegex.ReplaceAllString(path, "*")
25+
return _pathVariableRegex.ReplaceAllStringFunc(path, func(match string) string {
26+
return ":" + match[1:len(match)-1]
27+
})
2628
}
2729

2830
// Slugify converts a name to a valid name by removing and replacing unallowed characters

internal/pkg/openapi2apisix/openapi2apisix_test.go

+56-3
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ var (
7474
tagsTest []byte
7575
//go:embed testdata/tags.json
7676
tagsJsonTest []byte
77+
//go:embed testdata/urlVariables.yaml
78+
urlVariables []byte
7779
)
7880

7981
func TestConvert(t *testing.T) {
@@ -140,14 +142,14 @@ func TestConvert(t *testing.T) {
140142
Description: "Remove customer",
141143
// Labels: []string{"default"},
142144
Methods: []string{"DELETE"},
143-
Uris: []string{"/customer/*"},
145+
Uris: []string{"/customer/:customer_id"},
144146
},
145147
{
146148
Name: "api-101_put_customer-customer-id",
147149
Description: "Update customer",
148150
// Labels: []string{"default"},
149151
Methods: []string{"PUT"},
150-
Uris: []string{"/customer/*"},
152+
Uris: []string{"/customer/:customer_id"},
151153
},
152154
{
153155
Name: "api-101_get_customers",
@@ -195,7 +197,7 @@ func TestConvert(t *testing.T) {
195197
Description: "Update customer",
196198
// Labels: []string{"default"},
197199
Methods: []string{"PUT"},
198-
Uris: []string{"/customer/*"},
200+
Uris: []string{"/customer/:customer_id"},
199201
},
200202
{
201203
Name: "getCustomers",
@@ -294,6 +296,57 @@ func TestConvert(t *testing.T) {
294296
},
295297
wantErr: false,
296298
},
299+
{
300+
name: "urlVariables",
301+
args: args{
302+
content: urlVariables,
303+
},
304+
want: &apitypes.Configuration{
305+
Services: []*apitypes.Service{
306+
{
307+
Name: "URL variables",
308+
Upstream: &apitypes.Upstream{
309+
Scheme: "https",
310+
Nodes: []apitypes.UpstreamNode{
311+
{
312+
Host: "example.com",
313+
Port: 443,
314+
Weight: 100,
315+
},
316+
},
317+
Timeout: &apitypes.UpstreamTimeout{
318+
Connect: 60,
319+
Send: 60,
320+
Read: 60,
321+
},
322+
PassHost: apitypes.UpstreamPassHostPass,
323+
},
324+
// Labels: make([]apitypes.Labels, 0),
325+
},
326+
},
327+
Routes: []*apitypes.Route{
328+
{
329+
Name: "url-variables_get_base64-value",
330+
// Labels: []string{"default"},
331+
Methods: []string{"GET"},
332+
Uris: []string{"/base64/:value"},
333+
},
334+
{
335+
Name: "url-variables_get_basic-auth-user-passwd",
336+
// Labels: []string{"default"},
337+
Methods: []string{"GET"},
338+
Uris: []string{"/basic-auth/:user/:passwd"},
339+
},
340+
{
341+
Name: "url-variables_get_digest-auth-qop-user-passwd-algorithm-stale-after",
342+
// Labels: []string{"default"},
343+
Methods: []string{"GET"},
344+
Uris: []string{"/digest-auth/:qop/:user/:passwd/:algorithm/:stale_after"},
345+
},
346+
},
347+
},
348+
wantErr: false,
349+
},
297350
}
298351
for _, tt := range tests {
299352
t.Run(tt.name, func(t *testing.T) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
openapi: 3.0.1
2+
info:
3+
title: URL variables
4+
version: 1.0.0
5+
servers:
6+
- url: https://example.com/
7+
paths:
8+
/base64/{value}:
9+
get:
10+
tags:
11+
- Dynamic data
12+
parameters:
13+
- name: value
14+
in: path
15+
required: true
16+
schema:
17+
type: string
18+
default: SFRUUEJJTiBpcyBhd2Vzb21l
19+
responses:
20+
200:
21+
description: Decoded base64 content.
22+
content: {}
23+
/basic-auth/{user}/{passwd}:
24+
get:
25+
tags:
26+
- Auth
27+
parameters:
28+
- name: user
29+
in: path
30+
required: true
31+
schema:
32+
type: string
33+
- name: passwd
34+
in: path
35+
required: true
36+
schema:
37+
type: string
38+
responses:
39+
200:
40+
description: Sucessful authentication.
41+
content: {}
42+
401:
43+
description: Unsuccessful authentication.
44+
content: {}
45+
/digest-auth/{qop}/{user}/{passwd}/{algorithm}/{stale_after}:
46+
get:
47+
tags:
48+
- Auth
49+
parameters:
50+
- name: qop
51+
in: path
52+
description: auth or auth-int
53+
required: true
54+
schema:
55+
type: string
56+
- name: user
57+
in: path
58+
required: true
59+
schema:
60+
type: string
61+
- name: passwd
62+
in: path
63+
required: true
64+
schema:
65+
type: string
66+
- name: algorithm
67+
in: path
68+
description: MD5, SHA-256, SHA-512
69+
required: true
70+
schema:
71+
type: string
72+
default: MD5
73+
- name: stale_after
74+
in: path
75+
required: true
76+
schema:
77+
type: string
78+
default: never
79+
responses:
80+
200:
81+
description: Sucessful authentication.
82+
content: {}
83+
401:
84+
description: Unsuccessful authentication.
85+
content: {}
86+
components: {}

0 commit comments

Comments
 (0)