Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: convert url variables to colon x #109

Merged
merged 2 commits into from
Jan 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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: {}