From 29344853a4ed8c8b165501eb0cf3c87f186cf235 Mon Sep 17 00:00:00 2001 From: rdoboni Date: Thu, 22 Jan 2026 01:23:01 +0100 Subject: [PATCH] Fix isRelativeUrl incorrectly detecting URLs containing @, -, ~, . as not relative. --- .../codegen/utils/URLPathUtils.java | 2 +- .../codegen/DefaultGeneratorTest.java | 26 +++++++++++++++++++ .../test/resources/3_0/relative-url-at.yaml | 15 +++++++++++ .../test/resources/3_0/relative-url-dash.yaml | 15 +++++++++++ .../resources/3_0/relative-url-point.yaml | 15 +++++++++++ .../resources/3_0/relative-url-tilde.yaml | 15 +++++++++++ 6 files changed, 87 insertions(+), 1 deletion(-) create mode 100644 modules/openapi-generator/src/test/resources/3_0/relative-url-at.yaml create mode 100644 modules/openapi-generator/src/test/resources/3_0/relative-url-dash.yaml create mode 100644 modules/openapi-generator/src/test/resources/3_0/relative-url-point.yaml create mode 100644 modules/openapi-generator/src/test/resources/3_0/relative-url-tilde.yaml 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 289d9febe856..47c98f557411 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 @@ -235,7 +235,7 @@ private static URL getDefaultUrl() { 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 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 92fab8b73d95..1ba9a2c7c5c6 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 @@ -671,6 +671,32 @@ public void testHandlesTrailingSlashInServers() { Assert.assertEquals(servers.get(2).url, "http://notrailingslash.io:80/v2"); } + @Test + public void testHandlesRelativeUrlsWithSpecialChars() { + final Map specToBasePath = Map.of( + "src/test/resources/3_0/relative-url-point.yaml", "/api/v4.0", + "src/test/resources/3_0/relative-url-dash.yaml", "/api-v3", + "src/test/resources/3_0/relative-url-tilde.yaml", "/~api/v5", + "src/test/resources/3_0/relative-url-at.yaml", "/api/@6" + ); + + specToBasePath.forEach((spec, expectedBasePath) -> { + OpenAPI openAPI = TestUtils.parseFlattenSpec(spec); + ClientOptInput opts = new ClientOptInput(); + opts.openAPI(openAPI); + DefaultCodegen config = new DefaultCodegen(); + config.setStrictSpecBehavior(false); + opts.config(config); + final DefaultGenerator generator = new DefaultGenerator(); + generator.opts(opts); + generator.configureGeneratorProperties(); + + Map bundle = generator.buildSupportFileBundle(new ArrayList<>(), new ArrayList<>(), new ArrayList<>()); + final String actualBasePath = (String) bundle.get("basePath"); + Assert.assertEquals(actualBasePath, expectedBasePath); + }); + } + @Test public void testHandlesRelativeUrlsInServers() { OpenAPI openAPI = TestUtils.parseFlattenSpec("src/test/resources/3_0/issue_10056.yaml"); diff --git a/modules/openapi-generator/src/test/resources/3_0/relative-url-at.yaml b/modules/openapi-generator/src/test/resources/3_0/relative-url-at.yaml new file mode 100644 index 000000000000..8dbe70ba081b --- /dev/null +++ b/modules/openapi-generator/src/test/resources/3_0/relative-url-at.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: /api/@6 +tags: [] +paths: {} +components: + schemas: {} + securitySchemes: {} \ No newline at end of file diff --git a/modules/openapi-generator/src/test/resources/3_0/relative-url-dash.yaml b/modules/openapi-generator/src/test/resources/3_0/relative-url-dash.yaml new file mode 100644 index 000000000000..6295b3f3fd9f --- /dev/null +++ b/modules/openapi-generator/src/test/resources/3_0/relative-url-dash.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: /api-v3 +tags: [] +paths: {} +components: + schemas: {} + securitySchemes: {} \ No newline at end of file diff --git a/modules/openapi-generator/src/test/resources/3_0/relative-url-point.yaml b/modules/openapi-generator/src/test/resources/3_0/relative-url-point.yaml new file mode 100644 index 000000000000..35db6758ad6e --- /dev/null +++ b/modules/openapi-generator/src/test/resources/3_0/relative-url-point.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: /api/v4.0 +tags: [] +paths: {} +components: + schemas: {} + securitySchemes: {} \ No newline at end of file diff --git a/modules/openapi-generator/src/test/resources/3_0/relative-url-tilde.yaml b/modules/openapi-generator/src/test/resources/3_0/relative-url-tilde.yaml new file mode 100644 index 000000000000..376700a3d64c --- /dev/null +++ b/modules/openapi-generator/src/test/resources/3_0/relative-url-tilde.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: /~api/v5 +tags: [] +paths: {} +components: + schemas: {} + securitySchemes: {} \ No newline at end of file