From 6297adb743ac3f087e9e7e87ac445a56d70c77dc Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Wed, 20 May 2026 07:07:34 +0000 Subject: [PATCH 1/7] Initial plan From 3c13ebeafe807948130c17869649390116940bb1 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Wed, 20 May 2026 07:23:52 +0000 Subject: [PATCH 2/7] feat(java): add max-overload mode for model/protocol WithResponse methods Agent-Logs-Url: https://github.com/microsoft/typespec/sessions/aee86c1c-fc77-4e89-93e8-a148c5d49443 Co-authored-by: srnagar <51379715+srnagar@users.noreply.github.com> --- .../http-client-java/emitter/src/options.ts | 8 +++ .../core/extension/plugin/JavaSettings.java | 35 ++++++++++ .../core/mapper/ClientMethodMapper.java | 36 ++++++++-- .../ConvenienceAsyncMethodTemplate.java | 10 ++- .../ConvenienceMethodTemplateBase.java | 14 +++- .../ConvenienceSyncMethodTemplate.java | 9 ++- ...ientCoreConvenienceSyncMethodTemplate.java | 3 +- .../generator/core/util/ClientModelUtil.java | 36 +++++++++- .../core/mapper/ClientMethodMapperTests.java | 68 +++++++++++++++++++ 9 files changed, 204 insertions(+), 15 deletions(-) diff --git a/packages/http-client-java/emitter/src/options.ts b/packages/http-client-java/emitter/src/options.ts index 8371d18e6aa..8ce37219890 100644 --- a/packages/http-client-java/emitter/src/options.ts +++ b/packages/http-client-java/emitter/src/options.ts @@ -25,6 +25,7 @@ export interface License { export interface EmitterOptions { license?: License; "dev-options"?: DevOptions; + "max-overload"?: "protocol" | "model" | "all"; } export const EmitterOptionsSchema: JSONSchemaType = { @@ -66,6 +67,13 @@ export const EmitterOptionsSchema: JSONSchemaType = { additionalProperties: false, required: [], }, + "max-overload": { + type: "string", + description: + "Selects which max `WithResponse` overloads are generated for data-plane clients.", + nullable: true, + enum: ["protocol", "model", "all"], + }, }, additionalProperties: false, required: [], diff --git a/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/extension/plugin/JavaSettings.java b/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/extension/plugin/JavaSettings.java index 372d154f8cc..e001b8d04ae 100644 --- a/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/extension/plugin/JavaSettings.java +++ b/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/extension/plugin/JavaSettings.java @@ -216,6 +216,9 @@ private JavaSettings(AutorestSettings autorestSettings) { // The sync methods generation mode. this.syncMethods = SyncMethodsGeneration.fromValue(getStringValue(host, "sync-methods", "essential")); + // The max overload generation mode for WithResponse APIs. + this.maxOverload = MaxOverload.fromValue(getStringValue(host, "max-overload", "protocol")); + // Whether to add a logger to the generated clients. this.clientLogger = getBooleanValue(host, "client-logger", true); @@ -962,6 +965,20 @@ public final SyncMethodsGeneration getSyncMethods() { return syncMethods; } + private final MaxOverload maxOverload; + + public MaxOverload getMaxOverload() { + return maxOverload; + } + + public boolean isGenerateProtocolMaxOverload() { + return maxOverload == MaxOverload.PROTOCOL || maxOverload == MaxOverload.ALL; + } + + public boolean isGenerateModelMaxOverload() { + return maxOverload == MaxOverload.MODEL || maxOverload == MaxOverload.ALL; + } + /** * Whether to generate async methods. * @@ -1052,6 +1069,24 @@ public static SyncMethodsGeneration fromValue(String value) { } } + public enum MaxOverload { + PROTOCOL, + MODEL, + ALL; + + public static MaxOverload fromValue(String value) { + if (value == null) { + return PROTOCOL; + } else if (value.equals("model")) { + return MODEL; + } else if (value.equals("all")) { + return ALL; + } else { + return PROTOCOL; + } + } + } + private final List customTypes; /** diff --git a/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/mapper/ClientMethodMapper.java b/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/mapper/ClientMethodMapper.java index 78819015ee5..c4255738f20 100644 --- a/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/mapper/ClientMethodMapper.java +++ b/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/mapper/ClientMethodMapper.java @@ -161,7 +161,8 @@ private List createClientMethods(Operation operation, boolean isPr final ClientMethodsReturnDescription methodsReturnDescription = ClientMethodsReturnDescription .create(operation, isProtocolMethod, proxyMethod.isCustomHeaderIgnored()); final CreateMethodArgs createMethodArgs = new CreateMethodArgs(settings, isProtocolMethod, - methodsReturnDescription, methodNamer, getMethodOverloadType(paramsDetails)); + methodsReturnDescription, methodNamer, getMethodOverloadType(paramsDetails), + operation.getConvenienceApi() != null); if (operation.isPageable()) { // Create Paging Client Methods. @@ -789,6 +790,7 @@ private void createSimpleClientMethods(boolean isSync, ClientMethod baseMethod, private void createSimpleWithResponseClientMethods(boolean isSync, ClientMethod baseMethod, List methods, CreateMethodArgs createMethodArgs) { + final JavaSettings settings = createMethodArgs.settings; final boolean isProtocolMethod = createMethodArgs.isProtocolMethod; final MethodOverloadType methodOverloadType = createMethodArgs.methodOverloadType; final ClientMethodsReturnDescription methodsReturnDescription = createMethodArgs.methodsReturnDescription; @@ -805,10 +807,18 @@ private void createSimpleWithResponseClientMethods(boolean isSync, ClientMethod methodName = methodNamer.getSimpleAsyncRestResponseMethodName(); clientMethodType = ClientMethodType.SimpleAsyncRestResponse; } - final JavaVisibility methodVisibility + JavaVisibility methodVisibility = methodVisibility(clientMethodType, methodOverloadType, false, isProtocolMethod); - final JavaVisibility methodWithContextVisibility + JavaVisibility methodWithContextVisibility = methodVisibility(clientMethodType, methodOverloadType, true, isProtocolMethod); + if (settings.isDataPlaneClient() + && isProtocolMethod + && settings.getMaxOverload() == JavaSettings.MaxOverload.MODEL + && createMethodArgs.hasConvenienceApi + && (clientMethodType == ClientMethodType.SimpleSyncRestResponse + || clientMethodType == ClientMethodType.SimpleAsyncRestResponse)) { + methodWithContextVisibility = NOT_VISIBLE; + } final boolean hasContextOverload = methodWithContextVisibility != NOT_GENERATE; final ClientMethod withResponseMethod = baseMethod.newBuilder() @@ -824,8 +834,13 @@ private void createSimpleWithResponseClientMethods(boolean isSync, ClientMethod // Always generate an overload of WithResponse with non-required parameters without Context. It is only for sync // proxy method, and is usually filtered out in methodVisibility function. methods.add(withResponseMethod); - ClientMethod clientMethodWithContext - = addClientMethodWithContext(methods, withResponseMethod, methodWithContextVisibility, isProtocolMethod); + final boolean useProtocolContextParam = !isProtocolMethod + && settings.isDataPlaneClient() + && settings.isGenerateModelMaxOverload() + && (clientMethodType == ClientMethodType.SimpleSyncRestResponse + || clientMethodType == ClientMethodType.SimpleAsyncRestResponse); + ClientMethod clientMethodWithContext = addClientMethodWithContext(methods, withResponseMethod, + methodWithContextVisibility, useProtocolContextParam || isProtocolMethod); // Simple op '[Operation]WithResponse' overloads for versioning createOverloadForVersioning(methods, withResponseMethod, clientMethodWithContext, methodWithContextVisibility, @@ -959,6 +974,11 @@ protected JavaVisibility methodVisibility(ClientMethodType methodType, MethodOve } return VISIBLE; } else { + if ((methodType == ClientMethodType.SimpleSyncRestResponse + || methodType == ClientMethodType.SimpleAsyncRestResponse) + && settings.isGenerateModelMaxOverload()) { + return hasContextParameter ? VISIBLE : NOT_GENERATE; + } // at present, only generate convenience method for simple API and pageable API (no LRO) return ((methodType == ClientMethodType.SimpleAsync && !hasContextParameter) || (methodType == ClientMethodType.SimpleSync && !hasContextParameter) @@ -1044,22 +1064,24 @@ protected static class CreateMethodArgs { public final MethodOverloadType methodOverloadType; public final MethodNamer methodNamer; public final boolean generateRequiredOnlyParamsMethodOverload; + public final boolean hasConvenienceApi; CreateMethodArgs(JavaSettings settings, boolean isProtocolMethod, ClientMethodsReturnDescription methodsReturnDescription, MethodNamer methodNamer, - MethodOverloadType methodOverloadType) { + MethodOverloadType methodOverloadType, boolean hasConvenienceApi) { this.settings = settings; this.isProtocolMethod = isProtocolMethod; this.methodsReturnDescription = methodsReturnDescription; this.methodOverloadType = methodOverloadType; this.methodNamer = methodNamer; + this.hasConvenienceApi = hasConvenienceApi; this.generateRequiredOnlyParamsMethodOverload = settings.isRequiredParameterClientMethods() && methodOverloadType == MethodOverloadType.OVERLOAD_MAXIMUM; } CreateMethodArgs forPaging(PagingMetadata pagingMetadata, ClientMethodParametersDetails paramsDetails) { return new CreateMethodArgs(this.settings, this.isProtocolMethod, this.methodsReturnDescription, - this.methodNamer, getPageMethodOverloadType(pagingMetadata, paramsDetails)); + this.methodNamer, getPageMethodOverloadType(pagingMetadata, paramsDetails), this.hasConvenienceApi); } } diff --git a/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/template/ConvenienceAsyncMethodTemplate.java b/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/template/ConvenienceAsyncMethodTemplate.java index c2e26fa9886..a9f844514e4 100644 --- a/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/template/ConvenienceAsyncMethodTemplate.java +++ b/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/template/ConvenienceAsyncMethodTemplate.java @@ -3,6 +3,7 @@ package com.microsoft.typespec.http.client.generator.core.template; +import com.microsoft.typespec.http.client.generator.core.extension.plugin.JavaSettings; import com.microsoft.typespec.http.client.generator.core.model.clientmodel.ArrayType; import com.microsoft.typespec.http.client.generator.core.model.clientmodel.ClassType; import com.microsoft.typespec.http.client.generator.core.model.clientmodel.ClientMethod; @@ -50,7 +51,8 @@ protected boolean isMethodIncluded(ClientMethod method) { @Override protected boolean isMethodIncluded(ConvenienceMethod method) { - return isMethodAsync(method.getProtocolMethod()) && isMethodVisible(method.getProtocolMethod()) + return isMethodAsync(method.getProtocolMethod()) + && (isMethodVisible(method.getProtocolMethod()) || isModelOnlyProtocolWithResponse(method.getProtocolMethod())) // for LRO, we actually choose the protocol method of "WithModel" && (method.getProtocolMethod().getType() != ClientMethodType.LongRunningBeginAsync || (method.getProtocolMethod().getImplementationDetails() != null @@ -61,6 +63,12 @@ protected boolean isMethodIncluded(ConvenienceMethod method) { .noneMatch(p -> p.getClientType() == ClassType.CONTEXT); } + private boolean isModelOnlyProtocolWithResponse(ClientMethod protocolMethod) { + return JavaSettings.getInstance().getMaxOverload() == JavaSettings.MaxOverload.MODEL + && (protocolMethod.getType() == ClientMethodType.SimpleSyncRestResponse + || protocolMethod.getType() == ClientMethodType.SimpleAsyncRestResponse); + } + protected void writeInvocationAndConversion(ClientMethod convenienceMethod, ClientMethod protocolMethod, String invocationExpression, JavaBlock methodBlock, Set typeReferenceStaticClasses) { diff --git a/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/template/ConvenienceMethodTemplateBase.java b/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/template/ConvenienceMethodTemplateBase.java index 6ec3aa6f12b..3f91e5e37b8 100644 --- a/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/template/ConvenienceMethodTemplateBase.java +++ b/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/template/ConvenienceMethodTemplateBase.java @@ -113,7 +113,7 @@ protected void writeMethodImplementation(ClientMethod protocolMethod, ClientMeth = findParametersForConvenienceMethod(convenienceMethod, protocolMethod); // RequestOptions - createEmptyRequestOptions(methodBlock); + createEmptyRequestOptions(convenienceMethod, methodBlock); // parameter transformation final ParameterTransformations transformations = convenienceMethod.getParameterTransformations(); @@ -219,8 +219,16 @@ protected void addRequestCallback(JavaBlock javaBlock, String variableName) { javaBlock.line("requestOptions.setBody(" + variableName + ");"); } - protected void createEmptyRequestOptions(JavaBlock methodBlock) { - methodBlock.line("RequestOptions requestOptions = new RequestOptions();"); + protected void createEmptyRequestOptions(ClientMethod convenienceMethod, JavaBlock methodBlock) { + boolean hasRequestOptionsParameter = convenienceMethod.getMethodInputParameters() + .stream() + .anyMatch(p -> p.getClientType() == ClassType.REQUEST_OPTIONS); + if (hasRequestOptionsParameter) { + methodBlock.ifBlock("requestOptions == null", + block -> block.line("requestOptions = new RequestOptions();")); + } else { + methodBlock.line("RequestOptions requestOptions = new RequestOptions();"); + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/template/ConvenienceSyncMethodTemplate.java b/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/template/ConvenienceSyncMethodTemplate.java index a164dbb9ed4..0a01afcdab2 100644 --- a/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/template/ConvenienceSyncMethodTemplate.java +++ b/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/template/ConvenienceSyncMethodTemplate.java @@ -50,13 +50,20 @@ protected boolean isMethodIncluded(ClientMethod method) { @Override protected boolean isMethodIncluded(ConvenienceMethod method) { - return !isMethodAsync(method.getProtocolMethod()) && isMethodVisible(method.getProtocolMethod()) + return !isMethodAsync(method.getProtocolMethod()) + && (isMethodVisible(method.getProtocolMethod()) || isModelOnlyProtocolWithResponse(method.getProtocolMethod())) // for LRO, we actually choose the protocol method of "WithModel" && (method.getProtocolMethod().getType() != ClientMethodType.LongRunningBeginSync || (method.getProtocolMethod().getImplementationDetails() != null && method.getProtocolMethod().getImplementationDetails().isImplementationOnly())); } + private boolean isModelOnlyProtocolWithResponse(ClientMethod protocolMethod) { + return JavaSettings.getInstance().getMaxOverload() == JavaSettings.MaxOverload.MODEL + && (protocolMethod.getType() == ClientMethodType.SimpleSyncRestResponse + || protocolMethod.getType() == ClientMethodType.SimpleAsyncRestResponse); + } + @Override protected void writeMethodImplementation(ClientMethod protocolMethod, ClientMethod convenienceMethod, JavaBlock methodBlock, Set typeReferenceStaticClasses) { diff --git a/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/template/clientcore/ClientCoreConvenienceSyncMethodTemplate.java b/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/template/clientcore/ClientCoreConvenienceSyncMethodTemplate.java index 510061eba26..0d558c11f1e 100644 --- a/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/template/clientcore/ClientCoreConvenienceSyncMethodTemplate.java +++ b/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/template/clientcore/ClientCoreConvenienceSyncMethodTemplate.java @@ -4,6 +4,7 @@ package com.microsoft.typespec.http.client.generator.core.template.clientcore; import com.microsoft.typespec.http.client.generator.core.model.clientmodel.ClassType; +import com.microsoft.typespec.http.client.generator.core.model.clientmodel.ClientMethod; import com.microsoft.typespec.http.client.generator.core.model.javamodel.JavaBlock; import com.microsoft.typespec.http.client.generator.core.template.ConvenienceSyncMethodTemplate; @@ -20,7 +21,7 @@ public static ClientCoreConvenienceSyncMethodTemplate getInstance() { } @Override - protected void createEmptyRequestOptions(JavaBlock methodBlock) { + protected void createEmptyRequestOptions(ClientMethod convenienceMethod, JavaBlock methodBlock) { methodBlock.line("RequestContext requestContext = RequestContext.none();"); } diff --git a/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/util/ClientModelUtil.java b/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/util/ClientModelUtil.java index 80cd040e55b..9f3ffc6410c 100644 --- a/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/util/ClientModelUtil.java +++ b/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/util/ClientModelUtil.java @@ -16,6 +16,7 @@ import com.microsoft.typespec.http.client.generator.core.model.clientmodel.AsyncSyncClient; import com.microsoft.typespec.http.client.generator.core.model.clientmodel.ClassType; import com.microsoft.typespec.http.client.generator.core.model.clientmodel.ClientMethod; +import com.microsoft.typespec.http.client.generator.core.model.clientmodel.ClientMethodType; import com.microsoft.typespec.http.client.generator.core.model.clientmodel.ClientModel; import com.microsoft.typespec.http.client.generator.core.model.clientmodel.ClientModelProperty; import com.microsoft.typespec.http.client.generator.core.model.clientmodel.ClientModelPropertyAccess; @@ -170,6 +171,7 @@ public static void getAsyncSyncClients(Client client, ServiceClient serviceClien private static List getConvenienceMethods(Supplier> clientMethods, OperationGroup og) { + final JavaSettings settings = JavaSettings.getInstance(); return og.getOperations().stream().filter(o -> o.getConvenienceApi() != null).flatMap(o -> { List cMethods = Mappers.getClientMethodMapper() .map(o, false) @@ -182,14 +184,44 @@ private static List getConvenienceMethods(Supplier proxyMethodBaseName.equals(m.getProxyMethod().getBaseName()) - && m.getMethodVisibility() == JavaVisibility.Public) - .map(m -> new ConvenienceMethod(m, cMethods)); + && (m.getMethodVisibility() == JavaVisibility.Public + || (settings.getMaxOverload() == JavaSettings.MaxOverload.MODEL + && isSimpleWithResponseMethod(m)))) + .map(m -> new ConvenienceMethod(m, filterConvenienceMethodsForProtocolMethod(cMethods, m, settings))) + .filter(cm -> !cm.getConvenienceMethods().isEmpty()); } else { return Stream.empty(); } }).collect(Collectors.toList()); } + private static List filterConvenienceMethodsForProtocolMethod(List convenienceMethods, + ClientMethod protocolMethod, JavaSettings settings) { + Stream stream = convenienceMethods.stream(); + if (settings.getMaxOverload() == JavaSettings.MaxOverload.ALL && isSimpleWithResponseMethod(protocolMethod)) { + stream = stream.filter( + convenienceMethod -> !hasSamePublicSignature(protocolMethod, convenienceMethod) || !isSimpleWithResponseMethod(convenienceMethod)); + } + return stream.collect(Collectors.toList()); + } + + private static boolean hasSamePublicSignature(ClientMethod left, ClientMethod right) { + return toWrapperMethodName(left).equals(toWrapperMethodName(right)) + && left.getParametersDeclaration().equals(right.getParametersDeclaration()); + } + + private static String toWrapperMethodName(ClientMethod method) { + if (method.getType().name().contains("Async") && method.getName().endsWith("Async")) { + return method.getName().substring(0, method.getName().length() - "Async".length()); + } + return method.getName(); + } + + private static boolean isSimpleWithResponseMethod(ClientMethod method) { + return method.getType() == ClientMethodType.SimpleSyncRestResponse + || method.getType() == ClientMethodType.SimpleAsyncRestResponse; + } + /** * @param codeModel the code model * @return the interface name of service client. diff --git a/packages/http-client-java/generator/http-client-generator-core/src/test/java/com/microsoft/typespec/http/client/generator/core/mapper/ClientMethodMapperTests.java b/packages/http-client-java/generator/http-client-generator-core/src/test/java/com/microsoft/typespec/http/client/generator/core/mapper/ClientMethodMapperTests.java index 6f9e05fe38d..ac48ba78d52 100644 --- a/packages/http-client-java/generator/http-client-generator-core/src/test/java/com/microsoft/typespec/http/client/generator/core/mapper/ClientMethodMapperTests.java +++ b/packages/http-client-java/generator/http-client-generator-core/src/test/java/com/microsoft/typespec/http/client/generator/core/mapper/ClientMethodMapperTests.java @@ -3,6 +3,7 @@ package com.microsoft.typespec.http.client.generator.core.mapper; +import com.microsoft.typespec.http.client.generator.core.MockUnitJavagen; import com.microsoft.typespec.http.client.generator.core.extension.model.codemodel.Language; import com.microsoft.typespec.http.client.generator.core.extension.model.codemodel.Languages; import com.microsoft.typespec.http.client.generator.core.extension.model.codemodel.Operation; @@ -11,24 +12,59 @@ import com.microsoft.typespec.http.client.generator.core.extension.model.codemodel.Request; import com.microsoft.typespec.http.client.generator.core.extension.model.codemodel.Response; import com.microsoft.typespec.http.client.generator.core.extension.model.codemodel.Schema; +import com.microsoft.typespec.http.client.generator.core.extension.plugin.JavaSettings; import com.microsoft.typespec.http.client.generator.core.model.clientmodel.ClassType; +import com.microsoft.typespec.http.client.generator.core.model.clientmodel.ClientMethodType; import com.microsoft.typespec.http.client.generator.core.model.clientmodel.ClientMethodParameter; import com.microsoft.typespec.http.client.generator.core.model.clientmodel.GenericType; import com.microsoft.typespec.http.client.generator.core.model.clientmodel.IType; import com.microsoft.typespec.http.client.generator.core.model.clientmodel.MethodParameter; import com.microsoft.typespec.http.client.generator.core.model.clientmodel.PrimitiveType; import com.microsoft.typespec.http.client.generator.core.model.clientmodel.Versioning; +import com.microsoft.typespec.http.client.generator.core.model.javamodel.JavaVisibility; import io.clientcore.core.http.models.HttpMethod; +import java.lang.reflect.Field; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.List; +import java.util.Map; import java.util.stream.Collectors; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.parallel.Execution; +import org.junit.jupiter.api.parallel.ExecutionMode; +@Execution(ExecutionMode.SAME_THREAD) public class ClientMethodMapperTests { + private static class TestClientMethodMapper extends ClientMethodMapper { + public JavaVisibility methodVisibilityForTest(ClientMethodType methodType, boolean hasContextParameter, + boolean isProtocolMethod) { + return methodVisibility(methodType, MethodOverloadType.OVERLOAD_MAXIMUM, hasContextParameter, + isProtocolMethod); + } + } + + @SuppressWarnings("unchecked") + private static void configureDataPlaneSettings(String maxOverloadValue) { + try { + Field settingsField = MockUnitJavagen.class.getDeclaredField("SETTINGS_MAP"); + settingsField.setAccessible(true); + Map settings = (Map) settingsField.get(null); + settings.clear(); + settings.put("namespace", "com.azure.mock"); + settings.put("data-plane", true); + settings.put("flavor", "azure"); + settings.put("max-overload", maxOverloadValue); + new MockUnitJavagen(); + JavaSettings.clear(); + JavaSettings.getInstance(); + } catch (NoSuchFieldException | IllegalAccessException e) { + throw new RuntimeException(e); + } + } + @Test public void testOverloadedSignatures() { List parameters = new ArrayList<>(); @@ -201,6 +237,38 @@ public void returnTypeDescTest() { Assertions.assertEquals(expectedDescription, description); } + @Test + public void maxOverloadProtocolDoesNotGenerateModelWithResponse() { + configureDataPlaneSettings("protocol"); + TestClientMethodMapper mapper = new TestClientMethodMapper(); + Assertions.assertFalse(JavaSettings.getInstance().isGenerateModelMaxOverload()); + + Assertions.assertNull( + mapper.methodVisibilityForTest(ClientMethodType.SimpleSyncRestResponse, false, false)); + } + + @Test + public void maxOverloadModelGeneratesModelWithResponseWithRequestOptions() { + configureDataPlaneSettings("model"); + TestClientMethodMapper mapper = new TestClientMethodMapper(); + + Assertions.assertEquals(JavaVisibility.Public, + mapper.methodVisibilityForTest(ClientMethodType.SimpleSyncRestResponse, true, false)); + Assertions.assertNull( + mapper.methodVisibilityForTest(ClientMethodType.SimpleSyncRestResponse, false, false)); + } + + @Test + public void maxOverloadAllGeneratesBothProtocolAndModelWithResponse() { + configureDataPlaneSettings("all"); + TestClientMethodMapper mapper = new TestClientMethodMapper(); + + Assertions.assertEquals(JavaVisibility.Public, + mapper.methodVisibilityForTest(ClientMethodType.SimpleSyncRestResponse, true, true)); + Assertions.assertEquals(JavaVisibility.Public, + mapper.methodVisibilityForTest(ClientMethodType.SimpleSyncRestResponse, true, false)); + } + private Operation operationWithNoDesc() { return operationWithDescOnOperationAndResponseSchema(null, null); } From 32b13055e29e340313638132642f75090f62e891 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Wed, 20 May 2026 07:29:22 +0000 Subject: [PATCH 3/7] refactor(java): finalize max-overload generation updates Agent-Logs-Url: https://github.com/microsoft/typespec/sessions/aee86c1c-fc77-4e89-93e8-a148c5d49443 Co-authored-by: srnagar <51379715+srnagar@users.noreply.github.com> --- .../core/mapper/ClientMethodMapper.java | 19 +++++++----- .../ConvenienceMethodTemplateBase.java | 3 ++ .../generator/core/util/ClientModelUtil.java | 29 ++++++++++++++++--- .../generator/core/MockUnitJavagen.java | 9 ++++++ .../core/mapper/ClientMethodMapperTests.java | 27 ++++++----------- 5 files changed, 57 insertions(+), 30 deletions(-) diff --git a/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/mapper/ClientMethodMapper.java b/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/mapper/ClientMethodMapper.java index c4255738f20..f8ddf8609ea 100644 --- a/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/mapper/ClientMethodMapper.java +++ b/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/mapper/ClientMethodMapper.java @@ -815,8 +815,7 @@ private void createSimpleWithResponseClientMethods(boolean isSync, ClientMethod && isProtocolMethod && settings.getMaxOverload() == JavaSettings.MaxOverload.MODEL && createMethodArgs.hasConvenienceApi - && (clientMethodType == ClientMethodType.SimpleSyncRestResponse - || clientMethodType == ClientMethodType.SimpleAsyncRestResponse)) { + && isSimpleWithResponseType(clientMethodType)) { methodWithContextVisibility = NOT_VISIBLE; } final boolean hasContextOverload = methodWithContextVisibility != NOT_GENERATE; @@ -834,13 +833,12 @@ private void createSimpleWithResponseClientMethods(boolean isSync, ClientMethod // Always generate an overload of WithResponse with non-required parameters without Context. It is only for sync // proxy method, and is usually filtered out in methodVisibility function. methods.add(withResponseMethod); - final boolean useProtocolContextParam = !isProtocolMethod + final boolean useRequestOptionsParam = !isProtocolMethod && settings.isDataPlaneClient() && settings.isGenerateModelMaxOverload() - && (clientMethodType == ClientMethodType.SimpleSyncRestResponse - || clientMethodType == ClientMethodType.SimpleAsyncRestResponse); + && isSimpleWithResponseType(clientMethodType); ClientMethod clientMethodWithContext = addClientMethodWithContext(methods, withResponseMethod, - methodWithContextVisibility, useProtocolContextParam || isProtocolMethod); + methodWithContextVisibility, useRequestOptionsParam || isProtocolMethod); // Simple op '[Operation]WithResponse' overloads for versioning createOverloadForVersioning(methods, withResponseMethod, clientMethodWithContext, methodWithContextVisibility, @@ -974,8 +972,8 @@ protected JavaVisibility methodVisibility(ClientMethodType methodType, MethodOve } return VISIBLE; } else { - if ((methodType == ClientMethodType.SimpleSyncRestResponse - || methodType == ClientMethodType.SimpleAsyncRestResponse) + if (isSimpleWithResponseType(methodType) + && settings.isDataPlaneClient() && settings.isGenerateModelMaxOverload()) { return hasContextParameter ? VISIBLE : NOT_GENERATE; } @@ -1101,4 +1099,9 @@ private static MethodOverloadType getPageMethodOverloadType(PagingMetadata pagin return MethodOverloadType.OVERLOAD_MINIMUM_MAXIMUM; } } + + private static boolean isSimpleWithResponseType(ClientMethodType methodType) { + return methodType == ClientMethodType.SimpleSyncRestResponse + || methodType == ClientMethodType.SimpleAsyncRestResponse; + } } diff --git a/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/template/ConvenienceMethodTemplateBase.java b/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/template/ConvenienceMethodTemplateBase.java index 3f91e5e37b8..a97a7fe2906 100644 --- a/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/template/ConvenienceMethodTemplateBase.java +++ b/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/template/ConvenienceMethodTemplateBase.java @@ -224,9 +224,12 @@ protected void createEmptyRequestOptions(ClientMethod convenienceMethod, JavaBlo .stream() .anyMatch(p -> p.getClientType() == ClassType.REQUEST_OPTIONS); if (hasRequestOptionsParameter) { + // Model max-overload WithResponse takes RequestOptions from the method signature. + // Ensure it is initialized when null. methodBlock.ifBlock("requestOptions == null", block -> block.line("requestOptions = new RequestOptions();")); } else { + // Legacy convenience overloads synthesize RequestOptions internally. methodBlock.line("RequestOptions requestOptions = new RequestOptions();"); } } diff --git a/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/util/ClientModelUtil.java b/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/util/ClientModelUtil.java index 9f3ffc6410c..bb7d386a009 100644 --- a/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/util/ClientModelUtil.java +++ b/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/util/ClientModelUtil.java @@ -172,6 +172,7 @@ public static void getAsyncSyncClients(Client client, ServiceClient serviceClien private static List getConvenienceMethods(Supplier> clientMethods, OperationGroup og) { final JavaSettings settings = JavaSettings.getInstance(); + final JavaSettings.MaxOverload maxOverload = settings.getMaxOverload(); return og.getOperations().stream().filter(o -> o.getConvenienceApi() != null).flatMap(o -> { List cMethods = Mappers.getClientMethodMapper() .map(o, false) @@ -184,8 +185,10 @@ private static List getConvenienceMethods(Supplier proxyMethodBaseName.equals(m.getProxyMethod().getBaseName()) + // In MODEL mode protocol WithResponse methods are intentionally non-public in wrapper clients, + // but they are still required as the invocation target for generated model overloads. && (m.getMethodVisibility() == JavaVisibility.Public - || (settings.getMaxOverload() == JavaSettings.MaxOverload.MODEL + || (maxOverload == JavaSettings.MaxOverload.MODEL && isSimpleWithResponseMethod(m)))) .map(m -> new ConvenienceMethod(m, filterConvenienceMethodsForProtocolMethod(cMethods, m, settings))) .filter(cm -> !cm.getConvenienceMethods().isEmpty()); @@ -195,22 +198,40 @@ && isSimpleWithResponseMethod(m)))) }).collect(Collectors.toList()); } + /** + * Filters convenience methods for a protocol method. + *

