Skip to content

Commit 5926c31

Browse files
authored
Fix isRelativeUrl incorrectly detecting URLs containing @, -, ~, . as not relative. (#22768)
1 parent 761cb77 commit 5926c31

6 files changed

Lines changed: 87 additions & 1 deletion

File tree

modules/openapi-generator/src/main/java/org/openapitools/codegen/utils/URLPathUtils.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ private static URL getDefaultUrl() {
235235
public static boolean isRelativeUrl(List<Server> servers) {
236236
if (servers != null && servers.size() > 0) {
237237
final Server firstServer = servers.get(0);
238-
return Pattern.matches("^(\\/[\\w\\d]+)+", firstServer.getUrl());
238+
return Pattern.matches("^(\\/[\\w\\d.~@-]+)+", firstServer.getUrl());
239239
}
240240
return false;
241241
}

modules/openapi-generator/src/test/java/org/openapitools/codegen/DefaultGeneratorTest.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -671,6 +671,32 @@ public void testHandlesTrailingSlashInServers() {
671671
Assert.assertEquals(servers.get(2).url, "http://notrailingslash.io:80/v2");
672672
}
673673

674+
@Test
675+
public void testHandlesRelativeUrlsWithSpecialChars() {
676+
final Map<String, String> specToBasePath = Map.of(
677+
"src/test/resources/3_0/relative-url-point.yaml", "/api/v4.0",
678+
"src/test/resources/3_0/relative-url-dash.yaml", "/api-v3",
679+
"src/test/resources/3_0/relative-url-tilde.yaml", "/~api/v5",
680+
"src/test/resources/3_0/relative-url-at.yaml", "/api/@6"
681+
);
682+
683+
specToBasePath.forEach((spec, expectedBasePath) -> {
684+
OpenAPI openAPI = TestUtils.parseFlattenSpec(spec);
685+
ClientOptInput opts = new ClientOptInput();
686+
opts.openAPI(openAPI);
687+
DefaultCodegen config = new DefaultCodegen();
688+
config.setStrictSpecBehavior(false);
689+
opts.config(config);
690+
final DefaultGenerator generator = new DefaultGenerator();
691+
generator.opts(opts);
692+
generator.configureGeneratorProperties();
693+
694+
Map<String, Object> bundle = generator.buildSupportFileBundle(new ArrayList<>(), new ArrayList<>(), new ArrayList<>());
695+
final String actualBasePath = (String) bundle.get("basePath");
696+
Assert.assertEquals(actualBasePath, expectedBasePath);
697+
});
698+
}
699+
674700
@Test
675701
public void testHandlesRelativeUrlsInServers() {
676702
OpenAPI openAPI = TestUtils.parseFlattenSpec("src/test/resources/3_0/issue_10056.yaml");
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
openapi: 3.0.1
2+
info:
3+
title: OpenAPI Petstore
4+
description: "sample spec"
5+
license:
6+
name: Apache-2.0
7+
url: https://www.apache.org/licenses/LICENSE-2.0.html
8+
version: 1.0.0
9+
servers:
10+
- url: /api/@6
11+
tags: []
12+
paths: {}
13+
components:
14+
schemas: {}
15+
securitySchemes: {}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
openapi: 3.0.1
2+
info:
3+
title: OpenAPI Petstore
4+
description: "sample spec"
5+
license:
6+
name: Apache-2.0
7+
url: https://www.apache.org/licenses/LICENSE-2.0.html
8+
version: 1.0.0
9+
servers:
10+
- url: /api-v3
11+
tags: []
12+
paths: {}
13+
components:
14+
schemas: {}
15+
securitySchemes: {}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
openapi: 3.0.1
2+
info:
3+
title: OpenAPI Petstore
4+
description: "sample spec"
5+
license:
6+
name: Apache-2.0
7+
url: https://www.apache.org/licenses/LICENSE-2.0.html
8+
version: 1.0.0
9+
servers:
10+
- url: /api/v4.0
11+
tags: []
12+
paths: {}
13+
components:
14+
schemas: {}
15+
securitySchemes: {}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
openapi: 3.0.1
2+
info:
3+
title: OpenAPI Petstore
4+
description: "sample spec"
5+
license:
6+
name: Apache-2.0
7+
url: https://www.apache.org/licenses/LICENSE-2.0.html
8+
version: 1.0.0
9+
servers:
10+
- url: /~api/v5
11+
tags: []
12+
paths: {}
13+
components:
14+
schemas: {}
15+
securitySchemes: {}

0 commit comments

Comments
 (0)