Skip to content

Commit 1722f55

Browse files
authored
feat: prevent variable resolution when prefixed with $ in server URL templates (#22550)
* feat: no url replacement for $ variables * fix test
1 parent 20136f4 commit 1722f55

File tree

2 files changed

+54
-1
lines changed

2 files changed

+54
-1
lines changed

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
@@ -38,7 +38,7 @@ public class URLPathUtils {
3838

3939
private static final Logger LOGGER = LoggerFactory.getLogger(URLPathUtils.class);
4040
public static final String LOCAL_HOST = "http://localhost";
41-
public static final Pattern VARIABLE_PATTERN = Pattern.compile("\\{([^\\}]+)\\}");
41+
public static final Pattern VARIABLE_PATTERN = Pattern.compile("(?<!\\$)\\{([^\\}]+)\\}");
4242

4343
// TODO: This should probably be moved into generator/workflow type rather than a static like this.
4444
public static URL getServerURL(OpenAPI openAPI, Map<String, String> userDefinedVariables) {

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

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import java.net.URL;
2828
import java.util.Arrays;
2929
import java.util.HashMap;
30+
import java.util.Map;
3031

3132
public class URLPathUtilsTest {
3233

@@ -204,4 +205,56 @@ public void useDefaultUrlWhenServerUrlIsNull() {
204205
URL serverURL = URLPathUtils.getServerURL(server, null);
205206
Assert.assertEquals(serverURL.toString(), "http://localhost");
206207
}
208+
209+
@Test
210+
public void testPropertyUrl() {
211+
String[][] testData = {
212+
{"https://abc1.xyz:9999/some/${my.property}", "https://abc1.xyz:9999/some/${my.property}"},
213+
{"HTTPS://abc2.xyz:9999/some/${my.property}", "https://abc2.xyz:9999/some/${my.property}"},
214+
{"http://abc3.xyz:9999/${my.property}/path", "http://abc3.xyz:9999/${my.property}/path"},
215+
{"HTTP://abc4.xyz:9999/some/${my.property}", "http://abc4.xyz:9999/some/${my.property}"},
216+
{"//abc5.xyz:9999/some/${my.property}", "http://abc5.xyz:9999/some/${my.property}"},
217+
{"abc6.xyz:9999/some/path", "http://abc6.xyz:9999/some/path"},
218+
{"localhost:9000/${my.property}", "http://localhost:9000/${my.property}"},
219+
{"/${my.property}/path", "http://localhost/${my.property}/path"},
220+
{"https://abc1.xyz:9999/some/${my.property}/{version}", "https://abc1.xyz:9999/some/${my.property}/v1"},
221+
{"HTTPS://abc2.xyz:9999/${my.property}/{version}", "https://abc2.xyz:9999/${my.property}/v1"},
222+
{"https://abc1.xyz:9999/some/{version}/${my.property}", "https://abc1.xyz:9999/some/v1/${my.property}"},
223+
{"HTTPS://abc2.xyz:9999/{version}/${my.property}", "https://abc2.xyz:9999/v1/${my.property}"}
224+
225+
};
226+
227+
for (String[] t : testData) {
228+
OpenAPI openAPI = new OpenAPI();
229+
openAPI.addServersItem(new Server().url(t[0]));
230+
231+
Assert.assertEquals(URLPathUtils.getServerURL(openAPI, Map.of("version", "v1") ).toString(), t[1]);
232+
}
233+
}
234+
235+
@Test
236+
public void testPropertyUrlInVariable() {
237+
String[][] testData = {
238+
{"https://abc1.xyz:9999/some/{my.property}", "https://abc1.xyz:9999/some/${my.property}"},
239+
{"HTTPS://abc2.xyz:9999/some/{my.property}", "https://abc2.xyz:9999/some/${my.property}"},
240+
{"http://abc3.xyz:9999/{my.property}/path", "http://abc3.xyz:9999/${my.property}/path"},
241+
{"HTTP://abc4.xyz:9999/some/{my.property}", "http://abc4.xyz:9999/some/${my.property}"},
242+
{"//abc5.xyz:9999/some/{my.property}", "http://abc5.xyz:9999/some/${my.property}"},
243+
{"abc6.xyz:9999/some/path", "http://abc6.xyz:9999/some/path"},
244+
{"localhost:9000/{my.property}", "http://localhost:9000/${my.property}"},
245+
{"/{my.property}/path", "http://localhost/${my.property}/path"},
246+
{"https://abc1.xyz:9999/some/{my.property}/{version}", "https://abc1.xyz:9999/some/${my.property}/v1"},
247+
{"HTTPS://abc2.xyz:9999/{my.property}/{version}", "https://abc2.xyz:9999/${my.property}/v1"},
248+
{"https://abc1.xyz:9999/some/{version}/{my.property}", "https://abc1.xyz:9999/some/v1/${my.property}"},
249+
{"HTTPS://abc2.xyz:9999/{version}/{my.property}", "https://abc2.xyz:9999/v1/${my.property}"}
250+
251+
};
252+
253+
for (String[] t : testData) {
254+
OpenAPI openAPI = new OpenAPI();
255+
openAPI.addServersItem(new Server().url(t[0]));
256+
257+
Assert.assertEquals(URLPathUtils.getServerURL(openAPI, Map.of("version", "v1", "my.property", "${my.property}") ).toString(), t[1]);
258+
}
259+
}
207260
}

0 commit comments

Comments
 (0)