Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle path param validation regexes for undertow handlers #2119

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

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.

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.

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.

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()))
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the actual code change.

.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
Expand Up @@ -104,6 +104,11 @@ public long externalLongPath(AuthHeader _authHeader, long param) {
return param;
}

@Override
public String pathParamRegex(AuthHeader authHeader, String paramOne, String paramTwo, String paramThree) {
return paramOne + "," + paramTwo + "," + paramThree;
}

@Override
public Optional<Long> optionalExternalLongQuery(AuthHeader _authHeader, Optional<Long> param) {
return param;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,12 @@ public void testSpaceInPathParam() {
.isEqualTo(expected);
}

@Test
public void testRegexPath() {
assertThat(client.pathParamRegex(AuthHeader.valueOf("bearer"), "foo", "bar", "baz"))
.isEqualTo("foo,bar,baz");
}

@Test
public void testBinaryOptionalEmptyResponse() {
Optional<ResponseBody> response =
Expand Down
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}"));
}
}
12 changes: 12 additions & 0 deletions conjure-java-core/src/test/resources/ete-service.yml
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,18 @@ services:
param: Long
returns: Long

# The Conjure spec technically supports path params regexes even though they are unused
pathParamRegex:
http: GET /path/{paramOne}/{paramTwo:.+}/{paramThree:.*}
args:
paramOne:
type: string
paramTwo:
type: string
paramThree:
type: string
returns: string

optionalExternalLongQuery:
http: GET /optionalExternalLong
args:
Expand Down

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

Loading