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

Support RxJava-wrapped HttpResult response in annotated services #5386

Merged
merged 5 commits into from
Jan 24, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@
import com.linecorp.armeria.common.AggregatedHttpResponse;
import com.linecorp.armeria.common.ExchangeType;
import com.linecorp.armeria.common.Flags;
import com.linecorp.armeria.common.HttpHeaderNames;
import com.linecorp.armeria.common.HttpHeaders;
import com.linecorp.armeria.common.HttpRequest;
import com.linecorp.armeria.common.HttpResponse;
Expand Down Expand Up @@ -249,6 +248,11 @@ Route route() {
return route;
}

// TODO: Expose through `AnnotatedServiceConfig`, see #5382.
HttpStatus defaultStatus() {
return defaultStatus;
}

HttpService withExceptionHandler(HttpService service) {
if (exceptionHandler == null) {
return service;
Expand Down Expand Up @@ -398,7 +402,7 @@ private HttpResponse convertResponse(ServiceRequestContext ctx, @Nullable Object

if (result instanceof HttpResult) {
Copy link
Contributor

Choose a reason for hiding this comment

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

note; Perhaps in the future we may just remove this block since CompositeResponseConverterFunction has this logic already.

I understand this can't be removed this iteration due to breaking changes though

Copy link
Contributor

@minwoox minwoox Jan 23, 2024

Choose a reason for hiding this comment

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

I've tried that but it fails depends on the implementation:

when (result) {
is Bar -> responseConverter.convertResponse(ctx, headers, "hello, bar!", trailers)
else -> ResponseConverterFunction.fallthrough()
}

Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks, I also checked that the content unwrapping logic is the reason for the failure

result = httpResult.content();

final HttpResult<?> httpResult = (HttpResult<?>) result;
headers = buildResponseHeaders(ctx, httpResult.headers());
headers = HttpResultUtil.buildResponseHeaders(ctx, httpResult);
result = httpResult.content();
trailers = httpResult.trailers();
} else {
Expand Down Expand Up @@ -426,39 +430,17 @@ private HttpResponse convertResponseInternal(ServiceRequestContext ctx,
}
}

private ResponseHeaders buildResponseHeaders(ServiceRequestContext ctx, HttpHeaders customHeaders) {
final ResponseHeadersBuilder builder;

// Prefer ResponseHeaders#toBuilder because builder#add(Iterable) is an expensive operation.
if (customHeaders instanceof ResponseHeaders) {
builder = ((ResponseHeaders) customHeaders).toBuilder();
} else {
builder = ResponseHeaders.builder();
builder.add(customHeaders);
if (!builder.contains(HttpHeaderNames.STATUS)) {
builder.status(defaultStatus);
}
}
return maybeAddContentType(ctx, builder).build();
}

private ResponseHeaders buildResponseHeaders(ServiceRequestContext ctx) {
return maybeAddContentType(ctx, ResponseHeaders.builder(defaultStatus)).build();
}

private static ResponseHeadersBuilder maybeAddContentType(ServiceRequestContext ctx,
ResponseHeadersBuilder builder) {
final ResponseHeadersBuilder builder = ResponseHeaders.builder(defaultStatus);
if (builder.status().isContentAlwaysEmpty()) {
return builder;
}
if (builder.contentType() != null) {
return builder;
return builder.build();
}

final MediaType negotiatedResponseMediaType = ctx.negotiatedResponseMediaType();
if (negotiatedResponseMediaType != null) {
builder.contentType(negotiatedResponseMediaType);
}
return builder;
return builder.build();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import com.linecorp.armeria.common.util.SafeCloseable;
import com.linecorp.armeria.server.ServiceRequestContext;
import com.linecorp.armeria.server.annotation.FallthroughException;
import com.linecorp.armeria.server.annotation.HttpResult;
import com.linecorp.armeria.server.annotation.ResponseConverterFunction;

/**
Expand Down Expand Up @@ -62,6 +63,12 @@ public HttpResponse convertResponse(ServiceRequestContext ctx,
if (result instanceof HttpResponse) {
return (HttpResponse) result;
}
if (result instanceof HttpResult) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm not very familiar with the converter setup, so I'm not 100% sure if this is the only guaranteed entrypoint to the converter chain where we might receive an HttpResult.

Copy link
Contributor

Choose a reason for hiding this comment

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

I think this is the place to put logic because we don't support the conversion recursively.

final HttpResult<?> httpResult = (HttpResult<?>) result;
headers = HttpResultUtil.buildResponseHeaders(ctx, httpResult);
result = httpResult.content();
trailers = httpResult.trailers();
}
try (SafeCloseable ignored = ctx.push()) {
for (final ResponseConverterFunction func : functions) {
try {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
* Copyright 2024 LINE Corporation
*
* LINE Corporation licenses this file to you 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:
*
* https://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.linecorp.armeria.internal.server.annotation;

import com.linecorp.armeria.common.HttpHeaderNames;
import com.linecorp.armeria.common.HttpHeaders;
import com.linecorp.armeria.common.HttpStatus;
import com.linecorp.armeria.common.MediaType;
import com.linecorp.armeria.common.ResponseHeaders;
import com.linecorp.armeria.common.ResponseHeadersBuilder;
import com.linecorp.armeria.server.ServiceRequestContext;
import com.linecorp.armeria.server.annotation.HttpResult;

final class HttpResultUtil {
static ResponseHeaders buildResponseHeaders(ServiceRequestContext ctx, HttpResult<?> result) {
final ResponseHeadersBuilder builder;
final HttpHeaders customHeaders = result.headers();

// Prefer ResponseHeaders#toBuilder because builder#add(Iterable) is an expensive operation.
if (customHeaders instanceof ResponseHeaders) {
builder = ((ResponseHeaders) customHeaders).toBuilder();
} else {
builder = ResponseHeaders.builder();
builder.add(customHeaders);

if (!builder.contains(HttpHeaderNames.STATUS)) {
final AnnotatedService service = ctx.config().service().as(AnnotatedService.class);
if (service != null) {
builder.status(service.defaultStatus());
} else {
builder.status(HttpStatus.OK);
}
Comment on lines +41 to +46
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The only thing that changed from the original implementation in AnnotatedService

}
}

return maybeAddContentType(ctx, builder).build();
}

private static ResponseHeadersBuilder maybeAddContentType(ServiceRequestContext ctx,
ResponseHeadersBuilder builder) {
if (builder.status().isContentAlwaysEmpty()) {
return builder;
}
if (builder.contentType() != null) {
return builder;
}

final MediaType negotiatedResponseMediaType = ctx.negotiatedResponseMediaType();
if (negotiatedResponseMediaType != null) {
builder.contentType(negotiatedResponseMediaType);
}

return builder;
}

private HttpResultUtil() {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
/*
* Copyright 2024 LINE Corporation
*
* LINE Corporation licenses this file to you 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:
*
* https://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.linecorp.armeria.internal.server.annotation;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verifyNoInteractions;
import static org.mockito.Mockito.when;

import org.junit.jupiter.api.Test;

import com.linecorp.armeria.common.HttpHeaders;
import com.linecorp.armeria.common.HttpResponse;
import com.linecorp.armeria.common.HttpStatus;
import com.linecorp.armeria.common.MediaType;
import com.linecorp.armeria.common.ResponseHeaders;
import com.linecorp.armeria.server.Server;
import com.linecorp.armeria.server.ServiceRequestContext;
import com.linecorp.armeria.server.annotation.Get;
import com.linecorp.armeria.server.annotation.HttpResult;
import com.linecorp.armeria.server.annotation.StatusCode;

class HttpResultUtilTest {

@Test
void shouldReuseResponseHeaders() {
final ServiceRequestContext ctx = mock(ServiceRequestContext.class);

final ResponseHeaders headers = ResponseHeaders
.builder(HttpStatus.OK)
.contentType(MediaType.PLAIN_TEXT_UTF_8)
.add("foo", "bar")
.build();
final HttpResult<Integer> result = HttpResult.of(headers, 123);

final ResponseHeaders actual = HttpResultUtil.buildResponseHeaders(ctx, result);
assertThat(actual).isEqualTo(headers);
assertThat(actual.contentType()).isEqualTo(MediaType.PLAIN_TEXT_UTF_8);
assertThat(actual.get("foo")).isEqualTo("bar");

verifyNoInteractions(ctx);
}

@Test
void shouldNotAddContentTypeWhenNoContent() {
final ServiceRequestContext ctx = mock(ServiceRequestContext.class);

final ResponseHeaders headers = ResponseHeaders.of(HttpStatus.NO_CONTENT);
final HttpResult<Integer> result = HttpResult.of(headers, 123);

final ResponseHeaders actual = HttpResultUtil.buildResponseHeaders(ctx, result);
assertThat(actual).isEqualTo(headers);
assertThat(actual.contentType()).isNull();

verifyNoInteractions(ctx);
}

@Test
void shouldNegotiateContentType() {
final ServiceRequestContext ctx = mock(ServiceRequestContext.class);
when(ctx.negotiatedResponseMediaType()).thenReturn(MediaType.JSON_UTF_8);

final ResponseHeaders headers = ResponseHeaders.of(HttpStatus.OK, "foo", "bar");
final HttpResult<Integer> result = HttpResult.of(headers, 123);

final ResponseHeaders actual = HttpResultUtil.buildResponseHeaders(ctx, result);
assertThat(actual.status()).isEqualTo(HttpStatus.OK);
assertThat(actual.contentType()).isEqualTo(MediaType.JSON_UTF_8);
assertThat(actual.get("foo")).isEqualTo("bar");
}

@Test
void shouldAddStatusFromAnnotatedService() {
final Server server = Server
.builder()
.annotatedService("/", new MyAnnotatedService())
.build();

final ServiceRequestContext ctx = mock(ServiceRequestContext.class);
when(ctx.config()).thenReturn(server.serviceConfigs().get(0));
when(ctx.negotiatedResponseMediaType()).thenReturn(MediaType.PLAIN_TEXT_UTF_8);

final HttpHeaders headers = HttpHeaders.of("foo", "bar");
final HttpResult<Integer> result = HttpResult.of(headers, 123);

final ResponseHeaders actual = HttpResultUtil.buildResponseHeaders(ctx, result);
assertThat(actual.status()).isEqualTo(HttpStatus.ACCEPTED);
assertThat(actual.contentType()).isEqualTo(MediaType.PLAIN_TEXT_UTF_8);
assertThat(actual.get("foo")).isEqualTo("bar");
}

@Test
void shouldUseOkStatusWhenNotAnnotatedService() {
final Server server = Server
.builder()
.service("/", (ctx, req) -> HttpResponse.of(HttpStatus.ACCEPTED))
.build();

final ServiceRequestContext ctx = mock(ServiceRequestContext.class);
when(ctx.config()).thenReturn(server.serviceConfigs().get(0));
when(ctx.negotiatedResponseMediaType()).thenReturn(MediaType.JSON_UTF_8);

final HttpHeaders headers = HttpHeaders.of("foo", "bar");
final HttpResult<Integer> result = HttpResult.of(headers, 123);

final ResponseHeaders actual = HttpResultUtil.buildResponseHeaders(ctx, result);
assertThat(actual.status()).isEqualTo(HttpStatus.OK);
assertThat(actual.contentType()).isEqualTo(MediaType.JSON_UTF_8);
assertThat(actual.get("foo")).isEqualTo("bar");
}

public class MyAnnotatedService {
@Get
@StatusCode(202)
public int myMethod() {
return 123;
}
}
}