+ * In {@code max-overload=all}, if protocol and model {@code WithResponse} overloads collapse to the same public + * signature, model overload is removed to avoid invalid return-type-only overloading in Java. + * + * @param convenienceMethods candidate convenience methods. + * @param protocolMethod protocol method that convenience methods wrap. + * @param settings java settings. + * @return filtered convenience methods. + */ private static List filterConvenienceMethodsForProtocolMethod(List convenienceMethods, ClientMethod protocolMethod, JavaSettings settings) { + final JavaSettings.MaxOverload maxOverload = settings.getMaxOverload(); Stream stream = convenienceMethods.stream(); - if (settings.getMaxOverload() == JavaSettings.MaxOverload.ALL && isSimpleWithResponseMethod(protocolMethod)) { - stream = stream.filter( - convenienceMethod -> !hasSamePublicSignature(protocolMethod, convenienceMethod) || !isSimpleWithResponseMethod(convenienceMethod)); + if (maxOverload == JavaSettings.MaxOverload.ALL && isSimpleWithResponseMethod(protocolMethod)) { + stream = stream.filter(convenienceMethod -> !(hasSamePublicSignature(protocolMethod, convenienceMethod) + && isSimpleWithResponseMethod(convenienceMethod))); } return stream.collect(Collectors.toList()); } + /** + * Compares signatures as they appear in wrapper clients. + *

+ * Async wrapper methods drop the {@code Async} suffix, so method names are normalized before comparison. + */ private static boolean hasSamePublicSignature(ClientMethod left, ClientMethod right) { return toWrapperMethodName(left).equals(toWrapperMethodName(right)) && left.getParametersDeclaration().equals(right.getParametersDeclaration()); } private static String toWrapperMethodName(ClientMethod method) { + // Async wrapper methods omit the "Async" suffix. Compare wrapper-visible signatures when detecting collisions. if (method.getType().name().contains("Async") && method.getName().endsWith("Async")) { return method.getName().substring(0, method.getName().length() - "Async".length()); } diff --git a/packages/http-client-java/generator/http-client-generator-core/src/test/java/com/microsoft/typespec/http/client/generator/core/MockUnitJavagen.java b/packages/http-client-java/generator/http-client-generator-core/src/test/java/com/microsoft/typespec/http/client/generator/core/MockUnitJavagen.java index ddbeffd166b..5cb9e1277a5 100644 --- a/packages/http-client-java/generator/http-client-generator-core/src/test/java/com/microsoft/typespec/http/client/generator/core/MockUnitJavagen.java +++ b/packages/http-client-java/generator/http-client-generator-core/src/test/java/com/microsoft/typespec/http/client/generator/core/MockUnitJavagen.java @@ -21,6 +21,15 @@ public class MockUnitJavagen extends Javagen { SETTINGS_MAP.put("namespace", "com.azure.mock"); } + public static void resetSettings() { + SETTINGS_MAP.clear(); + SETTINGS_MAP.put("namespace", "com.azure.mock"); + } + + public static void setSetting(String key, Object value) { + SETTINGS_MAP.put(key, value); + } + public static class MockConnection extends Connection { public MockConnection() { diff --git a/packages/http-client-java/generator/http-client-generator-core/src/test/java/com/microsoft/typespec/http/client/generator/core/mapper/ClientMethodMapperTests.java b/packages/http-client-java/generator/http-client-generator-core/src/test/java/com/microsoft/typespec/http/client/generator/core/mapper/ClientMethodMapperTests.java index ac48ba78d52..fb45bf4ac85 100644 --- a/packages/http-client-java/generator/http-client-generator-core/src/test/java/com/microsoft/typespec/http/client/generator/core/mapper/ClientMethodMapperTests.java +++ b/packages/http-client-java/generator/http-client-generator-core/src/test/java/com/microsoft/typespec/http/client/generator/core/mapper/ClientMethodMapperTests.java @@ -23,18 +23,18 @@ import com.microsoft.typespec.http.client.generator.core.model.clientmodel.Versioning; import com.microsoft.typespec.http.client.generator.core.model.javamodel.JavaVisibility; import io.clientcore.core.http.models.HttpMethod; -import java.lang.reflect.Field; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.List; -import java.util.Map; import java.util.stream.Collectors; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.parallel.Execution; import org.junit.jupiter.api.parallel.ExecutionMode; +// JavaSettings is process-global and mutable in tests. +// Keep these tests single-threaded to avoid cross-test settings races. @Execution(ExecutionMode.SAME_THREAD) public class ClientMethodMapperTests { @@ -46,23 +46,14 @@ public JavaVisibility methodVisibilityForTest(ClientMethodType methodType, boole } } - @SuppressWarnings("unchecked") private static void configureDataPlaneSettings(String maxOverloadValue) { - try { - Field settingsField = MockUnitJavagen.class.getDeclaredField("SETTINGS_MAP"); - settingsField.setAccessible(true); - Map settings = (Map) settingsField.get(null); - settings.clear(); - settings.put("namespace", "com.azure.mock"); - settings.put("data-plane", true); - settings.put("flavor", "azure"); - settings.put("max-overload", maxOverloadValue); - new MockUnitJavagen(); - JavaSettings.clear(); - JavaSettings.getInstance(); - } catch (NoSuchFieldException | IllegalAccessException e) { - throw new RuntimeException(e); - } + MockUnitJavagen.resetSettings(); + MockUnitJavagen.setSetting("data-plane", true); + MockUnitJavagen.setSetting("flavor", "azure"); + MockUnitJavagen.setSetting("max-overload", maxOverloadValue); + new MockUnitJavagen(); + JavaSettings.clear(); + JavaSettings.getInstance(); } @Test From 4e1d8855664e4af6c40adb062c856a2431e914c7 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Wed, 20 May 2026 19:28:16 +0000 Subject: [PATCH 4/7] fix(java): remove max-overload all mode support Agent-Logs-Url: https://github.com/microsoft/typespec/sessions/b1794428-b7da-4a7c-bb62-831f79584f2c Co-authored-by: srnagar <51379715+srnagar@users.noreply.github.com> --- .../http-client-java/emitter/src/options.ts | 4 +- .../core/extension/plugin/JavaSettings.java | 9 ++-- .../generator/core/util/ClientModelUtil.java | 43 +------------------ .../core/mapper/ClientMethodMapperTests.java | 11 ----- 4 files changed, 6 insertions(+), 61 deletions(-) diff --git a/packages/http-client-java/emitter/src/options.ts b/packages/http-client-java/emitter/src/options.ts index 8ce37219890..e002b662f92 100644 --- a/packages/http-client-java/emitter/src/options.ts +++ b/packages/http-client-java/emitter/src/options.ts @@ -25,7 +25,7 @@ export interface License { export interface EmitterOptions { license?: License; "dev-options"?: DevOptions; - "max-overload"?: "protocol" | "model" | "all"; + "max-overload"?: "protocol" | "model"; } export const EmitterOptionsSchema: JSONSchemaType = { @@ -72,7 +72,7 @@ export const EmitterOptionsSchema: JSONSchemaType = { description: "Selects which max `WithResponse` overloads are generated for data-plane clients.", nullable: true, - enum: ["protocol", "model", "all"], + enum: ["protocol", "model"], }, }, additionalProperties: false, diff --git a/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/extension/plugin/JavaSettings.java b/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/extension/plugin/JavaSettings.java index e001b8d04ae..522760f0941 100644 --- a/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/extension/plugin/JavaSettings.java +++ b/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/extension/plugin/JavaSettings.java @@ -972,11 +972,11 @@ public MaxOverload getMaxOverload() { } public boolean isGenerateProtocolMaxOverload() { - return maxOverload == MaxOverload.PROTOCOL || maxOverload == MaxOverload.ALL; + return maxOverload == MaxOverload.PROTOCOL; } public boolean isGenerateModelMaxOverload() { - return maxOverload == MaxOverload.MODEL || maxOverload == MaxOverload.ALL; + return maxOverload == MaxOverload.MODEL; } /** @@ -1071,16 +1071,13 @@ public static SyncMethodsGeneration fromValue(String value) { public enum MaxOverload { PROTOCOL, - MODEL, - ALL; + MODEL; public static MaxOverload fromValue(String value) { if (value == null) { return PROTOCOL; } else if (value.equals("model")) { return MODEL; - } else if (value.equals("all")) { - return ALL; } else { return PROTOCOL; } diff --git a/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/util/ClientModelUtil.java b/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/util/ClientModelUtil.java index bb7d386a009..4303bf67a7e 100644 --- a/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/util/ClientModelUtil.java +++ b/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/util/ClientModelUtil.java @@ -190,54 +190,13 @@ private static List getConvenienceMethods(Supplier new ConvenienceMethod(m, filterConvenienceMethodsForProtocolMethod(cMethods, m, settings))) - .filter(cm -> !cm.getConvenienceMethods().isEmpty()); + .map(m -> new ConvenienceMethod(m, cMethods)); } else { return Stream.empty(); } }).collect(Collectors.toList()); } - /** - * Filters convenience methods for a protocol method. - *

- * In {@code max-overload=all}, if protocol and model {@code WithResponse} overloads collapse to the same public - * signature, model overload is removed to avoid invalid return-type-only overloading in Java. - * - * @param convenienceMethods candidate convenience methods. - * @param protocolMethod protocol method that convenience methods wrap. - * @param settings java settings. - * @return filtered convenience methods. - */ - private static List filterConvenienceMethodsForProtocolMethod(List convenienceMethods, - ClientMethod protocolMethod, JavaSettings settings) { - final JavaSettings.MaxOverload maxOverload = settings.getMaxOverload(); - Stream stream = convenienceMethods.stream(); - if (maxOverload == JavaSettings.MaxOverload.ALL && isSimpleWithResponseMethod(protocolMethod)) { - stream = stream.filter(convenienceMethod -> !(hasSamePublicSignature(protocolMethod, convenienceMethod) - && isSimpleWithResponseMethod(convenienceMethod))); - } - return stream.collect(Collectors.toList()); - } - - /** - * Compares signatures as they appear in wrapper clients. - *

- * Async wrapper methods drop the {@code Async} suffix, so method names are normalized before comparison. - */ - private static boolean hasSamePublicSignature(ClientMethod left, ClientMethod right) { - return toWrapperMethodName(left).equals(toWrapperMethodName(right)) - && left.getParametersDeclaration().equals(right.getParametersDeclaration()); - } - - private static String toWrapperMethodName(ClientMethod method) { - // Async wrapper methods omit the "Async" suffix. Compare wrapper-visible signatures when detecting collisions. - if (method.getType().name().contains("Async") && method.getName().endsWith("Async")) { - return method.getName().substring(0, method.getName().length() - "Async".length()); - } - return method.getName(); - } - private static boolean isSimpleWithResponseMethod(ClientMethod method) { return method.getType() == ClientMethodType.SimpleSyncRestResponse || method.getType() == ClientMethodType.SimpleAsyncRestResponse; diff --git a/packages/http-client-java/generator/http-client-generator-core/src/test/java/com/microsoft/typespec/http/client/generator/core/mapper/ClientMethodMapperTests.java b/packages/http-client-java/generator/http-client-generator-core/src/test/java/com/microsoft/typespec/http/client/generator/core/mapper/ClientMethodMapperTests.java index fb45bf4ac85..01bdfa028d0 100644 --- a/packages/http-client-java/generator/http-client-generator-core/src/test/java/com/microsoft/typespec/http/client/generator/core/mapper/ClientMethodMapperTests.java +++ b/packages/http-client-java/generator/http-client-generator-core/src/test/java/com/microsoft/typespec/http/client/generator/core/mapper/ClientMethodMapperTests.java @@ -249,17 +249,6 @@ public void maxOverloadModelGeneratesModelWithResponseWithRequestOptions() { mapper.methodVisibilityForTest(ClientMethodType.SimpleSyncRestResponse, false, false)); } - @Test - public void maxOverloadAllGeneratesBothProtocolAndModelWithResponse() { - configureDataPlaneSettings("all"); - TestClientMethodMapper mapper = new TestClientMethodMapper(); - - Assertions.assertEquals(JavaVisibility.Public, - mapper.methodVisibilityForTest(ClientMethodType.SimpleSyncRestResponse, true, true)); - Assertions.assertEquals(JavaVisibility.Public, - mapper.methodVisibilityForTest(ClientMethodType.SimpleSyncRestResponse, true, false)); - } - private Operation operationWithNoDesc() { return operationWithDescOnOperationAndResponseSchema(null, null); } From aef08e970b9777da1f557915ee4f08452f8998d8 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Wed, 20 May 2026 21:46:36 +0000 Subject: [PATCH 5/7] test(java): add max-overload protocol and model generator spec Agent-Logs-Url: https://github.com/microsoft/typespec/sessions/1a6bac46-b926-4622-8aa3-564c280f9ce1 Co-authored-by: srnagar <51379715+srnagar@users.noreply.github.com> --- .../http-client-java/emitter/src/options.ts | 2 +- .../http-client-generator-test/Generate.ps1 | 54 +++++++++++-------- .../tsp/max-overload.tsp | 24 +++++++++ 3 files changed, 56 insertions(+), 24 deletions(-) create mode 100644 packages/http-client-java/generator/http-client-generator-test/tsp/max-overload.tsp diff --git a/packages/http-client-java/emitter/src/options.ts b/packages/http-client-java/emitter/src/options.ts index e002b662f92..c4979c24873 100644 --- a/packages/http-client-java/emitter/src/options.ts +++ b/packages/http-client-java/emitter/src/options.ts @@ -70,7 +70,7 @@ export const EmitterOptionsSchema: JSONSchemaType = { "max-overload": { type: "string", description: - "Selects which max `WithResponse` overloads are generated for data-plane clients.", + "Selects which max `WithResponse` overloads are generated for data-plane clients. Defaults to `protocol` when not set.", nullable: true, enum: ["protocol", "model"], }, diff --git a/packages/http-client-java/generator/http-client-generator-test/Generate.ps1 b/packages/http-client-java/generator/http-client-generator-test/Generate.ps1 index 24c692adc8a..d732d59aa1f 100644 --- a/packages/http-client-java/generator/http-client-generator-test/Generate.ps1 +++ b/packages/http-client-java/generator/http-client-generator-test/Generate.ps1 @@ -129,30 +129,38 @@ $generateScript = { } $tspTrace = "--trace import-resolution --trace projection --trace http-client-java" - $tspCommand = "npx --no tsp compile $tspFile $tspOptions $tspTrace" - - # output of "tsp compile" seems trigger powershell error or exit, hence the "2>&1" - $timer = [Diagnostics.Stopwatch]::StartNew() - $generateOutput = Invoke-Expression $tspCommand 2>&1 - $timer.Stop() - - $global:ExitCode = $global:ExitCode -bor $LASTEXITCODE + $tspCommands = @("npx --no tsp compile $tspFile $tspOptions $tspTrace") + if ($tspFile -match "tsp[\\/]max-overload\.tsp$") { + $tspCommands = @( + "npx --no tsp compile $tspFile $tspOptions --option ""@typespec/http-client-java.max-overload=protocol"" --option ""@typespec/http-client-java.namespace=tsptest.maxoverload.protocol"" $tspTrace", + "npx --no tsp compile $tspFile $tspOptions --option ""@typespec/http-client-java.max-overload=model"" --option ""@typespec/http-client-java.namespace=tsptest.maxoverload.model"" $tspTrace" + ) + } - if ($LASTEXITCODE -ne 0) { - Write-Host " - ======================== - $tspCommand - ======================== - FAILED (Time elapsed: $($timer.ToString())) - $([String]::Join("`n", $generateOutput)) - " - } else { - Write-Host " - ======================== - $tspCommand - ======================== - SUCCEEDED (Time elapsed: $($timer.ToString())) - " + foreach ($tspCommand in $tspCommands) { + # output of "tsp compile" seems trigger powershell error or exit, hence the "2>&1" + $timer = [Diagnostics.Stopwatch]::StartNew() + $generateOutput = Invoke-Expression $tspCommand 2>&1 + $timer.Stop() + + $global:ExitCode = $global:ExitCode -bor $LASTEXITCODE + + if ($LASTEXITCODE -ne 0) { + Write-Host " + ======================== + $tspCommand + ======================== + FAILED (Time elapsed: $($timer.ToString())) + $([String]::Join("`n", $generateOutput)) + " + } else { + Write-Host " + ======================== + $tspCommand + ======================== + SUCCEEDED (Time elapsed: $($timer.ToString())) + " + } } if ($global:ExitCode -ne 0) { diff --git a/packages/http-client-java/generator/http-client-generator-test/tsp/max-overload.tsp b/packages/http-client-java/generator/http-client-generator-test/tsp/max-overload.tsp new file mode 100644 index 00000000000..454c0845fe5 --- /dev/null +++ b/packages/http-client-java/generator/http-client-generator-test/tsp/max-overload.tsp @@ -0,0 +1,24 @@ +import "@typespec/rest"; +import "@azure-tools/typespec-client-generator-core"; + +using TypeSpec.Http; +using Azure.ClientGenerator.Core; + +@service(#{ title: "MaxOverload" }) +namespace TspTest.MaxOverload; + +model CreateWidgetOptions { + name: string; +} + +model Widget { + name: string; +} + +@route("/max-overload") +interface MaxOverloadOperations { + @post + @convenientAPI(true) + @protocolAPI(true) + createWidget(@body body: CreateWidgetOptions): Widget; +} From 1e67e302aef7e3aa1f50ecd8c65d1eafaefad95e Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Wed, 20 May 2026 22:17:52 +0000 Subject: [PATCH 6/7] test(java): check in generated max-overload protocol and model code Agent-Logs-Url: https://github.com/microsoft/typespec/sessions/811619f6-9770-4590-bbff-a632e211d8a6 Co-authored-by: srnagar <51379715+srnagar@users.noreply.github.com> --- .../model/MaxOverloadAsyncClient.java | 98 ++++++ .../maxoverload/model/MaxOverloadClient.java | 95 ++++++ .../model/MaxOverloadClientBuilder.java | 288 ++++++++++++++++++ .../implementation/MaxOverloadClientImpl.java | 107 +++++++ .../MaxOverloadOperationsImpl.java | 157 ++++++++++ .../model/implementation/package-info.java | 10 + .../model/models/CreateWidgetOptions.java | 83 +++++ .../maxoverload/model/models/Widget.java | 83 +++++ .../model/models/package-info.java | 10 + .../maxoverload/model/package-info.java | 10 + .../protocol/MaxOverloadAsyncClient.java | 98 ++++++ .../protocol/MaxOverloadClient.java | 95 ++++++ .../protocol/MaxOverloadClientBuilder.java | 288 ++++++++++++++++++ .../implementation/MaxOverloadClientImpl.java | 107 +++++++ .../MaxOverloadOperationsImpl.java | 157 ++++++++++ .../protocol/implementation/package-info.java | 10 + .../protocol/models/CreateWidgetOptions.java | 83 +++++ .../maxoverload/protocol/models/Widget.java | 83 +++++ .../protocol/models/package-info.java | 10 + .../maxoverload/protocol/package-info.java | 10 + .../tsptest-maxoverload-model_metadata.json | 1 + ...tsptest-maxoverload-protocol_metadata.json | 1 + .../tsptest-maxoverload-model.properties | 2 + .../tsptest-maxoverload-protocol.properties | 2 + .../generated/MaxOverloadClientTestBase.java | 34 +++ .../generated/MaxOverloadClientTestBase.java | 34 +++ 26 files changed, 1956 insertions(+) create mode 100644 packages/http-client-java/generator/http-client-generator-test/src/main/java/tsptest/maxoverload/model/MaxOverloadAsyncClient.java create mode 100644 packages/http-client-java/generator/http-client-generator-test/src/main/java/tsptest/maxoverload/model/MaxOverloadClient.java create mode 100644 packages/http-client-java/generator/http-client-generator-test/src/main/java/tsptest/maxoverload/model/MaxOverloadClientBuilder.java create mode 100644 packages/http-client-java/generator/http-client-generator-test/src/main/java/tsptest/maxoverload/model/implementation/MaxOverloadClientImpl.java create mode 100644 packages/http-client-java/generator/http-client-generator-test/src/main/java/tsptest/maxoverload/model/implementation/MaxOverloadOperationsImpl.java create mode 100644 packages/http-client-java/generator/http-client-generator-test/src/main/java/tsptest/maxoverload/model/implementation/package-info.java create mode 100644 packages/http-client-java/generator/http-client-generator-test/src/main/java/tsptest/maxoverload/model/models/CreateWidgetOptions.java create mode 100644 packages/http-client-java/generator/http-client-generator-test/src/main/java/tsptest/maxoverload/model/models/Widget.java create mode 100644 packages/http-client-java/generator/http-client-generator-test/src/main/java/tsptest/maxoverload/model/models/package-info.java create mode 100644 packages/http-client-java/generator/http-client-generator-test/src/main/java/tsptest/maxoverload/model/package-info.java create mode 100644 packages/http-client-java/generator/http-client-generator-test/src/main/java/tsptest/maxoverload/protocol/MaxOverloadAsyncClient.java create mode 100644 packages/http-client-java/generator/http-client-generator-test/src/main/java/tsptest/maxoverload/protocol/MaxOverloadClient.java create mode 100644 packages/http-client-java/generator/http-client-generator-test/src/main/java/tsptest/maxoverload/protocol/MaxOverloadClientBuilder.java create mode 100644 packages/http-client-java/generator/http-client-generator-test/src/main/java/tsptest/maxoverload/protocol/implementation/MaxOverloadClientImpl.java create mode 100644 packages/http-client-java/generator/http-client-generator-test/src/main/java/tsptest/maxoverload/protocol/implementation/MaxOverloadOperationsImpl.java create mode 100644 packages/http-client-java/generator/http-client-generator-test/src/main/java/tsptest/maxoverload/protocol/implementation/package-info.java create mode 100644 packages/http-client-java/generator/http-client-generator-test/src/main/java/tsptest/maxoverload/protocol/models/CreateWidgetOptions.java create mode 100644 packages/http-client-java/generator/http-client-generator-test/src/main/java/tsptest/maxoverload/protocol/models/Widget.java create mode 100644 packages/http-client-java/generator/http-client-generator-test/src/main/java/tsptest/maxoverload/protocol/models/package-info.java create mode 100644 packages/http-client-java/generator/http-client-generator-test/src/main/java/tsptest/maxoverload/protocol/package-info.java create mode 100644 packages/http-client-java/generator/http-client-generator-test/src/main/resources/META-INF/tsptest-maxoverload-model_metadata.json create mode 100644 packages/http-client-java/generator/http-client-generator-test/src/main/resources/META-INF/tsptest-maxoverload-protocol_metadata.json create mode 100644 packages/http-client-java/generator/http-client-generator-test/src/main/resources/tsptest-maxoverload-model.properties create mode 100644 packages/http-client-java/generator/http-client-generator-test/src/main/resources/tsptest-maxoverload-protocol.properties create mode 100644 packages/http-client-java/generator/http-client-generator-test/src/test/java/tsptest/maxoverload/model/generated/MaxOverloadClientTestBase.java create mode 100644 packages/http-client-java/generator/http-client-generator-test/src/test/java/tsptest/maxoverload/protocol/generated/MaxOverloadClientTestBase.java diff --git a/packages/http-client-java/generator/http-client-generator-test/src/main/java/tsptest/maxoverload/model/MaxOverloadAsyncClient.java b/packages/http-client-java/generator/http-client-generator-test/src/main/java/tsptest/maxoverload/model/MaxOverloadAsyncClient.java new file mode 100644 index 00000000000..f5d11b2ba76 --- /dev/null +++ b/packages/http-client-java/generator/http-client-generator-test/src/main/java/tsptest/maxoverload/model/MaxOverloadAsyncClient.java @@ -0,0 +1,98 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. +// Code generated by Microsoft (R) TypeSpec Code Generator. + +package tsptest.maxoverload.model; + +import com.azure.core.annotation.Generated; +import com.azure.core.annotation.ReturnType; +import com.azure.core.annotation.ServiceClient; +import com.azure.core.annotation.ServiceMethod; +import com.azure.core.exception.ClientAuthenticationException; +import com.azure.core.exception.HttpResponseException; +import com.azure.core.exception.ResourceModifiedException; +import com.azure.core.exception.ResourceNotFoundException; +import com.azure.core.http.rest.RequestOptions; +import com.azure.core.http.rest.Response; +import com.azure.core.util.BinaryData; +import com.azure.core.util.FluxUtil; +import reactor.core.publisher.Mono; +import tsptest.maxoverload.model.implementation.MaxOverloadOperationsImpl; +import tsptest.maxoverload.model.models.CreateWidgetOptions; +import tsptest.maxoverload.model.models.Widget; + +/** + * Initializes a new instance of the asynchronous MaxOverloadClient type. + */ +@ServiceClient(builder = MaxOverloadClientBuilder.class, isAsync = true) +public final class MaxOverloadAsyncClient { + @Generated + private final MaxOverloadOperationsImpl serviceClient; + + /** + * Initializes an instance of MaxOverloadAsyncClient class. + * + * @param serviceClient the service client implementation. + */ + @Generated + MaxOverloadAsyncClient(MaxOverloadOperationsImpl serviceClient) { + this.serviceClient = serviceClient; + } + + /** + * The createWidget operation. + *

Request Body Schema

+ * + *
+     * {@code
+     * {
+     *     name: String (Required)
+     * }
+     * }
+     * 
+ * + *

Response Body Schema

+ * + *
+     * {@code
+     * {
+     *     name: String (Required)
+     * }
+     * }
+     * 
+ * + * @param body The body parameter. + * @param requestOptions The options to configure the HTTP request before HTTP client sends it. + * @throws HttpResponseException thrown if the request is rejected by server. + * @throws ClientAuthenticationException thrown if the request is rejected by server on status code 401. + * @throws ResourceNotFoundException thrown if the request is rejected by server on status code 404. + * @throws ResourceModifiedException thrown if the request is rejected by server on status code 409. + * @return the response body along with {@link Response} on successful completion of {@link Mono}. + */ + @Generated + @ServiceMethod(returns = ReturnType.SINGLE) + public Mono> createWidgetWithResponse(BinaryData body, RequestOptions requestOptions) { + return this.serviceClient.createWidgetWithResponseAsync(body, requestOptions); + } + + /** + * The createWidget operation. + * + * @param body The body parameter. + * @throws IllegalArgumentException thrown if parameters fail the validation. + * @throws HttpResponseException thrown if the request is rejected by server. + * @throws ClientAuthenticationException thrown if the request is rejected by server on status code 401. + * @throws ResourceNotFoundException thrown if the request is rejected by server on status code 404. + * @throws ResourceModifiedException thrown if the request is rejected by server on status code 409. + * @throws RuntimeException all other wrapped checked exceptions if the request fails to be sent. + * @return the response body on successful completion of {@link Mono}. + */ + @Generated + @ServiceMethod(returns = ReturnType.SINGLE) + public Mono createWidget(CreateWidgetOptions body) { + // Generated convenience method for createWidgetWithResponse + RequestOptions requestOptions = new RequestOptions(); + return createWidgetWithResponse(BinaryData.fromObject(body), requestOptions).flatMap(FluxUtil::toMono) + .map(protocolMethodData -> protocolMethodData.toObject(Widget.class)); + } +} diff --git a/packages/http-client-java/generator/http-client-generator-test/src/main/java/tsptest/maxoverload/model/MaxOverloadClient.java b/packages/http-client-java/generator/http-client-generator-test/src/main/java/tsptest/maxoverload/model/MaxOverloadClient.java new file mode 100644 index 00000000000..b0cf6f11026 --- /dev/null +++ b/packages/http-client-java/generator/http-client-generator-test/src/main/java/tsptest/maxoverload/model/MaxOverloadClient.java @@ -0,0 +1,95 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. +// Code generated by Microsoft (R) TypeSpec Code Generator. + +package tsptest.maxoverload.model; + +import com.azure.core.annotation.Generated; +import com.azure.core.annotation.ReturnType; +import com.azure.core.annotation.ServiceClient; +import com.azure.core.annotation.ServiceMethod; +import com.azure.core.exception.ClientAuthenticationException; +import com.azure.core.exception.HttpResponseException; +import com.azure.core.exception.ResourceModifiedException; +import com.azure.core.exception.ResourceNotFoundException; +import com.azure.core.http.rest.RequestOptions; +import com.azure.core.http.rest.Response; +import com.azure.core.util.BinaryData; +import tsptest.maxoverload.model.implementation.MaxOverloadOperationsImpl; +import tsptest.maxoverload.model.models.CreateWidgetOptions; +import tsptest.maxoverload.model.models.Widget; + +/** + * Initializes a new instance of the synchronous MaxOverloadClient type. + */ +@ServiceClient(builder = MaxOverloadClientBuilder.class) +public final class MaxOverloadClient { + @Generated + private final MaxOverloadOperationsImpl serviceClient; + + /** + * Initializes an instance of MaxOverloadClient class. + * + * @param serviceClient the service client implementation. + */ + @Generated + MaxOverloadClient(MaxOverloadOperationsImpl serviceClient) { + this.serviceClient = serviceClient; + } + + /** + * The createWidget operation. + *

Request Body Schema

+ * + *
+     * {@code
+     * {
+     *     name: String (Required)
+     * }
+     * }
+     * 
+ * + *

Response Body Schema

+ * + *
+     * {@code
+     * {
+     *     name: String (Required)
+     * }
+     * }
+     * 
+ * + * @param body The body parameter. + * @param requestOptions The options to configure the HTTP request before HTTP client sends it. + * @throws HttpResponseException thrown if the request is rejected by server. + * @throws ClientAuthenticationException thrown if the request is rejected by server on status code 401. + * @throws ResourceNotFoundException thrown if the request is rejected by server on status code 404. + * @throws ResourceModifiedException thrown if the request is rejected by server on status code 409. + * @return the response body along with {@link Response}. + */ + @Generated + @ServiceMethod(returns = ReturnType.SINGLE) + public Response createWidgetWithResponse(BinaryData body, RequestOptions requestOptions) { + return this.serviceClient.createWidgetWithResponse(body, requestOptions); + } + + /** + * The createWidget operation. + * + * @param body The body parameter. + * @throws IllegalArgumentException thrown if parameters fail the validation. + * @throws HttpResponseException thrown if the request is rejected by server. + * @throws ClientAuthenticationException thrown if the request is rejected by server on status code 401. + * @throws ResourceNotFoundException thrown if the request is rejected by server on status code 404. + * @throws ResourceModifiedException thrown if the request is rejected by server on status code 409. + * @throws RuntimeException all other wrapped checked exceptions if the request fails to be sent. + * @return the response. + */ + @Generated + @ServiceMethod(returns = ReturnType.SINGLE) + public Widget createWidget(CreateWidgetOptions body) { + // Generated convenience method for createWidgetWithResponse + RequestOptions requestOptions = new RequestOptions(); + return createWidgetWithResponse(BinaryData.fromObject(body), requestOptions).getValue().toObject(Widget.class); + } +} diff --git a/packages/http-client-java/generator/http-client-generator-test/src/main/java/tsptest/maxoverload/model/MaxOverloadClientBuilder.java b/packages/http-client-java/generator/http-client-generator-test/src/main/java/tsptest/maxoverload/model/MaxOverloadClientBuilder.java new file mode 100644 index 00000000000..2b0e529d9a3 --- /dev/null +++ b/packages/http-client-java/generator/http-client-generator-test/src/main/java/tsptest/maxoverload/model/MaxOverloadClientBuilder.java @@ -0,0 +1,288 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. +// Code generated by Microsoft (R) TypeSpec Code Generator. + +package tsptest.maxoverload.model; + +import com.azure.core.annotation.Generated; +import com.azure.core.annotation.ServiceClientBuilder; +import com.azure.core.client.traits.ConfigurationTrait; +import com.azure.core.client.traits.EndpointTrait; +import com.azure.core.client.traits.HttpTrait; +import com.azure.core.http.HttpClient; +import com.azure.core.http.HttpHeaders; +import com.azure.core.http.HttpPipeline; +import com.azure.core.http.HttpPipelineBuilder; +import com.azure.core.http.HttpPipelinePosition; +import com.azure.core.http.policy.AddDatePolicy; +import com.azure.core.http.policy.AddHeadersFromContextPolicy; +import com.azure.core.http.policy.AddHeadersPolicy; +import com.azure.core.http.policy.HttpLogOptions; +import com.azure.core.http.policy.HttpLoggingPolicy; +import com.azure.core.http.policy.HttpPipelinePolicy; +import com.azure.core.http.policy.HttpPolicyProviders; +import com.azure.core.http.policy.RequestIdPolicy; +import com.azure.core.http.policy.RetryOptions; +import com.azure.core.http.policy.RetryPolicy; +import com.azure.core.http.policy.UserAgentPolicy; +import com.azure.core.util.ClientOptions; +import com.azure.core.util.Configuration; +import com.azure.core.util.CoreUtils; +import com.azure.core.util.builder.ClientBuilderUtil; +import com.azure.core.util.logging.ClientLogger; +import com.azure.core.util.serializer.JacksonAdapter; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.Objects; +import tsptest.maxoverload.model.implementation.MaxOverloadClientImpl; + +/** + * A builder for creating a new instance of the MaxOverloadClient type. + */ +@ServiceClientBuilder(serviceClients = { MaxOverloadClient.class, MaxOverloadAsyncClient.class }) +public final class MaxOverloadClientBuilder implements HttpTrait, + ConfigurationTrait, EndpointTrait { + @Generated + private static final String SDK_NAME = "name"; + + @Generated + private static final String SDK_VERSION = "version"; + + @Generated + private static final Map PROPERTIES + = CoreUtils.getProperties("tsptest-maxoverload-model.properties"); + + @Generated + private final List pipelinePolicies; + + /** + * Create an instance of the MaxOverloadClientBuilder. + */ + @Generated + public MaxOverloadClientBuilder() { + this.pipelinePolicies = new ArrayList<>(); + } + + /* + * The HTTP client used to send the request. + */ + @Generated + private HttpClient httpClient; + + /** + * {@inheritDoc}. + */ + @Generated + @Override + public MaxOverloadClientBuilder httpClient(HttpClient httpClient) { + this.httpClient = httpClient; + return this; + } + + /* + * The HTTP pipeline to send requests through. + */ + @Generated + private HttpPipeline pipeline; + + /** + * {@inheritDoc}. + */ + @Generated + @Override + public MaxOverloadClientBuilder pipeline(HttpPipeline pipeline) { + if (this.pipeline != null && pipeline == null) { + LOGGER.atInfo().log("HttpPipeline is being set to 'null' when it was previously configured."); + } + this.pipeline = pipeline; + return this; + } + + /* + * The logging configuration for HTTP requests and responses. + */ + @Generated + private HttpLogOptions httpLogOptions; + + /** + * {@inheritDoc}. + */ + @Generated + @Override + public MaxOverloadClientBuilder httpLogOptions(HttpLogOptions httpLogOptions) { + this.httpLogOptions = httpLogOptions; + return this; + } + + /* + * The client options such as application ID and custom headers to set on a request. + */ + @Generated + private ClientOptions clientOptions; + + /** + * {@inheritDoc}. + */ + @Generated + @Override + public MaxOverloadClientBuilder clientOptions(ClientOptions clientOptions) { + this.clientOptions = clientOptions; + return this; + } + + /* + * The retry options to configure retry policy for failed requests. + */ + @Generated + private RetryOptions retryOptions; + + /** + * {@inheritDoc}. + */ + @Generated + @Override + public MaxOverloadClientBuilder retryOptions(RetryOptions retryOptions) { + this.retryOptions = retryOptions; + return this; + } + + /** + * {@inheritDoc}. + */ + @Generated + @Override + public MaxOverloadClientBuilder addPolicy(HttpPipelinePolicy customPolicy) { + Objects.requireNonNull(customPolicy, "'customPolicy' cannot be null."); + pipelinePolicies.add(customPolicy); + return this; + } + + /* + * The configuration store that is used during construction of the service client. + */ + @Generated + private Configuration configuration; + + /** + * {@inheritDoc}. + */ + @Generated + @Override + public MaxOverloadClientBuilder configuration(Configuration configuration) { + this.configuration = configuration; + return this; + } + + /* + * The service endpoint + */ + @Generated + private String endpoint; + + /** + * {@inheritDoc}. + */ + @Generated + @Override + public MaxOverloadClientBuilder endpoint(String endpoint) { + this.endpoint = endpoint; + return this; + } + + /* + * The retry policy that will attempt to retry failed requests, if applicable. + */ + @Generated + private RetryPolicy retryPolicy; + + /** + * Sets The retry policy that will attempt to retry failed requests, if applicable. + * + * @param retryPolicy the retryPolicy value. + * @return the MaxOverloadClientBuilder. + */ + @Generated + public MaxOverloadClientBuilder retryPolicy(RetryPolicy retryPolicy) { + this.retryPolicy = retryPolicy; + return this; + } + + /** + * Builds an instance of MaxOverloadClientImpl with the provided parameters. + * + * @return an instance of MaxOverloadClientImpl. + */ + @Generated + private MaxOverloadClientImpl buildInnerClient() { + this.validateClient(); + HttpPipeline localPipeline = (pipeline != null) ? pipeline : createHttpPipeline(); + MaxOverloadClientImpl client + = new MaxOverloadClientImpl(localPipeline, JacksonAdapter.createDefaultSerializerAdapter(), this.endpoint); + return client; + } + + @Generated + private void validateClient() { + // This method is invoked from 'buildInnerClient'/'buildClient' method. + // Developer can customize this method, to validate that the necessary conditions are met for the new client. + Objects.requireNonNull(endpoint, "'endpoint' cannot be null."); + } + + @Generated + private HttpPipeline createHttpPipeline() { + Configuration buildConfiguration + = (configuration == null) ? Configuration.getGlobalConfiguration() : configuration; + HttpLogOptions localHttpLogOptions = this.httpLogOptions == null ? new HttpLogOptions() : this.httpLogOptions; + ClientOptions localClientOptions = this.clientOptions == null ? new ClientOptions() : this.clientOptions; + List policies = new ArrayList<>(); + String clientName = PROPERTIES.getOrDefault(SDK_NAME, "UnknownName"); + String clientVersion = PROPERTIES.getOrDefault(SDK_VERSION, "UnknownVersion"); + String applicationId = CoreUtils.getApplicationId(localClientOptions, localHttpLogOptions); + policies.add(new UserAgentPolicy(applicationId, clientName, clientVersion, buildConfiguration)); + policies.add(new RequestIdPolicy()); + policies.add(new AddHeadersFromContextPolicy()); + HttpHeaders headers = CoreUtils.createHttpHeadersFromClientOptions(localClientOptions); + if (headers != null) { + policies.add(new AddHeadersPolicy(headers)); + } + this.pipelinePolicies.stream() + .filter(p -> p.getPipelinePosition() == HttpPipelinePosition.PER_CALL) + .forEach(p -> policies.add(p)); + HttpPolicyProviders.addBeforeRetryPolicies(policies); + policies.add(ClientBuilderUtil.validateAndGetRetryPolicy(retryPolicy, retryOptions, new RetryPolicy())); + policies.add(new AddDatePolicy()); + this.pipelinePolicies.stream() + .filter(p -> p.getPipelinePosition() == HttpPipelinePosition.PER_RETRY) + .forEach(p -> policies.add(p)); + HttpPolicyProviders.addAfterRetryPolicies(policies); + policies.add(new HttpLoggingPolicy(localHttpLogOptions)); + HttpPipeline httpPipeline = new HttpPipelineBuilder().policies(policies.toArray(new HttpPipelinePolicy[0])) + .httpClient(httpClient) + .clientOptions(localClientOptions) + .build(); + return httpPipeline; + } + + /** + * Builds an instance of MaxOverloadAsyncClient class. + * + * @return an instance of MaxOverloadAsyncClient. + */ + @Generated + public MaxOverloadAsyncClient buildAsyncClient() { + return new MaxOverloadAsyncClient(buildInnerClient().getMaxOverloadOperations()); + } + + /** + * Builds an instance of MaxOverloadClient class. + * + * @return an instance of MaxOverloadClient. + */ + @Generated + public MaxOverloadClient buildClient() { + return new MaxOverloadClient(buildInnerClient().getMaxOverloadOperations()); + } + + private static final ClientLogger LOGGER = new ClientLogger(MaxOverloadClientBuilder.class); +} diff --git a/packages/http-client-java/generator/http-client-generator-test/src/main/java/tsptest/maxoverload/model/implementation/MaxOverloadClientImpl.java b/packages/http-client-java/generator/http-client-generator-test/src/main/java/tsptest/maxoverload/model/implementation/MaxOverloadClientImpl.java new file mode 100644 index 00000000000..b3575f6cba5 --- /dev/null +++ b/packages/http-client-java/generator/http-client-generator-test/src/main/java/tsptest/maxoverload/model/implementation/MaxOverloadClientImpl.java @@ -0,0 +1,107 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. +// Code generated by Microsoft (R) TypeSpec Code Generator. + +package tsptest.maxoverload.model.implementation; + +import com.azure.core.http.HttpPipeline; +import com.azure.core.http.HttpPipelineBuilder; +import com.azure.core.http.policy.RetryPolicy; +import com.azure.core.http.policy.UserAgentPolicy; +import com.azure.core.util.serializer.JacksonAdapter; +import com.azure.core.util.serializer.SerializerAdapter; + +/** + * Initializes a new instance of the MaxOverloadClient type. + */ +public final class MaxOverloadClientImpl { + /** + * Service host. + */ + private final String endpoint; + + /** + * Gets Service host. + * + * @return the endpoint value. + */ + public String getEndpoint() { + return this.endpoint; + } + + /** + * The HTTP pipeline to send requests through. + */ + private final HttpPipeline httpPipeline; + + /** + * Gets The HTTP pipeline to send requests through. + * + * @return the httpPipeline value. + */ + public HttpPipeline getHttpPipeline() { + return this.httpPipeline; + } + + /** + * The serializer to serialize an object into a string. + */ + private final SerializerAdapter serializerAdapter; + + /** + * Gets The serializer to serialize an object into a string. + * + * @return the serializerAdapter value. + */ + public SerializerAdapter getSerializerAdapter() { + return this.serializerAdapter; + } + + /** + * The MaxOverloadOperationsImpl object to access its operations. + */ + private final MaxOverloadOperationsImpl maxOverloadOperations; + + /** + * Gets the MaxOverloadOperationsImpl object to access its operations. + * + * @return the MaxOverloadOperationsImpl object. + */ + public MaxOverloadOperationsImpl getMaxOverloadOperations() { + return this.maxOverloadOperations; + } + + /** + * Initializes an instance of MaxOverloadClient client. + * + * @param endpoint Service host. + */ + public MaxOverloadClientImpl(String endpoint) { + this(new HttpPipelineBuilder().policies(new UserAgentPolicy(), new RetryPolicy()).build(), + JacksonAdapter.createDefaultSerializerAdapter(), endpoint); + } + + /** + * Initializes an instance of MaxOverloadClient client. + * + * @param httpPipeline The HTTP pipeline to send requests through. + * @param endpoint Service host. + */ + public MaxOverloadClientImpl(HttpPipeline httpPipeline, String endpoint) { + this(httpPipeline, JacksonAdapter.createDefaultSerializerAdapter(), endpoint); + } + + /** + * Initializes an instance of MaxOverloadClient client. + * + * @param httpPipeline The HTTP pipeline to send requests through. + * @param serializerAdapter The serializer to serialize an object into a string. + * @param endpoint Service host. + */ + public MaxOverloadClientImpl(HttpPipeline httpPipeline, SerializerAdapter serializerAdapter, String endpoint) { + this.httpPipeline = httpPipeline; + this.serializerAdapter = serializerAdapter; + this.endpoint = endpoint; + this.maxOverloadOperations = new MaxOverloadOperationsImpl(this); + } +} diff --git a/packages/http-client-java/generator/http-client-generator-test/src/main/java/tsptest/maxoverload/model/implementation/MaxOverloadOperationsImpl.java b/packages/http-client-java/generator/http-client-generator-test/src/main/java/tsptest/maxoverload/model/implementation/MaxOverloadOperationsImpl.java new file mode 100644 index 00000000000..0f69d2d9408 --- /dev/null +++ b/packages/http-client-java/generator/http-client-generator-test/src/main/java/tsptest/maxoverload/model/implementation/MaxOverloadOperationsImpl.java @@ -0,0 +1,157 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. +// Code generated by Microsoft (R) TypeSpec Code Generator. + +package tsptest.maxoverload.model.implementation; + +import com.azure.core.annotation.BodyParam; +import com.azure.core.annotation.ExpectedResponses; +import com.azure.core.annotation.HeaderParam; +import com.azure.core.annotation.Host; +import com.azure.core.annotation.HostParam; +import com.azure.core.annotation.Post; +import com.azure.core.annotation.ReturnType; +import com.azure.core.annotation.ServiceInterface; +import com.azure.core.annotation.ServiceMethod; +import com.azure.core.annotation.UnexpectedResponseExceptionType; +import com.azure.core.exception.ClientAuthenticationException; +import com.azure.core.exception.HttpResponseException; +import com.azure.core.exception.ResourceModifiedException; +import com.azure.core.exception.ResourceNotFoundException; +import com.azure.core.http.rest.RequestOptions; +import com.azure.core.http.rest.Response; +import com.azure.core.http.rest.RestProxy; +import com.azure.core.util.BinaryData; +import com.azure.core.util.Context; +import com.azure.core.util.FluxUtil; +import reactor.core.publisher.Mono; + +/** + * An instance of this class provides access to all the operations defined in MaxOverloadOperations. + */ +public final class MaxOverloadOperationsImpl { + /** + * The proxy service used to perform REST calls. + */ + private final MaxOverloadOperationsService service; + + /** + * The service client containing this operation class. + */ + private final MaxOverloadClientImpl client; + + /** + * Initializes an instance of MaxOverloadOperationsImpl. + * + * @param client the instance of the service client containing this operation class. + */ + MaxOverloadOperationsImpl(MaxOverloadClientImpl client) { + this.service = RestProxy.create(MaxOverloadOperationsService.class, client.getHttpPipeline(), + client.getSerializerAdapter()); + this.client = client; + } + + /** + * The interface defining all the services for MaxOverloadClientMaxOverloadOperations to be used by the proxy + * service to perform REST calls. + */ + @Host("{endpoint}") + @ServiceInterface(name = "MaxOverloadClientMaxOverloadOperations") + public interface MaxOverloadOperationsService { + @Post("/max-overload") + @ExpectedResponses({ 200 }) + @UnexpectedResponseExceptionType(value = ClientAuthenticationException.class, code = { 401 }) + @UnexpectedResponseExceptionType(value = ResourceNotFoundException.class, code = { 404 }) + @UnexpectedResponseExceptionType(value = ResourceModifiedException.class, code = { 409 }) + @UnexpectedResponseExceptionType(HttpResponseException.class) + Mono> createWidget(@HostParam("endpoint") String endpoint, + @HeaderParam("Content-Type") String contentType, @HeaderParam("Accept") String accept, + @BodyParam("application/json") BinaryData body, RequestOptions requestOptions, Context context); + + @Post("/max-overload") + @ExpectedResponses({ 200 }) + @UnexpectedResponseExceptionType(value = ClientAuthenticationException.class, code = { 401 }) + @UnexpectedResponseExceptionType(value = ResourceNotFoundException.class, code = { 404 }) + @UnexpectedResponseExceptionType(value = ResourceModifiedException.class, code = { 409 }) + @UnexpectedResponseExceptionType(HttpResponseException.class) + Response createWidgetSync(@HostParam("endpoint") String endpoint, + @HeaderParam("Content-Type") String contentType, @HeaderParam("Accept") String accept, + @BodyParam("application/json") BinaryData body, RequestOptions requestOptions, Context context); + } + + /** + * The createWidget operation. + *

Request Body Schema

+ * + *
+     * {@code
+     * {
+     *     name: String (Required)
+     * }
+     * }
+     * 
+ * + *

Response Body Schema

+ * + *
+     * {@code
+     * {
+     *     name: String (Required)
+     * }
+     * }
+     * 
+ * + * @param body The body parameter. + * @param requestOptions The options to configure the HTTP request before HTTP client sends it. + * @throws HttpResponseException thrown if the request is rejected by server. + * @throws ClientAuthenticationException thrown if the request is rejected by server on status code 401. + * @throws ResourceNotFoundException thrown if the request is rejected by server on status code 404. + * @throws ResourceModifiedException thrown if the request is rejected by server on status code 409. + * @return the response body along with {@link Response} on successful completion of {@link Mono}. + */ + @ServiceMethod(returns = ReturnType.SINGLE) + public Mono> createWidgetWithResponseAsync(BinaryData body, RequestOptions requestOptions) { + final String contentType = "application/json"; + final String accept = "application/json"; + return FluxUtil.withContext(context -> service.createWidget(this.client.getEndpoint(), contentType, accept, + body, requestOptions, context)); + } + + /** + * The createWidget operation. + *

Request Body Schema

+ * + *
+     * {@code
+     * {
+     *     name: String (Required)
+     * }
+     * }
+     * 
+ * + *

Response Body Schema

+ * + *
+     * {@code
+     * {
+     *     name: String (Required)
+     * }
+     * }
+     * 
+ * + * @param body The body parameter. + * @param requestOptions The options to configure the HTTP request before HTTP client sends it. + * @throws HttpResponseException thrown if the request is rejected by server. + * @throws ClientAuthenticationException thrown if the request is rejected by server on status code 401. + * @throws ResourceNotFoundException thrown if the request is rejected by server on status code 404. + * @throws ResourceModifiedException thrown if the request is rejected by server on status code 409. + * @return the response body along with {@link Response}. + */ + @ServiceMethod(returns = ReturnType.SINGLE) + public Response createWidgetWithResponse(BinaryData body, RequestOptions requestOptions) { + final String contentType = "application/json"; + final String accept = "application/json"; + return service.createWidgetSync(this.client.getEndpoint(), contentType, accept, body, requestOptions, + Context.NONE); + } +} diff --git a/packages/http-client-java/generator/http-client-generator-test/src/main/java/tsptest/maxoverload/model/implementation/package-info.java b/packages/http-client-java/generator/http-client-generator-test/src/main/java/tsptest/maxoverload/model/implementation/package-info.java new file mode 100644 index 00000000000..d2f8ae023b9 --- /dev/null +++ b/packages/http-client-java/generator/http-client-generator-test/src/main/java/tsptest/maxoverload/model/implementation/package-info.java @@ -0,0 +1,10 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. +// Code generated by Microsoft (R) TypeSpec Code Generator. + +/** + * + * Package containing the implementations for MaxOverload. + * + */ +package tsptest.maxoverload.model.implementation; diff --git a/packages/http-client-java/generator/http-client-generator-test/src/main/java/tsptest/maxoverload/model/models/CreateWidgetOptions.java b/packages/http-client-java/generator/http-client-generator-test/src/main/java/tsptest/maxoverload/model/models/CreateWidgetOptions.java new file mode 100644 index 00000000000..430debcd4e3 --- /dev/null +++ b/packages/http-client-java/generator/http-client-generator-test/src/main/java/tsptest/maxoverload/model/models/CreateWidgetOptions.java @@ -0,0 +1,83 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. +// Code generated by Microsoft (R) TypeSpec Code Generator. + +package tsptest.maxoverload.model.models; + +import com.azure.core.annotation.Generated; +import com.azure.core.annotation.Immutable; +import com.azure.json.JsonReader; +import com.azure.json.JsonSerializable; +import com.azure.json.JsonToken; +import com.azure.json.JsonWriter; +import java.io.IOException; + +/** + * The CreateWidgetOptions model. + */ +@Immutable +public final class CreateWidgetOptions implements JsonSerializable { + /* + * The name property. + */ + @Generated + private final String name; + + /** + * Creates an instance of CreateWidgetOptions class. + * + * @param name the name value to set. + */ + @Generated + public CreateWidgetOptions(String name) { + this.name = name; + } + + /** + * Get the name property: The name property. + * + * @return the name value. + */ + @Generated + public String getName() { + return this.name; + } + + /** + * {@inheritDoc} + */ + @Generated + @Override + public JsonWriter toJson(JsonWriter jsonWriter) throws IOException { + jsonWriter.writeStartObject(); + jsonWriter.writeStringField("name", this.name); + return jsonWriter.writeEndObject(); + } + + /** + * Reads an instance of CreateWidgetOptions from the JsonReader. + * + * @param jsonReader The JsonReader being read. + * @return An instance of CreateWidgetOptions if the JsonReader was pointing to an instance of it, or null if it was + * pointing to JSON null. + * @throws IllegalStateException If the deserialized JSON object was missing any required properties. + * @throws IOException If an error occurs while reading the CreateWidgetOptions. + */ + @Generated + public static CreateWidgetOptions fromJson(JsonReader jsonReader) throws IOException { + return jsonReader.readObject(reader -> { + String name = null; + while (reader.nextToken() != JsonToken.END_OBJECT) { + String fieldName = reader.getFieldName(); + reader.nextToken(); + + if ("name".equals(fieldName)) { + name = reader.getString(); + } else { + reader.skipChildren(); + } + } + return new CreateWidgetOptions(name); + }); + } +} diff --git a/packages/http-client-java/generator/http-client-generator-test/src/main/java/tsptest/maxoverload/model/models/Widget.java b/packages/http-client-java/generator/http-client-generator-test/src/main/java/tsptest/maxoverload/model/models/Widget.java new file mode 100644 index 00000000000..52d725b8e73 --- /dev/null +++ b/packages/http-client-java/generator/http-client-generator-test/src/main/java/tsptest/maxoverload/model/models/Widget.java @@ -0,0 +1,83 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. +// Code generated by Microsoft (R) TypeSpec Code Generator. + +package tsptest.maxoverload.model.models; + +import com.azure.core.annotation.Generated; +import com.azure.core.annotation.Immutable; +import com.azure.json.JsonReader; +import com.azure.json.JsonSerializable; +import com.azure.json.JsonToken; +import com.azure.json.JsonWriter; +import java.io.IOException; + +/** + * The Widget model. + */ +@Immutable +public final class Widget implements JsonSerializable { + /* + * The name property. + */ + @Generated + private final String name; + + /** + * Creates an instance of Widget class. + * + * @param name the name value to set. + */ + @Generated + private Widget(String name) { + this.name = name; + } + + /** + * Get the name property: The name property. + * + * @return the name value. + */ + @Generated + public String getName() { + return this.name; + } + + /** + * {@inheritDoc} + */ + @Generated + @Override + public JsonWriter toJson(JsonWriter jsonWriter) throws IOException { + jsonWriter.writeStartObject(); + jsonWriter.writeStringField("name", this.name); + return jsonWriter.writeEndObject(); + } + + /** + * Reads an instance of Widget from the JsonReader. + * + * @param jsonReader The JsonReader being read. + * @return An instance of Widget if the JsonReader was pointing to an instance of it, or null if it was pointing to + * JSON null. + * @throws IllegalStateException If the deserialized JSON object was missing any required properties. + * @throws IOException If an error occurs while reading the Widget. + */ + @Generated + public static Widget fromJson(JsonReader jsonReader) throws IOException { + return jsonReader.readObject(reader -> { + String name = null; + while (reader.nextToken() != JsonToken.END_OBJECT) { + String fieldName = reader.getFieldName(); + reader.nextToken(); + + if ("name".equals(fieldName)) { + name = reader.getString(); + } else { + reader.skipChildren(); + } + } + return new Widget(name); + }); + } +} diff --git a/packages/http-client-java/generator/http-client-generator-test/src/main/java/tsptest/maxoverload/model/models/package-info.java b/packages/http-client-java/generator/http-client-generator-test/src/main/java/tsptest/maxoverload/model/models/package-info.java new file mode 100644 index 00000000000..f6b5866d6bd --- /dev/null +++ b/packages/http-client-java/generator/http-client-generator-test/src/main/java/tsptest/maxoverload/model/models/package-info.java @@ -0,0 +1,10 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. +// Code generated by Microsoft (R) TypeSpec Code Generator. + +/** + * + * Package containing the data models for MaxOverload. + * + */ +package tsptest.maxoverload.model.models; diff --git a/packages/http-client-java/generator/http-client-generator-test/src/main/java/tsptest/maxoverload/model/package-info.java b/packages/http-client-java/generator/http-client-generator-test/src/main/java/tsptest/maxoverload/model/package-info.java new file mode 100644 index 00000000000..dc77e21cea0 --- /dev/null +++ b/packages/http-client-java/generator/http-client-generator-test/src/main/java/tsptest/maxoverload/model/package-info.java @@ -0,0 +1,10 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. +// Code generated by Microsoft (R) TypeSpec Code Generator. + +/** + * + * Package containing the classes for MaxOverload. + * + */ +package tsptest.maxoverload.model; diff --git a/packages/http-client-java/generator/http-client-generator-test/src/main/java/tsptest/maxoverload/protocol/MaxOverloadAsyncClient.java b/packages/http-client-java/generator/http-client-generator-test/src/main/java/tsptest/maxoverload/protocol/MaxOverloadAsyncClient.java new file mode 100644 index 00000000000..5fd0ba646a7 --- /dev/null +++ b/packages/http-client-java/generator/http-client-generator-test/src/main/java/tsptest/maxoverload/protocol/MaxOverloadAsyncClient.java @@ -0,0 +1,98 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. +// Code generated by Microsoft (R) TypeSpec Code Generator. + +package tsptest.maxoverload.protocol; + +import com.azure.core.annotation.Generated; +import com.azure.core.annotation.ReturnType; +import com.azure.core.annotation.ServiceClient; +import com.azure.core.annotation.ServiceMethod; +import com.azure.core.exception.ClientAuthenticationException; +import com.azure.core.exception.HttpResponseException; +import com.azure.core.exception.ResourceModifiedException; +import com.azure.core.exception.ResourceNotFoundException; +import com.azure.core.http.rest.RequestOptions; +import com.azure.core.http.rest.Response; +import com.azure.core.util.BinaryData; +import com.azure.core.util.FluxUtil; +import reactor.core.publisher.Mono; +import tsptest.maxoverload.protocol.implementation.MaxOverloadOperationsImpl; +import tsptest.maxoverload.protocol.models.CreateWidgetOptions; +import tsptest.maxoverload.protocol.models.Widget; + +/** + * Initializes a new instance of the asynchronous MaxOverloadClient type. + */ +@ServiceClient(builder = MaxOverloadClientBuilder.class, isAsync = true) +public final class MaxOverloadAsyncClient { + @Generated + private final MaxOverloadOperationsImpl serviceClient; + + /** + * Initializes an instance of MaxOverloadAsyncClient class. + * + * @param serviceClient the service client implementation. + */ + @Generated + MaxOverloadAsyncClient(MaxOverloadOperationsImpl serviceClient) { + this.serviceClient = serviceClient; + } + + /** + * The createWidget operation. + *

Request Body Schema

+ * + *
+     * {@code
+     * {
+     *     name: String (Required)
+     * }
+     * }
+     * 
+ * + *

Response Body Schema

+ * + *
+     * {@code
+     * {
+     *     name: String (Required)
+     * }
+     * }
+     * 
+ * + * @param body The body parameter. + * @param requestOptions The options to configure the HTTP request before HTTP client sends it. + * @throws HttpResponseException thrown if the request is rejected by server. + * @throws ClientAuthenticationException thrown if the request is rejected by server on status code 401. + * @throws ResourceNotFoundException thrown if the request is rejected by server on status code 404. + * @throws ResourceModifiedException thrown if the request is rejected by server on status code 409. + * @return the response body along with {@link Response} on successful completion of {@link Mono}. + */ + @Generated + @ServiceMethod(returns = ReturnType.SINGLE) + public Mono> createWidgetWithResponse(BinaryData body, RequestOptions requestOptions) { + return this.serviceClient.createWidgetWithResponseAsync(body, requestOptions); + } + + /** + * The createWidget operation. + * + * @param body The body parameter. + * @throws IllegalArgumentException thrown if parameters fail the validation. + * @throws HttpResponseException thrown if the request is rejected by server. + * @throws ClientAuthenticationException thrown if the request is rejected by server on status code 401. + * @throws ResourceNotFoundException thrown if the request is rejected by server on status code 404. + * @throws ResourceModifiedException thrown if the request is rejected by server on status code 409. + * @throws RuntimeException all other wrapped checked exceptions if the request fails to be sent. + * @return the response body on successful completion of {@link Mono}. + */ + @Generated + @ServiceMethod(returns = ReturnType.SINGLE) + public Mono createWidget(CreateWidgetOptions body) { + // Generated convenience method for createWidgetWithResponse + RequestOptions requestOptions = new RequestOptions(); + return createWidgetWithResponse(BinaryData.fromObject(body), requestOptions).flatMap(FluxUtil::toMono) + .map(protocolMethodData -> protocolMethodData.toObject(Widget.class)); + } +} diff --git a/packages/http-client-java/generator/http-client-generator-test/src/main/java/tsptest/maxoverload/protocol/MaxOverloadClient.java b/packages/http-client-java/generator/http-client-generator-test/src/main/java/tsptest/maxoverload/protocol/MaxOverloadClient.java new file mode 100644 index 00000000000..dfec1bf148e --- /dev/null +++ b/packages/http-client-java/generator/http-client-generator-test/src/main/java/tsptest/maxoverload/protocol/MaxOverloadClient.java @@ -0,0 +1,95 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. +// Code generated by Microsoft (R) TypeSpec Code Generator. + +package tsptest.maxoverload.protocol; + +import com.azure.core.annotation.Generated; +import com.azure.core.annotation.ReturnType; +import com.azure.core.annotation.ServiceClient; +import com.azure.core.annotation.ServiceMethod; +import com.azure.core.exception.ClientAuthenticationException; +import com.azure.core.exception.HttpResponseException; +import com.azure.core.exception.ResourceModifiedException; +import com.azure.core.exception.ResourceNotFoundException; +import com.azure.core.http.rest.RequestOptions; +import com.azure.core.http.rest.Response; +import com.azure.core.util.BinaryData; +import tsptest.maxoverload.protocol.implementation.MaxOverloadOperationsImpl; +import tsptest.maxoverload.protocol.models.CreateWidgetOptions; +import tsptest.maxoverload.protocol.models.Widget; + +/** + * Initializes a new instance of the synchronous MaxOverloadClient type. + */ +@ServiceClient(builder = MaxOverloadClientBuilder.class) +public final class MaxOverloadClient { + @Generated + private final MaxOverloadOperationsImpl serviceClient; + + /** + * Initializes an instance of MaxOverloadClient class. + * + * @param serviceClient the service client implementation. + */ + @Generated + MaxOverloadClient(MaxOverloadOperationsImpl serviceClient) { + this.serviceClient = serviceClient; + } + + /** + * The createWidget operation. + *

Request Body Schema

+ * + *
+     * {@code
+     * {
+     *     name: String (Required)
+     * }
+     * }
+     * 
+ * + *

Response Body Schema

+ * + *
+     * {@code
+     * {
+     *     name: String (Required)
+     * }
+     * }
+     * 
+ * + * @param body The body parameter. + * @param requestOptions The options to configure the HTTP request before HTTP client sends it. + * @throws HttpResponseException thrown if the request is rejected by server. + * @throws ClientAuthenticationException thrown if the request is rejected by server on status code 401. + * @throws ResourceNotFoundException thrown if the request is rejected by server on status code 404. + * @throws ResourceModifiedException thrown if the request is rejected by server on status code 409. + * @return the response body along with {@link Response}. + */ + @Generated + @ServiceMethod(returns = ReturnType.SINGLE) + public Response createWidgetWithResponse(BinaryData body, RequestOptions requestOptions) { + return this.serviceClient.createWidgetWithResponse(body, requestOptions); + } + + /** + * The createWidget operation. + * + * @param body The body parameter. + * @throws IllegalArgumentException thrown if parameters fail the validation. + * @throws HttpResponseException thrown if the request is rejected by server. + * @throws ClientAuthenticationException thrown if the request is rejected by server on status code 401. + * @throws ResourceNotFoundException thrown if the request is rejected by server on status code 404. + * @throws ResourceModifiedException thrown if the request is rejected by server on status code 409. + * @throws RuntimeException all other wrapped checked exceptions if the request fails to be sent. + * @return the response. + */ + @Generated + @ServiceMethod(returns = ReturnType.SINGLE) + public Widget createWidget(CreateWidgetOptions body) { + // Generated convenience method for createWidgetWithResponse + RequestOptions requestOptions = new RequestOptions(); + return createWidgetWithResponse(BinaryData.fromObject(body), requestOptions).getValue().toObject(Widget.class); + } +} diff --git a/packages/http-client-java/generator/http-client-generator-test/src/main/java/tsptest/maxoverload/protocol/MaxOverloadClientBuilder.java b/packages/http-client-java/generator/http-client-generator-test/src/main/java/tsptest/maxoverload/protocol/MaxOverloadClientBuilder.java new file mode 100644 index 00000000000..0ceb138493f --- /dev/null +++ b/packages/http-client-java/generator/http-client-generator-test/src/main/java/tsptest/maxoverload/protocol/MaxOverloadClientBuilder.java @@ -0,0 +1,288 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. +// Code generated by Microsoft (R) TypeSpec Code Generator. + +package tsptest.maxoverload.protocol; + +import com.azure.core.annotation.Generated; +import com.azure.core.annotation.ServiceClientBuilder; +import com.azure.core.client.traits.ConfigurationTrait; +import com.azure.core.client.traits.EndpointTrait; +import com.azure.core.client.traits.HttpTrait; +import com.azure.core.http.HttpClient; +import com.azure.core.http.HttpHeaders; +import com.azure.core.http.HttpPipeline; +import com.azure.core.http.HttpPipelineBuilder; +import com.azure.core.http.HttpPipelinePosition; +import com.azure.core.http.policy.AddDatePolicy; +import com.azure.core.http.policy.AddHeadersFromContextPolicy; +import com.azure.core.http.policy.AddHeadersPolicy; +import com.azure.core.http.policy.HttpLogOptions; +import com.azure.core.http.policy.HttpLoggingPolicy; +import com.azure.core.http.policy.HttpPipelinePolicy; +import com.azure.core.http.policy.HttpPolicyProviders; +import com.azure.core.http.policy.RequestIdPolicy; +import com.azure.core.http.policy.RetryOptions; +import com.azure.core.http.policy.RetryPolicy; +import com.azure.core.http.policy.UserAgentPolicy; +import com.azure.core.util.ClientOptions; +import com.azure.core.util.Configuration; +import com.azure.core.util.CoreUtils; +import com.azure.core.util.builder.ClientBuilderUtil; +import com.azure.core.util.logging.ClientLogger; +import com.azure.core.util.serializer.JacksonAdapter; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.Objects; +import tsptest.maxoverload.protocol.implementation.MaxOverloadClientImpl; + +/** + * A builder for creating a new instance of the MaxOverloadClient type. + */ +@ServiceClientBuilder(serviceClients = { MaxOverloadClient.class, MaxOverloadAsyncClient.class }) +public final class MaxOverloadClientBuilder implements HttpTrait, + ConfigurationTrait, EndpointTrait { + @Generated + private static final String SDK_NAME = "name"; + + @Generated + private static final String SDK_VERSION = "version"; + + @Generated + private static final Map PROPERTIES + = CoreUtils.getProperties("tsptest-maxoverload-protocol.properties"); + + @Generated + private final List pipelinePolicies; + + /** + * Create an instance of the MaxOverloadClientBuilder. + */ + @Generated + public MaxOverloadClientBuilder() { + this.pipelinePolicies = new ArrayList<>(); + } + + /* + * The HTTP client used to send the request. + */ + @Generated + private HttpClient httpClient; + + /** + * {@inheritDoc}. + */ + @Generated + @Override + public MaxOverloadClientBuilder httpClient(HttpClient httpClient) { + this.httpClient = httpClient; + return this; + } + + /* + * The HTTP pipeline to send requests through. + */ + @Generated + private HttpPipeline pipeline; + + /** + * {@inheritDoc}. + */ + @Generated + @Override + public MaxOverloadClientBuilder pipeline(HttpPipeline pipeline) { + if (this.pipeline != null && pipeline == null) { + LOGGER.atInfo().log("HttpPipeline is being set to 'null' when it was previously configured."); + } + this.pipeline = pipeline; + return this; + } + + /* + * The logging configuration for HTTP requests and responses. + */ + @Generated + private HttpLogOptions httpLogOptions; + + /** + * {@inheritDoc}. + */ + @Generated + @Override + public MaxOverloadClientBuilder httpLogOptions(HttpLogOptions httpLogOptions) { + this.httpLogOptions = httpLogOptions; + return this; + } + + /* + * The client options such as application ID and custom headers to set on a request. + */ + @Generated + private ClientOptions clientOptions; + + /** + * {@inheritDoc}. + */ + @Generated + @Override + public MaxOverloadClientBuilder clientOptions(ClientOptions clientOptions) { + this.clientOptions = clientOptions; + return this; + } + + /* + * The retry options to configure retry policy for failed requests. + */ + @Generated + private RetryOptions retryOptions; + + /** + * {@inheritDoc}. + */ + @Generated + @Override + public MaxOverloadClientBuilder retryOptions(RetryOptions retryOptions) { + this.retryOptions = retryOptions; + return this; + } + + /** + * {@inheritDoc}. + */ + @Generated + @Override + public MaxOverloadClientBuilder addPolicy(HttpPipelinePolicy customPolicy) { + Objects.requireNonNull(customPolicy, "'customPolicy' cannot be null."); + pipelinePolicies.add(customPolicy); + return this; + } + + /* + * The configuration store that is used during construction of the service client. + */ + @Generated + private Configuration configuration; + + /** + * {@inheritDoc}. + */ + @Generated + @Override + public MaxOverloadClientBuilder configuration(Configuration configuration) { + this.configuration = configuration; + return this; + } + + /* + * The service endpoint + */ + @Generated + private String endpoint; + + /** + * {@inheritDoc}. + */ + @Generated + @Override + public MaxOverloadClientBuilder endpoint(String endpoint) { + this.endpoint = endpoint; + return this; + } + + /* + * The retry policy that will attempt to retry failed requests, if applicable. + */ + @Generated + private RetryPolicy retryPolicy; + + /** + * Sets The retry policy that will attempt to retry failed requests, if applicable. + * + * @param retryPolicy the retryPolicy value. + * @return the MaxOverloadClientBuilder. + */ + @Generated + public MaxOverloadClientBuilder retryPolicy(RetryPolicy retryPolicy) { + this.retryPolicy = retryPolicy; + return this; + } + + /** + * Builds an instance of MaxOverloadClientImpl with the provided parameters. + * + * @return an instance of MaxOverloadClientImpl. + */ + @Generated + private MaxOverloadClientImpl buildInnerClient() { + this.validateClient(); + HttpPipeline localPipeline = (pipeline != null) ? pipeline : createHttpPipeline(); + MaxOverloadClientImpl client + = new MaxOverloadClientImpl(localPipeline, JacksonAdapter.createDefaultSerializerAdapter(), this.endpoint); + return client; + } + + @Generated + private void validateClient() { + // This method is invoked from 'buildInnerClient'/'buildClient' method. + // Developer can customize this method, to validate that the necessary conditions are met for the new client. + Objects.requireNonNull(endpoint, "'endpoint' cannot be null."); + } + + @Generated + private HttpPipeline createHttpPipeline() { + Configuration buildConfiguration + = (configuration == null) ? Configuration.getGlobalConfiguration() : configuration; + HttpLogOptions localHttpLogOptions = this.httpLogOptions == null ? new HttpLogOptions() : this.httpLogOptions; + ClientOptions localClientOptions = this.clientOptions == null ? new ClientOptions() : this.clientOptions; + List policies = new ArrayList<>(); + String clientName = PROPERTIES.getOrDefault(SDK_NAME, "UnknownName"); + String clientVersion = PROPERTIES.getOrDefault(SDK_VERSION, "UnknownVersion"); + String applicationId = CoreUtils.getApplicationId(localClientOptions, localHttpLogOptions); + policies.add(new UserAgentPolicy(applicationId, clientName, clientVersion, buildConfiguration)); + policies.add(new RequestIdPolicy()); + policies.add(new AddHeadersFromContextPolicy()); + HttpHeaders headers = CoreUtils.createHttpHeadersFromClientOptions(localClientOptions); + if (headers != null) { + policies.add(new AddHeadersPolicy(headers)); + } + this.pipelinePolicies.stream() + .filter(p -> p.getPipelinePosition() == HttpPipelinePosition.PER_CALL) + .forEach(p -> policies.add(p)); + HttpPolicyProviders.addBeforeRetryPolicies(policies); + policies.add(ClientBuilderUtil.validateAndGetRetryPolicy(retryPolicy, retryOptions, new RetryPolicy())); + policies.add(new AddDatePolicy()); + this.pipelinePolicies.stream() + .filter(p -> p.getPipelinePosition() == HttpPipelinePosition.PER_RETRY) + .forEach(p -> policies.add(p)); + HttpPolicyProviders.addAfterRetryPolicies(policies); + policies.add(new HttpLoggingPolicy(localHttpLogOptions)); + HttpPipeline httpPipeline = new HttpPipelineBuilder().policies(policies.toArray(new HttpPipelinePolicy[0])) + .httpClient(httpClient) + .clientOptions(localClientOptions) + .build(); + return httpPipeline; + } + + /** + * Builds an instance of MaxOverloadAsyncClient class. + * + * @return an instance of MaxOverloadAsyncClient. + */ + @Generated + public MaxOverloadAsyncClient buildAsyncClient() { + return new MaxOverloadAsyncClient(buildInnerClient().getMaxOverloadOperations()); + } + + /** + * Builds an instance of MaxOverloadClient class. + * + * @return an instance of MaxOverloadClient. + */ + @Generated + public MaxOverloadClient buildClient() { + return new MaxOverloadClient(buildInnerClient().getMaxOverloadOperations()); + } + + private static final ClientLogger LOGGER = new ClientLogger(MaxOverloadClientBuilder.class); +} diff --git a/packages/http-client-java/generator/http-client-generator-test/src/main/java/tsptest/maxoverload/protocol/implementation/MaxOverloadClientImpl.java b/packages/http-client-java/generator/http-client-generator-test/src/main/java/tsptest/maxoverload/protocol/implementation/MaxOverloadClientImpl.java new file mode 100644 index 00000000000..8fc78ebc14a --- /dev/null +++ b/packages/http-client-java/generator/http-client-generator-test/src/main/java/tsptest/maxoverload/protocol/implementation/MaxOverloadClientImpl.java @@ -0,0 +1,107 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. +// Code generated by Microsoft (R) TypeSpec Code Generator. + +package tsptest.maxoverload.protocol.implementation; + +import com.azure.core.http.HttpPipeline; +import com.azure.core.http.HttpPipelineBuilder; +import com.azure.core.http.policy.RetryPolicy; +import com.azure.core.http.policy.UserAgentPolicy; +import com.azure.core.util.serializer.JacksonAdapter; +import com.azure.core.util.serializer.SerializerAdapter; + +/** + * Initializes a new instance of the MaxOverloadClient type. + */ +public final class MaxOverloadClientImpl { + /** + * Service host. + */ + private final String endpoint; + + /** + * Gets Service host. + * + * @return the endpoint value. + */ + public String getEndpoint() { + return this.endpoint; + } + + /** + * The HTTP pipeline to send requests through. + */ + private final HttpPipeline httpPipeline; + + /** + * Gets The HTTP pipeline to send requests through. + * + * @return the httpPipeline value. + */ + public HttpPipeline getHttpPipeline() { + return this.httpPipeline; + } + + /** + * The serializer to serialize an object into a string. + */ + private final SerializerAdapter serializerAdapter; + + /** + * Gets The serializer to serialize an object into a string. + * + * @return the serializerAdapter value. + */ + public SerializerAdapter getSerializerAdapter() { + return this.serializerAdapter; + } + + /** + * The MaxOverloadOperationsImpl object to access its operations. + */ + private final MaxOverloadOperationsImpl maxOverloadOperations; + + /** + * Gets the MaxOverloadOperationsImpl object to access its operations. + * + * @return the MaxOverloadOperationsImpl object. + */ + public MaxOverloadOperationsImpl getMaxOverloadOperations() { + return this.maxOverloadOperations; + } + + /** + * Initializes an instance of MaxOverloadClient client. + * + * @param endpoint Service host. + */ + public MaxOverloadClientImpl(String endpoint) { + this(new HttpPipelineBuilder().policies(new UserAgentPolicy(), new RetryPolicy()).build(), + JacksonAdapter.createDefaultSerializerAdapter(), endpoint); + } + + /** + * Initializes an instance of MaxOverloadClient client. + * + * @param httpPipeline The HTTP pipeline to send requests through. + * @param endpoint Service host. + */ + public MaxOverloadClientImpl(HttpPipeline httpPipeline, String endpoint) { + this(httpPipeline, JacksonAdapter.createDefaultSerializerAdapter(), endpoint); + } + + /** + * Initializes an instance of MaxOverloadClient client. + * + * @param httpPipeline The HTTP pipeline to send requests through. + * @param serializerAdapter The serializer to serialize an object into a string. + * @param endpoint Service host. + */ + public MaxOverloadClientImpl(HttpPipeline httpPipeline, SerializerAdapter serializerAdapter, String endpoint) { + this.httpPipeline = httpPipeline; + this.serializerAdapter = serializerAdapter; + this.endpoint = endpoint; + this.maxOverloadOperations = new MaxOverloadOperationsImpl(this); + } +} diff --git a/packages/http-client-java/generator/http-client-generator-test/src/main/java/tsptest/maxoverload/protocol/implementation/MaxOverloadOperationsImpl.java b/packages/http-client-java/generator/http-client-generator-test/src/main/java/tsptest/maxoverload/protocol/implementation/MaxOverloadOperationsImpl.java new file mode 100644 index 00000000000..1bb6f567c3a --- /dev/null +++ b/packages/http-client-java/generator/http-client-generator-test/src/main/java/tsptest/maxoverload/protocol/implementation/MaxOverloadOperationsImpl.java @@ -0,0 +1,157 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. +// Code generated by Microsoft (R) TypeSpec Code Generator. + +package tsptest.maxoverload.protocol.implementation; + +import com.azure.core.annotation.BodyParam; +import com.azure.core.annotation.ExpectedResponses; +import com.azure.core.annotation.HeaderParam; +import com.azure.core.annotation.Host; +import com.azure.core.annotation.HostParam; +import com.azure.core.annotation.Post; +import com.azure.core.annotation.ReturnType; +import com.azure.core.annotation.ServiceInterface; +import com.azure.core.annotation.ServiceMethod; +import com.azure.core.annotation.UnexpectedResponseExceptionType; +import com.azure.core.exception.ClientAuthenticationException; +import com.azure.core.exception.HttpResponseException; +import com.azure.core.exception.ResourceModifiedException; +import com.azure.core.exception.ResourceNotFoundException; +import com.azure.core.http.rest.RequestOptions; +import com.azure.core.http.rest.Response; +import com.azure.core.http.rest.RestProxy; +import com.azure.core.util.BinaryData; +import com.azure.core.util.Context; +import com.azure.core.util.FluxUtil; +import reactor.core.publisher.Mono; + +/** + * An instance of this class provides access to all the operations defined in MaxOverloadOperations. + */ +public final class MaxOverloadOperationsImpl { + /** + * The proxy service used to perform REST calls. + */ + private final MaxOverloadOperationsService service; + + /** + * The service client containing this operation class. + */ + private final MaxOverloadClientImpl client; + + /** + * Initializes an instance of MaxOverloadOperationsImpl. + * + * @param client the instance of the service client containing this operation class. + */ + MaxOverloadOperationsImpl(MaxOverloadClientImpl client) { + this.service = RestProxy.create(MaxOverloadOperationsService.class, client.getHttpPipeline(), + client.getSerializerAdapter()); + this.client = client; + } + + /** + * The interface defining all the services for MaxOverloadClientMaxOverloadOperations to be used by the proxy + * service to perform REST calls. + */ + @Host("{endpoint}") + @ServiceInterface(name = "MaxOverloadClientMaxOverloadOperations") + public interface MaxOverloadOperationsService { + @Post("/max-overload") + @ExpectedResponses({ 200 }) + @UnexpectedResponseExceptionType(value = ClientAuthenticationException.class, code = { 401 }) + @UnexpectedResponseExceptionType(value = ResourceNotFoundException.class, code = { 404 }) + @UnexpectedResponseExceptionType(value = ResourceModifiedException.class, code = { 409 }) + @UnexpectedResponseExceptionType(HttpResponseException.class) + Mono> createWidget(@HostParam("endpoint") String endpoint, + @HeaderParam("Content-Type") String contentType, @HeaderParam("Accept") String accept, + @BodyParam("application/json") BinaryData body, RequestOptions requestOptions, Context context); + + @Post("/max-overload") + @ExpectedResponses({ 200 }) + @UnexpectedResponseExceptionType(value = ClientAuthenticationException.class, code = { 401 }) + @UnexpectedResponseExceptionType(value = ResourceNotFoundException.class, code = { 404 }) + @UnexpectedResponseExceptionType(value = ResourceModifiedException.class, code = { 409 }) + @UnexpectedResponseExceptionType(HttpResponseException.class) + Response createWidgetSync(@HostParam("endpoint") String endpoint, + @HeaderParam("Content-Type") String contentType, @HeaderParam("Accept") String accept, + @BodyParam("application/json") BinaryData body, RequestOptions requestOptions, Context context); + } + + /** + * The createWidget operation. + *

Request Body Schema

+ * + *
+     * {@code
+     * {
+     *     name: String (Required)
+     * }
+     * }
+     * 
+ * + *

Response Body Schema

+ * + *
+     * {@code
+     * {
+     *     name: String (Required)
+     * }
+     * }
+     * 
+ * + * @param body The body parameter. + * @param requestOptions The options to configure the HTTP request before HTTP client sends it. + * @throws HttpResponseException thrown if the request is rejected by server. + * @throws ClientAuthenticationException thrown if the request is rejected by server on status code 401. + * @throws ResourceNotFoundException thrown if the request is rejected by server on status code 404. + * @throws ResourceModifiedException thrown if the request is rejected by server on status code 409. + * @return the response body along with {@link Response} on successful completion of {@link Mono}. + */ + @ServiceMethod(returns = ReturnType.SINGLE) + public Mono> createWidgetWithResponseAsync(BinaryData body, RequestOptions requestOptions) { + final String contentType = "application/json"; + final String accept = "application/json"; + return FluxUtil.withContext(context -> service.createWidget(this.client.getEndpoint(), contentType, accept, + body, requestOptions, context)); + } + + /** + * The createWidget operation. + *

Request Body Schema

+ * + *
+     * {@code
+     * {
+     *     name: String (Required)
+     * }
+     * }
+     * 
+ * + *

Response Body Schema

+ * + *
+     * {@code
+     * {
+     *     name: String (Required)
+     * }
+     * }
+     * 
+ * + * @param body The body parameter. + * @param requestOptions The options to configure the HTTP request before HTTP client sends it. + * @throws HttpResponseException thrown if the request is rejected by server. + * @throws ClientAuthenticationException thrown if the request is rejected by server on status code 401. + * @throws ResourceNotFoundException thrown if the request is rejected by server on status code 404. + * @throws ResourceModifiedException thrown if the request is rejected by server on status code 409. + * @return the response body along with {@link Response}. + */ + @ServiceMethod(returns = ReturnType.SINGLE) + public Response createWidgetWithResponse(BinaryData body, RequestOptions requestOptions) { + final String contentType = "application/json"; + final String accept = "application/json"; + return service.createWidgetSync(this.client.getEndpoint(), contentType, accept, body, requestOptions, + Context.NONE); + } +} diff --git a/packages/http-client-java/generator/http-client-generator-test/src/main/java/tsptest/maxoverload/protocol/implementation/package-info.java b/packages/http-client-java/generator/http-client-generator-test/src/main/java/tsptest/maxoverload/protocol/implementation/package-info.java new file mode 100644 index 00000000000..e333e2087e3 --- /dev/null +++ b/packages/http-client-java/generator/http-client-generator-test/src/main/java/tsptest/maxoverload/protocol/implementation/package-info.java @@ -0,0 +1,10 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. +// Code generated by Microsoft (R) TypeSpec Code Generator. + +/** + * + * Package containing the implementations for MaxOverload. + * + */ +package tsptest.maxoverload.protocol.implementation; diff --git a/packages/http-client-java/generator/http-client-generator-test/src/main/java/tsptest/maxoverload/protocol/models/CreateWidgetOptions.java b/packages/http-client-java/generator/http-client-generator-test/src/main/java/tsptest/maxoverload/protocol/models/CreateWidgetOptions.java new file mode 100644 index 00000000000..0fd91236459 --- /dev/null +++ b/packages/http-client-java/generator/http-client-generator-test/src/main/java/tsptest/maxoverload/protocol/models/CreateWidgetOptions.java @@ -0,0 +1,83 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. +// Code generated by Microsoft (R) TypeSpec Code Generator. + +package tsptest.maxoverload.protocol.models; + +import com.azure.core.annotation.Generated; +import com.azure.core.annotation.Immutable; +import com.azure.json.JsonReader; +import com.azure.json.JsonSerializable; +import com.azure.json.JsonToken; +import com.azure.json.JsonWriter; +import java.io.IOException; + +/** + * The CreateWidgetOptions model. + */ +@Immutable +public final class CreateWidgetOptions implements JsonSerializable { + /* + * The name property. + */ + @Generated + private final String name; + + /** + * Creates an instance of CreateWidgetOptions class. + * + * @param name the name value to set. + */ + @Generated + public CreateWidgetOptions(String name) { + this.name = name; + } + + /** + * Get the name property: The name property. + * + * @return the name value. + */ + @Generated + public String getName() { + return this.name; + } + + /** + * {@inheritDoc} + */ + @Generated + @Override + public JsonWriter toJson(JsonWriter jsonWriter) throws IOException { + jsonWriter.writeStartObject(); + jsonWriter.writeStringField("name", this.name); + return jsonWriter.writeEndObject(); + } + + /** + * Reads an instance of CreateWidgetOptions from the JsonReader. + * + * @param jsonReader The JsonReader being read. + * @return An instance of CreateWidgetOptions if the JsonReader was pointing to an instance of it, or null if it was + * pointing to JSON null. + * @throws IllegalStateException If the deserialized JSON object was missing any required properties. + * @throws IOException If an error occurs while reading the CreateWidgetOptions. + */ + @Generated + public static CreateWidgetOptions fromJson(JsonReader jsonReader) throws IOException { + return jsonReader.readObject(reader -> { + String name = null; + while (reader.nextToken() != JsonToken.END_OBJECT) { + String fieldName = reader.getFieldName(); + reader.nextToken(); + + if ("name".equals(fieldName)) { + name = reader.getString(); + } else { + reader.skipChildren(); + } + } + return new CreateWidgetOptions(name); + }); + } +} diff --git a/packages/http-client-java/generator/http-client-generator-test/src/main/java/tsptest/maxoverload/protocol/models/Widget.java b/packages/http-client-java/generator/http-client-generator-test/src/main/java/tsptest/maxoverload/protocol/models/Widget.java new file mode 100644 index 00000000000..8ff416c7e81 --- /dev/null +++ b/packages/http-client-java/generator/http-client-generator-test/src/main/java/tsptest/maxoverload/protocol/models/Widget.java @@ -0,0 +1,83 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. +// Code generated by Microsoft (R) TypeSpec Code Generator. + +package tsptest.maxoverload.protocol.models; + +import com.azure.core.annotation.Generated; +import com.azure.core.annotation.Immutable; +import com.azure.json.JsonReader; +import com.azure.json.JsonSerializable; +import com.azure.json.JsonToken; +import com.azure.json.JsonWriter; +import java.io.IOException; + +/** + * The Widget model. + */ +@Immutable +public final class Widget implements JsonSerializable { + /* + * The name property. + */ + @Generated + private final String name; + + /** + * Creates an instance of Widget class. + * + * @param name the name value to set. + */ + @Generated + private Widget(String name) { + this.name = name; + } + + /** + * Get the name property: The name property. + * + * @return the name value. + */ + @Generated + public String getName() { + return this.name; + } + + /** + * {@inheritDoc} + */ + @Generated + @Override + public JsonWriter toJson(JsonWriter jsonWriter) throws IOException { + jsonWriter.writeStartObject(); + jsonWriter.writeStringField("name", this.name); + return jsonWriter.writeEndObject(); + } + + /** + * Reads an instance of Widget from the JsonReader. + * + * @param jsonReader The JsonReader being read. + * @return An instance of Widget if the JsonReader was pointing to an instance of it, or null if it was pointing to + * JSON null. + * @throws IllegalStateException If the deserialized JSON object was missing any required properties. + * @throws IOException If an error occurs while reading the Widget. + */ + @Generated + public static Widget fromJson(JsonReader jsonReader) throws IOException { + return jsonReader.readObject(reader -> { + String name = null; + while (reader.nextToken() != JsonToken.END_OBJECT) { + String fieldName = reader.getFieldName(); + reader.nextToken(); + + if ("name".equals(fieldName)) { + name = reader.getString(); + } else { + reader.skipChildren(); + } + } + return new Widget(name); + }); + } +} diff --git a/packages/http-client-java/generator/http-client-generator-test/src/main/java/tsptest/maxoverload/protocol/models/package-info.java b/packages/http-client-java/generator/http-client-generator-test/src/main/java/tsptest/maxoverload/protocol/models/package-info.java new file mode 100644 index 00000000000..ff1a1a57db7 --- /dev/null +++ b/packages/http-client-java/generator/http-client-generator-test/src/main/java/tsptest/maxoverload/protocol/models/package-info.java @@ -0,0 +1,10 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. +// Code generated by Microsoft (R) TypeSpec Code Generator. + +/** + * + * Package containing the data models for MaxOverload. + * + */ +package tsptest.maxoverload.protocol.models; diff --git a/packages/http-client-java/generator/http-client-generator-test/src/main/java/tsptest/maxoverload/protocol/package-info.java b/packages/http-client-java/generator/http-client-generator-test/src/main/java/tsptest/maxoverload/protocol/package-info.java new file mode 100644 index 00000000000..1f0800c12e0 --- /dev/null +++ b/packages/http-client-java/generator/http-client-generator-test/src/main/java/tsptest/maxoverload/protocol/package-info.java @@ -0,0 +1,10 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. +// Code generated by Microsoft (R) TypeSpec Code Generator. + +/** + * + * Package containing the classes for MaxOverload. + * + */ +package tsptest.maxoverload.protocol; diff --git a/packages/http-client-java/generator/http-client-generator-test/src/main/resources/META-INF/tsptest-maxoverload-model_metadata.json b/packages/http-client-java/generator/http-client-generator-test/src/main/resources/META-INF/tsptest-maxoverload-model_metadata.json new file mode 100644 index 00000000000..3b79705f252 --- /dev/null +++ b/packages/http-client-java/generator/http-client-generator-test/src/main/resources/META-INF/tsptest-maxoverload-model_metadata.json @@ -0,0 +1 @@ +{"flavor":"Azure","apiVersions":{},"crossLanguagePackageId":"TspTest.MaxOverload","crossLanguageVersion":"89e8b26dabe3","crossLanguageDefinitions":{"tsptest.maxoverload.model.MaxOverloadAsyncClient":"TspTest.MaxOverload.MaxOverloadOperations","tsptest.maxoverload.model.MaxOverloadAsyncClient.createWidget":"TspTest.MaxOverload.MaxOverloadOperations.createWidget","tsptest.maxoverload.model.MaxOverloadAsyncClient.createWidgetWithResponse":"TspTest.MaxOverload.MaxOverloadOperations.createWidget","tsptest.maxoverload.model.MaxOverloadClient":"TspTest.MaxOverload.MaxOverloadOperations","tsptest.maxoverload.model.MaxOverloadClient.createWidget":"TspTest.MaxOverload.MaxOverloadOperations.createWidget","tsptest.maxoverload.model.MaxOverloadClient.createWidgetWithResponse":"TspTest.MaxOverload.MaxOverloadOperations.createWidget","tsptest.maxoverload.model.MaxOverloadClientBuilder":"TspTest.MaxOverload","tsptest.maxoverload.model.models.CreateWidgetOptions":"TspTest.MaxOverload.CreateWidgetOptions","tsptest.maxoverload.model.models.Widget":"TspTest.MaxOverload.Widget"},"generatedFiles":["src/main/java/module-info.java","src/main/java/tsptest/maxoverload/model/MaxOverloadAsyncClient.java","src/main/java/tsptest/maxoverload/model/MaxOverloadClient.java","src/main/java/tsptest/maxoverload/model/MaxOverloadClientBuilder.java","src/main/java/tsptest/maxoverload/model/implementation/MaxOverloadClientImpl.java","src/main/java/tsptest/maxoverload/model/implementation/MaxOverloadOperationsImpl.java","src/main/java/tsptest/maxoverload/model/implementation/package-info.java","src/main/java/tsptest/maxoverload/model/models/CreateWidgetOptions.java","src/main/java/tsptest/maxoverload/model/models/Widget.java","src/main/java/tsptest/maxoverload/model/models/package-info.java","src/main/java/tsptest/maxoverload/model/package-info.java"]} \ No newline at end of file diff --git a/packages/http-client-java/generator/http-client-generator-test/src/main/resources/META-INF/tsptest-maxoverload-protocol_metadata.json b/packages/http-client-java/generator/http-client-generator-test/src/main/resources/META-INF/tsptest-maxoverload-protocol_metadata.json new file mode 100644 index 00000000000..323c3e3b979 --- /dev/null +++ b/packages/http-client-java/generator/http-client-generator-test/src/main/resources/META-INF/tsptest-maxoverload-protocol_metadata.json @@ -0,0 +1 @@ +{"flavor":"Azure","apiVersions":{},"crossLanguagePackageId":"TspTest.MaxOverload","crossLanguageVersion":"89e8b26dabe3","crossLanguageDefinitions":{"tsptest.maxoverload.protocol.MaxOverloadAsyncClient":"TspTest.MaxOverload.MaxOverloadOperations","tsptest.maxoverload.protocol.MaxOverloadAsyncClient.createWidget":"TspTest.MaxOverload.MaxOverloadOperations.createWidget","tsptest.maxoverload.protocol.MaxOverloadAsyncClient.createWidgetWithResponse":"TspTest.MaxOverload.MaxOverloadOperations.createWidget","tsptest.maxoverload.protocol.MaxOverloadClient":"TspTest.MaxOverload.MaxOverloadOperations","tsptest.maxoverload.protocol.MaxOverloadClient.createWidget":"TspTest.MaxOverload.MaxOverloadOperations.createWidget","tsptest.maxoverload.protocol.MaxOverloadClient.createWidgetWithResponse":"TspTest.MaxOverload.MaxOverloadOperations.createWidget","tsptest.maxoverload.protocol.MaxOverloadClientBuilder":"TspTest.MaxOverload","tsptest.maxoverload.protocol.models.CreateWidgetOptions":"TspTest.MaxOverload.CreateWidgetOptions","tsptest.maxoverload.protocol.models.Widget":"TspTest.MaxOverload.Widget"},"generatedFiles":["src/main/java/module-info.java","src/main/java/tsptest/maxoverload/protocol/MaxOverloadAsyncClient.java","src/main/java/tsptest/maxoverload/protocol/MaxOverloadClient.java","src/main/java/tsptest/maxoverload/protocol/MaxOverloadClientBuilder.java","src/main/java/tsptest/maxoverload/protocol/implementation/MaxOverloadClientImpl.java","src/main/java/tsptest/maxoverload/protocol/implementation/MaxOverloadOperationsImpl.java","src/main/java/tsptest/maxoverload/protocol/implementation/package-info.java","src/main/java/tsptest/maxoverload/protocol/models/CreateWidgetOptions.java","src/main/java/tsptest/maxoverload/protocol/models/Widget.java","src/main/java/tsptest/maxoverload/protocol/models/package-info.java","src/main/java/tsptest/maxoverload/protocol/package-info.java"]} \ No newline at end of file diff --git a/packages/http-client-java/generator/http-client-generator-test/src/main/resources/tsptest-maxoverload-model.properties b/packages/http-client-java/generator/http-client-generator-test/src/main/resources/tsptest-maxoverload-model.properties new file mode 100644 index 00000000000..ca812989b4f --- /dev/null +++ b/packages/http-client-java/generator/http-client-generator-test/src/main/resources/tsptest-maxoverload-model.properties @@ -0,0 +1,2 @@ +name=${project.artifactId} +version=${project.version} diff --git a/packages/http-client-java/generator/http-client-generator-test/src/main/resources/tsptest-maxoverload-protocol.properties b/packages/http-client-java/generator/http-client-generator-test/src/main/resources/tsptest-maxoverload-protocol.properties new file mode 100644 index 00000000000..ca812989b4f --- /dev/null +++ b/packages/http-client-java/generator/http-client-generator-test/src/main/resources/tsptest-maxoverload-protocol.properties @@ -0,0 +1,2 @@ +name=${project.artifactId} +version=${project.version} diff --git a/packages/http-client-java/generator/http-client-generator-test/src/test/java/tsptest/maxoverload/model/generated/MaxOverloadClientTestBase.java b/packages/http-client-java/generator/http-client-generator-test/src/test/java/tsptest/maxoverload/model/generated/MaxOverloadClientTestBase.java new file mode 100644 index 00000000000..e9d485b44bf --- /dev/null +++ b/packages/http-client-java/generator/http-client-generator-test/src/test/java/tsptest/maxoverload/model/generated/MaxOverloadClientTestBase.java @@ -0,0 +1,34 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. +// Code generated by Microsoft (R) TypeSpec Code Generator. + +package tsptest.maxoverload.model.generated; + +// The Java test files under 'generated' package are generated for your reference. +// If you wish to modify these files, please copy them out of the 'generated' package, and modify there. +// See https://aka.ms/azsdk/dpg/java/tests for guide on adding a test. + +import com.azure.core.http.policy.HttpLogDetailLevel; +import com.azure.core.http.policy.HttpLogOptions; +import com.azure.core.test.TestMode; +import com.azure.core.test.TestProxyTestBase; +import com.azure.core.util.Configuration; +import tsptest.maxoverload.model.MaxOverloadClient; +import tsptest.maxoverload.model.MaxOverloadClientBuilder; + +class MaxOverloadClientTestBase extends TestProxyTestBase { + protected MaxOverloadClient maxOverloadClient; + + @Override + protected void beforeTest() { + MaxOverloadClientBuilder maxOverloadClientbuilder = new MaxOverloadClientBuilder() + .endpoint(Configuration.getGlobalConfiguration().get("ENDPOINT", "endpoint")) + .httpClient(getHttpClientOrUsePlayback(getHttpClients().findFirst().orElse(null))) + .httpLogOptions(new HttpLogOptions().setLogLevel(HttpLogDetailLevel.BASIC)); + if (getTestMode() == TestMode.RECORD) { + maxOverloadClientbuilder.addPolicy(interceptorManager.getRecordPolicy()); + } + maxOverloadClient = maxOverloadClientbuilder.buildClient(); + + } +} diff --git a/packages/http-client-java/generator/http-client-generator-test/src/test/java/tsptest/maxoverload/protocol/generated/MaxOverloadClientTestBase.java b/packages/http-client-java/generator/http-client-generator-test/src/test/java/tsptest/maxoverload/protocol/generated/MaxOverloadClientTestBase.java new file mode 100644 index 00000000000..f1e54663cec --- /dev/null +++ b/packages/http-client-java/generator/http-client-generator-test/src/test/java/tsptest/maxoverload/protocol/generated/MaxOverloadClientTestBase.java @@ -0,0 +1,34 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. +// Code generated by Microsoft (R) TypeSpec Code Generator. + +package tsptest.maxoverload.protocol.generated; + +// The Java test files under 'generated' package are generated for your reference. +// If you wish to modify these files, please copy them out of the 'generated' package, and modify there. +// See https://aka.ms/azsdk/dpg/java/tests for guide on adding a test. + +import com.azure.core.http.policy.HttpLogDetailLevel; +import com.azure.core.http.policy.HttpLogOptions; +import com.azure.core.test.TestMode; +import com.azure.core.test.TestProxyTestBase; +import com.azure.core.util.Configuration; +import tsptest.maxoverload.protocol.MaxOverloadClient; +import tsptest.maxoverload.protocol.MaxOverloadClientBuilder; + +class MaxOverloadClientTestBase extends TestProxyTestBase { + protected MaxOverloadClient maxOverloadClient; + + @Override + protected void beforeTest() { + MaxOverloadClientBuilder maxOverloadClientbuilder = new MaxOverloadClientBuilder() + .endpoint(Configuration.getGlobalConfiguration().get("ENDPOINT", "endpoint")) + .httpClient(getHttpClientOrUsePlayback(getHttpClients().findFirst().orElse(null))) + .httpLogOptions(new HttpLogOptions().setLogLevel(HttpLogDetailLevel.BASIC)); + if (getTestMode() == TestMode.RECORD) { + maxOverloadClientbuilder.addPolicy(interceptorManager.getRecordPolicy()); + } + maxOverloadClient = maxOverloadClientbuilder.buildClient(); + + } +} From fb1565fee655584663ed97c6d703a3c89023ea9e Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Thu, 21 May 2026 07:41:34 +0000 Subject: [PATCH 7/7] fix(java): return model response types for max-overload model withresponse Agent-Logs-Url: https://github.com/microsoft/typespec/sessions/48392fa5-7032-4b09-bc91-47f9a7453ac8 Co-authored-by: srnagar <51379715+srnagar@users.noreply.github.com> --- .../generator/core/mapper/ClientMethodMapper.java | 4 ++++ .../core/mapper/ClientMethodMapperTests.java | 11 +++++++++++ .../maxoverload/model/MaxOverloadAsyncClient.java | 10 ++++++---- .../tsptest/maxoverload/model/MaxOverloadClient.java | 9 ++++++--- 4 files changed, 27 insertions(+), 7 deletions(-) diff --git a/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/mapper/ClientMethodMapper.java b/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/mapper/ClientMethodMapper.java index f8ddf8609ea..b65f336c096 100644 --- a/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/mapper/ClientMethodMapper.java +++ b/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/mapper/ClientMethodMapper.java @@ -966,6 +966,10 @@ protected JavaVisibility methodVisibility(ClientMethodType methodType, MethodOve return NOT_GENERATE; } + if (settings.isGenerateModelMaxOverload() && isSimpleWithResponseType(methodType)) { + return NOT_VISIBLE; + } + if (methodType == ClientMethodType.PagingAsyncSinglePage || (methodType == ClientMethodType.PagingSyncSinglePage && settings.isSyncStackEnabled())) { return NOT_VISIBLE; diff --git a/packages/http-client-java/generator/http-client-generator-core/src/test/java/com/microsoft/typespec/http/client/generator/core/mapper/ClientMethodMapperTests.java b/packages/http-client-java/generator/http-client-generator-core/src/test/java/com/microsoft/typespec/http/client/generator/core/mapper/ClientMethodMapperTests.java index 01bdfa028d0..5340b53dd45 100644 --- a/packages/http-client-java/generator/http-client-generator-core/src/test/java/com/microsoft/typespec/http/client/generator/core/mapper/ClientMethodMapperTests.java +++ b/packages/http-client-java/generator/http-client-generator-core/src/test/java/com/microsoft/typespec/http/client/generator/core/mapper/ClientMethodMapperTests.java @@ -249,6 +249,17 @@ public void maxOverloadModelGeneratesModelWithResponseWithRequestOptions() { mapper.methodVisibilityForTest(ClientMethodType.SimpleSyncRestResponse, false, false)); } + @Test + public void maxOverloadModelHidesProtocolWithResponse() { + configureDataPlaneSettings("model"); + TestClientMethodMapper mapper = new TestClientMethodMapper(); + + Assertions.assertEquals(JavaVisibility.Private, + mapper.methodVisibilityForTest(ClientMethodType.SimpleSyncRestResponse, true, true)); + Assertions.assertEquals(JavaVisibility.Private, + mapper.methodVisibilityForTest(ClientMethodType.SimpleAsyncRestResponse, true, true)); + } + private Operation operationWithNoDesc() { return operationWithDescOnOperationAndResponseSchema(null, null); } diff --git a/packages/http-client-java/generator/http-client-generator-test/src/main/java/tsptest/maxoverload/model/MaxOverloadAsyncClient.java b/packages/http-client-java/generator/http-client-generator-test/src/main/java/tsptest/maxoverload/model/MaxOverloadAsyncClient.java index f5d11b2ba76..d2212842b62 100644 --- a/packages/http-client-java/generator/http-client-generator-test/src/main/java/tsptest/maxoverload/model/MaxOverloadAsyncClient.java +++ b/packages/http-client-java/generator/http-client-generator-test/src/main/java/tsptest/maxoverload/model/MaxOverloadAsyncClient.java @@ -71,8 +71,11 @@ public final class MaxOverloadAsyncClient { */ @Generated @ServiceMethod(returns = ReturnType.SINGLE) - public Mono> createWidgetWithResponse(BinaryData body, RequestOptions requestOptions) { - return this.serviceClient.createWidgetWithResponseAsync(body, requestOptions); + public Mono> createWidgetWithResponse(BinaryData body, RequestOptions requestOptions) { + return this.serviceClient.createWidgetWithResponseAsync(body, requestOptions) + .map(protocolMethodResponse -> new com.azure.core.http.rest.SimpleResponse<>(protocolMethodResponse.getRequest(), + protocolMethodResponse.getStatusCode(), protocolMethodResponse.getHeaders(), + protocolMethodResponse.getValue().toObject(Widget.class))); } /** @@ -92,7 +95,6 @@ public Mono> createWidgetWithResponse(BinaryData body, Requ public Mono createWidget(CreateWidgetOptions body) { // Generated convenience method for createWidgetWithResponse RequestOptions requestOptions = new RequestOptions(); - return createWidgetWithResponse(BinaryData.fromObject(body), requestOptions).flatMap(FluxUtil::toMono) - .map(protocolMethodData -> protocolMethodData.toObject(Widget.class)); + return createWidgetWithResponse(BinaryData.fromObject(body), requestOptions).flatMap(FluxUtil::toMono); } } diff --git a/packages/http-client-java/generator/http-client-generator-test/src/main/java/tsptest/maxoverload/model/MaxOverloadClient.java b/packages/http-client-java/generator/http-client-generator-test/src/main/java/tsptest/maxoverload/model/MaxOverloadClient.java index b0cf6f11026..f44a7044808 100644 --- a/packages/http-client-java/generator/http-client-generator-test/src/main/java/tsptest/maxoverload/model/MaxOverloadClient.java +++ b/packages/http-client-java/generator/http-client-generator-test/src/main/java/tsptest/maxoverload/model/MaxOverloadClient.java @@ -69,8 +69,11 @@ public final class MaxOverloadClient { */ @Generated @ServiceMethod(returns = ReturnType.SINGLE) - public Response createWidgetWithResponse(BinaryData body, RequestOptions requestOptions) { - return this.serviceClient.createWidgetWithResponse(body, requestOptions); + public Response createWidgetWithResponse(BinaryData body, RequestOptions requestOptions) { + Response protocolMethodResponse = this.serviceClient.createWidgetWithResponse(body, requestOptions); + return new com.azure.core.http.rest.SimpleResponse<>(protocolMethodResponse.getRequest(), + protocolMethodResponse.getStatusCode(), protocolMethodResponse.getHeaders(), + protocolMethodResponse.getValue().toObject(Widget.class)); } /** @@ -90,6 +93,6 @@ public Response createWidgetWithResponse(BinaryData body, RequestOpt public Widget createWidget(CreateWidgetOptions body) { // Generated convenience method for createWidgetWithResponse RequestOptions requestOptions = new RequestOptions(); - return createWidgetWithResponse(BinaryData.fromObject(body), requestOptions).getValue().toObject(Widget.class); + return createWidgetWithResponse(BinaryData.fromObject(body), requestOptions).getValue(); } }