Skip to content

Commit 3ae36ed

Browse files
committed
Ignore undertow regex path params
1 parent 4610af8 commit 3ae36ed

File tree

5 files changed

+70
-4
lines changed

5 files changed

+70
-4
lines changed

conjure-java-core/src/integrationInput/java/com/palantir/product/EteServiceEndpoints.java

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

conjure-java-core/src/main/java/com/palantir/conjure/java/services/UndertowServiceHandlerGenerator.java

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package com.palantir.conjure.java.services;
1818

19+
import com.google.common.annotations.VisibleForTesting;
1920
import com.google.common.collect.Collections2;
2021
import com.google.common.collect.ImmutableList;
2122
import com.google.common.collect.ImmutableSet;
@@ -53,6 +54,7 @@
5354
import com.palantir.conjure.spec.EndpointName;
5455
import com.palantir.conjure.spec.ExternalReference;
5556
import com.palantir.conjure.spec.HeaderAuthType;
57+
import com.palantir.conjure.spec.HttpPath;
5658
import com.palantir.conjure.spec.ListType;
5759
import com.palantir.conjure.spec.LogSafety;
5860
import com.palantir.conjure.spec.OptionalType;
@@ -95,11 +97,16 @@
9597
import java.util.Optional;
9698
import java.util.Set;
9799
import java.util.function.Function;
100+
import java.util.regex.Matcher;
101+
import java.util.regex.Pattern;
98102
import javax.lang.model.element.Modifier;
99103
import org.apache.commons.lang3.StringUtils;
100104

101105
final class UndertowServiceHandlerGenerator {
102106

107+
private static final Pattern REGEX_PATH_TEMPLATES =
108+
Pattern.compile("\\{([a-zA-Z0-9]+)" + "(" + Pattern.quote(":.+") + "|" + Pattern.quote(":.*") + ")" + "}");
109+
103110
private static final String EXCHANGE_VAR_NAME = "exchange";
104111
private static final String DELEGATE_VAR_NAME = "delegate";
105112
private static final String RUNTIME_VAR_NAME = "runtime";
@@ -329,7 +336,7 @@ private TypeSpec generateEndpointHandler(
329336
.addModifiers(Modifier.PUBLIC)
330337
.addAnnotation(Override.class)
331338
.returns(String.class)
332-
.addStatement("return $1S", endpointDefinition.getHttpPath())
339+
.addStatement("return $1S", normalizeHttpPathTemplates(endpointDefinition.getHttpPath()))
333340
.build())
334341
.addMethod(MethodSpec.methodBuilder("serviceName")
335342
.addModifiers(Modifier.PUBLIC)
@@ -1168,4 +1175,17 @@ && requiresRequestContext(endpoint, evaluator))) {
11681175
}
11691176
return value;
11701177
}
1178+
1179+
/**
1180+
* The Conjure spec allows for regex path param validations ("{param:.+}" and "{param:.*}"), however Undertow does
1181+
* not support these for path templates. Based on that, we strip these validation regexes from the http path
1182+
* template.
1183+
* TODO(fw): Link github
1184+
*/
1185+
@VisibleForTesting
1186+
static HttpPath normalizeHttpPathTemplates(HttpPath httpPath) {
1187+
Matcher matcher = REGEX_PATH_TEMPLATES.matcher(httpPath.get());
1188+
String normalized = matcher.replaceAll("{$1}");
1189+
return HttpPath.of(normalized);
1190+
}
11711191
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* (c) Copyright 2023 Palantir Technologies Inc. All rights reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.palantir.conjure.java.services;
18+
19+
import static org.assertj.core.api.Assertions.assertThat;
20+
21+
import com.palantir.conjure.spec.HttpPath;
22+
import java.util.stream.Stream;
23+
import org.junit.jupiter.params.ParameterizedTest;
24+
import org.junit.jupiter.params.provider.Arguments;
25+
import org.junit.jupiter.params.provider.MethodSource;
26+
27+
final class UndertowServiceHandlerGeneratorTest {
28+
29+
@ParameterizedTest
30+
@MethodSource("normalizes_http_path_templates")
31+
void normalizes_http_path_templates(String given, String expected) {
32+
HttpPath normalized = UndertowServiceHandlerGenerator.normalizeHttpPathTemplates(HttpPath.of(given));
33+
34+
assertThat(normalized).isEqualTo(HttpPath.of(expected));
35+
}
36+
37+
private static Stream<Arguments> normalizes_http_path_templates() {
38+
return Stream.of(
39+
Arguments.of("{param:.+}", "{param}"),
40+
Arguments.of("{param:.*}", "{param}"),
41+
Arguments.of("{param}", "{param}"),
42+
// We ignore any other regex, because those are not allowed by the Conjure spec.
43+
Arguments.of("{param:[a-zA-Z0-9]+}", "{param:[a-zA-Z0-9]+}"),
44+
Arguments.of("/foo/{paramA:.*}/{paramB:.+}/{paramC}", "/foo/{paramA}/{paramB}/{paramC}"));
45+
}
46+
}

conjure-java-core/src/test/resources/test/api/TestServiceEndpoints.java.undertow

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

conjure-java-core/src/test/resources/test/api/TestServiceEndpoints.java.undertow.prefix

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)