bzp2010 commented on code in PR #2460:
URL: https://github.com/apache/apisix-dashboard/pull/2460#discussion_r894314863


##########
api/internal/handler/data_loader/loader/openapi3/import.go:
##########
@@ -0,0 +1,180 @@
+/*
+ * 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 (
+       "errors"
+       "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) {
+       if input == nil {
+               return nil, errors.New("input is nil")
+       }
+
+       // load OAS3 document
+       swagger, err := 
openapi3.NewSwaggerLoader().LoadSwaggerFromData(input.([]byte))
+       if err != nil {
+               return nil, err
+       }
+
+       // no paths in OAS3 document
+       if len(swagger.Paths) <= 0 {
+               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 uri prefix
+               globalPath = ""
+
+               // authentication plugins supported by APISIX
+               interestedAuthentication     = false
+               interestedAuthenticationList = 
make(map[string]*openapi3.SecuritySchemeRef)
+       )
+
+       // check securitySchemes settings
+       if len(s.Components.SecuritySchemes) > 0 {
+               for name, security := range s.Components.SecuritySchemes {
+                       if strings.ToLower(security.Value.Type) == "http" &&
+                               strings.ToLower(security.Value.Scheme) == 
"apikey" {
+                               interestedAuthentication = true
+                               interestedAuthenticationList[name] = security
+                       }
+               }
+       }
+
+       // create upstream when servers field not empty
+       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 {
+               // replace parameter in uri to wildcard
+               realUri := regURIVar.ReplaceAllString(uri, "*")
+               // generate route name
+               routeID := o.TaskName + "_" + strings.NewReplacer("/", "-", 
"{", "", "}", "").Replace(strings.TrimPrefix(uri, "/"))

Review Comment:
   After testing, using URI + base64 will generate some very long IDs, which 
may exceed the schema check limit of APISIX on the one hand, and confuse the 
format of ID display in the route list on the other. The symbols in base64 will 
also trigger the schema ID check of APISIX, while for Name only the length is 
required and the content is completely customizable.
   
   
![image](https://user-images.githubusercontent.com/8078418/173029394-579af554-477b-4ba5-a4af-b7e5e771fc95.png)
   
   So I took the following approach, instead of generating IDs, which are 
automatically generated by the dashboard storage layer, we store the 
information in the route name, which has this format `TaskName_URI[_Method]`. 
For a route with a URI of `/consumer/{cid}`, the method is GET and POST, it 
generates this `TaskName_consumer/{cid}[_GET/POST]`
   
   ```text
   taskName: test
   
   methodMerge: true
   Method + URI:
   GET|POST /consumer/{cid} => test_consumer/{cid}
   
   methodMerge: false
   Method + URI:
   GET|POST /consumer/{cid} => test_consumer/{cid}_GET  +  
test_consumer/{cid}_POST
   ```
   



-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to