Skip to content

Commit d14a9d2

Browse files
client, repeatability-first-sent should be set when sending request (#2839)
* client, repeatability-first-sent should be set when sending request * re-generate test code * use MockHttpResponse from core-test --------- Co-authored-by: actions-user <[email protected]>
1 parent 879418d commit d14a9d2

File tree

16 files changed

+156
-263
lines changed

16 files changed

+156
-263
lines changed

fluentgen/src/main/java/com/azure/autorest/fluent/template/FluentClientMethodTemplate.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ protected void generatePagedAsyncSinglePage(ClientMethod clientMethod, JavaType
3939
addOptionalAndConstantVariables(function, clientMethod, restAPIMethod.getParameters(), settings);
4040
applyParameterTransformations(function, clientMethod, settings);
4141
convertClientTypesToWireTypes(function, clientMethod, restAPIMethod.getParameters(), clientMethod.getClientReference(), settings);
42-
addSpecialHeadersToLocalVariables(function, clientMethod);
4342
if (mergeContextParameter) {
4443
function.line(String.format("context = %s.mergeContext(context);", clientMethod.getClientReference()));
4544
}
@@ -206,7 +205,6 @@ protected void generateSimpleAsyncRestResponse(ClientMethod clientMethod, JavaTy
206205
addOptionalAndConstantVariables(function, clientMethod, restAPIMethod.getParameters(), settings);
207206
applyParameterTransformations(function, clientMethod, settings);
208207
convertClientTypesToWireTypes(function, clientMethod, restAPIMethod.getParameters(), clientMethod.getClientReference(), settings);
209-
addSpecialHeadersToLocalVariables(function, clientMethod);
210208

211209
String restAPIMethodArgumentList = String.join(", ", clientMethod.getProxyMethodArguments(settings));
212210
String serviceMethodCall = String.format("service.%s(%s)", restAPIMethod.getName(), restAPIMethodArgumentList);

javagen/src/main/java/com/azure/autorest/mapper/ProxyMethodMapper.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -739,13 +739,13 @@ protected List<ProxyMethodParameter> getSpecialParameters(Operation operation) {
739739

740740
specialParameters.add(commonBuilderSetting.apply(new ProxyMethodParameter.Builder()
741741
.name(MethodUtil.REPEATABILITY_REQUEST_ID_VARIABLE_NAME)
742-
.parameterReference(MethodUtil.REPEATABILITY_REQUEST_ID_VARIABLE_NAME)
742+
.parameterReference(MethodUtil.REPEATABILITY_REQUEST_ID_EXPRESSION)
743743
.requestParameterName(MethodUtil.REPEATABILITY_REQUEST_ID_HEADER)
744744
.description("Repeatability request ID header"))
745745
.build());
746746
specialParameters.add(commonBuilderSetting.apply(new ProxyMethodParameter.Builder()
747747
.name(MethodUtil.REPEATABILITY_FIRST_SENT_VARIABLE_NAME)
748-
.parameterReference(MethodUtil.REPEATABILITY_FIRST_SENT_VARIABLE_NAME)
748+
.parameterReference(MethodUtil.REPEATABILITY_FIRST_SENT_EXPRESSION)
749749
.requestParameterName(MethodUtil.REPEATABILITY_FIRST_SENT_HEADER)
750750
.description("Repeatability first sent header as HTTP-date"))
751751
.build());

javagen/src/main/java/com/azure/autorest/template/ClientMethodTemplate.java

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -503,9 +503,8 @@ private static boolean addSpecialHeadersToRequestOptions(JavaBlock function, Cli
503503

504504
// repeatability headers
505505
if (repeatabilityRequestHeaders) {
506-
addSpecialHeadersToLocalVariables(function, clientMethod);
507-
requestOptionsSetHeaderIfAbsent(function, MethodUtil.REPEATABILITY_REQUEST_ID_VARIABLE_NAME, MethodUtil.REPEATABILITY_REQUEST_ID_HEADER);
508-
requestOptionsSetHeaderIfAbsent(function, MethodUtil.REPEATABILITY_FIRST_SENT_VARIABLE_NAME, MethodUtil.REPEATABILITY_FIRST_SENT_HEADER);
506+
requestOptionsSetHeaderIfAbsent(function, MethodUtil.REPEATABILITY_REQUEST_ID_EXPRESSION, MethodUtil.REPEATABILITY_REQUEST_ID_HEADER);
507+
requestOptionsSetHeaderIfAbsent(function, MethodUtil.REPEATABILITY_FIRST_SENT_EXPRESSION, MethodUtil.REPEATABILITY_FIRST_SENT_HEADER);
509508
}
510509

511510
// content-type headers for optional body parameter
@@ -523,23 +522,16 @@ private static boolean addSpecialHeadersToRequestOptions(JavaBlock function, Cli
523522
return requestOptionsLocal;
524523
}
525524

526-
private static void requestOptionsSetHeaderIfAbsent(JavaBlock function, String variableName, String headerName) {
525+
private static void requestOptionsSetHeaderIfAbsent(JavaBlock function, String expression, String headerName) {
527526
function.line("requestOptionsLocal.addRequestCallback(requestLocal -> {");
528527
function.indent(() -> {
529528
function.ifBlock(String.format("requestLocal.getHeaders().get(HttpHeaderName.fromString(\"%1$s\")) == null", headerName), ifBlock -> {
530-
function.line(String.format("requestLocal.getHeaders().set(HttpHeaderName.fromString(\"%1$s\"), %2$s);", headerName, variableName));
529+
function.line(String.format("requestLocal.getHeaders().set(HttpHeaderName.fromString(\"%1$s\"), %2$s);", headerName, expression));
531530
});
532531
});
533532
function.line("});");
534533
}
535534

536-
protected static void addSpecialHeadersToLocalVariables(JavaBlock function, ClientMethod clientMethod) {
537-
if (MethodUtil.isMethodIncludeRepeatableRequestHeaders(clientMethod.getProxyMethod())) {
538-
function.line(String.format("String %1$s = CoreUtils.randomUuid().toString();", MethodUtil.REPEATABILITY_REQUEST_ID_VARIABLE_NAME));
539-
function.line(String.format("String %1$s = DateTimeRfc1123.toRfc1123String(OffsetDateTime.now());", MethodUtil.REPEATABILITY_FIRST_SENT_VARIABLE_NAME));
540-
}
541-
}
542-
543535
protected static void writeMethod(JavaType typeBlock, JavaVisibility visibility, String methodSignature, Consumer<JavaBlock> method) {
544536
if (visibility == JavaVisibility.Public) {
545537
typeBlock.publicMethod(methodSignature, method);
@@ -742,8 +734,6 @@ private void generatePagedSinglePage(ClientMethod clientMethod, JavaType typeBlo
742734
boolean requestOptionsLocal = false;
743735
if (settings.isDataPlaneClient()) {
744736
requestOptionsLocal = addSpecialHeadersToRequestOptions(function, clientMethod);
745-
} else {
746-
addSpecialHeadersToLocalVariables(function, clientMethod);
747737
}
748738

749739
String serviceMethodCall = checkAndReplaceParamNameCollision(clientMethod, restAPIMethod, requestOptionsLocal, settings);
@@ -1063,8 +1053,6 @@ protected void generatePlainSyncMethod(ClientMethod clientMethod, JavaType typeB
10631053
boolean requestOptionsLocal = false;
10641054
if (settings.isDataPlaneClient()) {
10651055
requestOptionsLocal = addSpecialHeadersToRequestOptions(function, clientMethod);
1066-
} else {
1067-
addSpecialHeadersToLocalVariables(function, clientMethod);
10681056
}
10691057

10701058
String serviceMethodCall = checkAndReplaceParamNameCollision(clientMethod, restAPIMethod.toSync(), requestOptionsLocal,
@@ -1163,8 +1151,6 @@ protected void generatePagedAsyncSinglePage(ClientMethod clientMethod, JavaType
11631151
boolean requestOptionsLocal = false;
11641152
if (settings.isDataPlaneClient()) {
11651153
requestOptionsLocal = addSpecialHeadersToRequestOptions(function, clientMethod);
1166-
} else {
1167-
addSpecialHeadersToLocalVariables(function, clientMethod);
11681154
}
11691155

11701156
String serviceMethodCall = checkAndReplaceParamNameCollision(clientMethod, restAPIMethod, requestOptionsLocal, settings);
@@ -1300,8 +1286,6 @@ protected void generateSimpleAsyncRestResponse(ClientMethod clientMethod, JavaTy
13001286
boolean requestOptionsLocal = false;
13011287
if (settings.isDataPlaneClient()) {
13021288
requestOptionsLocal = addSpecialHeadersToRequestOptions(function, clientMethod);
1303-
} else {
1304-
addSpecialHeadersToLocalVariables(function, clientMethod);
13051289
}
13061290

13071291
String serviceMethodCall = checkAndReplaceParamNameCollision(clientMethod, restAPIMethod, requestOptionsLocal, settings);

javagen/src/main/java/com/azure/autorest/util/MethodUtil.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ public class MethodUtil {
4747
public static final String REPEATABILITY_FIRST_SENT_HEADER = "repeatability-first-sent";
4848
public static final String REPEATABILITY_REQUEST_ID_VARIABLE_NAME = CodeNamer.toCamelCase(REPEATABILITY_REQUEST_ID_HEADER);
4949
public static final String REPEATABILITY_FIRST_SENT_VARIABLE_NAME = CodeNamer.toCamelCase(REPEATABILITY_FIRST_SENT_HEADER);
50+
public static final String REPEATABILITY_REQUEST_ID_EXPRESSION = "CoreUtils.randomUuid().toString()";
51+
public static final String REPEATABILITY_FIRST_SENT_EXPRESSION = "DateTimeRfc1123.toRfc1123String(OffsetDateTime.now())";
5052

5153
private static final Set<HttpMethod> REPEATABILITY_REQUEST_HTTP_METHODS
5254
= EnumSet.of(HttpMethod.PUT, HttpMethod.PATCH, HttpMethod.DELETE, HttpMethod.POST);

protocol-tests/pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,12 @@
2323
<groupId>com.azure</groupId>
2424
<artifactId>azure-core</artifactId>
2525
</dependency>
26+
<dependency>
27+
<groupId>com.azure</groupId>
28+
<artifactId>azure-core-test</artifactId>
29+
<version>1.26.0</version>
30+
<scope>test</scope>
31+
</dependency>
2632
<dependency>
2733
<groupId>com.azure</groupId>
2834
<artifactId>azure-core-http-netty</artifactId>

0 commit comments

Comments
 (0)