Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,39 +3,49 @@

package com.azure.autorest.template;

import com.azure.autorest.Javagen;
import com.azure.autorest.extension.base.model.codemodel.RequestParameterLocation;
import com.azure.autorest.extension.base.plugin.JavaSettings;
import com.azure.autorest.extension.base.plugin.PluginLogger;
import com.azure.autorest.model.clientmodel.AsyncSyncClient;
import com.azure.autorest.model.clientmodel.ClassType;
import com.azure.autorest.model.clientmodel.ClientMethod;
import com.azure.autorest.model.clientmodel.ClientMethodParameter;
import com.azure.autorest.model.clientmodel.ProtocolExample;
import com.azure.autorest.model.clientmodel.ProxyMethod;
import com.azure.autorest.model.clientmodel.ProxyMethodExample;
import com.azure.autorest.model.clientmodel.ProxyMethodParameter;
import com.azure.autorest.model.clientmodel.ServiceClient;
import com.azure.autorest.model.clientmodel.ServiceClientProperty;
import com.azure.autorest.model.javamodel.JavaFile;
import com.azure.autorest.util.CodeNamer;
import com.azure.core.http.rest.PagedIterable;
import com.azure.core.http.rest.Response;
import com.azure.core.util.BinaryData;
import com.azure.core.util.Configuration;
import com.azure.core.util.Context;
import org.slf4j.Logger;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;

public class ProtocolSampleTemplate implements IJavaTemplate<ProtocolExample, JavaFile> {
private static final ProtocolSampleTemplate _instance = new ProtocolSampleTemplate();

private final Logger LOGGER = new PluginLogger(Javagen.getPluginInstance(), ProtocolSampleTemplate.class);

private static final ProtocolSampleTemplate INSTANCE = new ProtocolSampleTemplate();

protected ProtocolSampleTemplate() {}

public static ProtocolSampleTemplate getInstance() {
return _instance;
return INSTANCE;
}

@SuppressWarnings("unchecked")
Expand All @@ -45,7 +55,7 @@ public void write(ProtocolExample protocolExample, JavaFile javaFile) {
ServiceClient serviceClient = protocolExample.getServiceClient();
String builderName = protocolExample.getBuilderName();
String filename = protocolExample.getFilename();
ProxyMethodExample example = protocolExample.getProxyMethodExample();
ProxyMethodExample proxyMethodExample = protocolExample.getProxyMethodExample();

// Import
List<String> imports = new ArrayList<>();
Expand Down Expand Up @@ -74,32 +84,39 @@ public void write(ProtocolExample protocolExample, JavaFile javaFile) {
List<String> clientParameterLines = new ArrayList<>();
Set<ServiceClientProperty> processedServiceClientProperties = new HashSet<>();

example.getParameters().forEach((parameterName, parameterValue) -> {
List<ProxyMethodParameter> proxyMethodParameters = getProxyMethodParameters(method.getProxyMethod(), method.getParameters());

proxyMethodExample.getParameters().forEach((parameterName, parameterValue) -> {
boolean matchRequiredParameter = false;
for (int i = 0; i < numParam; i++) {
ClientMethodParameter p = method.getParameters().get(i);
// TODO: should use getRequestParameterName from proxy method parameter, instead of getName from client method parameter
if (p.getName().equalsIgnoreCase(parameterName)) {
if (p.getClientType() != ClassType.BinaryData) {
// TODO: handle query with array

String exampleValue = p.getLocation() == RequestParameterLocation.QUERY
? parameterValue.getUnescapedQueryValue().toString()
: parameterValue.getObjectValue().toString();
params.set(i, p.getClientType().defaultValueExpression(exampleValue));
} else {
// BinaryData
String binaryDataValue = ClassType.String.defaultValueExpression(parameterValue.getJsonString());
binaryDataStmt.append(
String.format("BinaryData %s = BinaryData.fromString(%s);",
parameterName, binaryDataValue));
params.set(i, parameterName);
for (int parameterIndex = 0; parameterIndex < numParam; parameterIndex++) {
ProxyMethodParameter proxyMethodParameter = proxyMethodParameters.get(parameterIndex);
if (proxyMethodParameter != null) {
if (getSerializedName(proxyMethodParameter).equalsIgnoreCase(parameterName)) {
// parameter in example found in method signature

if (proxyMethodParameter.getClientType() != ClassType.BinaryData) {
// ignore query with array, query parameter is not included in LLC method signature

String exampleValue = proxyMethodParameter.getRequestParameterLocation() == RequestParameterLocation.QUERY
? parameterValue.getUnescapedQueryValue().toString()
: parameterValue.getObjectValue().toString();
params.set(parameterIndex, proxyMethodParameter.getClientType().defaultValueExpression(exampleValue));
} else {
// BinaryData
String binaryDataValue = ClassType.String.defaultValueExpression(parameterValue.getJsonString());
binaryDataStmt.append(
String.format("BinaryData %s = BinaryData.fromString(%s);",
parameterName, binaryDataValue));
params.set(parameterIndex, parameterName);
}
matchRequiredParameter = true;
break;
}
matchRequiredParameter = true;
break;
}
}
if (!matchRequiredParameter) {
// parameter in example not found in method signature, check those parameters defined in spec but was left out of method signature

method.getProxyMethod().getAllParameters().stream().filter(p -> !p.getFromClient()).filter(p -> getSerializedName(p).equalsIgnoreCase(parameterName)).findFirst().ifPresent(p -> {
switch (p.getRequestParameterLocation()) {
case QUERY:
Expand Down Expand Up @@ -232,4 +249,24 @@ private static String getSerializedName(ProxyMethodParameter parameter) {
}
return serializedName;
}

private List<ProxyMethodParameter> getProxyMethodParameters(
ProxyMethod proxyMethod,
List<ClientMethodParameter> clientMethodParameters) {
// the list of proxy method parameters will be 1-1 with list of client method parameters

Map<String, ProxyMethodParameter> proxyMethodParameterByClientParameterName = proxyMethod.getParameters().stream()
.collect(Collectors.toMap(p -> CodeNamer.getEscapedReservedClientMethodParameterName(p.getName()), Function.identity()));
List<ProxyMethodParameter> proxyMethodParameters = new ArrayList<>();
for (ClientMethodParameter clientMethodParameter : clientMethodParameters) {
ProxyMethodParameter proxyMethodParameter = proxyMethodParameterByClientParameterName.get(clientMethodParameter.getName());
proxyMethodParameters.add(proxyMethodParameter);
Comment on lines +258 to +263
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we should put requestParameterLocation to client method parameter, instead of making the mapping.


if (proxyMethodParameter == null) {
// this should not happen unless we changed the naming of client method parameter from proxy method parameter
LOGGER.warn("Failed to find proxy method parameter for client method parameter with name '{}'", clientMethodParameter.getName());
}
}
return proxyMethodParameters;
}
}