Skip to content

Commit

Permalink
Dialogue validates RequestBody implementations (#2057)
Browse files Browse the repository at this point in the history
Dialogue validates RequestBody implementations
  • Loading branch information
carterkozak authored Nov 3, 2023
1 parent fa58314 commit 431045c
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 0 deletions.
5 changes: 5 additions & 0 deletions changelog/@unreleased/pr-2057.v2.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
type: improvement
improvement:
description: Dialogue validates RequestBody implementations
links:
- https://github.com/palantir/dialogue/pull/2057
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,7 @@ private static EndpointChannelFactory createEndpointChannelFactory(Channel multi
// high cardinality.
channel = TimingEndpointChannel.create(cf, channel, endpoint);
}
channel = new RequestBodyValidationChannel(channel);
channel = new InterruptionChannel(channel);
return new NeverThrowEndpointChannel(channel); // this must come last as a defensive backstop
};
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* (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.dialogue.core;

import com.google.common.util.concurrent.ListenableFuture;
import com.palantir.dialogue.EndpointChannel;
import com.palantir.dialogue.Request;
import com.palantir.dialogue.RequestBody;
import com.palantir.dialogue.Response;
import com.palantir.logsafe.Preconditions;
import java.util.Optional;
import java.util.function.Consumer;

/**
* This channel validates early in the process that request body implementations
* do not violate the {@link RequestBody} contract.
*/
final class RequestBodyValidationChannel implements EndpointChannel {

private static final Consumer<RequestBody> BODY_VALIDATOR = RequestBodyValidationChannel::validate;
private final EndpointChannel delegate;

RequestBodyValidationChannel(EndpointChannel delegate) {
this.delegate = Preconditions.checkNotNull(delegate, "delegate is required");
}

@Override
public ListenableFuture<Response> execute(Request request) {
validate(request);
return delegate.execute(request);
}

private static void validate(Request request) {
Optional<RequestBody> body = request.body();
body.ifPresent(BODY_VALIDATOR);
}

private static void validate(RequestBody body) {
Preconditions.checkNotNull(body.contentType(), "RequestBody.contentType is required");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
import com.palantir.dialogue.TestResponse;
import com.palantir.dialogue.TypeMarker;
import com.palantir.logsafe.exceptions.SafeIllegalStateException;
import com.palantir.logsafe.exceptions.SafeNullPointerException;
import com.palantir.logsafe.exceptions.SafeRuntimeException;
import com.palantir.tracing.TestTracing;
import java.io.IOException;
Expand Down Expand Up @@ -139,6 +140,38 @@ public ListenableFuture<Response> execute(Endpoint _endpoint, Request _request)
assertThatThrownBy(() -> Futures.getUnchecked(future)).hasCauseInstanceOf(IllegalStateException.class);
}

@Test
public void testRequestBodyRequiresContentType() {
ListenableFuture<Response> req = channel.execute(
endpoint,
Request.builder()
.body(new RequestBody() {
@Override
public void writeTo(OutputStream _output) {}

@Override
@SuppressWarnings("NullAway")
public String contentType() {
// Null values are not allowed -- this should fail
return null;
}

@Override
public boolean repeatable() {
return false;
}

@Override
public void close() {}
})
.build());
assertThat(req)
.failsWithin(Duration.ZERO)
.withThrowableThat()
.withRootCauseInstanceOf(SafeNullPointerException.class)
.withMessageContaining("contentType is required");
}

@Test
public void bad_channel_throwing_an_error_still_returns_a_future() {
Channel badUserImplementation = new Channel() {
Expand Down

0 comments on commit 431045c

Please sign in to comment.