From 567d5b89d0159bcd81588a2b08e8c9cb167839b8 Mon Sep 17 00:00:00 2001 From: Zeping Bai Date: Wed, 10 Jan 2024 16:15:43 +0800 Subject: [PATCH] fix: convert url variables to colon x (#109) --- internal/pkg/openapi2apisix/openapi2apisix.go | 4 +- .../pkg/openapi2apisix/openapi2apisix_test.go | 59 ++++++++++++- .../openapi2apisix/testdata/urlVariables.yaml | 86 +++++++++++++++++++ 3 files changed, 145 insertions(+), 4 deletions(-) create mode 100755 internal/pkg/openapi2apisix/testdata/urlVariables.yaml diff --git a/internal/pkg/openapi2apisix/openapi2apisix.go b/internal/pkg/openapi2apisix/openapi2apisix.go index db36a28..316b544 100644 --- a/internal/pkg/openapi2apisix/openapi2apisix.go +++ b/internal/pkg/openapi2apisix/openapi2apisix.go @@ -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 diff --git a/internal/pkg/openapi2apisix/openapi2apisix_test.go b/internal/pkg/openapi2apisix/openapi2apisix_test.go index c1c8b68..6e8848a 100644 --- a/internal/pkg/openapi2apisix/openapi2apisix_test.go +++ b/internal/pkg/openapi2apisix/openapi2apisix_test.go @@ -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) { @@ -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", @@ -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", @@ -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) { diff --git a/internal/pkg/openapi2apisix/testdata/urlVariables.yaml b/internal/pkg/openapi2apisix/testdata/urlVariables.yaml new file mode 100755 index 0000000..ae03680 --- /dev/null +++ b/internal/pkg/openapi2apisix/testdata/urlVariables.yaml @@ -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: {}