Copilot commented on code in PR #6050:
URL: https://github.com/apache/shenyu/pull/6050#discussion_r2196308311


##########
shenyu-admin/src/main/java/org/apache/shenyu/admin/service/manager/impl/SwaggerDocParser.java:
##########
@@ -56,7 +57,12 @@ public class SwaggerDocParser implements DocParser {
      */
     @Override
     public DocInfo parseJson(final JsonObject docRoot) {
-        final String basePath = docRoot.get("basePath").getAsString();
+        // Detect Swagger version
+        SwaggerVersion version = detectSwaggerVersion(docRoot);
+        
+        final String basePath = Optional.ofNullable(docRoot.get("basePath"))
+            .map(JsonElement::getAsString)
+            .orElse("/");

Review Comment:
   For OpenAPI 3.0, the base path is usually defined in the `servers` array 
rather than `basePath`. Consider adding logic to extract `servers[0].url` when 
`openapi` is present to build correct request paths.
   ```suggestion
           final String basePath;
           if (docRoot.has("openapi")) { // Check for OpenAPI 3.0
               JsonArray servers = 
Optional.ofNullable(docRoot.getAsJsonArray("servers")).orElse(new JsonArray());
               basePath = servers.size() > 0 
                   ? 
Optional.ofNullable(servers.get(0).getAsJsonObject().get("url"))
                       .map(JsonElement::getAsString)
                       .orElse("/")
                   : "/";
           } else { // Fallback to Swagger 2.0 logic
               basePath = Optional.ofNullable(docRoot.get("basePath"))
                   .map(JsonElement::getAsString)
                   .orElse("/");
           }
   ```



##########
shenyu-admin/src/main/java/org/apache/shenyu/admin/service/impl/SwaggerImportServiceImpl.java:
##########
@@ -0,0 +1,163 @@
+/*
+ * 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 org.apache.shenyu.admin.service.impl;
+
+import org.apache.shenyu.admin.model.bean.UpstreamInstance;
+import org.apache.shenyu.admin.model.dto.SwaggerImportRequest;
+import org.apache.shenyu.admin.service.SwaggerImportService;
+import org.apache.shenyu.admin.service.manager.DocManager;
+import org.apache.shenyu.admin.utils.HttpUtils;
+import org.apache.shenyu.common.utils.GsonUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.stereotype.Service;
+import okhttp3.Response;
+
+import java.io.IOException;
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.util.Collections;
+import java.util.Objects;
+import java.util.concurrent.atomic.AtomicReference;
+import com.google.gson.JsonObject;
+
+/**
+ * Implementation of the {@link 
org.apache.shenyu.admin.service.SwaggerImportService}.
+ */
+@Service
+public class SwaggerImportServiceImpl implements SwaggerImportService {
+    
+    private static final Logger LOG = 
LoggerFactory.getLogger(SwaggerImportServiceImpl.class);
+
+    private final DocManager docManager;
+    
+    public SwaggerImportServiceImpl(final DocManager docManager) {
+        this.docManager = docManager;
+    }
+    
+    @Override
+    public String importSwagger(final SwaggerImportRequest request) {
+        LOG.info("Start importing Swagger document: {}", request);
+        
+        try {
+            // 1. Validate URL
+            validateSwaggerUrl(request.getSwaggerUrl());
+            
+            // 2. Get swagger document
+            String swaggerJson = fetchSwaggerDoc(request.getSwaggerUrl());
+            
+            // 3. Validate Swagger content and version
+            validateSwaggerContent(swaggerJson);
+            
+            // 4. Create virtual instance
+            UpstreamInstance instance = createVirtualInstance(request);
+            
+            // 5. Parse and save document
+            AtomicReference<String> docMd5 = new AtomicReference<>();

Review Comment:
   [nitpick] Using an `AtomicReference` here adds unnecessary complexity since 
the code runs single-threaded. Consider refactoring `addDocInfo` to return the 
MD5 directly or use a simple one-element holder object.



##########
shenyu-admin/src/main/java/org/apache/shenyu/admin/service/impl/SwaggerImportServiceImpl.java:
##########
@@ -0,0 +1,163 @@
+/*
+ * 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 org.apache.shenyu.admin.service.impl;
+
+import org.apache.shenyu.admin.model.bean.UpstreamInstance;
+import org.apache.shenyu.admin.model.dto.SwaggerImportRequest;
+import org.apache.shenyu.admin.service.SwaggerImportService;
+import org.apache.shenyu.admin.service.manager.DocManager;
+import org.apache.shenyu.admin.utils.HttpUtils;
+import org.apache.shenyu.common.utils.GsonUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.stereotype.Service;
+import okhttp3.Response;
+
+import java.io.IOException;
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.util.Collections;
+import java.util.Objects;
+import java.util.concurrent.atomic.AtomicReference;
+import com.google.gson.JsonObject;
+
+/**
+ * Implementation of the {@link 
org.apache.shenyu.admin.service.SwaggerImportService}.
+ */
+@Service
+public class SwaggerImportServiceImpl implements SwaggerImportService {
+    
+    private static final Logger LOG = 
LoggerFactory.getLogger(SwaggerImportServiceImpl.class);
+
+    private final DocManager docManager;
+    
+    public SwaggerImportServiceImpl(final DocManager docManager) {
+        this.docManager = docManager;
+    }
+    
+    @Override
+    public String importSwagger(final SwaggerImportRequest request) {
+        LOG.info("Start importing Swagger document: {}", request);
+        
+        try {
+            // 1. Validate URL
+            validateSwaggerUrl(request.getSwaggerUrl());
+            
+            // 2. Get swagger document
+            String swaggerJson = fetchSwaggerDoc(request.getSwaggerUrl());
+            
+            // 3. Validate Swagger content and version
+            validateSwaggerContent(swaggerJson);
+            
+            // 4. Create virtual instance
+            UpstreamInstance instance = createVirtualInstance(request);
+            
+            // 5. Parse and save document
+            AtomicReference<String> docMd5 = new AtomicReference<>();
+            docManager.addDocInfo(instance, swaggerJson, null, docInfo -> {
+                docMd5.set(docInfo.getDocMd5());
+                LOG.info("Successfully imported swagger document: {}", 
request.getProjectName());
+            });
+            
+            return "Import successful, supports Swagger 2.0 and OpenAPI 3.0 
formats";
+            
+        } catch (Exception e) {
+            LOG.error("Failed to import swagger document: {}", 
request.getProjectName(), e);
+            throw new RuntimeException("Import failed: " + e.getMessage());
+        }
+    }
+    
+    @Override
+    public boolean testConnection(final String swaggerUrl) {
+        try {
+            HttpUtils httpUtils = new HttpUtils();

Review Comment:
   [nitpick] Instantiating `HttpUtils` directly makes it hard to mock in unit 
tests. Consider injecting an `HttpUtils` bean (via constructor or setter) so 
HTTP calls can be stubbed for reliable testing.
   ```suggestion
   
   ```



-- 
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-unsubscr...@shenyu.apache.org

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

Reply via email to