{"record":{"id":"f23fdb8ac03d476a","repo":"apache/shenyu","slug":"invalid-swagger-json-format","errorCode":null,"errorMessage":"Invalid Swagger JSON format: ","messagePattern":"Invalid Swagger JSON format: ","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":400,"severity":"error","filePath":"shenyu-admin/src/main/java/org/apache/shenyu/admin/service/impl/SwaggerImportServiceImpl.java","lineNumber":300,"sourceCode":"\n\n\n    private void validateSwaggerContent(final String swaggerJson) {\n        try {\n            JsonObject docRoot = GsonUtils.getInstance().fromJson(swaggerJson, JsonObject.class);\n            \n            // Detect version\n            boolean isV2 = docRoot.has(\"swagger\") && docRoot.get(\"swagger\").getAsString().startsWith(\"2.\");\n            boolean isV3 = docRoot.has(\"openapi\") && docRoot.get(\"openapi\").getAsString().startsWith(\"3.\");\n            \n            if (!isV2 && !isV3) {\n                throw new IllegalArgumentException(\"Unsupported Swagger version, only Swagger 2.0 and OpenAPI 3.0 formats are supported\");\n            }\n            \n            LOG.info(\"Detected Swagger version: {}\", isV2 ? \"2.0\" : \"3.0\");\n            \n        } catch (Exception e) {\n            throw new IllegalArgumentException(\"Invalid Swagger JSON format: \" + e.getMessage());\n        }\n    }\n    \n    private UpstreamInstance createVirtualInstance(final SwaggerImportRequest request) {\n        UpstreamInstance instance = new UpstreamInstance();\n        instance.setContextPath(request.getProjectName());\n        \n        // Try to parse IP and port from URL\n        try {\n            URL url = new URL(request.getSwaggerUrl());\n            instance.setIp(url.getHost());\n            instance.setPort(url.getPort() == -1 ? (url.getProtocol().equals(\"https\") ? 443 : 80) : url.getPort());\n        } catch (Exception e) {\n            instance.setIp(\"unknown\");\n            instance.setPort(80);\n        }\n        \n        return instance;","sourceCodeStart":282,"sourceCodeEnd":318,"githubUrl":"https://github.com/apache/shenyu/blob/567142e07261b3e615ae8850b30f4421f455cc5d/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/impl/SwaggerImportServiceImpl.java#L282-L318","documentation":"validateSwaggerContent() catches all exceptions during JSON parsing/version detection and re-throws IllegalArgumentException('Invalid Swagger JSON format: ' + message). It fires when the fetched content is not parseable JSON at all (JsonSyntaxException / parse error), not merely a wrong version. The original parse message is appended for diagnosis.","triggerScenarios":"The swaggerUrl returns HTML (login page, error page), an empty body, or truncated/malformed JSON — the initial docRoot parse inside the try block throws and is converted to IllegalArgumentException.","commonSituations":"Endpoint redirects to an SSO login page returning HTML; proxy returns a gzip/compressed body that is not decoded; response body truncated by a size limit; content served as JSON5 or with comments.","solutions":["Inspect the appended message in 'Invalid Swagger JSON format: ...' to see the exact parse failure.","curl the URL and confirm the body is pure JSON (login/error HTML is the most common culprit).","Fix auth so the raw JSON document is served without redirect or HTML wrapping.","Validate the JSON with a linter/JSON parser before importing."],"exampleFix":null,"handlingStrategy":"validation","validationCode":"String body = fetch(swaggerUrl);\nif (body == null || body.isBlank()) throw new IllegalArgumentException(\"empty response from swagger URL\");\ntry { JsonParser.parseString(body); } catch (JsonSyntaxException e) {\n    throw new IllegalArgumentException(\"URL did not return JSON (got HTML/error page?): \" + e.getMessage());\n}","typeGuard":null,"tryCatchPattern":"try {\n    service.importSwagger(request);\n} catch (IllegalArgumentException e) {\n    if (e.getMessage().startsWith(\"Invalid Swagger JSON format\")) {\n        // log the raw fetched body to inspect what was actually returned\n    }\n    throw e;\n}","preventionTips":["Confirm with curl that the endpoint serves raw JSON, not an HTML login/error page.","Handle auth/SSO so requests to the swagger URL are not redirected to HTML.","Validate JSON with a parser before import.","Beware proxies/gateways returning error pages with 200 status."],"tags":["json","validation","swagger","parse-error"],"backgroundTag":"json-parse-error","analyzedSha":"567142e07261b3e615ae8850b30f4421f455cc5d","analyzedAt":"2026-09-12T10:08:21.293Z","contentChangedAt":"2026-09-12T10:08:21.293Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}