Skip to content

Commit afc912c

Browse files
committed
rebase with upstream/merging corev2 changes
1 parent 6a4281e commit afc912c

File tree

8 files changed

+97
-74
lines changed

8 files changed

+97
-74
lines changed

packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/extension/plugin/JavaSettings.java

Lines changed: 36 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@
66
import com.azure.json.JsonProviders;
77
import com.azure.json.JsonReader;
88
import com.azure.json.JsonToken;
9+
import com.microsoft.typespec.http.client.generator.core.mapper.Mappers;
10+
import com.microsoft.typespec.http.client.generator.core.mapper.azurevnext.AzureVNextMapperFactory;
11+
import com.microsoft.typespec.http.client.generator.core.mapper.clientcore.ClientCoreMapperFactory;
12+
import com.microsoft.typespec.http.client.generator.core.template.Templates;
13+
import com.microsoft.typespec.http.client.generator.core.template.azurevnext.AzureVNextTemplateFactory;
14+
import com.microsoft.typespec.http.client.generator.core.template.clientcore.ClientCoreTemplateFactory;
915
import java.io.IOException;
1016
import java.util.ArrayList;
1117
import java.util.Arrays;
@@ -33,6 +39,7 @@ public class JavaSettings {
3339
private final String flavor;
3440
private final boolean noCustomHeaders;
3541
private final boolean disableTypedHeadersMethods;
42+
private final boolean useRestProxy;
3643

3744
static void setHeader(String value) {
3845
if ("MICROSOFT_MIT".equals(value)) {
@@ -137,9 +144,6 @@ private JavaSettings(AutorestSettings autorestSettings) {
137144
this.modelerSettings = new ModelerSettings(
138145
host.getValueWithJsonReader("modelerfour", jsonReader -> jsonReader.readMap(JsonReader::readUntyped)));
139146

140-
// Whether to generate the Azure.
141-
this.azure = getBooleanValue(host, "azure-arm", false);
142-
143147
// Whether to generate the SDK integration.
144148
this.sdkIntegration = getBooleanValue(host, "sdk-integration", false);
145149

@@ -178,7 +182,9 @@ private JavaSettings(AutorestSettings autorestSettings) {
178182
// The brand name we use to generate SDK.
179183
this.flavor = getStringValue(host, "flavor", "azure");
180184

181-
this.modelsSubpackage = getStringValue(host, "models-subpackage", isBranded(this.flavor) ? "models" : "");
185+
updateFlavorFactories();
186+
187+
this.modelsSubpackage = getStringValue(host, "models-subpackage", isAzureV1() || isAzureV2() ? "models" : "");
182188

183189
// The custom types that will be generated.
184190
String customTypes = getStringValue(host, "custom-types", "");
@@ -287,6 +293,8 @@ private JavaSettings(AutorestSettings autorestSettings) {
287293
// Whether to generate tests.
288294
this.generateTests = getBooleanValue(host, "generate-tests", false);
289295

296+
this.useRestProxy = getBooleanValue(host, "use-rest-proxy", false);
297+
290298
// Whether to generate the send request method.
291299
this.generateSendRequestMethod = false;
292300

@@ -380,19 +388,37 @@ private JavaSettings(AutorestSettings autorestSettings) {
380388
this.useObjectForUnknown = getBooleanValue(host, "use-object-for-unknown", false);
381389
}
382390

391+
private void updateFlavorFactories() {
392+
if (isAzureV2()) {
393+
Mappers.setFactory(new AzureVNextMapperFactory());
394+
Templates.setFactory(new AzureVNextTemplateFactory());
395+
} else if (!isAzureV1()) {
396+
Mappers.setFactory(new ClientCoreMapperFactory());
397+
Templates.setFactory(new ClientCoreTemplateFactory());
398+
}
399+
}
400+
383401
/**
384-
* Whether to generate with Azure branding.
402+
* Whether to generate with Azure V1.
385403
*
386-
* @return Whether to generate with Azure branding.
404+
* @return Whether to generate with Azure V1.
387405
*/
388-
public boolean isBranded() {
389-
return isBranded(this.flavor);
406+
public boolean isAzureV1() {
407+
return isAzureV1(this.flavor);
390408
}
391409

392-
private static boolean isBranded(String flavor) {
410+
private static boolean isAzureV1(String flavor) {
393411
return "azure".equalsIgnoreCase(flavor);
394412
}
395413

414+
public boolean isAzureV2() {
415+
return "azurev2".equalsIgnoreCase(this.flavor);
416+
}
417+
418+
public boolean useRestProxy() {
419+
return this.useRestProxy;
420+
}
421+
396422
private final String keyCredentialHeaderName;
397423

398424
/**
@@ -426,17 +452,6 @@ public Set<String> getCredentialScopes() {
426452
return credentialScopes;
427453
}
428454

429-
private final boolean azure;
430-
431-
/**
432-
* Whether to generate the Azure.
433-
*
434-
* @return Whether to generate the Azure.
435-
*/
436-
public final boolean isAzure() {
437-
return azure;
438-
}
439-
440455
private final String artifactId;
441456

442457
/**
@@ -560,15 +575,6 @@ public final boolean isFluentPremium() {
560575
return fluent == Fluent.PREMIUM;
561576
}
562577

563-
/**
564-
* Whether to generate the Azure or Fluent.
565-
*
566-
* @return Whether to generate the Azure or Fluent.
567-
*/
568-
public final boolean isAzureOrFluent() {
569-
return isAzure() || isFluent();
570-
}
571-
572578
// configure for model flatten in client
573579

574580
/**
@@ -1156,7 +1162,7 @@ public boolean isDataPlaneClient() {
11561162
* @return Whether the client is a vanilla client.
11571163
*/
11581164
public boolean isVanilla() {
1159-
return isBranded() && !isDataPlaneClient() && !isFluent();
1165+
return isAzureV1() && !isDataPlaneClient() && !isFluent();
11601166
}
11611167

11621168
private final boolean useIterable;

packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/extension/plugin/PollingSettings.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,13 @@ public PollingSettings() {
6464
= String.format(INSTANTIATE_POLLING_STRATEGY_FORMAT, "SyncDefaultPollingStrategy");
6565
}
6666

67+
public static final String DEFAULT_CLIENTCORE_POLLING_STRATEGY_FORMAT
68+
= String.join("\n", "new %s<>(new PollingStrategyOptions({httpPipeline})", " .setEndpoint({endpoint})",
69+
" .setRequestContext({context})", " .setServiceVersion({serviceVersion}))");
70+
71+
private static final String DEFAULT_CLIENTCORE_POLLING_CODE
72+
= String.format(DEFAULT_CLIENTCORE_POLLING_STRATEGY_FORMAT, "DefaultPollingStrategy");
73+
6774
/**
6875
* Gets the strategy for polling.
6976
* <p>
@@ -90,6 +97,9 @@ public String getPollingStrategy() {
9097
*/
9198
public String getSyncPollingStrategy() {
9299
if (syncPollingStrategy == null || "default".equalsIgnoreCase(syncPollingStrategy)) {
100+
if (JavaSettings.getInstance().isAzureV2()) {
101+
return DEFAULT_CLIENTCORE_POLLING_CODE;
102+
}
93103
return INSTANTIATE_DEFAULT_SYNC_POLLING_STRATEGY;
94104
} else {
95105
return syncPollingStrategy;

packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/mapper/ClientMethodMapper.java

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import com.microsoft.typespec.http.client.generator.core.extension.model.codemodel.Parameter;
1010
import com.microsoft.typespec.http.client.generator.core.extension.model.codemodel.Request;
1111
import com.microsoft.typespec.http.client.generator.core.extension.model.codemodel.RequestParameterLocation;
12+
import com.microsoft.typespec.http.client.generator.core.extension.model.extensionmodel.XmsPageable;
1213
import com.microsoft.typespec.http.client.generator.core.extension.plugin.JavaSettings;
1314
import com.microsoft.typespec.http.client.generator.core.extension.plugin.JavaSettings.SyncMethodsGeneration;
1415
import com.microsoft.typespec.http.client.generator.core.model.clientmodel.ClassType;
@@ -143,7 +144,9 @@ private List<ClientMethod> createClientMethods(Operation operation, boolean isPr
143144
for (Request request : requests) {
144145
final List<ProxyMethod> proxyMethods = proxyMethodsMap.get(request);
145146
for (ProxyMethod proxyMethod : proxyMethods) {
146-
147+
if (proxyMethod.getImplementation() != null) {
148+
continue;
149+
}
147150
final List<Parameter> codeModelParameters
148151
= getCodeModelParameters(request, operation, isProtocolMethod);
149152
final ClientMethodParameterProcessor.Result result = ClientMethodParameterProcessor.process(request,
@@ -900,7 +903,7 @@ protected JavaVisibility methodVisibility(ClientMethodType methodType, MethodOve
900903
if (isProtocolMethod) {
901904
/*
902905
* Rule for DPG protocol method
903-
*
906+
*
904907
* 1. Only generate "WithResponse" method for simple API (hence exclude SimpleAsync and SimpleSync).
905908
* 2. For sync method, Context is included in "RequestOptions", hence do not generate method with
906909
* Context parameter.
@@ -966,7 +969,7 @@ protected void addClientMethodWithContext(List<ClientMethod> methods, ClientMeth
966969
JavaVisibility visibility, boolean isProtocolMethod) {
967970
final ClientMethodParameter contextParameter = getContextParameter(isProtocolMethod);
968971
final List<ClientMethodParameter> parameters = new ArrayList<>(baseMethod.getParameters());
969-
if (JavaSettings.getInstance().isBranded()
972+
if (JavaSettings.getInstance().isAzureV1()
970973
|| contextParameter.getClientType().equals(ClassType.REQUEST_OPTIONS)) {
971974
// update parameters to include Context.
972975
parameters.add(contextParameter);
@@ -992,6 +995,12 @@ private static MethodNamer resolveMethodNamer(ProxyMethod proxyMethod, Convenien
992995
}
993996
}
994997

998+
protected static MethodPageDetails.ContinuationToken fromContinuationToken(XmsPageable xmsPageable,
999+
IType responseBodyType) {
1000+
// TODO: anu remove this method once ClientCoreClientMethodMapper is refactored similar to ClientMethodMapper
1001+
return PagingMetadata.getContinuationToken(xmsPageable, responseBodyType);
1002+
}
1003+
9951004
/**
9961005
* Type holding immutable common arguments shared across all client method creator functions.
9971006
*/

packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/mapper/ClientMethodsReturnDescription.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -231,9 +231,9 @@ public ReturnValue getReturnValue(ClientMethodType methodType, MethodPollingDeta
231231
syncReturnType.asNullable());
232232
return createReturnValue(returnType, syncReturnType);
233233
} else if (settings.isAzureV2()) {
234-
IType returnType = GenericType.AzureVNextPoller(pollingDetails.getIntermediateType(),
235-
pollingDetails.getFinalType());
236-
return createReturnValue(returnType, pollingDetails.getFinalType());
234+
IType returnType = GenericType.AzureVNextPoller(pollingDetails.getPollResultType(),
235+
pollingDetails.getFinalResultType());
236+
return createReturnValue(returnType, pollingDetails.getFinalResultType());
237237
} else {
238238
IType returnType = GenericType.SyncPoller(pollingDetails.getPollResultType(),
239239
pollingDetails.getFinalResultType());

packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/mapper/PagingMetadata.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,8 +138,8 @@ private static ModelPropertySegment getPageableNextLink(XmsPageable xmsPageable,
138138
return ClientModelUtil.getModelPropertySegment(responseBodyType, xmsPageable.getNextLinkName());
139139
}
140140

141-
private static MethodPageDetails.ContinuationToken getContinuationToken(XmsPageable xmsPageable,
142-
IType responseBodyType) {
141+
static MethodPageDetails.ContinuationToken getContinuationToken(XmsPageable xmsPageable, IType responseBodyType) {
142+
// TODO: anu change this method to 'private' once ClientCoreClientMethodMapper is refactored.
143143
final PageableContinuationToken rawContinuationToken = xmsPageable.getContinuationToken();
144144
if (rawContinuationToken == null) {
145145
return null;

0 commit comments

Comments
 (0)