-
Notifications
You must be signed in to change notification settings - Fork 926
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
Changes from 4 commits
3a734fc
2d0547b
a5dc644
ea25d7c
4693039
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
||
/** | ||
|
@@ -62,6 +63,12 @@ public HttpResponse convertResponse(ServiceRequestContext ctx, | |
if (result instanceof HttpResponse) { | ||
return (HttpResponse) result; | ||
} | ||
if (result instanceof HttpResult) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 { | ||
|
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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The only thing that changed from the original implementation in |
||
} | ||
} | ||
|
||
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; | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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:
armeria/kotlin/src/test/kotlin/com/linecorp/armeria/server/kotlin/SuspendingAnnotatedServiceTest.kt
Lines 374 to 377 in d34cee7
There was a problem hiding this comment.
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