Skip to content

Commit

Permalink
Generic core update (#2607)
Browse files Browse the repository at this point in the history
* Update codgen to support latest generic APIs

* latest updates from generic core

* update pom to add repositories section

* fix checkstyle

* fix annotation

* fix formatting

* regen
  • Loading branch information
srnagar authored Mar 7, 2024
1 parent 9faafda commit 74f51a7
Show file tree
Hide file tree
Showing 33 changed files with 673 additions and 147 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ public static JavaSettings getInstance() {
getBooleanValue(host, "include-read-only-in-constructor-args", false),
// setting the default as true as the Java design guideline recommends using String for URLs.
getBooleanValue(host, "url-as-string", true),
getBooleanValue(host, "uuid-as-string", false),

// setting this to false by default as a lot of existing libraries still use swagger and
// were generated with required = true set in JsonProperty annotation
Expand Down Expand Up @@ -320,6 +321,7 @@ private JavaSettings(AutorestSettings autorestSettings,
boolean noCustomHeaders,
boolean includeReadOnlyInConstructorArgs,
boolean urlAsString,
boolean uuidAsString,
boolean disableRequiredPropertyAnnotation,
boolean pageSizeEnabled,
boolean useKeyCredential,
Expand Down Expand Up @@ -417,6 +419,7 @@ private JavaSettings(AutorestSettings autorestSettings,
this.noCustomHeaders = noCustomHeaders;
this.includeReadOnlyInConstructorArgs = includeReadOnlyInConstructorArgs;
this.urlAsString = urlAsString;
this.uuidAsString = uuidAsString;
this.disableRequiredJsonAnnotation = disableRequiredPropertyAnnotation;
this.pageSizeEnabled = pageSizeEnabled;
this.useKeyCredential = useKeyCredential;
Expand Down Expand Up @@ -515,6 +518,11 @@ public boolean urlAsString() {
return urlAsString;
}

private final boolean uuidAsString;
public boolean uuidAsString() {
return uuidAsString;
}

private final boolean disableRequiredJsonAnnotation;

/**
Expand Down
12 changes: 12 additions & 0 deletions javagen/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@
<artifactId>azure-autorest-javagen</artifactId>
<version>1.0.0-beta.1</version>

<repositories>
<repository>
<id>clientcore</id>
<url>https://clientcore.blob.core.windows.net/artifacts</url>
</repository>
</repositories>

<dependencies>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
Expand Down Expand Up @@ -68,6 +75,11 @@
<version>4.13.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.generic</groupId>
<artifactId>generic-core</artifactId>
<version>1.0.0-beta.1</version>
</dependency>
</dependencies>

<profiles>
Expand Down
18 changes: 8 additions & 10 deletions javagen/src/main/java/com/azure/autorest/mapper/ClientMapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

package com.azure.autorest.mapper;

import com.azure.autorest.Javagen;
import com.azure.autorest.extension.base.model.codemodel.ArraySchema;
import com.azure.autorest.extension.base.model.codemodel.ChoiceSchema;
import com.azure.autorest.extension.base.model.codemodel.CodeModel;
Expand Down Expand Up @@ -458,27 +459,24 @@ protected Map<ServiceClient, com.azure.autorest.extension.base.model.codemodel.C
}

private void addBuilderTraits(ClientBuilder clientBuilder, ServiceClient serviceClient) {
if (!JavaSettings.getInstance().isBranded()) {
// TODO: generic
if (serviceClient.getSecurityInfo().getSecurityTypes().contains(Scheme.SecuritySchemeType.KEY)) {
clientBuilder.addBuilderTrait(ClientBuilderTrait.KEY_CREDENTIAL_TRAIT);
}
return;
}

clientBuilder.addBuilderTrait(ClientBuilderTrait.HTTP_TRAIT);

clientBuilder.addBuilderTrait(ClientBuilderTrait.CONFIGURATION_TRAIT);
if (serviceClient.getSecurityInfo().getSecurityTypes().contains(Scheme.SecuritySchemeType.OAUTH2)) {
clientBuilder.addBuilderTrait(ClientBuilderTrait.TOKEN_CREDENTIAL_TRAIT);
}
if (serviceClient.getSecurityInfo().getSecurityTypes().contains(Scheme.SecuritySchemeType.KEY)) {
if (JavaSettings.getInstance().isUseKeyCredential()) {
if (!JavaSettings.getInstance().isBranded() || JavaSettings.getInstance().isUseKeyCredential()) {
clientBuilder.addBuilderTrait(ClientBuilderTrait.KEY_CREDENTIAL_TRAIT);
} else {
clientBuilder.addBuilderTrait(ClientBuilderTrait.AZURE_KEY_CREDENTIAL_TRAIT);
}
}
serviceClient.getProperties().stream()
.map(property -> {
Javagen.getPluginInstance().getLogger().info("Client property name " + property.getName());
return property;
})
.filter(property -> property.getName().equals("endpoint"))
.findFirst()
.ifPresent(property -> clientBuilder.addBuilderTrait(ClientBuilderTrait.getEndpointTrait(property)));
Expand Down Expand Up @@ -611,7 +609,7 @@ private static ModuleInfo getModuleInfo(List<String> modelsPackages) {
List<ModuleInfo.RequireModule> requireModules = moduleInfo.getRequireModules();
requireModules.add(new ModuleInfo.RequireModule(ExternalPackage.CORE.getPackageName(), true));
if (settings.isStreamStyleSerialization()) {
requireModules.add(new ModuleInfo.RequireModule(ExternalPackage.JSON.getPackageName(), false));
requireModules.add(new ModuleInfo.RequireModule(ExternalPackage.JSON.getPackageName(), true));
}

List<ModuleInfo.ExportModule> exportModules = moduleInfo.getExportModules();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@
import com.azure.autorest.model.projectmodel.Project;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

Expand Down Expand Up @@ -83,6 +85,9 @@ private Pom createGenericPom(Project project) {

pom.setServiceName(project.getServiceName());
pom.setServiceDescription(project.getServiceDescriptionForPom());
Map<String, String> repositories = new HashMap<>();
repositories.put("clientcore", "https://clientcore.blob.core.windows.net/artifacts");
pom.setRepositories(repositories);

Set<String> addedDependencyPrefixes = new HashSet<>();
List<String> dependencyIdentifiers = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public IType map(PrimitiveSchema primaryType) {
protected IType createPrimitiveType(PrimitiveSchema primaryType) {
boolean isLowLevelClient = JavaSettings.getInstance().isDataPlaneClient();
boolean urlAsString = JavaSettings.getInstance().urlAsString();
boolean uuidAsString = JavaSettings.getInstance().uuidAsString();

switch (primaryType.getType()) {
// case null:
Expand Down Expand Up @@ -106,7 +107,7 @@ protected IType createPrimitiveType(PrimitiveSchema primaryType) {
}
return durationType;
case UNIXTIME: return isLowLevelClient ? PrimitiveType.LONG : PrimitiveType.UNIX_TIME_LONG;
case UUID: return isLowLevelClient ? ClassType.STRING : ClassType.UUID;
case UUID: return isLowLevelClient || uuidAsString ? ClassType.STRING : ClassType.UUID;
case OBJECT: return ClassType.OBJECT;
case CREDENTIAL: return ClassType.TOKEN_CREDENTIAL;
default:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,19 @@ public class Annotation {
.knownClass(com.azure.core.annotation.HeaderCollection.class)
.build();

public static final Annotation METADATA = new Annotation.Builder()
.knownClass(com.generic.core.annotation.Metadata.class)
.build();
public static final Annotation HTTP_REQUEST_INFORMATION = new Annotation.Builder()
.knownClass(com.generic.core.http.annotation.HttpRequestInformation.class)
.build();
public static final Annotation UNEXPECTED_RESPONSE_EXCEPTION_INFORMATION = new Annotation.Builder()
.knownClass(com.generic.core.http.annotation.UnexpectedResponseExceptionInformation.class)
.build();
public static final Annotation TYPE_CONDITIONS = new Annotation.Builder()
.knownClass(com.generic.core.annotation.TypeConditions.class)
.build();

private final String fullName;
private final String packageName;
private final String name;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
import com.azure.autorest.extension.base.plugin.JavaSettings;
import com.azure.autorest.util.ClientModelUtil;
import com.azure.autorest.util.TemplateUtil;
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.client.traits.KeyCredentialTrait;
import com.azure.core.credential.AzureKeyCredential;
import com.azure.core.credential.KeyCredential;
Expand Down Expand Up @@ -46,9 +49,11 @@
import com.azure.core.util.DateTimeRfc1123;
import com.azure.core.util.ExpandableStringEnum;
import com.azure.core.util.logging.ClientLogger;
import com.azure.core.util.logging.LogLevel;
import com.azure.core.util.polling.PollOperationDetails;
import com.azure.core.util.serializer.JsonSerializer;
import com.azure.core.util.serializer.SerializerAdapter;
import com.azure.core.util.serializer.TypeReference;
import com.azure.json.JsonReader;
import com.azure.json.JsonSerializable;
import com.azure.json.JsonToken;
Expand All @@ -75,48 +80,6 @@
*/
public class ClassType implements IType {

// private static class ClassDetails {
//
// private final Class<?> azureClass;
// private final Class<?> genericClass;
//
// public ClassDetails(Class<?> azureClass, Class<?> genericClass) {
// this.azureClass = azureClass;
// this.genericClass = genericClass;
// }
//
// public Class<?> getAzureClass() {
// return azureClass;
// }
//
// public Class<?> getGenericClass() {
// return genericClass;
// }
//
// }
//
// private static final Map<Class<?>, ClassDetails> CLASS_TYPE_MAPPING = new HashMap<Class<?>, ClassDetails>() {{
// put(com.azure.core.http.rest.RestProxy.class, new ClassDetails(com.azure.core.http.rest.RestProxy.class, com.generic.core.http.RestProxy.class));
// put(com.azure.core.http.HttpPipeline.class, new ClassDetails(com.azure.core.http.HttpPipeline.class, com.generic.core.http.pipeline.HttpPipeline.class));
// put(com.azure.core.http.HttpPipelineBuilder.class, new ClassDetails(com.azure.core.http.HttpPipelineBuilder.class, com.generic.core.http.pipeline.HttpPipelineBuilder.class));
// put(com.azure.core.util.Context.class, new ClassDetails(com.azure.core.util.Context.class, com.generic.core.models.Context.class));
// put(com.azure.core.http.HttpClient.class, new ClassDetails(com.azure.core.http.HttpClient.class, com.generic.core.http.client.HttpClient.class));
// put(com.azure.core.http.policy.HttpLogOptions.class, new ClassDetails(com.azure.core.http.policy.HttpLogOptions.class, com.generic.core.http.policy.logging.HttpLogOptions.class));
// put(com.azure.core.http.policy.HttpPipelinePolicy.class, new ClassDetails(com.azure.core.http.policy.HttpPipelinePolicy.class, com.generic.core.http.pipeline.HttpPipelinePolicy.class));
// put(com.azure.core.http.policy.KeyCredentialPolicy.class, new ClassDetails(com.azure.core.http.policy.KeyCredentialPolicy.class, com.generic.core.http.policy.credential.KeyCredentialPolicy.class));
// put(com.azure.core.util.Configuration.class, new ClassDetails(com.azure.core.util.Configuration.class, com.generic.core.util.configuration.Configuration.class));
// put(com.azure.core.http.HttpHeaders.class, new ClassDetails(com.azure.core.http.HttpHeaders.class, com.generic.core.models.Headers.class));
// put(com.azure.core.http.HttpHeaderName.class, new ClassDetails(com.azure.core.http.HttpHeaderName.class, com.generic.core.http.models.HttpHeaderName.class));
// put(com.azure.core.http.HttpRequest.class, new ClassDetails(com.azure.core.http.HttpRequest.class, com.generic.core.http.models.HttpRequest.class));
// // TODO: generic not having ClientOptions
//// put(com.azure.core.util.ClientOptions.class, new ClassDetails(com.azure.core.util.ClientOptions.class, com.generic.core.models.ClientOptions.class));
// put(com.azure.core.http.rest.RequestOptions.class, new ClassDetails(com.azure.core.http.rest.RequestOptions.class, com.generic.core.models.RequestOptions.class));
// put(com.azure.core.util.BinaryData.class, new ClassDetails(com.azure.core.util.BinaryData.class, com.generic.core.models.BinaryData.class));
// put(com.azure.core.http.policy.RetryOptions.class, new ClassDetails(com.azure.core.http.policy.RetryOptions.class, com.generic.core.http.policy.retry.RetryOptions.class));
// put(com.azure.core.http.rest.Response.class, new ClassDetails(com.azure.core.http.rest.Response.class, com.generic.core.http.Response.class));
// put(com.azure.core.http.rest.SimpleResponse.class, new ClassDetails(com.azure.core.http.rest.SimpleResponse.class, com.generic.core.http.SimpleResponse.class));
// put(com.azure.core.util.ExpandableStringEnum.class, new ClassDetails(com.azure.core.util.ExpandableStringEnum.class, com.generic.core.models.ExpandableStringEnum.class));
// }};

private static class ClassDetails {

Expand All @@ -139,24 +102,32 @@ public String getGenericClass() {
}

private static final Map<Class<?>, ClassDetails> CLASS_TYPE_MAPPING = new HashMap<Class<?>, ClassDetails>() {{
put(RestProxy.class, new ClassDetails(RestProxy.class, "com.generic.core.http.RestProxy"));
put(RestProxy.class, new ClassDetails(RestProxy.class, "com.generic.core.implementation.http.RestProxy"));
put(HttpPipeline.class, new ClassDetails(HttpPipeline.class, "com.generic.core.http.pipeline.HttpPipeline"));
put(HttpPipelineBuilder.class, new ClassDetails(HttpPipelineBuilder.class, "com.generic.core.http.pipeline.HttpPipelineBuilder"));
put(Context.class, new ClassDetails(Context.class, "com.generic.core.models.Context"));
put(HttpClient.class, new ClassDetails(HttpClient.class, "com.generic.core.http.client.HttpClient"));
put(HttpLogOptions.class, new ClassDetails(HttpLogOptions.class, "com.generic.core.http.policy.logging.HttpLogOptions"));
put(HttpLogOptions.class, new ClassDetails(HttpLogOptions.class, "com.generic.core.http.policy.HttpLoggingPolicy.HttpLogOptions"));
put(HttpPipelinePolicy.class, new ClassDetails(HttpPipelinePolicy.class, "com.generic.core.http.pipeline.HttpPipelinePolicy"));
put(KeyCredentialPolicy.class, new ClassDetails(KeyCredentialPolicy.class, "com.generic.core.http.policy.credential.KeyCredentialPolicy"));
put(KeyCredentialPolicy.class, new ClassDetails(KeyCredentialPolicy.class, "com.generic.core.http.policy.KeyCredentialPolicy"));
put(Configuration.class, new ClassDetails(Configuration.class, "com.generic.core.util.configuration.Configuration"));
put(HttpHeaders.class, new ClassDetails(HttpHeaders.class, "com.generic.core.models.Headers"));
put(HttpHeaderName.class, new ClassDetails(HttpHeaderName.class, "com.generic.core.http.models.HttpHeaderName"));
put(HttpRequest.class, new ClassDetails(HttpRequest.class, "com.generic.core.http.models.HttpRequest"));
put(RequestOptions.class, new ClassDetails(RequestOptions.class, "com.generic.core.models.RequestOptions"));
put(RequestOptions.class, new ClassDetails(RequestOptions.class, "com.generic.core.http.models.RequestOptions"));
put(BinaryData.class, new ClassDetails(BinaryData.class, "com.generic.core.models.BinaryData"));
put(RetryOptions.class, new ClassDetails(RetryOptions.class, "com.generic.core.http.policy.retry.RetryOptions"));
put(RetryOptions.class, new ClassDetails(RetryOptions.class, "com.generic.core.http.models.RetryOptions"));
put(Response.class, new ClassDetails(Response.class, "com.generic.core.http.Response"));
put(SimpleResponse.class, new ClassDetails(SimpleResponse.class, "com.generic.core.http.SimpleResponse"));
put(ExpandableStringEnum.class, new ClassDetails(ExpandableStringEnum.class, "com.generic.core.models.ExpandableStringEnum"));
put(HttpResponseException.class, new ClassDetails(HttpResponseException.class, "com.generic.core.http.exception.HttpResponseException"));
put(HttpTrait.class, new ClassDetails(HttpTrait.class, "com.generic.core.models.traits.HttpTrait"));
put(ConfigurationTrait.class, new ClassDetails(ConfigurationTrait.class, "com.generic.core.models.traits.ConfigurationTrait"));
put(EndpointTrait.class, new ClassDetails(EndpointTrait.class, "com.generic.core.models.traits.EndpointTrait"));
put(KeyCredentialTrait.class, new ClassDetails(KeyCredentialTrait.class, "com.generic.core.models.traits.KeyCredentialTrait"));
put(TypeReference.class, new ClassDetails(TypeReference.class, "com.generic.core.models.TypeReference"));
put(ClientLogger.class, new ClassDetails(ClientLogger.class, "com.generic.core.util.ClientLogger"));
put(LogLevel.class, new ClassDetails(LogLevel.class, "com.generic.core.util.ClientLogger.LogLevel"));
}};

private static ClassType.Builder getClassTypeBuilder(Class<?> classKey) {
Expand Down Expand Up @@ -416,6 +387,7 @@ private static ClassType.Builder getClassTypeBuilder(Class<?> classKey) {
.build();

public static final ClassType CLIENT_LOGGER = ClassType.getClassTypeBuilder(ClientLogger.class).build();
public static final ClassType LOG_LEVEL = ClassType.getClassTypeBuilder(LogLevel.class).build();

public static final ClassType AZURE_ENVIRONMENT = new ClassType.Builder(false)
.packageName("com.azure.core.management").name("AzureEnvironment")
Expand Down Expand Up @@ -508,11 +480,15 @@ private static ClassType.Builder getClassTypeBuilder(Class<?> classKey) {
public static final ClassType HTTP_PIPELINE_BUILDER = getClassTypeBuilder(HttpPipelineBuilder.class).build();
public static final ClassType KEY_CREDENTIAL_POLICY = getClassTypeBuilder(KeyCredentialPolicy.class).build();
public static final ClassType KEY_CREDENTIAL_TRAIT = getClassTypeBuilder(KeyCredentialTrait.class).build();
public static final ClassType ENDPOINT_TRAIT = getClassTypeBuilder(EndpointTrait.class).build();
public static final ClassType HTTP_TRAIT = getClassTypeBuilder(HttpTrait.class).build();
public static final ClassType CONFIGURATION_TRAIT = getClassTypeBuilder(ConfigurationTrait.class).build();
public static final ClassType POLL_OPERATION_DETAILS = getClassTypeBuilder(PollOperationDetails.class).build();
public static final ClassType JSON_SERIALIZABLE = getClassTypeBuilder(JsonSerializable.class).build();
public static final ClassType JSON_WRITER = getClassTypeBuilder(JsonWriter.class).build();
public static final ClassType JSON_READER = getClassTypeBuilder(JsonReader.class).build();
public static final ClassType JSON_TOKEN = getClassTypeBuilder(JsonToken.class).build();
public static final ClassType TYPE_REFERENCE = getClassTypeBuilder(TypeReference.class).build();

private final String fullName;
private final String packageName;
Expand Down
Loading

0 comments on commit 74f51a7

Please sign in to comment.