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


##########
api/internal/handler/data_loader/loader/openapi3/import.go:
##########
@@ -0,0 +1,149 @@
+/*
+ * 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"
+       "fmt"
+       "net/url"
+       "reflect"
+       "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")
+       }
+
+       d, ok := input.([]byte)
+       if !ok {
+               return nil, fmt.Errorf("input format error: expected []byte but 
it is %s", reflect.TypeOf(input).Kind().String())
+       }
+
+       // load OAS3 document
+       swagger, err := openapi3.NewSwaggerLoader().LoadSwaggerFromData(d)
+       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 = ""
+       )
+
+       // 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
+               routeName := o.TaskName + "_" + strings.TrimPrefix(uri, "/")

Review Comment:
   I took a look at the name length limit in APISIX and it has a maximum value 
of 100, so it is indeed possible to trigger this limit. do you have a better 
idea for the route name generation rules.
   
   That's the way now:
   https://github.com/apache/apisix-dashboard/pull/2460#issuecomment-1152153563



##########
api/internal/handler/data_loader/loader/openapi3/import.go:
##########
@@ -0,0 +1,149 @@
+/*
+ * 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"
+       "fmt"
+       "net/url"
+       "reflect"
+       "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")
+       }
+
+       d, ok := input.([]byte)
+       if !ok {
+               return nil, fmt.Errorf("input format error: expected []byte but 
it is %s", reflect.TypeOf(input).Kind().String())
+       }
+
+       // load OAS3 document
+       swagger, err := openapi3.NewSwaggerLoader().LoadSwaggerFromData(d)
+       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 = ""
+       )
+
+       // 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
+               routeName := o.TaskName + "_" + strings.TrimPrefix(uri, "/")

Review Comment:
   @nic-6443 I took a look at the name length limit in APISIX and it has a 
maximum value of 100, so it is indeed possible to trigger this limit. do you 
have a better idea for the route name generation rules.
   
   That's the way now:
   https://github.com/apache/apisix-dashboard/pull/2460#issuecomment-1152153563



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