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

Add delegating response converter function provider #4336

Conversation

resquivel-squareup
Copy link
Contributor

@resquivel-squareup resquivel-squareup commented Jul 6, 2022

Potentially resolves issue 4301

Motivation:

Fix bug with Protobuf response converter. The fix now allows us to use a custom converter, while still preserving desired behaviour in other areas.

Please see the discussion issue 4301 for more information.

Modifications:

  • Take functionality from AnnotatedService into its own ResponseConverterFunctionSelector class. This isolates the behaviour of selecting response converters and lets us test its functionality in isolation.
  • Tests regarding desired order of converter usage
  • Test to ensure the bug with the Protobuf response converter is fixed
  • Split existing ResponseConverterFunctionProvider functionality into two separate interfaces --- ResponseConverterFunctionProvider and DelegatingResponseConverterFunctionProvider. These interfaces have different expectations in terms of parameters and usage of passed-in parameters
  • Several later commits involve resolving merge conflicts which were introduced because there were changes in the master branch to the code which we had pulled out into ResponseConverterFunctionSelector in this branch

Result:

@CLAassistant
Copy link

CLAassistant commented Jul 6, 2022

CLA assistant check
All committers have signed the CLA.

@resquivel-squareup resquivel-squareup changed the title Delegating response converter function provider Work-in-progress - Delegating response converter function provider Jul 6, 2022
…-converter-function-provider

