Skip to content

Commit

Permalink
fix: convert url variables to colon x (#109)
Browse files Browse the repository at this point in the history
  • Loading branch information
bzp2010 authored Jan 10, 2024
1 parent 1398493 commit 567d5b8
Show file tree
Hide file tree
Showing 3 changed files with 145 additions and 4 deletions.
4 changes: 3 additions & 1 deletion internal/pkg/openapi2apisix/openapi2apisix.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ var (
)

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

// Slugify converts a name to a valid name by removing and replacing unallowed characters
Expand Down
59 changes: 56 additions & 3 deletions internal/pkg/openapi2apisix/openapi2apisix_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ var (
tagsTest []byte
//go:embed testdata/tags.json
tagsJsonTest []byte
//go:embed testdata/urlVariables.yaml
urlVariables []byte
)

func TestConvert(t *testing.T) {
Expand Down Expand Up @@ -140,14 +142,14 @@ func TestConvert(t *testing.T) {
Description: "Remove customer",
// Labels: []string{"default"},
Methods: []string{"DELETE"},
Uris: []string{"/customer/*"},
Uris: []string{"/customer/:customer_id"},
},
{
Name: "api-101_put_customer-customer-id",
Description: "Update customer",
// Labels: []string{"default"},
Methods: []string{"PUT"},
Uris: []string{"/customer/*"},
Uris: []string{"/customer/:customer_id"},
},
{
Name: "api-101_get_customers",
Expand Down Expand Up @@ -195,7 +197,7 @@ func TestConvert(t *testing.T) {
Description: "Update customer",
// Labels: []string{"default"},
Methods: []string{"PUT"},
Uris: []string{"/customer/*"},
Uris: []string{"/customer/:customer_id"},
},
{
Name: "getCustomers",
Expand Down Expand Up @@ -294,6 +296,57 @@ func TestConvert(t *testing.T) {
},
wantErr: false,
},
{
name: "urlVariables",
args: args{
content: urlVariables,
},
want: &apitypes.Configuration{
Services: []*apitypes.Service{
{
Name: "URL variables",
Upstream: &apitypes.Upstream{
Scheme: "https",
Nodes: []apitypes.UpstreamNode{
{
Host: "example.com",
Port: 443,
Weight: 100,
},
},
Timeout: &apitypes.UpstreamTimeout{
Connect: 60,
Send: 60,
Read: 60,
},
PassHost: apitypes.UpstreamPassHostPass,
},
// Labels: make([]apitypes.Labels, 0),
},
},
Routes: []*apitypes.Route{
{
Name: "url-variables_get_base64-value",
// Labels: []string{"default"},
Methods: []string{"GET"},
Uris: []string{"/base64/:value"},
},
{
Name: "url-variables_get_basic-auth-user-passwd",
// Labels: []string{"default"},
Methods: []string{"GET"},
Uris: []string{"/basic-auth/:user/:passwd"},
},
{
Name: "url-variables_get_digest-auth-qop-user-passwd-algorithm-stale-after",
// Labels: []string{"default"},
Methods: []string{"GET"},
Uris: []string{"/digest-auth/:qop/:user/:passwd/:algorithm/:stale_after"},
},
},
},
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down
86 changes: 86 additions & 0 deletions internal/pkg/openapi2apisix/testdata/urlVariables.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
openapi: 3.0.1
info:
title: URL variables
version: 1.0.0
servers:
- url: https://example.com/
paths:
/base64/{value}:
get:
tags:
- Dynamic data
parameters:
- name: value
in: path
required: true
schema:
type: string
default: SFRUUEJJTiBpcyBhd2Vzb21l
responses:
200:
description: Decoded base64 content.
content: {}
/basic-auth/{user}/{passwd}:
get:
tags:
- Auth
parameters:
- name: user
in: path
required: true
schema:
type: string
- name: passwd
in: path
required: true
schema:
type: string
responses:
200:
description: Sucessful authentication.
content: {}
401:
description: Unsuccessful authentication.
content: {}
/digest-auth/{qop}/{user}/{passwd}/{algorithm}/{stale_after}:
get:
tags:
- Auth
parameters:
- name: qop
in: path
description: auth or auth-int
required: true
schema:
type: string
- name: user
in: path
required: true
schema:
type: string
- name: passwd
in: path
required: true
schema:
type: string
- name: algorithm
in: path
description: MD5, SHA-256, SHA-512
required: true
schema:
type: string
default: MD5
- name: stale_after
in: path
required: true
schema:
type: string
default: never
responses:
200:
description: Sucessful authentication.
content: {}
401:
description: Unsuccessful authentication.
content: {}
components: {}

0 comments on commit 567d5b8

Please sign in to comment.