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


##########
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, "/"))
+
+               // decide whether to merge multi-method routes based on 
configuration
+               if o.MergeMethod {

Review Comment:
   @starsz As I mentioned in the Proposal, different methods under the same URI 
may have different properties, for example:
   
   ```text
   GET /user/{id}    Get a user's infomation
   POST /user/{id}    Modify a user's infomation.
   
   On POST method that need a Admin permission session, but GET not need it, so 
the `/user/{id}`'s method has different `securitySchema`.
   ```
   
   To handle this case, we need to allow the user to choose not to merge 
methods.



-- 
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