You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@apisix.apache.org by GitBox <gi...@apache.org> on 2022/03/17 06:49:32 UTC

[GitHub] [apisix-dashboard] nic-chen commented on a change in pull request #2373: refactor: OpenAPI import

nic-chen commented on a change in pull request #2373:
URL: https://github.com/apache/apisix-dashboard/pull/2373#discussion_r828792940



##########
File path: api/internal/handler/data_loader/loader/openapi3/import.go
##########
@@ -0,0 +1,172 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package openapi3
+
+import (
+	"net/url"
+	"strings"
+	"time"
+
+	"github.com/getkin/kin-openapi/openapi3"
+
+	"github.com/apisix/manager-api/internal/core/entity"
+	"github.com/apisix/manager-api/internal/handler/data_loader/loader"
+	"github.com/apisix/manager-api/internal/utils/consts"
+)
+
+func (o Loader) Import(input interface{}) (*loader.DataSets, error) {
+	swagger, err := openapi3.NewSwaggerLoader().LoadSwaggerFromData(input.([]byte))
+	if err != nil {
+		return nil, err
+	}
+
+	if len(swagger.Paths) < 1 {
+		return nil, consts.ErrImportFile
+	}
+
+	if o.TaskName == "" {
+		o.TaskName = "openapi_" + time.Now().Format("20060102150405")
+	}
+	data, err := o.convertToEntities(swagger)
+	if err != nil {
+		return nil, err
+	}
+
+	return data, nil
+}
+
+func (o Loader) convertToEntities(s *openapi3.Swagger) (*loader.DataSets, error) {
+	var (
+		// temporarily save the parsed data
+		data = &loader.DataSets{}
+		// global upstream ID
+		globalUpstreamID = o.TaskName + "_global"
+		// global uri prefix
+		globalPath = ""
+	)
+
+	// create upstream when global Servers exist
+	if len(s.Servers) > 0 {
+		var upstream entity.Upstream
+		upstream, globalPath = generateUpstreamByServers(s.Servers, globalUpstreamID)
+		data.Upstreams = append(data.Upstreams, upstream)
+	}
+
+	// each one will correspond to a route
+	for uri, v := range s.Paths {
+		realUri := regURIVar.ReplaceAllString(uri, "*")
+		routeID := o.TaskName + "_" + strings.NewReplacer("/", "-", "{", "", "}", "").Replace(strings.TrimPrefix(uri, "/"))
+
+		// decide whether to merge multi method routes based on configuration
+		if o.MergeRoute {

Review comment:
       We have a contributor who submitted a PR to merge routes with the same URI:
   https://github.com/apache/apisix-dashboard/pull/2330
   
   I think this feature should also be included after the refactoring:
   

##########
File path: api/internal/handler/data_loader/loader/openapi3/import.go
##########
@@ -0,0 +1,172 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package openapi3
+
+import (
+	"net/url"
+	"strings"
+	"time"
+
+	"github.com/getkin/kin-openapi/openapi3"
+
+	"github.com/apisix/manager-api/internal/core/entity"
+	"github.com/apisix/manager-api/internal/handler/data_loader/loader"
+	"github.com/apisix/manager-api/internal/utils/consts"
+)
+
+func (o Loader) Import(input interface{}) (*loader.DataSets, error) {
+	swagger, err := openapi3.NewSwaggerLoader().LoadSwaggerFromData(input.([]byte))
+	if err != nil {
+		return nil, err
+	}
+
+	if len(swagger.Paths) < 1 {
+		return nil, consts.ErrImportFile
+	}
+
+	if o.TaskName == "" {
+		o.TaskName = "openapi_" + time.Now().Format("20060102150405")
+	}
+	data, err := o.convertToEntities(swagger)
+	if err != nil {
+		return nil, err
+	}
+
+	return data, nil
+}
+
+func (o Loader) convertToEntities(s *openapi3.Swagger) (*loader.DataSets, error) {
+	var (
+		// temporarily save the parsed data
+		data = &loader.DataSets{}
+		// global upstream ID
+		globalUpstreamID = o.TaskName + "_global"
+		// global uri prefix
+		globalPath = ""
+	)
+
+	// create upstream when global Servers exist
+	if len(s.Servers) > 0 {
+		var upstream entity.Upstream
+		upstream, globalPath = generateUpstreamByServers(s.Servers, globalUpstreamID)
+		data.Upstreams = append(data.Upstreams, upstream)
+	}
+
+	// each one will correspond to a route
+	for uri, v := range s.Paths {
+		realUri := regURIVar.ReplaceAllString(uri, "*")
+		routeID := o.TaskName + "_" + strings.NewReplacer("/", "-", "{", "", "}", "").Replace(strings.TrimPrefix(uri, "/"))
+
+		// decide whether to merge multi method routes based on configuration
+		if o.MergeRoute {
+			// create a single route for each path
+			route := generateBaseRoute(routeID, v.Summary)
+			route.Uris = []string{realUri}
+
+			// merge methods
+			for method := range v.Operations() {
+				route.Methods = append(route.Methods, strings.ToUpper(method))
+			}
+
+			// add upstream
+			if len(v.Servers) > 0 {
+				// create a new upstream when current path has separate Servers
+				upstreamID := routeID
+				upstream, path := generateUpstreamByServers(s.Servers, upstreamID)

Review comment:
       `s.Servers` or `v.Servers`?




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org