-
Notifications
You must be signed in to change notification settings - Fork 923
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support RxJava-wrapped
HttpResult
response in annotated services (#…
…5386) Motivation: When returning `Single<HttpResult<T>>` from an annotated service, the response is always serialized as `{}`, since we currently just try to serialize `HttpResult` with Jackson. Instead, we should convert it to `HttpResponse` and only do serialization on `HttpResult.content()`. Modifications: - Create `HttpResultUtil` with the logic needed to build `HttpResponse` headers from `HttpResult` - Apply the same `HttpResult` conversion logic from `AnnotatedService` to `CompositeResponseConverterFunction` Result: - Closes #5380 - Annotated services support `HttpResult` wrapped in RxJava objects
- Loading branch information
1 parent
7d5ed55
commit c20e058
Showing
7 changed files
with
278 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
core/src/main/java/com/linecorp/armeria/internal/server/annotation/HttpResultUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} | ||
|
||
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() {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
133 changes: 133 additions & 0 deletions
133
core/src/test/java/com/linecorp/armeria/internal/server/annotation/HttpResultUtilTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.