-
Notifications
You must be signed in to change notification settings - Fork 104
[LRO] Add ApiMessageOperationTransformer #683
[LRO] Add ApiMessageOperationTransformer #683
Conversation
Codecov Report
@@ Coverage Diff @@
## master #683 +/- ##
============================================
+ Coverage 75.32% 75.53% +0.21%
- Complexity 1030 1035 +5
============================================
Files 195 196 +1
Lines 4628 4656 +28
Branches 357 358 +1
============================================
+ Hits 3486 3517 +31
+ Misses 984 981 -3
Partials 158 158
Continue to review full report at Codecov.
|
|
PTAL |
| if (!operationSnapshot.getResponse().getClass().isAssignableFrom(responseTClass)) { | ||
| throw ApiExceptionFactory.createException( | ||
| new Throwable( | ||
| String.format( |
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.
Minor: this is inconsistent with how other error meessages are constructed (L59 uses string concatenation). Please use one style (pick any (String concatenation is technically faster, String.format is more readable)) for all of them.
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 choose readability!
| /** Unwraps an OperationSnapshot and returns the contained method response message. */ | ||
| @SuppressWarnings("unchecked") | ||
| public ResponseT apply(OperationSnapshot operationSnapshot) { | ||
| if (!operationSnapshot.getErrorCode().getCode().equals(Code.OK)) { |
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.
Same issue applies to ProtoOperationTransformers. It looks like we are interested if the call was successful or no. This treats only 200 as success, but any 2xx code can be considered success HTTP Status Codes.
Though, this is unlikely that we can get anything but 200 here for success in practical cases (maybe 201 also). Please add at least a comment about "potential need to handle other error codes". If that ever happens (other than 200 success status code returned) it may save time for those who will be fixing it.
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.
TIL
See the new diff in HttpJsonStatusCode.java to squash all 2xx codes into the OK 200 code.
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.
Please add a comment here about potential need to handle more than only 200.
| } | ||
|
|
||
| /** Unwraps an OperationSnapshot and returns the contained method response message. */ | ||
| @SuppressWarnings("unchecked") |
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.
Please move this annotation to where it applies if possible (i assume it was line 80). Reducing the scope of warning suppression helps to prevent false suppressions (for cases, when that was actually an error).
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.
done!
| operationSnapshot.getErrorCode(), | ||
| false); | ||
| } | ||
| if (!operationSnapshot.getResponse().getClass().isAssignableFrom(responseTClass)) { |
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.
Is operationSnapshot.getResponse() returning a Java proxy class? Or in other words, why isAssignableFrom() instead of isInstance(). isAssignableFrom() is heavier.
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 don't know why isInstance() doesn't work; the error says
... its response type com.google.api.gax.httpjson.EmptyMessage cannot be cast to com.google.api.gax.httpjson.EmptyMessage.
But isAssignableFrom() does work for this use case.
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.
This is very suspicious, the error message says it cannot cast class to itself. Something is wrong here.
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 know! i looked on stackoverflow and some answers say that this might happen if you have different classloaders? idk
| } | ||
| } | ||
|
|
||
| @SuppressWarnings("unchecked") |
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.
Please move the annotation to where it applies, if possible.
|
|
||
| /** Unwraps an OperationSnapshot and returns the contained operation metadata message. */ | ||
| @Override | ||
| public MetadataT apply(OperationSnapshot operationSnapshot) { |
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.
It seems like this is same logic as in the response transformer (the "response type" and "metadata type" in error message string can be a parameter). Please consider putting this in helper method and reuse it (static or make transformers extend same class and put this method in the parent class).
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.
Done; implemented in transformEntityFromOperationSnapshot()
| DONE | ||
| } | ||
|
|
||
| private static class MetadataMessage<ResponseT extends ApiMessage> implements ApiMessage { |
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.
Can these classes be replaced with Mockito mocks (we should avoid creating mocks manually where possible, because they are usually much longer and require lots of boilerplate code, which has to be updated if interface changes (here the ApiMessage)). If it is not possible to use mocks instead, please rename them to Fake* or Mock* or something like that, so they do not appear as false positive results in code search (when searching for real stuff).
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.
Naming them Fake*. Because the test logic relies on calling class type methods like instanceof, I wasn't sure how to mock that with mockito.
c43c468 to
b5cec71
Compare
|
PTAL |
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.
Sorry for the 2xx confusion. Please revert that change.
|
|
||
| static StatusCode.Code httpStatusToStatusCode(int httpStatus, String errorMessage) { | ||
| String causeMessage = Strings.nullToEmpty(errorMessage).toUpperCase(); | ||
| if (httpStatus >= 200 && httpStatus < 300) { |
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.
Please revert this. Sorry for confusion I did not mean that any 2xx status code should be treated equally (at long last they are different codes for a reason). For our particular case (LRO), most probably only 200 and 201 would fit. In general handling only 200 is much safer than treating all 2xx equally. Also we are loosing information here: all 2xx httpStatus codes are mapped into 200 (this is a potential time bomb).
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.
Done.
|
Reverted status code change. |
vam-google
left a comment
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.
LGTM with a comment.
| /** Unwraps an OperationSnapshot and returns the contained method response message. */ | ||
| @SuppressWarnings("unchecked") | ||
| public ResponseT apply(OperationSnapshot operationSnapshot) { | ||
| if (!operationSnapshot.getErrorCode().getCode().equals(Code.OK)) { |
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.
Please add a comment here about potential need to handle more than only 200.
This is the ApiMessage equivalent of ProtoOperationTransformers.