Skip to content
This repository was archived by the owner on Sep 26, 2023. It is now read-only.

Conversation

@andreamlin
Copy link
Contributor

This is the ApiMessage equivalent of ProtoOperationTransformers.

@andreamlin andreamlin changed the title Add ApiMessageOperationTransformer [LRO] Add ApiMessageOperationTransformer Feb 22, 2019
@googlebot googlebot added the cla: yes This human has signed the Contributor License Agreement. label Feb 22, 2019
@codecov
Copy link

codecov bot commented Feb 22, 2019

Codecov Report

Merging #683 into master will increase coverage by 0.21%.
The diff coverage is 100%.

Impacted file tree graph

@@             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
Impacted Files Coverage Δ Complexity Δ
.../gax/httpjson/ApiMessageOperationTransformers.java 100% <100%> (ø) 2 <2> (?)
...om/google/api/gax/httpjson/HttpJsonStatusCode.java 29.26% <0%> (+2.43%) 8% <0%> (+1%) ⬆️
...java/com/google/api/gax/httpjson/EmptyMessage.java 12.5% <0%> (+12.5%) 2% <0%> (+2%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 1f25467...a311056. Read the comment docs.

@andreamlin
Copy link
Contributor Author

PTAL

if (!operationSnapshot.getResponse().getClass().isAssignableFrom(responseTClass)) {
throw ApiExceptionFactory.createException(
new Throwable(
String.format(
Copy link
Contributor

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.

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 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)) {
Copy link
Contributor

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.

Copy link
Contributor Author

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.

Copy link
Contributor

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")
Copy link
Contributor

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).

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!

operationSnapshot.getErrorCode(),
false);
}
if (!operationSnapshot.getResponse().getClass().isAssignableFrom(responseTClass)) {
Copy link
Contributor

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.

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 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.

Copy link
Contributor

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.

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 know! i looked on stackoverflow and some answers say that this might happen if you have different classloaders? idk

}
}

@SuppressWarnings("unchecked")
Copy link
Contributor

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) {
Copy link
Contributor

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).

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; implemented in transformEntityFromOperationSnapshot()

DONE
}

private static class MetadataMessage<ResponseT extends ApiMessage> implements ApiMessage {
Copy link
Contributor

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).

Copy link
Contributor Author

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.

@andreamlin andreamlin force-pushed the just_apimessageoptransformers branch from c43c468 to b5cec71 Compare March 1, 2019 23:50
@andreamlin
Copy link
Contributor Author

PTAL

@yoshi-automation yoshi-automation added the 🚨 This issue needs some love. label Mar 4, 2019
Copy link
Contributor

@vam-google vam-google left a 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) {
Copy link
Contributor

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).

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.

@andreamlin
Copy link
Contributor Author

andreamlin commented Mar 5, 2019

Reverted status code change.
PTAL I missed a comment!

Copy link
Contributor

@vam-google vam-google left a 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)) {
Copy link
Contributor

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.

@andreamlin andreamlin merged commit d47e073 into googleapis:master Mar 5, 2019
@andreamlin andreamlin deleted the just_apimessageoptransformers branch March 5, 2019 00:18
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

cla: yes This human has signed the Contributor License Agreement. 🚨 This issue needs some love.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants