Skip to content

Commit 3f75748

Browse files
authored
Fix nextLink when host is also provided usecase in annotation processor (#45454)
1 parent 4cdf8a1 commit 3f75748

File tree

4 files changed

+60
-3
lines changed

4 files changed

+60
-3
lines changed

sdk/clientcore/annotation-processor/src/main/java/io/clientcore/annotation/processor/models/HttpRequestContext.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ public final class HttpRequestContext {
5555

5656
private int[] expectedStatusCodes;
5757
private TypeMirror defaultExceptionBodyType;
58+
private boolean isUriNextLink;
5859

5960
/**
6061
* Constructs a new HttpRequestContext with default values.
@@ -421,6 +422,24 @@ public TypeMirror getDefaultExceptionBodyType() {
421422
return defaultExceptionBodyType;
422423
}
423424

425+
/**
426+
* Sets whether the URI is a {nextlink}.
427+
*
428+
* @param isUriNextLink true if it is a {nextlink}, false otherwise.
429+
*/
430+
public void setIsUriNextLink(boolean isUriNextLink) {
431+
this.isUriNextLink = isUriNextLink;
432+
}
433+
434+
/**
435+
* Checks if the URI is a {nextlink}.
436+
*
437+
* @return true if it is a {nextlink}, false otherwise.
438+
*/
439+
public boolean isUriNextLink() {
440+
return isUriNextLink;
441+
}
442+
424443
/**
425444
* Represents a method parameter.
426445
*/

sdk/clientcore/annotation-processor/src/main/java/io/clientcore/annotation/processor/templating/JavaParserTemplateProcessor.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -352,8 +352,7 @@ void setHttpRequestUri(BlockStmt body, Expression createHttpRequest, HttpRequest
352352
.filter(param -> param.getVariableElement().getAnnotation(HostParam.class) != null)
353353
.map(HttpRequestContext.MethodParameter::getName)
354354
.collect(Collectors.joining(" + "));
355-
356-
if (CoreUtils.isNullOrEmpty(concatenatedHostParams)) {
355+
if (method.isUriNextLink() || CoreUtils.isNullOrEmpty(concatenatedHostParams)) {
357356
urlStatement = method.getHost();
358357
} else {
359358
urlStatement = concatenatedHostParams + " + \"/\" + " + method.getHost();

sdk/clientcore/annotation-processor/src/main/java/io/clientcore/annotation/processor/utils/PathBuilder.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,18 @@ public static String buildPath(String rawPath, HttpRequestContext method) {
6060
if (substitution != null) {
6161
String substitutionValue
6262
= serialize(JsonSerializer.getInstance(), substitution.getParameterVariableName());
63-
63+
// Special case: if the path is for "{nextLink}", use the variable
64+
if (rawPath.contains("{nextLink}")) {
65+
Substitution nextLinkSubstitution = method.getSubstitution("nextLink");
66+
if (nextLinkSubstitution != null) {
67+
// No escaping or concatenation, use the variable name
68+
method.setIsUriNextLink(true);
69+
return nextLinkSubstitution.getParameterVariableName();
70+
} else {
71+
throw new MissingSubstitutionException(
72+
"Could not find substitution for 'nextLink' in method '" + method.getMethodName() + "'");
73+
}
74+
}
6475
String replacementValue;
6576
Optional<HttpRequestContext.MethodParameter> paramOpt = method.getParameters()
6677
.stream()

sdk/clientcore/annotation-processor/src/test/java/io/clientcore/annotation/processor/utils/PathBuilderTest.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -387,4 +387,32 @@ public void buildsPathWithNullQueryParameterValue() {
387387
String result = PathBuilder.buildPath("https://{endpoint}/keys", context);
388388
assertEquals("\"https://\" + myEndpoint + \"/keys\"", result);
389389
}
390+
391+
@Test
392+
public void buildsPathWithNextLinkSpecialCase() {
393+
HttpRequestContext context = new HttpRequestContext();
394+
context.setMethodName("linkNext");
395+
context.addSubstitution(new Substitution("nextLink", "nextLinkVar"));
396+
String result = PathBuilder.buildPath("{nextLink}", context);
397+
assertEquals("nextLinkVar", result);
398+
}
399+
400+
@Test
401+
public void buildsPathWithNextLinkSpecialCaseMissingSubstitution() {
402+
HttpRequestContext context = new HttpRequestContext();
403+
context.setMethodName("linkNext");
404+
MissingSubstitutionException ex
405+
= assertThrows(MissingSubstitutionException.class, () -> PathBuilder.buildPath("{nextLink}", context));
406+
assertEquals("Could not find substitution for 'nextLink' in method 'linkNext'", ex.getMessage());
407+
}
408+
409+
@Test
410+
public void buildsPathWithEndpointAndNextLink() {
411+
HttpRequestContext context = new HttpRequestContext();
412+
context.setMethodName("linkNext");
413+
context.addSubstitution(new Substitution("endpoint", "myEndpoint"));
414+
context.addSubstitution(new Substitution("nextLink", "nextLinkVar"));
415+
String result = PathBuilder.buildPath("{nextLink}", context);
416+
assertEquals("nextLinkVar", result);
417+
}
390418
}

0 commit comments

Comments
 (0)