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

Fix GrpcStatus for InvalidProtocolBufferException #5554

Merged
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,9 @@ private static Status statusFromThrowable(Throwable t) {
if (t instanceof ClosedStreamException || t instanceof RequestTimeoutException) {
return Status.CANCELLED.withCause(t);
}
if (t instanceof InvalidProtocolBufferException) {
return Status.INVALID_ARGUMENT.withCause(t);
}
if (t instanceof UnprocessedRequestException ||
t instanceof IOException ||
t instanceof FailFastException) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import org.junit.jupiter.api.Test;

import com.google.api.gax.grpc.GrpcStatusCode;
import com.google.protobuf.InvalidProtocolBufferException;

import com.linecorp.armeria.client.circuitbreaker.CircuitBreaker;
import com.linecorp.armeria.client.circuitbreaker.FailFastException;
Expand All @@ -58,4 +59,11 @@ void failFastExceptionToUnavailableCode() {
.getCode())
.isEqualTo(Status.Code.UNAVAILABLE);
}

@Test
void invalidProtocolBufferExceptionToInvalidArgumentCode() {
assertThat(GrpcStatus.fromThrowable(new InvalidProtocolBufferException("Failed to parse message"))
.getCode())
.isEqualTo(Status.Code.INVALID_ARGUMENT);
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Could we also add an integration test that sends invalid data described in the issue?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Okay, I'll

}
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@
import com.linecorp.armeria.server.grpc.HttpJsonTranscodingQueryParamMatchRule;
import com.linecorp.armeria.testing.junit5.server.ServerExtension;

import io.grpc.Status.Code;
import io.grpc.stub.StreamObserver;
import testing.grpc.HttpJsonTranscodingTestServiceGrpc.HttpJsonTranscodingTestServiceBlockingStub;
import testing.grpc.HttpJsonTranscodingTestServiceGrpc.HttpJsonTranscodingTestServiceImplBase;
Expand Down Expand Up @@ -816,6 +817,21 @@ void shouldDenyMalformedJson() throws JsonProcessingException {
assertThat(response.status()).isEqualTo(HttpStatus.BAD_REQUEST);
}

@ParameterizedTest
@ValueSource(strings = {
"{\"int32Val\": 1.1}",
"{\"int64Val\": 2.2}",
"{\"uint32Val\": 3.3}",
"{\"uint64Val\": 4.4}"
})
void shouldDenyTypeMismatchedValue(String jsonContent)
throws JsonProcessingException {
final AggregatedHttpResponse response = jsonPostRequest(webClient, "/v1/echo/wrappers", jsonContent);
final JsonNode root = mapper.readTree(response.contentUtf8());
assertThat(response.headers().status()).isEqualTo(HttpStatus.BAD_REQUEST);
assertThat(root.get("code").asInt()).isEqualTo(Code.INVALID_ARGUMENT.value());
}

@Test
void shouldDenyMissingContentType() {
final String validJson = "{\"value\":\"value\",\"structBody\":{\"structBody\":\"struct_value\"} }";
Expand Down
5 changes: 5 additions & 0 deletions grpc/src/test/proto/testing/grpc/transcoding.proto
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,11 @@ service HttpJsonTranscodingTestService {
rpc EchoWrappers(EchoWrappersRequest) returns (EchoWrappersResponse) {
option (google.api.http) = {
get: "/v1/echo/wrappers"

additional_bindings {
post: "/v1/echo/wrappers"
body: "value"
}
};
}

Expand Down