Skip to content

Commit

Permalink
code-quality-reports, allow RequestOptions instead of Context in sync…
Browse files Browse the repository at this point in the history
… service method (Azure#25429)
  • Loading branch information
weidongxu-microsoft authored Nov 18, 2021
1 parent fcc34f4 commit de9dd2a
Showing 1 changed file with 20 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public class ServiceClientCheck extends AbstractCheck {
private static final String CLIENT = "Client";
private static final String IS_ASYNC = "isAsync";
private static final String CONTEXT = "Context";
private static final String REQUEST_OPTIONS = "RequestOptions";

private static final String RESPONSE_BRACKET = "Response<";
private static final String MONO_BRACKET = "Mono<";
Expand Down Expand Up @@ -78,7 +79,7 @@ public class ServiceClientCheck extends AbstractCheck {
private static final String ASYNC_CONTEXT_ERROR =
"Asynchronous method with annotation @ServiceMethod must not has ''%s'' as a method parameter.";
private static final String SYNC_CONTEXT_ERROR =
"Synchronous method with annotation @ServiceMethod must has ''%s'' as a method parameter.";
"Synchronous method with annotation @ServiceMethod must has ''%s'' or ''%s'' as a method parameter.";

// Add all imported classes into a map, key is the name of class and value is the full package path of class.
private final Map<String, String> simpleClassNameToQualifiedNameMap = new HashMap<>();
Expand Down Expand Up @@ -390,7 +391,7 @@ private void checkReturnTypeNamingPattern(DetailAST methodDefToken, String metho
}

/**
* Checks the type Context should be in the right place. Context should be passed in as an argument to all public
* Checks the type Context should be in the right place. Context should be passed in as an argument to all public
* methods annotated with @ServiceMethod that return {@code Response<T>} in synchronous clients.
* Synchronous method with annotation @ServiceMethod has to have {@code Context} as a parameter.
* Asynchronous method with annotation @ServiceMethod must not has {@code Context} as a parameter.
Expand All @@ -413,17 +414,30 @@ private void checkContextInRightPlace(DetailAST methodDefToken) {
})
.isPresent();

final boolean containsRequestOptionsParameter = TokenUtil.findFirstTokenByPredicate(parametersToken,
parameterToken -> {
if (parameterToken.getType() != TokenTypes.PARAMETER_DEF) {
return false;
}
final DetailAST paramTypeIdentToken =
parameterToken.findFirstToken(TokenTypes.TYPE).findFirstToken(TokenTypes.IDENT);
return paramTypeIdentToken != null && REQUEST_OPTIONS.equals(paramTypeIdentToken.getText());
})
.isPresent();

if (containsContextParameter) {
// MONO and PagedFlux return type implies Asynchronous method
if (returnType.startsWith(MONO_BRACKET) || returnType.startsWith(PAGED_FLUX_BRACKET)
|| returnType.startsWith(POLLER_FLUX_BRACKET)) {
log(methodDefToken, String.format(ASYNC_CONTEXT_ERROR, CONTEXT));
}
} else {
// Context should be passed in as an argument to all public methods annotated with @ServiceMethod that
// return Response<T> in sync clients.
if (returnType.startsWith(RESPONSE_BRACKET)) {
log(methodDefToken, String.format(SYNC_CONTEXT_ERROR, CONTEXT));
if (!containsRequestOptionsParameter) {
// Context or RequestOptions should be passed in as an argument to all public methods
// annotated with @ServiceMethod that return Response<T> in sync clients.
if (returnType.startsWith(RESPONSE_BRACKET)) {
log(methodDefToken, String.format(SYNC_CONTEXT_ERROR, CONTEXT, REQUEST_OPTIONS));
}
}
}
}
Expand Down

0 comments on commit de9dd2a

Please sign in to comment.