Skip to content

Commit 7912cb5

Browse files
committed
ClientMethodTemplate: make parameterWireType identifcation code similar to the rest of the code in convertClientTypesToWireTypes
1 parent e73a408 commit 7912cb5

File tree

1 file changed

+40
-27
lines changed
  • packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/template

1 file changed

+40
-27
lines changed

packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/template/ClientMethodTemplate.java

Lines changed: 40 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -145,14 +145,13 @@ protected static void addOptionalAndConstantVariables(JavaBlock function, Client
145145
// parameter is scoped to the client, hence no local variable instantiation for it.
146146
continue;
147147
}
148-
148+
final IType parameterClientType = parameter.getClientType();
149149
IType parameterWireType;
150150
if (parameter.isNullable()) {
151151
parameterWireType = parameter.getWireType().asNullable();
152152
} else {
153153
parameterWireType = parameter.getWireType();
154154
}
155-
final IType parameterClientType = parameter.getClientType();
156155

157156
// TODO (alzimmer): There are a few similar transforms like this but they all have slight nuances on output.
158157
// This always turns ArrayType and ListType into String, the case further down this file may not.
@@ -320,33 +319,44 @@ protected static void applyParameterTransformations(JavaBlock function, ClientMe
320319
protected static void convertClientTypesToWireTypes(JavaBlock function, ClientMethod clientMethod) {
321320
final List<ProxyMethodParameter> proxyMethodParameters = clientMethod.getProxyMethod().getParameters();
322321
for (ProxyMethodParameter parameter : proxyMethodParameters) {
323-
IType parameterWireType = parameter.getWireType();
322+
final RequestParameterLocation location = parameter.getRequestParameterLocation();
323+
final IType parameterClientType = parameter.getClientType();
324324

325+
IType parameterWireType;
325326
if (parameter.isNullable()) {
326-
parameterWireType = parameterWireType.asNullable();
327+
parameterWireType = parameter.getWireType().asNullable();
328+
} else {
329+
parameterWireType = parameter.getWireType();
327330
}
328331

329-
IType parameterClientType = parameter.getClientType();
330-
331-
// TODO (alzimmer): Reconcile the logic here with that earlier in the file.
332-
// This check parameter explosion but earlier in the file it doesn't.
333-
if (parameterWireType != ClassType.BASE_64_URL
334-
&& parameter.getRequestParameterLocation() != RequestParameterLocation.BODY
335-
// && parameter.getRequestParameterLocation() != RequestParameterLocation.FormData &&
336-
&& (parameterClientType instanceof ArrayType || parameterClientType instanceof IterableType)) {
337-
parameterWireType = (parameter.getExplode()) ? new ListType(ClassType.STRING) : ClassType.STRING;
332+
if (location != RequestParameterLocation.BODY) {
333+
if (parameterClientType == ArrayType.BYTE_ARRAY) {
334+
if (parameterWireType != ClassType.BASE_64_URL) {
335+
// A byte[] with wire-type not as Base64-encoded URL is converted to Base64-encoded 'String'.
336+
// Refer byteArrayToBase64Encoded(..)
337+
parameterWireType = ClassType.STRING;
338+
}
339+
} else if (parameterClientType instanceof IterableType) {
340+
if (!parameter.getExplode()) {
341+
// Iterable gets converted to a delimited 'String' of wire values.
342+
// Refer iterableToDelimitedStringOfWireValues(..)
343+
parameterWireType = ClassType.STRING;
344+
} else {
345+
// Iterable gets converted to 'List<String>'.
346+
// Refer iterableToWireStringValuesList(..)'
347+
parameterWireType = new ListType(ClassType.STRING);
348+
}
349+
}
338350
}
339351

340-
// If the wire type and client type are the same there is no conversion needed.
341352
if (parameterWireType == parameterClientType) {
353+
// If the wire type and client type are the same there is no conversion needed.
342354
continue;
343355
}
344356

345-
String parameterName = parameter.getParameterReference();
346-
String parameterWireName = parameter.getParameterReferenceConverted();
347-
357+
final String parameterName = parameter.getParameterReference();
358+
final String parameterWireName = parameter.getParameterReferenceConverted();
348359
final boolean alwaysNull = clientMethod.getOnlyRequiredParameters() && !parameter.isRequired();
349-
final RequestParameterLocation location = parameter.getRequestParameterLocation();
350360

351361
boolean addedConversion = false;
352362
if (location != RequestParameterLocation.BODY) {
@@ -355,7 +365,7 @@ protected static void convertClientTypesToWireTypes(JavaBlock function, ClientMe
355365
if (alwaysNull) {
356366
function.line("%s %s = %s;", parameterWireType, parameterWireName, "null");
357367
} else {
358-
final String expression = byteArrayToBase64UrlEncodedString(parameterName, parameterWireType);
368+
final String expression = byteArrayToBase64Encoded(parameterName, parameterWireType);
359369
function.line("%s %s = %s;", parameterWireType, parameterWireName, expression);
360370
}
361371
addedConversion = true;
@@ -552,17 +562,20 @@ private static String iterableToWireStringValuesList(String parameterName, boole
552562
}
553563

554564
/**
555-
* Obtain the Java code that converts a parameter of type byte[] to a Base64 URL encoded string.
565+
* Obtain the Java code that converts a parameter of type byte[] to Base64 encoded form (encoded as string or url).
556566
*
557567
* @param parameterName the name of the byte[] parameter to convert.
558-
* @param wireType the wire type of the parameter, used to determine the method to call for encoding.
559-
* @return Java code that converts a byte[] parameter to a Base64 URL encoded string.
568+
* @param wireType the wire type of the parameter, used to determine whether to encode as a string or url.
569+
* @return Java code that converts a byte[] parameter to Base64 encoded form.
560570
*/
561-
private static String byteArrayToBase64UrlEncodedString(String parameterName, IType wireType) {
562-
final String methodCall = (wireType == ClassType.STRING)
563-
? "Base64Util.encodeToString"
564-
: (ClassType.BASE_64_URL.getName() + ".encode");
565-
return methodCall + "(" + parameterName + ")";
571+
private static String byteArrayToBase64Encoded(String parameterName, IType wireType) {
572+
if ((wireType == ClassType.STRING)) {
573+
// byte[] to Base64-encoded String.
574+
return "Base64Util.encodeToString" + "(" + parameterName + ")";
575+
} else {
576+
// byte[] to Base64-encoded URL.
577+
return ClassType.BASE_64_URL.getName() + ".encode" + "(" + parameterName + ")";
578+
}
566579
}
567580

568581
private static boolean addSpecialHeadersToRequestOptions(JavaBlock function, ClientMethod clientMethod) {

0 commit comments

Comments
 (0)