Skip to content

Commit 0ec26fb

Browse files
tsp, TCGC getHttpOperationWithCache (#2849)
1 parent 3c5964a commit 0ec26fb

File tree

17 files changed

+341
-299
lines changed

17 files changed

+341
-299
lines changed

javagen/src/main/java/com/azure/autorest/template/example/ClientMethodExampleWriter.java

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
package com.azure.autorest.template.example;
55

6+
import com.azure.autorest.extension.base.model.codemodel.RequestParameterLocation;
67
import com.azure.autorest.model.clientmodel.ClassType;
78
import com.azure.autorest.model.clientmodel.ClientMethod;
89
import com.azure.autorest.model.clientmodel.ClientMethodParameter;
@@ -42,6 +43,7 @@
4243
import java.util.Set;
4344
import java.util.function.BiConsumer;
4445
import java.util.function.Consumer;
46+
import java.util.function.Function;
4547
import java.util.stream.Collectors;
4648

4749
public class ClientMethodExampleWriter {
@@ -284,7 +286,7 @@ private ExampleNode parseNodeFromParameter(ClientMethod convenienceMethod, Proxy
284286
// output parameter's name is the "escaped reserved client method parameter name" of the real parameter's serialized name
285287
// since flattened parameter is always in body, we can deal with that explicitly
286288
ClientMethodParameter outputParameter = detail.getOutParameter();
287-
Map<String, Object> flattenedParameterValue = getFlattenedBodyParameterExampleValue(proxyMethodExample, outputParameter.getName());
289+
Map<String, Object> flattenedParameterValue = getFlattenedBodyParameterExampleValue(proxyMethodExample, outputParameter);
288290
if (flattenedParameterValue != null) {
289291
exampleValue.putAll(flattenedParameterValue);
290292
}
@@ -307,8 +309,8 @@ private ExampleNode parseNodeFromParameter(ClientMethod convenienceMethod, Proxy
307309
return ModelExampleUtil.parseNode(type, wireType, exampleValue);
308310
} else if (isFlattenParameter(convenienceMethod, methodParameter)) {
309311
// flatten, no grouping
310-
String outputParameterName = convenienceMethod.getMethodTransformationDetails().iterator().next().getOutParameter().getName();
311-
Map<String, Object> realParameterValue = getFlattenedBodyParameterExampleValue(proxyMethodExample, outputParameterName);
312+
ClientMethodParameter outputParameter = convenienceMethod.getMethodTransformationDetails().iterator().next().getOutParameter();
313+
Map<String, Object> realParameterValue = getFlattenedBodyParameterExampleValue(proxyMethodExample, outputParameter);
312314

313315
IType type = methodParameter.getClientMethodParameter().getClientType();
314316
IType wireType = methodParameter.getClientMethodParameter().getWireType();
@@ -329,18 +331,25 @@ private ExampleNode parseNodeFromParameter(ClientMethod convenienceMethod, Proxy
329331
}
330332
}
331333

332-
private Map<String, Object> getFlattenedBodyParameterExampleValue(ProxyMethodExample example, String clientMethodParameterName) {
333-
ProxyMethodExample.ParameterValue parameterValue = example.getParameters().entrySet()
334+
@SuppressWarnings("unchecked")
335+
private Map<String, Object> getFlattenedBodyParameterExampleValue(ProxyMethodExample example, ClientMethodParameter clientMethodParameter) {
336+
String clientMethodParameterName = clientMethodParameter.getName();
337+
Function<String, ProxyMethodExample.ParameterValue> getParameterValue = (parameterSerializedName) -> example.getParameters().entrySet()
334338
.stream().filter(
335339
p -> CodeNamer.getEscapedReservedClientMethodParameterName(p.getKey())
336-
.equalsIgnoreCase(clientMethodParameterName))
340+
.equalsIgnoreCase(parameterSerializedName))
337341
.map(Map.Entry::getValue)
338342
.findFirst()
339343
.orElse(null);
340-
if (parameterValue == null) {
341-
return null;
344+
ProxyMethodExample.ParameterValue parameterValue = getParameterValue.apply(clientMethodParameterName);
345+
346+
if (parameterValue == null && clientMethodParameter.getRequestParameterLocation() == RequestParameterLocation.BODY && !"body".equalsIgnoreCase(clientMethodParameterName)) {
347+
// fallback, "body" is commonly used in example JSON for request body
348+
clientMethodParameterName = "body";
349+
parameterValue = getParameterValue.apply(clientMethodParameterName);
342350
}
343-
return (Map<String, Object>) parameterValue.getObjectValue();
351+
352+
return parameterValue == null ? null : (Map<String, Object>) parameterValue.getObjectValue();
344353
}
345354

346355
private boolean isGroupingParameter(ClientMethod convenienceMethod, MethodParameter methodParameter) {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,7 @@ public static Object getParameterExampleValue(ProxyMethodExample example, String
307307
}
308308

309309
// fallback, "body" is commonly used in example JSON for request body
310-
if (parameterValue == null) {
310+
if (parameterValue == null && !"body".equalsIgnoreCase(serializedName)) {
311311
serializedName = "body";
312312
parameterValue = findParameter(example, serializedName);
313313
}

typespec-extension/src/code-model-builder.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ import {
6363
getClientType,
6464
getCrossLanguageDefinitionId,
6565
getDefaultApiVersion,
66+
getHttpOperationWithCache,
6667
getWireName,
6768
isApiVersion,
6869
isInternal,
@@ -93,7 +94,6 @@ import {
9394
getProjectedName,
9495
getSummary,
9596
getVisibility,
96-
ignoreDiagnostics,
9797
isArrayModelType,
9898
isErrorModel,
9999
isRecordModelType,
@@ -113,7 +113,6 @@ import {
113113
getAuthentication,
114114
getHeaderFieldName,
115115
getHeaderFieldOptions,
116-
getHttpOperation,
117116
getPathParamName,
118117
getQueryParamName,
119118
getQueryParamOptions,
@@ -709,7 +708,7 @@ export class CodeModelBuilder {
709708
}
710709

711710
private processOperation(groupName: string, operation: Operation, clientContext: ClientContext): CodeModelOperation {
712-
const op = ignoreDiagnostics(getHttpOperation(this.program, operation));
711+
const op = getHttpOperationWithCache(this.sdkContext, operation);
713712

714713
const operationGroup = this.codeModel.getOperationGroup(groupName);
715714
const operationName = this.getName(operation);

0 commit comments

Comments
 (0)