Skip to content

Commit de9dd2a

Browse files
code-quality-reports, allow RequestOptions instead of Context in sync service method (Azure#25429)
1 parent fcc34f4 commit de9dd2a

File tree

1 file changed

+20
-6
lines changed

1 file changed

+20
-6
lines changed

eng/code-quality-reports/src/main/java/com/azure/tools/checkstyle/checks/ServiceClientCheck.java

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ public class ServiceClientCheck extends AbstractCheck {
4646
private static final String CLIENT = "Client";
4747
private static final String IS_ASYNC = "isAsync";
4848
private static final String CONTEXT = "Context";
49+
private static final String REQUEST_OPTIONS = "RequestOptions";
4950

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

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

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

417+
final boolean containsRequestOptionsParameter = TokenUtil.findFirstTokenByPredicate(parametersToken,
418+
parameterToken -> {
419+
if (parameterToken.getType() != TokenTypes.PARAMETER_DEF) {
420+
return false;
421+
}
422+
final DetailAST paramTypeIdentToken =
423+
parameterToken.findFirstToken(TokenTypes.TYPE).findFirstToken(TokenTypes.IDENT);
424+
return paramTypeIdentToken != null && REQUEST_OPTIONS.equals(paramTypeIdentToken.getText());
425+
})
426+
.isPresent();
427+
416428
if (containsContextParameter) {
417429
// MONO and PagedFlux return type implies Asynchronous method
418430
if (returnType.startsWith(MONO_BRACKET) || returnType.startsWith(PAGED_FLUX_BRACKET)
419431
|| returnType.startsWith(POLLER_FLUX_BRACKET)) {
420432
log(methodDefToken, String.format(ASYNC_CONTEXT_ERROR, CONTEXT));
421433
}
422434
} else {
423-
// Context should be passed in as an argument to all public methods annotated with @ServiceMethod that
424-
// return Response<T> in sync clients.
425-
if (returnType.startsWith(RESPONSE_BRACKET)) {
426-
log(methodDefToken, String.format(SYNC_CONTEXT_ERROR, CONTEXT));
435+
if (!containsRequestOptionsParameter) {
436+
// Context or RequestOptions should be passed in as an argument to all public methods
437+
// annotated with @ServiceMethod that return Response<T> in sync clients.
438+
if (returnType.startsWith(RESPONSE_BRACKET)) {
439+
log(methodDefToken, String.format(SYNC_CONTEXT_ERROR, CONTEXT, REQUEST_OPTIONS));
440+
}
427441
}
428442
}
429443
}

0 commit comments

Comments
 (0)