Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tsp, dpg, keep enum type for client parameters, not change it to string type #4911

Draft
wants to merge 11 commits into
base: main
Choose a base branch
from
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ export class CodeModelBuilder {
} else {
const schema = this.processSchema(arg.type, arg.name);
this.trackSchemaUsage(schema, {
usage: [SchemaContext.Input, SchemaContext.Output /*SchemaContext.Public*/],
usage: [SchemaContext.Input, SchemaContext.Output, SchemaContext.Public],
});
parameter = new Parameter(arg.name, arg.doc ?? "", schema, {
implementation: ImplementationLocation.Client,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,10 @@ public ProxyMethodParameter map(Parameter parameter) {

IType clientType = wireType.getClientType();

if (settings.isDataPlaneClient()) {
if (isRemoveModelFromParameter(parameter, clientType)) {
clientType = SchemaUtil.removeModelFromParameter(parameterRequestLocation, clientType);
}

builder.clientType(clientType);

if (wireType instanceof ListType
Expand All @@ -88,7 +89,7 @@ public ProxyMethodParameter map(Parameter parameter) {
if (parameterRequestLocation
!= RequestParameterLocation.BODY /* && parameterRequestLocation != RequestParameterLocation.FormData */) {
wireType = ClassType.STRING;
} else if (settings.isDataPlaneClient()) {
} else if (isRemoveModelFromParameter(parameter, wireType)) {
wireType = SchemaUtil.removeModelFromParameter(parameterRequestLocation, wireType);
}
} else if (wireType instanceof ListType
Expand All @@ -102,7 +103,7 @@ public ProxyMethodParameter map(Parameter parameter) {
} else {
wireType = ClassType.STRING;
}
} else if (settings.isDataPlaneClient()) {
} else if (isRemoveModelFromParameter(parameter, wireType)) {
wireType = SchemaUtil.removeModelFromParameter(parameterRequestLocation, wireType);
}
builder.wireType(wireType);
Expand Down Expand Up @@ -182,6 +183,10 @@ public ProxyMethodParameter map(Parameter parameter) {
return builder.build();
}

protected boolean isRemoveModelFromParameter(Parameter parameter, IType clientType) {
return JavaSettings.getInstance().isDataPlaneClient();
}

protected ProxyMethodParameter.Builder createProxyMethodParameterBuilder() {
return new ProxyMethodParameter.Builder();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ protected List<ServiceClientProperty> processClientProperties(Client client, Str
String serviceClientPropertyName = CodeNamer.getPropertyName(p.getLanguage().getJava().getName());

IType serviceClientPropertyClientType = Mappers.getSchemaMapper().map(p.getSchema());
if (settings.isDataPlaneClient()) {
if (isRemoveModelFromParameter(p, serviceClientPropertyClientType)) {
// mostly for Enum to String
serviceClientPropertyClientType = SchemaUtil.removeModelFromParameter(RequestParameterLocation.URI,
serviceClientPropertyClientType);
Expand Down Expand Up @@ -281,6 +281,10 @@ protected List<ServiceClientProperty> processClientProperties(Client client, Str
return serviceClientProperties;
}

protected boolean isRemoveModelFromParameter(Parameter parameter, IType type) {
return JavaSettings.getInstance().isDataPlaneClient();
}

protected void processParametersAndConstructors(ServiceClient.Builder builder, Client client, CodeModel codeModel,
List<ServiceClientProperty> serviceClientProperties, Proxy proxy) {
JavaSettings settings = JavaSettings.getInstance();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import com.microsoft.typespec.http.client.generator.core.extension.model.codemodel.Scheme;
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.EnumType;
import com.microsoft.typespec.http.client.generator.core.model.clientmodel.ServiceClient;
import com.microsoft.typespec.http.client.generator.core.model.clientmodel.ServiceClientProperty;
import com.microsoft.typespec.http.client.generator.core.model.clientmodel.TestContext;
Expand Down Expand Up @@ -83,9 +84,21 @@ public ProtocolTestWriter(TestContext testContext) {
String defaultValueExpression = serviceClientProperty.getDefaultValueExpression();
String expr;
if (defaultValueExpression == null) {
expr = String.format("Configuration.getGlobalConfiguration().get(\"%1$s\", %2$s)",
serviceClientProperty.getName().toUpperCase(Locale.ROOT), ClassType.STRING
.defaultValueExpression(serviceClientProperty.getName().toLowerCase(Locale.ROOT)));
if (serviceClientProperty.getType() instanceof EnumType) {
// TODO: handle non-string enum
expr = String.format(
"%1$s.fromString(Configuration.getGlobalConfiguration().get(\"%2$s\", %3$s))",
serviceClientProperty.getType(),
serviceClientProperty.getName().toUpperCase(Locale.ROOT),
ClassType.STRING.defaultValueExpression(
serviceClientProperty.getName().toLowerCase(Locale.ROOT)));

} else {
expr = String.format("Configuration.getGlobalConfiguration().get(\"%1$s\", %2$s)",
serviceClientProperty.getName().toUpperCase(Locale.ROOT),
ClassType.STRING.defaultValueExpression(
serviceClientProperty.getName().toLowerCase(Locale.ROOT)));
}
} else {
expr = String.format("Configuration.getGlobalConfiguration().get(\"%1$s\", %2$s)",
serviceClientProperty.getName().toUpperCase(Locale.ROOT), defaultValueExpression);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package client.structure.service;

import client.structure.service.implementation.ClientAClientImpl;
import client.structure.service.models.ClientType;
import com.azure.core.annotation.Generated;
import com.azure.core.annotation.ServiceClientBuilder;
import com.azure.core.client.traits.ConfigurationTrait;
Expand Down Expand Up @@ -194,7 +195,7 @@ public ClientAClientBuilder endpoint(String endpoint) {
* Need to be set as 'default', 'multi-client', 'renamed-operation', 'two-operation-group' in client.
*/
@Generated
private String client;
private ClientType client;

/**
* Sets Need to be set as 'default', 'multi-client', 'renamed-operation', 'two-operation-group' in client.
Expand All @@ -203,7 +204,7 @@ public ClientAClientBuilder endpoint(String endpoint) {
* @return the ClientAClientBuilder.
*/
@Generated
public ClientAClientBuilder client(String client) {
public ClientAClientBuilder client(ClientType client) {
this.client = client;
return this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package client.structure.service;

import client.structure.service.implementation.ClientBClientImpl;
import client.structure.service.models.ClientType;
import com.azure.core.annotation.Generated;
import com.azure.core.annotation.ServiceClientBuilder;
import com.azure.core.client.traits.ConfigurationTrait;
Expand Down Expand Up @@ -194,7 +195,7 @@ public ClientBClientBuilder endpoint(String endpoint) {
* Need to be set as 'default', 'multi-client', 'renamed-operation', 'two-operation-group' in client.
*/
@Generated
private String client;
private ClientType client;

/**
* Sets Need to be set as 'default', 'multi-client', 'renamed-operation', 'two-operation-group' in client.
Expand All @@ -203,7 +204,7 @@ public ClientBClientBuilder endpoint(String endpoint) {
* @return the ClientBClientBuilder.
*/
@Generated
public ClientBClientBuilder client(String client) {
public ClientBClientBuilder client(ClientType client) {
this.client = client;
return this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package client.structure.service;

import client.structure.service.implementation.FirstClientImpl;
import client.structure.service.models.ClientType;
import com.azure.core.annotation.Generated;
import com.azure.core.annotation.ServiceClientBuilder;
import com.azure.core.client.traits.ConfigurationTrait;
Expand Down Expand Up @@ -201,7 +202,7 @@ public FirstClientBuilder endpoint(String endpoint) {
* Need to be set as 'default', 'multi-client', 'renamed-operation', 'two-operation-group' in client.
*/
@Generated
private String client;
private ClientType client;

/**
* Sets Need to be set as 'default', 'multi-client', 'renamed-operation', 'two-operation-group' in client.
Expand All @@ -210,7 +211,7 @@ public FirstClientBuilder endpoint(String endpoint) {
* @return the FirstClientBuilder.
*/
@Generated
public FirstClientBuilder client(String client) {
public FirstClientBuilder client(ClientType client) {
this.client = client;
return this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package client.structure.service;

import client.structure.service.implementation.RenamedOperationClientImpl;
import client.structure.service.models.ClientType;
import com.azure.core.annotation.Generated;
import com.azure.core.annotation.ServiceClientBuilder;
import com.azure.core.client.traits.ConfigurationTrait;
Expand Down Expand Up @@ -199,7 +200,7 @@ public RenamedOperationClientBuilder endpoint(String endpoint) {
* Need to be set as 'default', 'multi-client', 'renamed-operation', 'two-operation-group' in client.
*/
@Generated
private String client;
private ClientType client;

/**
* Sets Need to be set as 'default', 'multi-client', 'renamed-operation', 'two-operation-group' in client.
Expand All @@ -208,7 +209,7 @@ public RenamedOperationClientBuilder endpoint(String endpoint) {
* @return the RenamedOperationClientBuilder.
*/
@Generated
public RenamedOperationClientBuilder client(String client) {
public RenamedOperationClientBuilder client(ClientType client) {
this.client = client;
return this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package client.structure.service;

import client.structure.service.implementation.ServiceClientClientImpl;
import client.structure.service.models.ClientType;
import com.azure.core.annotation.Generated;
import com.azure.core.annotation.ServiceClientBuilder;
import com.azure.core.client.traits.ConfigurationTrait;
Expand Down Expand Up @@ -207,7 +208,7 @@ public ServiceClientClientBuilder endpoint(String endpoint) {
* Need to be set as 'default', 'multi-client', 'renamed-operation', 'two-operation-group' in client.
*/
@Generated
private String client;
private ClientType client;

/**
* Sets Need to be set as 'default', 'multi-client', 'renamed-operation', 'two-operation-group' in client.
Expand All @@ -216,7 +217,7 @@ public ServiceClientClientBuilder endpoint(String endpoint) {
* @return the ServiceClientClientBuilder.
*/
@Generated
public ServiceClientClientBuilder client(String client) {
public ServiceClientClientBuilder client(ClientType client) {
this.client = client;
return this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package client.structure.service;

import client.structure.service.implementation.TwoOperationGroupClientImpl;
import client.structure.service.models.ClientType;
import com.azure.core.annotation.Generated;
import com.azure.core.annotation.ServiceClientBuilder;
import com.azure.core.client.traits.ConfigurationTrait;
Expand Down Expand Up @@ -195,7 +196,7 @@ public TwoOperationGroupClientBuilder endpoint(String endpoint) {
* Need to be set as 'default', 'multi-client', 'renamed-operation', 'two-operation-group' in client.
*/
@Generated
private String client;
private ClientType client;

/**
* Sets Need to be set as 'default', 'multi-client', 'renamed-operation', 'two-operation-group' in client.
Expand All @@ -204,7 +205,7 @@ public TwoOperationGroupClientBuilder endpoint(String endpoint) {
* @return the TwoOperationGroupClientBuilder.
*/
@Generated
public TwoOperationGroupClientBuilder client(String client) {
public TwoOperationGroupClientBuilder client(ClientType client) {
this.client = client;
return this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

package client.structure.service.implementation;

import client.structure.service.models.ClientType;
import com.azure.core.annotation.ExpectedResponses;
import com.azure.core.annotation.Host;
import com.azure.core.annotation.HostParam;
Expand Down Expand Up @@ -60,7 +61,7 @@ public interface BarsService {
@UnexpectedResponseExceptionType(value = ResourceNotFoundException.class, code = { 404 })
@UnexpectedResponseExceptionType(value = ResourceModifiedException.class, code = { 409 })
@UnexpectedResponseExceptionType(HttpResponseException.class)
Mono<Response<Void>> five(@HostParam("endpoint") String endpoint, @HostParam("client") String client,
Mono<Response<Void>> five(@HostParam("endpoint") String endpoint, @HostParam("client") ClientType client,
RequestOptions requestOptions, Context context);

@Post("/five")
Expand All @@ -69,7 +70,7 @@ Mono<Response<Void>> five(@HostParam("endpoint") String endpoint, @HostParam("cl
@UnexpectedResponseExceptionType(value = ResourceNotFoundException.class, code = { 404 })
@UnexpectedResponseExceptionType(value = ResourceModifiedException.class, code = { 409 })
@UnexpectedResponseExceptionType(HttpResponseException.class)
Response<Void> fiveSync(@HostParam("endpoint") String endpoint, @HostParam("client") String client,
Response<Void> fiveSync(@HostParam("endpoint") String endpoint, @HostParam("client") ClientType client,
RequestOptions requestOptions, Context context);

@Post("/six")
Expand All @@ -78,7 +79,7 @@ Response<Void> fiveSync(@HostParam("endpoint") String endpoint, @HostParam("clie
@UnexpectedResponseExceptionType(value = ResourceNotFoundException.class, code = { 404 })
@UnexpectedResponseExceptionType(value = ResourceModifiedException.class, code = { 409 })
@UnexpectedResponseExceptionType(HttpResponseException.class)
Mono<Response<Void>> six(@HostParam("endpoint") String endpoint, @HostParam("client") String client,
Mono<Response<Void>> six(@HostParam("endpoint") String endpoint, @HostParam("client") ClientType client,
RequestOptions requestOptions, Context context);

@Post("/six")
Expand All @@ -87,7 +88,7 @@ Mono<Response<Void>> six(@HostParam("endpoint") String endpoint, @HostParam("cli
@UnexpectedResponseExceptionType(value = ResourceNotFoundException.class, code = { 404 })
@UnexpectedResponseExceptionType(value = ResourceModifiedException.class, code = { 409 })
@UnexpectedResponseExceptionType(HttpResponseException.class)
Response<Void> sixSync(@HostParam("endpoint") String endpoint, @HostParam("client") String client,
Response<Void> sixSync(@HostParam("endpoint") String endpoint, @HostParam("client") ClientType client,
RequestOptions requestOptions, Context context);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

package client.structure.service.implementation;

import client.structure.service.models.ClientType;
import com.azure.core.annotation.ExpectedResponses;
import com.azure.core.annotation.Host;
import com.azure.core.annotation.HostParam;
Expand Down Expand Up @@ -60,7 +61,7 @@ public interface BazFoosService {
@UnexpectedResponseExceptionType(value = ResourceNotFoundException.class, code = { 404 })
@UnexpectedResponseExceptionType(value = ResourceModifiedException.class, code = { 409 })
@UnexpectedResponseExceptionType(HttpResponseException.class)
Mono<Response<Void>> seven(@HostParam("endpoint") String endpoint, @HostParam("client") String client,
Mono<Response<Void>> seven(@HostParam("endpoint") String endpoint, @HostParam("client") ClientType client,
RequestOptions requestOptions, Context context);

@Post("/seven")
Expand All @@ -69,7 +70,7 @@ Mono<Response<Void>> seven(@HostParam("endpoint") String endpoint, @HostParam("c
@UnexpectedResponseExceptionType(value = ResourceNotFoundException.class, code = { 404 })
@UnexpectedResponseExceptionType(value = ResourceModifiedException.class, code = { 409 })
@UnexpectedResponseExceptionType(HttpResponseException.class)
Response<Void> sevenSync(@HostParam("endpoint") String endpoint, @HostParam("client") String client,
Response<Void> sevenSync(@HostParam("endpoint") String endpoint, @HostParam("client") ClientType client,
RequestOptions requestOptions, Context context);
}

Expand Down
Loading
Loading