Skip to content

Commit

Permalink
Ignore undertow regex path params
Browse files Browse the repository at this point in the history
  • Loading branch information
fawind committed Oct 20, 2023
1 parent 4610af8 commit bde7260
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 4 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package com.palantir.conjure.java.services;

import com.google.common.annotations.VisibleForTesting;
import com.google.common.collect.Collections2;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
Expand Down Expand Up @@ -53,6 +54,7 @@
import com.palantir.conjure.spec.EndpointName;
import com.palantir.conjure.spec.ExternalReference;
import com.palantir.conjure.spec.HeaderAuthType;
import com.palantir.conjure.spec.HttpPath;
import com.palantir.conjure.spec.ListType;
import com.palantir.conjure.spec.LogSafety;
import com.palantir.conjure.spec.OptionalType;
Expand Down Expand Up @@ -95,11 +97,16 @@
import java.util.Optional;
import java.util.Set;
import java.util.function.Function;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.lang.model.element.Modifier;
import org.apache.commons.lang3.StringUtils;

final class UndertowServiceHandlerGenerator {

private static final Pattern REGEX_PATH_TEMPLATES =
Pattern.compile("\\{([a-zA-Z0-9]+)" + "(" + Pattern.quote(":.+") + "|" + Pattern.quote(":.*") + ")" + "}");

private static final String EXCHANGE_VAR_NAME = "exchange";
private static final String DELEGATE_VAR_NAME = "delegate";
private static final String RUNTIME_VAR_NAME = "runtime";
Expand Down Expand Up @@ -329,7 +336,7 @@ private TypeSpec generateEndpointHandler(
.addModifiers(Modifier.PUBLIC)
.addAnnotation(Override.class)
.returns(String.class)
.addStatement("return $1S", endpointDefinition.getHttpPath())
.addStatement("return $1S", normalizeHttpPathTemplates(endpointDefinition.getHttpPath()))
.build())
.addMethod(MethodSpec.methodBuilder("serviceName")
.addModifiers(Modifier.PUBLIC)
Expand Down Expand Up @@ -1168,4 +1175,17 @@ && requiresRequestContext(endpoint, evaluator))) {
}
return value;
}

/**
* The Conjure spec allows for regex path param validations ("{param:.+}" and "{param:.*}"), however Undertow does
* not support these for path templates. Based on that, we strip these validation regexes from the http path
* template.
* @see "https://github.com/palantir/conjure-java/pull/2119"
*/
@VisibleForTesting
static HttpPath normalizeHttpPathTemplates(HttpPath httpPath) {
Matcher matcher = REGEX_PATH_TEMPLATES.matcher(httpPath.get());
String normalized = matcher.replaceAll("{$1}");
return HttpPath.of(normalized);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* (c) Copyright 2023 Palantir Technologies Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.palantir.conjure.java.services;

import static org.assertj.core.api.Assertions.assertThat;

import com.palantir.conjure.spec.HttpPath;
import java.util.stream.Stream;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;

final class UndertowServiceHandlerGeneratorTest {

@ParameterizedTest
@MethodSource("normalizes_http_path_templates")
void normalizes_http_path_templates(String given, String expected) {
HttpPath normalized = UndertowServiceHandlerGenerator.normalizeHttpPathTemplates(HttpPath.of(given));

assertThat(normalized).isEqualTo(HttpPath.of(expected));
}

private static Stream<Arguments> normalizes_http_path_templates() {
return Stream.of(
Arguments.of("{param:.+}", "{param}"),
Arguments.of("{param:.*}", "{param}"),
Arguments.of("{param}", "{param}"),
// We ignore any other regex, because those are not allowed by the Conjure spec.
Arguments.of("{param:[a-zA-Z0-9]+}", "{param:[a-zA-Z0-9]+}"),
Arguments.of("/foo/{paramA:.*}/{paramB:.+}/{paramC}", "/foo/{paramA}/{paramB}/{paramC}"));
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit bde7260

Please sign in to comment.