Skip to content

Commit

Permalink
http-client-java, fix unbranded pageable overload (#5665)
Browse files Browse the repository at this point in the history
  • Loading branch information
weidongxu-microsoft authored Jan 20, 2025
1 parent 04182d2 commit db8af17
Show file tree
Hide file tree
Showing 11 changed files with 39 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
},
"dependencies": {
"@typespec/http-specs": "0.1.0-alpha.7",
"@typespec/http-client-java": "file:../../typespec-http-client-java-0.1.8.tgz",
"@typespec/http-client-java": "file:../../typespec-http-client-java-0.1.9.tgz",
"@typespec/http-client-java-tests": "file:"
},
"overrides": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,19 @@ public final class PageableClient {
public PagedIterable<Pet> link(RequestOptions requestOptions) {
return this.serviceClient.link(requestOptions);
}

/**
* The link operation.
*
* @throws HttpResponseException thrown if the service returns an error.
* @throws RuntimeException all other wrapped checked exceptions if the request fails to be sent.
* @return the response.
*/
@Metadata(generated = true)
@ServiceMethod(returns = ReturnType.COLLECTION)
public PagedIterable<Pet> link() {
// Generated convenience method for link
RequestOptions requestOptions = new RequestOptions();
return serviceClient.link(requestOptions);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"flavor": "generic",
"CrossLanguageDefinitionId": {
"payload.pageable.PageableClient": "Payload.Pageable.ServerDrivenPagination",
"payload.pageable.PageableClient.link": "Payload.Pageable.ServerDrivenPagination.link",
"payload.pageable.PageableClientBuilder": "Payload.Pageable",
"payload.pageable.Pet": "Payload.Pageable.Pet",
"payload.pageable.serverdrivenpagination.implementation.LinkResponse": "Payload.Pageable.ServerDrivenPagination.link.Response.anonymous"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class PageableTests {

@Test
public void test() {
PagedIterable<Pet> pagedIterable = client.link(null);
PagedIterable<Pet> pagedIterable = client.link();

Assertions.assertEquals(4, pagedIterable.stream().count());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -784,9 +784,7 @@ private static void createPageableClientMethods(Operation operation, boolean isP
.groupedParameterRequired(false)
.methodVisibility(methodVisibility);

if (settings.isGenerateAsyncMethods()) {
methods.add(builder.build());
}
methods.add(builder.build());

// Generate an overload with all parameters, optionally include context.
builder.methodVisibility(visibilityFunction.methodVisibility(true, defaultOverloadType, true));
Expand All @@ -808,12 +806,10 @@ private static void createPageableClientMethods(Operation operation, boolean isP
.groupedParameterRequired(false)
.methodVisibility(visibilityFunction.methodVisibility(false, defaultOverloadType, false));

if (settings.isGenerateAsyncMethods()) {
methods.add(builder.build());
methods.add(builder.build());

// overload for versioning
createOverloadForVersioning(isProtocolMethod, methods, builder, parameters);
}
// overload for versioning
createOverloadForVersioning(isProtocolMethod, methods, builder, parameters);

if (generateClientMethodWithOnlyRequiredParameters) {
methods.add(builder.onlyRequiredParameters(true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1591,7 +1591,7 @@ private String getPagingSinglePageExpression(ClientMethod clientMethod, String m
if (settings.isDataPlaneClient() && settings.isPageSizeEnabled()) {
Optional<String> maxPageSizeSerializedName
= MethodUtil.serializedNameOfMaxPageSizeParameter(clientMethod.getProxyMethod());
if (maxPageSizeSerializedName.isPresent() && settings.isBranded()) {
if (maxPageSizeSerializedName.isPresent()) {
argumentLine = argumentLine.replace("requestOptions", "requestOptionsLocal");
StringBuilder expression = new StringBuilder();
expression.append("(pageSize) -> {");
Expand Down Expand Up @@ -1625,7 +1625,7 @@ private String getPagingNextPageExpression(ClientMethod clientMethod, String met
if (settings.isDataPlaneClient() && settings.isPageSizeEnabled()) {
Optional<String> maxPageSizeSerializedName
= MethodUtil.serializedNameOfMaxPageSizeParameter(clientMethod.getProxyMethod());
if (maxPageSizeSerializedName.isPresent() && settings.isBranded()) {
if (maxPageSizeSerializedName.isPresent()) {
argumentLine = argumentLine.replace("requestOptions", "requestOptionsLocal");
StringBuilder expression = new StringBuilder();
expression.append("(nextLink, pageSize) -> {");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,16 @@ protected void writeInvocationAndConversion(ClientMethod convenienceMethod, Clie
= convenienceMethod.getType() == ClientMethodType.SimpleSyncRestResponse ? "" : ".getValue()";

if (convenienceMethod.getType() == ClientMethodType.PagingSync) {
methodBlock.methodReturn(String.format("serviceClient.%1$s(%2$s).mapPage(bodyItemValue -> %3$s)",
protocolMethod.getName(), invocationExpression,
expressionConvertFromBinaryData(responseBodyType, rawResponseBodyType, "bodyItemValue",
protocolMethod.getProxyMethod().getResponseContentTypes(), typeReferenceStaticClasses)));
String mapPageExpression = "";
if (JavaSettings.getInstance().isBranded()) {
mapPageExpression
= ".mapPage(bodyItemValue -> "
+ expressionConvertFromBinaryData(responseBodyType, rawResponseBodyType, "bodyItemValue",
protocolMethod.getProxyMethod().getResponseContentTypes(), typeReferenceStaticClasses)
+ ")";
}
methodBlock.methodReturn(String.format("serviceClient.%1$s(%2$s)%3$s", protocolMethod.getName(),
invocationExpression, mapPageExpression));
} else if (convenienceMethod.getType() == ClientMethodType.LongRunningBeginSync) {
String methodName = protocolMethod.getName();
methodBlock.methodReturn(String.format("serviceClient.%1$s(%2$s)", methodName, invocationExpression));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"dependencies": {
"@typespec/http-specs": "0.1.0-alpha.7",
"@azure-tools/azure-http-specs": "0.1.0-alpha.5",
"@typespec/http-client-java": "file:../../typespec-http-client-java-0.1.8.tgz",
"@typespec/http-client-java": "file:../../typespec-http-client-java-0.1.9.tgz",
"@typespec/http-client-java-tests": "file:"
},
"overrides": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,7 @@ public TypeSpecPlugin(EmitterOptions options, boolean sdkIntegration) {
SETTINGS_MAP.put("license-header", "SMALL_TYPESPEC");

SETTINGS_MAP.put("sync-methods", "sync-only");
SETTINGS_MAP.put("enable-page-size", false);
SETTINGS_MAP.put("use-default-http-status-code-to-exception-type-mapping", false);
SETTINGS_MAP.put("generate-samples", false);
SETTINGS_MAP.put("generate-tests", false);
Expand Down
4 changes: 2 additions & 2 deletions packages/http-client-java/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/http-client-java/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@typespec/http-client-java",
"version": "0.1.8",
"version": "0.1.9",
"description": "TypeSpec library for emitting Java client from the TypeSpec REST protocol binding",
"keywords": [
"TypeSpec"
Expand Down

0 comments on commit db8af17

Please sign in to comment.