diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultGenerator.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultGenerator.java index 62e678621d4..815342dfea7 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultGenerator.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultGenerator.java @@ -268,7 +268,11 @@ public class DefaultGenerator implements Generator { URL url = URLPathUtils.getServerURL(openAPI, config.serverVariableOverrides()); contextPath = removeTrailingSlash(config.escapeText(url.getPath())); // for backward compatibility basePathWithoutHost = contextPath; - basePath = removeTrailingSlash(config.escapeText(URLPathUtils.getHost(openAPI, config.serverVariableOverrides()))); + if (URLPathUtils.isRelativeUrl(openAPI.getServers())) { + basePath = removeTrailingSlash(basePathWithoutHost); + } else { + basePath = removeTrailingSlash(config.escapeText(URLPathUtils.getHost(openAPI, config.serverVariableOverrides()))); + } } private void configureOpenAPIInfo() { diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/utils/URLPathUtils.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/utils/URLPathUtils.java index 5ecdf65d2bd..7bdaac0d04f 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/utils/URLPathUtils.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/utils/URLPathUtils.java @@ -231,4 +231,12 @@ public class URLPathUtils { return null; } } + + public static boolean isRelativeUrl(List servers) { + if (servers != null && servers.size() > 0) { + final Server firstServer = servers.get(0); + return Pattern.matches("^(\\/[\\w\\d]+)+", firstServer.getUrl()); + } + return false; + } } diff --git a/modules/openapi-generator/src/test/java/org/openapitools/codegen/DefaultGeneratorTest.java b/modules/openapi-generator/src/test/java/org/openapitools/codegen/DefaultGeneratorTest.java index 81869d78f91..e5613d745cf 100644 --- a/modules/openapi-generator/src/test/java/org/openapitools/codegen/DefaultGeneratorTest.java +++ b/modules/openapi-generator/src/test/java/org/openapitools/codegen/DefaultGeneratorTest.java @@ -662,6 +662,30 @@ public class DefaultGeneratorTest { Assert.assertEquals(servers.get(1).url, "http://trailingshlash.io:80/v1"); Assert.assertEquals(servers.get(2).url, "http://notrailingslash.io:80/v2"); } + + @Test + public void testHandlesRelativeUrlsInServers() { + OpenAPI openAPI = TestUtils.parseFlattenSpec("src/test/resources/3_0/issue_10056.yaml"); + ClientOptInput opts = new ClientOptInput(); + opts.openAPI(openAPI); + DefaultCodegen config = new DefaultCodegen(); + config.setStrictSpecBehavior(true); + opts.config(config); + final DefaultGenerator generator = new DefaultGenerator(); + generator.opts(opts); + generator.configureGeneratorProperties(); + + List files = new ArrayList<>(); + List filteredSchemas = ModelUtils.getSchemasUsedOnlyInFormParam(openAPI); + List allModels = new ArrayList<>(); + generator.generateModels(files, allModels, filteredSchemas); + List allOperations = new ArrayList<>(); + generator.generateApis(files, allOperations, allModels); + + Map bundle = generator.buildSupportFileBundle(allOperations, allModels); + LinkedList servers = (LinkedList) bundle.get("servers"); + Assert.assertEquals(servers.get(0).url, "/relative/url"); + } @Test public void testProcessUserDefinedTemplatesWithConfig() throws IOException { diff --git a/modules/openapi-generator/src/test/resources/3_0/issue_10056.yaml b/modules/openapi-generator/src/test/resources/3_0/issue_10056.yaml new file mode 100644 index 00000000000..b33765bdf17 --- /dev/null +++ b/modules/openapi-generator/src/test/resources/3_0/issue_10056.yaml @@ -0,0 +1,15 @@ +openapi: 3.0.1 +info: + title: OpenAPI Petstore + description: "sample spec" + license: + name: Apache-2.0 + url: https://www.apache.org/licenses/LICENSE-2.0.html + version: 1.0.0 +servers: + - url: /relative/url +tags: [] +paths: {} +components: + schemas: {} + securitySchemes: {} \ No newline at end of file