Skip to content

Commit

Permalink
fix: openapi converter generated route missing service id (#106)
Browse files Browse the repository at this point in the history
  • Loading branch information
bzp2010 authored Jan 9, 2024
1 parent 13594d4 commit 29f2570
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 31 deletions.
13 changes: 7 additions & 6 deletions internal/pkg/openapi2apisix/openapi2apisix.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func Convert(ctx context.Context, oas []byte) (*apitypes.Configuration, error) {
ups.ID = common.GenID(ups.Name)

result.Services = []*apitypes.Service{
&apitypes.Service{
{
ID: common.GenID(doc.Info.Title),
Name: doc.Info.Title,
Description: doc.Info.Description,
Expand All @@ -76,13 +76,13 @@ func Convert(ctx context.Context, oas []byte) (*apitypes.Configuration, error) {
}

// handle routes
routes := createRoutes(doc, doc.Info.Title)
routes := createRoutes(doc, result.Services[0].ID)
result.Routes = routes

return &result, nil
}

func createRoutes(doc *openapi3.T, serviceName string) []*apitypes.Route {
func createRoutes(doc *openapi3.T, serviceID string) []*apitypes.Route {
// create a sorted array of paths, to be deterministic in our output order
sortedPaths := make([]string, len(doc.Paths))
i := 0
Expand All @@ -108,7 +108,7 @@ func createRoutes(doc *openapi3.T, serviceName string) []*apitypes.Route {
// route name
routeName := operation.OperationID
if routeName == "" {
routeName = Slugify(serviceName, method, path)
routeName = Slugify(doc.Info.Title, method, path)
}
routeDescription := operation.Summary
if routeDescription == "" {
Expand All @@ -124,8 +124,9 @@ func createRoutes(doc *openapi3.T, serviceName string) []*apitypes.Route {
Name: routeName,
Description: routeDescription,
// Labels: tags,
Methods: []string{method},
Uris: []string{routePath},
Methods: []string{method},
Uris: []string{routePath},
ServiceID: serviceID,
}
routes = append(routes, &route)
}
Expand Down
53 changes: 28 additions & 25 deletions internal/pkg/openapi2apisix/openapi2apisix_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func TestConvert(t *testing.T) {
},
want: &apitypes.Configuration{
Services: []*apitypes.Service{
&apitypes.Service{
{
Name: "API 101",
Description: `API 101 template for learning API request basics. Follow along with the webinar / video or just open the first request and hit **Send**!`,
Upstream: &apitypes.Upstream{
Expand Down Expand Up @@ -121,35 +121,35 @@ func TestConvert(t *testing.T) {
},
},
Routes: []*apitypes.Route{
&apitypes.Route{
{
Name: "api-101_get_customer",
Description: "Get one customer",
// Labels: []string{"default"},
Methods: []string{"GET"},
Uris: []string{"/customer"},
},
&apitypes.Route{
{
Name: "api-101_post_customer",
Description: "Add new customer",
// Labels: []string{"default"},
Methods: []string{"POST"},
Uris: []string{"/customer"},
},
&apitypes.Route{
{
Name: "api-101_delete_customer-customer-id",
Description: "Remove customer",
// Labels: []string{"default"},
Methods: []string{"DELETE"},
Uris: []string{"/customer/*"},
},
&apitypes.Route{
{
Name: "api-101_put_customer-customer-id",
Description: "Update customer",
// Labels: []string{"default"},
Methods: []string{"PUT"},
Uris: []string{"/customer/*"},
},
&apitypes.Route{
{
Name: "api-101_get_customers",
Description: "Get all customers",
// Labels: []string{"default"},
Expand All @@ -167,7 +167,7 @@ func TestConvert(t *testing.T) {
},
want: &apitypes.Configuration{
Services: []*apitypes.Service{
&apitypes.Service{
{
Name: "API 101",
Description: "modify operationId",
Upstream: &apitypes.Upstream{
Expand All @@ -190,14 +190,14 @@ func TestConvert(t *testing.T) {
},
},
Routes: []*apitypes.Route{
&apitypes.Route{
{
Name: "update Customer",
Description: "Update customer",
// Labels: []string{"default"},
Methods: []string{"PUT"},
Uris: []string{"/customer/*"},
},
&apitypes.Route{
{
Name: "getCustomers",
Description: "Get all customers",
// Labels: []string{"default"},
Expand All @@ -215,7 +215,7 @@ func TestConvert(t *testing.T) {
},
want: &apitypes.Configuration{
Services: []*apitypes.Service{
&apitypes.Service{
{
Name: "API 101",
Description: "modify operationId",
Upstream: &apitypes.Upstream{
Expand All @@ -240,7 +240,7 @@ func TestConvert(t *testing.T) {
},
},
Routes: []*apitypes.Route{
&apitypes.Route{
{
Name: "getCustomers",
Description: "Get all customers",
// Labels: []string{"default", "customer"},
Expand All @@ -258,7 +258,7 @@ func TestConvert(t *testing.T) {
},
want: &apitypes.Configuration{
Services: []*apitypes.Service{
&apitypes.Service{
{
Name: "API 101",
Description: "modify operationId",
Upstream: &apitypes.Upstream{
Expand All @@ -283,7 +283,7 @@ func TestConvert(t *testing.T) {
},
},
Routes: []*apitypes.Route{
&apitypes.Route{
{
Name: "getCustomers",
Description: "Get all customers",
// Labels: []string{"default", "customer"},
Expand All @@ -303,21 +303,24 @@ func TestConvert(t *testing.T) {
return
}
if got != nil {
for _, route := range tt.want.Routes {
route.ID = common.GenID(route.Name)
}
for _, svc := range tt.want.Services {
svc.ID = common.GenID(svc.Name)
if svc.Upstream != nil {
if svc.Upstream.Nodes == nil {
svc.Upstream.Nodes = []apitypes.UpstreamNode{
apitypes.UpstreamNode{},
}
// normally the converter should only generate one service
svc := tt.want.Services[0]
svc.ID = common.GenID(svc.Name)
if svc.Upstream != nil {
if svc.Upstream.Nodes == nil {
svc.Upstream.Nodes = []apitypes.UpstreamNode{
{},
}
svc.Upstream.Name = Slugify("Upstream for", svc.Name)
svc.Upstream.ID = common.GenID(svc.Upstream.Name)
}
svc.Upstream.Name = Slugify("Upstream for", svc.Name)
svc.Upstream.ID = common.GenID(svc.Upstream.Name)
}

for _, route := range tt.want.Routes {
route.ID = common.GenID(route.Name)
route.ServiceID = svc.ID
}

assert.Equal(t, tt.want.Services, got.Services)
assert.Equal(t, tt.want.Routes, got.Routes)
}
Expand Down
1 change: 1 addition & 0 deletions test/cli/suites-consumer/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ var _ = ginkgo.Describe("`adc sync` tests", func() {
"time_window": 60,
"rejected_code": 503,
"key": "remote_addr",
"_vid": "suites-consumer",
},
},
}
Expand Down

0 comments on commit 29f2570

Please sign in to comment.