* origin/master:
  Update the project version to 1.17.1-SNAPSHOT
  Add the release note for 1.17.0 (line#4330)
  Send 400 Bad Request when HTTP2 cleartext upgrade contains invalid headers (line#4224)
  Fix the test failure of `GrpcServiceLogNameTest.logName()` (line#4334)
  Support `ExchangeType` for the response of annotated service (line#4177)
  Recover HttpResponse according to a specific exception class (line#4283)
@codecov
Copy link

codecov bot commented Jul 8, 2022

Codecov Report

Merging #4336 (f4161d6) into master (3e85b61) will increase coverage by 0.01%.
The diff coverage is 90.00%.

@@             Coverage Diff              @@
##             master    #4336      +/-   ##
============================================
+ Coverage     73.81%   73.82%   +0.01%     
- Complexity    17740    17752      +12     
============================================
  Files          1510     1512       +2     
  Lines         66244    66252       +8     
  Branches       8322     8320       -2     
============================================
+ Hits          48896    48911      +15     
- Misses        13336    13342       +6     
+ Partials       4012     3999      -13     
Impacted Files Coverage Δ
...nnotation/AggregatedResponseConverterFunction.java 70.00% <70.00%> (ø)
...a/internal/server/annotation/AnnotatedService.java 88.00% <100.00%> (+0.91%) ⬆️
...rver/annotation/ResponseConverterFunctionUtil.java 100.00% <100.00%> (ø)
...on/kotlin/FlowResponseConverterFunctionProvider.kt 72.72% <100.00%> (ø)
...buf/ProtobufResponseConverterFunctionProvider.java 83.33% <100.00%> (-0.88%) ⬇️
...2/ObservableResponseConverterFunctionProvider.java 90.00% <100.00%> (ø)
...3/ObservableResponseConverterFunctionProvider.java 84.00% <100.00%> (ø)
...m/linecorp/armeria/client/DecodedHttpResponse.java 85.00% <0.00%> (-5.00%) ⬇️
...rnal/common/GracefulConnectionShutdownHandler.java 70.21% <0.00%> (-4.26%) ⬇️
...ecorp/armeria/server/grpc/StreamingServerCall.java 82.02% <0.00%> (-2.25%) ⬇️
... and 22 more

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

@resquivel-squareup resquivel-squareup marked this pull request as ready for review July 11, 2022 05:22
@ikhoon ikhoon added the defect label Jul 13, 2022
@resquivel-squareup resquivel-squareup changed the title Work-in-progress - Delegating response converter function provider Delegating response converter function provider Jul 14, 2022
@jrhee17 jrhee17 added this to the 1.18.0 milestone Jul 14, 2022
Copy link
Contributor

@minwoox minwoox left a comment

Choose a reason for hiding this comment

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

Thanks! We are going in the right direction!
Just left a few minor comments. 😉

new CompositeResponseConverterFunction(backingConverters)))
.build());

for (final DelegatingResponseConverterFunctionProvider provider : delegatingResponseConverterFunctionProviders) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Could you fix the lint?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed~

);

final HttpResponse response = converterFunction.convertResponse(ctx, null,
new TestClassWithDelegatingResponseConverterProvider(),
Copy link
Contributor

Choose a reason for hiding this comment

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

Could you check the lint of test files? 😄

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done 👍 Let me know if there are still issues, checkStyle seems to have passed

Copy link
Contributor

@jrhee17 jrhee17 left a comment

Choose a reason for hiding this comment

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

I wasn't able to review the test files yet, but the changes look good to me 👍
Left some minor comments 🙏

Copy link
Contributor

@minwoox minwoox left a comment

Choose a reason for hiding this comment

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

Looks great. 😄
Left only minor comments.

import com.linecorp.armeria.server.annotation.ResponseConverterFunctionProvider;

/**
* For use with ResponseConverterFunctionSelectorTest.
Copy link
Contributor

Choose a reason for hiding this comment

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

ditto

}
}

private Class<?> toClass(Type type) {
Copy link
Contributor

Choose a reason for hiding this comment

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

ditto. Let's reduce the cognitive load.

Copy link
Contributor

Choose a reason for hiding this comment

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

Otherwise, we can just replace this with ClassUtil.typeToClass().

@Test
void returnEmptyJsonArrayGivenCustomJsonPrinter() {
final AggregatedHttpResponse response = client.get("/json").aggregate().join();
assertThat(Objects.requireNonNull(Objects.requireNonNull(response.headers().contentType()))
Copy link
Contributor

Choose a reason for hiding this comment

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

Objects.requireNonNull is used twice for the same value.
Let's use AssertJ API.

final MediaType mediaType = response.headers().contentType();
assertThat(mediaType).isNotNull();
assertThat(mediaType).isSameAs(MediaType.JSON_UTF_8);

Copy link
Contributor

Choose a reason for hiding this comment

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

We can use MediaType.isJson() which is a shortcut for that.

@@ -218,8 +218,8 @@ void protobufJsonResponse(String contentType) throws InvalidProtocolBufferExcept
void protobufStreamResponse(String path) {
final AggregatedHttpResponse response = client.get(path).aggregate().join();
assertThat(response.status()).isEqualTo(HttpStatus.INTERNAL_SERVER_ERROR);
assertThat(cause).isInstanceOf(IllegalArgumentException.class)
.hasMessageContaining("Cannot convert a");
assertThat(cause).isInstanceOf(IllegalStateException.class)
Copy link
Contributor

@minwoox minwoox Jul 29, 2022

Choose a reason for hiding this comment

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

@@ -17,15 +17,15 @@
package com.linecorp.armeria.server.scalapb

import com.linecorp.armeria.common.annotation.{Nullable, UnstableApi}
import com.linecorp.armeria.server.annotation.{ResponseConverterFunction, ResponseConverterFunctionProvider}
import com.linecorp.armeria.server.annotation.{ResponseConverterFunction, DelegatingResponseConverterFunctionProvider}
Copy link
Contributor

Choose a reason for hiding this comment

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

We could probably do
import com.linecorp.armeria.server.annotation._ to fix the lint failure. 😉

@minwoox minwoox modified the milestones: 1.18.0, 1.19.0 Aug 1, 2022
Co-authored-by: minux <songmw725@gmail.com>
Copy link
Contributor

@minwoox minwoox left a comment

Choose a reason for hiding this comment

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

Thanks! I added more minor comments. 😄

ikhoon and others added 5 commits August 16, 2022 16:21
…tion/ResponseConverterFunctionUtil.java

Co-authored-by: minux <songmw725@gmail.com>
…tion/ResponseConverterFunctionUtil.java

Co-authored-by: minux <songmw725@gmail.com>
…gatingResponseConverterFunctionProvider.java

Co-authored-by: minux <songmw725@gmail.com>
@ikhoon
Copy link
Contributor

ikhoon commented Aug 16, 2022

@resquivel-squareup I fixed the code commented by @minwoox and slightly cleaned it up.

Copy link
Contributor

@ikhoon ikhoon left a comment

Choose a reason for hiding this comment

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

Thanks a lot, @resquivel-squareup 🙇‍♂️💯

@minwoox
Copy link
Contributor

minwoox commented Aug 24, 2022

@resquivel-squareup
Would you mind signing the CLA? 😄
#4336 (comment)

@resquivel-squareup
Copy link
Contributor Author

Thanks a lot, @resquivel-squareup 🙇‍♂️💯

Thanks for the added fixes, too! I've been busy recently and so haven't been able to add the fixes 😅

I've signed the CLA @minwoox. Apologies for the delay!

@minwoox
Copy link
Contributor

minwoox commented Sep 2, 2022

Please, don't worry about it. Thanks for signing the CLA. 😄

Copy link
Contributor

@minwoox minwoox left a comment

Choose a reason for hiding this comment

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

Awesome. Thanks! 😄

Copy link
Contributor

@jrhee17 jrhee17 left a comment

Choose a reason for hiding this comment

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

Looks great! Thanks @resquivel-squareup 🙇 👍 🙇

@minwoox minwoox changed the title Delegating response converter function provider Add delegating response converter function provider Sep 5, 2022
@minwoox minwoox merged commit 70d410d into line:master Sep 5, 2022
@minwoox
Copy link
Contributor

minwoox commented Sep 5, 2022

Congratulations @resquivel-squareup! 🎉 🎉 🎉
Hope to see your next PR again soon. 🤣

heowc pushed a commit to heowc/armeria that referenced this pull request Sep 24, 2022
Potentially resolves issue [4301](line#4301 (comment))


Motivation:

Fix bug with Protobuf response converter. The fix now allows us to use a custom converter, while still preserving desired behaviour in other areas.

Please see the discussion issue 4301 for more information.

Modifications:

- Take functionality from `AnnotatedService` into its own `ResponseConverterFunctionSelector` class. This isolates the behaviour of selecting response converters and lets us test its functionality in isolation.
- Tests regarding desired order of converter usage
- Test to ensure the bug with the Protobuf response converter is fixed
- Split existing `ResponseConverterFunctionProvider` functionality into two separate interfaces --- `ResponseConverterFunctionProvider` and `DelegatingResponseConverterFunctionProvider`. These interfaces have different expectations in terms of parameters and usage of passed-in parameters
- Several later commits involve resolving merge conflicts which were introduced because there were changes in the master branch to the code which we had pulled out into `ResponseConverterFunctionSelector` in this branch

Result:

- Closes line#4301.
- You will now be able to use a custom JsonFormat.Printer for ProtobufResponseConverterFunction 
- You will have to update any custom SPI implementations to conform to either `ResponseConverterFunctionProvider` or `DelegatingResponseConverterFunctionProvider`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Unable to use custom JsonFormat.Printer for ProtobufResponseConverterFunction
5 participants