From d37d3965aa78e5b4e6bf388d79ef12b383cc1ff7 Mon Sep 17 00:00:00 2001 From: XiaofeiCao Date: Wed, 18 Sep 2024 13:55:38 +0800 Subject: [PATCH 01/18] test arm test --- .../generator/http-client-generator-test/tsp/arm.tsp | 8 ++++++++ .../generator/http-client-generator-test/tsp/enum.tsp | 8 ++++++++ 2 files changed, 16 insertions(+) diff --git a/packages/http-client-java/generator/http-client-generator-test/tsp/arm.tsp b/packages/http-client-java/generator/http-client-generator-test/tsp/arm.tsp index ef52c170d8..3d714542cc 100644 --- a/packages/http-client-java/generator/http-client-generator-test/tsp/arm.tsp +++ b/packages/http-client-java/generator/http-client-generator-test/tsp/arm.tsp @@ -147,6 +147,8 @@ model CustomTemplateResourceProperties { dog: Dog; namedEmptyModel: EmptyModel; anonymousEmptyModel: {}; + + priority: PriorityModel; } @doc("Empty model.") @@ -183,6 +185,12 @@ model Result { reason?: string; } +union PriorityModel { + int32, + HIGH: 0, + LOW: 1, +} + // #suppress "@azure-tools/typespec-azure-resource-manager/arm-resource-invalid-envelope-property" "built-in conditional request includes time based conditional headers" alias IfMatchParameters = { @header("If-Match") diff --git a/packages/http-client-java/generator/http-client-generator-test/tsp/enum.tsp b/packages/http-client-java/generator/http-client-generator-test/tsp/enum.tsp index 316447e11b..34de706783 100644 --- a/packages/http-client-java/generator/http-client-generator-test/tsp/enum.tsp +++ b/packages/http-client-java/generator/http-client-generator-test/tsp/enum.tsp @@ -37,6 +37,12 @@ union PriorityModel { Low: 0, } +union OlympicRecordModel { + float32, + Olympic_100_Meters: 9.58, + Olympic_200_Meters: 19.30, +} + #suppress "@azure-tools/typespec-azure-core/use-extensible-enum" "For testing" enum Priority { High: 100, @@ -69,6 +75,8 @@ model Operation { colorValue: Color.Green; colorModelValue: ColorModel.Blue; unitValue?: Unit.Milligram; + olympicRecord?: OlympicRecordModel; + olympicRecordValue?: OlympicRecordModel.Olympic_100_Meters; } @client({ From 54af0f10a3eacbef0919552b55e97de0cf309359 Mon Sep 17 00:00:00 2001 From: XiaofeiCao Date: Wed, 18 Sep 2024 17:42:19 +0800 Subject: [PATCH 02/18] script change --- .../http-client-generator-test/Generate.ps1 | 23 +------------------ .../http-client-generator-test/Setup.ps1 | 8 +++++++ 2 files changed, 9 insertions(+), 22 deletions(-) create mode 100644 packages/http-client-java/generator/http-client-generator-test/Setup.ps1 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 8a2e843bf8..ddabe27356 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 @@ -113,28 +113,7 @@ $generateScript = { } } -Set-Location (Resolve-Path (Join-Path $PSScriptRoot '..' '..')) - -npm install -npm run build -npm pack - -Set-Location $PSScriptRoot - - -if (Test-Path node_modules) { - Remove-Item node_modules -Recurse -Force -} - -if (Test-Path package-lock.json) { - Remove-Item package-lock.json -} - -# delete output -if (Test-Path tsp-output) { - Remove-Item tsp-output -Recurse -Force -} -npm install +./Setup.ps1 New-Item -Path ./existingcode/src/main/java/com/cadl/ -ItemType Directory -Force | Out-Null diff --git a/packages/http-client-java/generator/http-client-generator-test/Setup.ps1 b/packages/http-client-java/generator/http-client-generator-test/Setup.ps1 new file mode 100644 index 0000000000..f72480dd10 --- /dev/null +++ b/packages/http-client-java/generator/http-client-generator-test/Setup.ps1 @@ -0,0 +1,8 @@ +# re-build http-client-java +Set-Location (Resolve-Path (Join-Path $PSScriptRoot '..' '..')) + +./Setup.ps1 + +Set-Location $PSScriptRoot + +npm run clean && npm install From 45f46e15c81d6eef7d6a82b96aae61a5dbb14be2 Mon Sep 17 00:00:00 2001 From: xiaofeicao Date: Thu, 19 Sep 2024 17:23:57 +0800 Subject: [PATCH 03/18] bump azure-core for ExpandableEnum --- .../generator/http-client-generator-core/pom.xml | 2 +- .../generator/http-client-generator-test/pom.xml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/http-client-java/generator/http-client-generator-core/pom.xml b/packages/http-client-java/generator/http-client-generator-core/pom.xml index 334423264d..c9a80c0fc3 100644 --- a/packages/http-client-java/generator/http-client-generator-core/pom.xml +++ b/packages/http-client-java/generator/http-client-generator-core/pom.xml @@ -22,7 +22,7 @@ com.azure azure-core - 1.51.0 + 1.52.0 com.azure diff --git a/packages/http-client-java/generator/http-client-generator-test/pom.xml b/packages/http-client-java/generator/http-client-generator-test/pom.xml index 0a6b6bd719..ce5f0c0e33 100644 --- a/packages/http-client-java/generator/http-client-generator-test/pom.xml +++ b/packages/http-client-java/generator/http-client-generator-test/pom.xml @@ -22,7 +22,7 @@ com.azure azure-core - 1.51.0 + 1.52.0 com.azure From 29841a7ca0d407c233d24b34b3cc645941f6fe4b Mon Sep 17 00:00:00 2001 From: XiaofeiCao Date: Wed, 18 Sep 2024 14:24:54 +0800 Subject: [PATCH 04/18] template reference code fix typespec fix --- .../generator/core/mapper/MapperUtils.java | 43 ++++-- .../core/model/clientmodel/ClassType.java | 2 + .../core/model/clientmodel/EnumType.java | 42 ++++-- .../generator/core/template/EnumTemplate.java | 13 +- .../http/client/generator/TypeSpecPlugin.java | 3 + .../fluent/TypeSpecFluentMapperFactory.java | 3 +- .../fluent/TypeSpecFluentPlugin.java | 2 + .../fluent/TypeSpecFluentTemplateFactory.java | 18 +++ .../mapper/TypeSpecChoiceMapper.java | 29 ++++ .../mapper/TypeSpecMapperFactory.java | 6 + .../template/TypeSpecEnumTemplate.java | 135 ++++++++++++++++++ .../template/TypeSpecTemplateFactory.java | 17 +++ 12 files changed, 289 insertions(+), 24 deletions(-) create mode 100644 packages/http-client-java/generator/http-client-generator/src/main/java/com/microsoft/typespec/http/client/generator/fluent/TypeSpecFluentTemplateFactory.java create mode 100644 packages/http-client-java/generator/http-client-generator/src/main/java/com/microsoft/typespec/http/client/generator/mapper/TypeSpecChoiceMapper.java create mode 100644 packages/http-client-java/generator/http-client-generator/src/main/java/com/microsoft/typespec/http/client/generator/template/TypeSpecEnumTemplate.java create mode 100644 packages/http-client-java/generator/http-client-generator/src/main/java/com/microsoft/typespec/http/client/generator/template/TypeSpecTemplateFactory.java diff --git a/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/mapper/MapperUtils.java b/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/mapper/MapperUtils.java index 72dc862ed3..eab48abe10 100644 --- a/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/mapper/MapperUtils.java +++ b/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/mapper/MapperUtils.java @@ -30,12 +30,27 @@ public final class MapperUtils { /** * Create enum client type from code model. - * @param enumType code model schema for enum - * @param expandable whether it's expandable enum + * + * @param enumType code model schema for enum + * @param expandable whether it's expandable enum * @param useCodeModelNameForEnumMember whether to use code model enum member name for client enum member name * @return enum client type */ public static IType createEnumType(ChoiceSchema enumType, boolean expandable, boolean useCodeModelNameForEnumMember) { + return createEnumType(enumType, expandable, useCodeModelNameForEnumMember, null, null); + } + + /** + * Create enum client type from code model. + * + * @param enumType code model schema for enum + * @param expandable whether it's expandable enum + * @param useCodeModelNameForEnumMember whether to use code model enum member name for client enum member name + * @param serializationMethodName method name for serialization + * @param deserializationMethodName method name for deserialization + * @return enum client type + */ + public static IType createEnumType(ChoiceSchema enumType, boolean expandable, boolean useCodeModelNameForEnumMember, String serializationMethodName, String deserializationMethodName) { JavaSettings settings = JavaSettings.getInstance(); String enumTypeName = enumType.getLanguage().getJava().getName(); @@ -82,17 +97,19 @@ public static IType createEnumType(ChoiceSchema enumType, boolean expandable, bo } return new EnumType.Builder() - .packageName(enumPackage) - .name(enumTypeName) - .description(description) - .expandable(expandable) - .values(enumValues) - .elementType(Mappers.getSchemaMapper().map(enumType.getChoiceType())) - .implementationDetails(new ImplementationDetails.Builder() - .usages(SchemaUtil.mapSchemaContext(enumType.getUsage())) - .build()) - .crossLanguageDefinitionId(enumType.getCrossLanguageDefinitionId()) - .build(); + .packageName(enumPackage) + .name(enumTypeName) + .description(description) + .expandable(expandable) + .values(enumValues) + .elementType(Mappers.getSchemaMapper().map(enumType.getChoiceType())) + .implementationDetails(new ImplementationDetails.Builder() + .usages(SchemaUtil.mapSchemaContext(enumType.getUsage())) + .build()) + .crossLanguageDefinitionId(enumType.getCrossLanguageDefinitionId()) + .fromMethodName(deserializationMethodName) + .toMethodName(serializationMethodName) + .build(); } } diff --git a/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/model/clientmodel/ClassType.java b/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/model/clientmodel/ClassType.java index 4723530ff2..0463a45160 100644 --- a/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/model/clientmodel/ClassType.java +++ b/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/model/clientmodel/ClassType.java @@ -3,6 +3,7 @@ package com.microsoft.typespec.http.client.generator.core.model.clientmodel; +import com.azure.core.util.ExpandableEnum; import com.microsoft.typespec.http.client.generator.core.extension.model.extensionmodel.XmsExtensions; import com.microsoft.typespec.http.client.generator.core.extension.plugin.JavaSettings; import com.microsoft.typespec.http.client.generator.core.util.TemplateUtil; @@ -159,6 +160,7 @@ private static ClassType.Builder getClassTypeBuilder(Class classKey) { public static final ClassType RESPONSE = getClassTypeBuilder(Response.class).build(); public static final ClassType SIMPLE_RESPONSE = getClassTypeBuilder(SimpleResponse.class).build(); public static final ClassType EXPANDABLE_STRING_ENUM = getClassTypeBuilder(ExpandableStringEnum.class).build(); + public static final ClassType EXPANDABLE_ENUM = getClassTypeBuilder(ExpandableEnum.class).build(); 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(); diff --git a/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/model/clientmodel/EnumType.java b/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/model/clientmodel/EnumType.java index 010187d5f0..21140eea9c 100644 --- a/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/model/clientmodel/EnumType.java +++ b/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/model/clientmodel/EnumType.java @@ -3,6 +3,7 @@ package com.microsoft.typespec.http.client.generator.core.model.clientmodel; +import com.azure.core.util.CoreUtils; import com.microsoft.typespec.http.client.generator.core.util.CodeNamer; import java.util.List; @@ -35,19 +36,24 @@ public class EnumType implements IType { private final ImplementationDetails implementationDetails; private String crossLanguageDefinitionId; + private final String fromMethodName; + private final String toMethodName; /** * Create a new Enum with the provided properties. - * @param name The name of the new Enum. - * @param description The description of the Enum. - * @param expandable Whether this will be an ExpandableStringEnum type. - * @param values The values of the Enum. + * + * @param name The name of the new Enum. + * @param description The description of the Enum. + * @param expandable Whether this will be an ExpandableStringEnum type. + * @param values The values of the Enum. + * @param fromMethodName The method name used to convert JSON to the enum type. + * @param toMethodName The method name used to convert the enum type to JSON. */ private EnumType(String packageKeyword, String name, String description, boolean expandable, List values, IType elementType, ImplementationDetails implementationDetails, - String crossLanguageDefinitionId) { + String crossLanguageDefinitionId, String fromMethodName, String toMethodName) { this.name = name; this.packageName = packageKeyword; this.description = description; @@ -56,6 +62,8 @@ private EnumType(String packageKeyword, String name, String description, this.elementType = elementType; this.implementationDetails = implementationDetails; this.crossLanguageDefinitionId = crossLanguageDefinitionId; + this.fromMethodName = fromMethodName; + this.toMethodName = toMethodName; } public String getCrossLanguageDefinitionId() { @@ -134,7 +142,9 @@ public final String defaultValueExpression(String sourceExpression) { * @return The method name used to convert JSON to the enum type. */ public final String getFromMethodName() { - return "from" + CodeNamer.toPascalCase(elementType.getClientType().toString()); + return CoreUtils.isNullOrEmpty(fromMethodName) + ? "from" + CodeNamer.toPascalCase(elementType.getClientType().toString()) + : fromMethodName; } /** @@ -143,7 +153,9 @@ public final String getFromMethodName() { * @return The method name used to convert the enum type to JSON. */ public final String getToMethodName() { - return "to" + CodeNamer.toPascalCase(elementType.getClientType().toString()); + return CoreUtils.isNullOrEmpty(toMethodName) + ? "to" + CodeNamer.toPascalCase(elementType.getClientType().toString()) + : toMethodName; } @Override @@ -231,6 +243,8 @@ public static class Builder { private ImplementationDetails implementationDetails; private String crossLanguageDefinitionId; + private String fromMethodName; + private String toMethodName; /** * Sets the name of the Enum. @@ -309,6 +323,16 @@ public Builder crossLanguageDefinitionId(String crossLanguageDefinitionId) { return this; } + public Builder fromMethodName(String fromMethodName) { + this.fromMethodName = fromMethodName; + return this; + } + + public Builder toMethodName(String toMethodName) { + this.toMethodName = toMethodName; + return this; + } + /** * @return an immutable EnumType instance with the configurations on this builder. */ @@ -321,8 +345,8 @@ public EnumType build() { values, elementType, implementationDetails, - crossLanguageDefinitionId - ); + crossLanguageDefinitionId, + fromMethodName, toMethodName); } } } diff --git a/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/template/EnumTemplate.java b/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/template/EnumTemplate.java index 39a229a3ab..aadb608265 100644 --- a/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/template/EnumTemplate.java +++ b/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/template/EnumTemplate.java @@ -40,7 +40,7 @@ public final void write(EnumType enumType, JavaFile javaFile) { if (enumType.getExpandable()) { if(settings.isBranded()) { - writeExpandableStringEnum(enumType, javaFile, settings); + writeBrandedExpandableEnum(enumType, javaFile, settings); } else { writeExpandableStringEnumInterface(enumType, javaFile, settings); } @@ -49,6 +49,17 @@ public final void write(EnumType enumType, JavaFile javaFile) { } } + /** + * Extension point for expandable enum implementation of branded flavor. + * + * @param enumType enumType to write implementation + * @param javaFile javaFile to write into + * @param settings {@link JavaSettings} instance + */ + protected void writeBrandedExpandableEnum(EnumType enumType, JavaFile javaFile, JavaSettings settings) { + writeExpandableStringEnum(enumType, javaFile, settings); + } + private void writeExpandableStringEnumInterface(EnumType enumType, JavaFile javaFile, JavaSettings settings) { Set imports = new HashSet<>(); imports.add("java.util.Collection"); diff --git a/packages/http-client-java/generator/http-client-generator/src/main/java/com/microsoft/typespec/http/client/generator/TypeSpecPlugin.java b/packages/http-client-java/generator/http-client-generator/src/main/java/com/microsoft/typespec/http/client/generator/TypeSpecPlugin.java index 2b15e43907..8dc661413b 100644 --- a/packages/http-client-java/generator/http-client-generator/src/main/java/com/microsoft/typespec/http/client/generator/TypeSpecPlugin.java +++ b/packages/http-client-java/generator/http-client-generator/src/main/java/com/microsoft/typespec/http/client/generator/TypeSpecPlugin.java @@ -16,12 +16,14 @@ import com.microsoft.typespec.http.client.generator.core.model.javamodel.JavaPackage; import com.microsoft.typespec.http.client.generator.core.preprocessor.Preprocessor; import com.microsoft.typespec.http.client.generator.core.preprocessor.tranformer.Transformer; +import com.microsoft.typespec.http.client.generator.core.template.Templates; import com.microsoft.typespec.http.client.generator.core.util.ClientModelUtil; import com.azure.core.util.CoreUtils; import com.azure.json.JsonReader; import com.azure.json.ReadValueCallback; import com.microsoft.typespec.http.client.generator.mapper.TypeSpecMapperFactory; import com.microsoft.typespec.http.client.generator.model.EmitterOptions; +import com.microsoft.typespec.http.client.generator.template.TypeSpecTemplateFactory; import com.microsoft.typespec.http.client.generator.util.FileUtil; import com.microsoft.typespec.http.client.generator.util.ModelUtil; import org.slf4j.Logger; @@ -287,6 +289,7 @@ public TypeSpecPlugin(EmitterOptions options, boolean sdkIntegration) { LOGGER.info("Namespace: {}", JavaSettings.getInstance().getPackage()); Mappers.setFactory(new TypeSpecMapperFactory()); + Templates.setFactory(new TypeSpecTemplateFactory()); } @SuppressWarnings("unchecked") diff --git a/packages/http-client-java/generator/http-client-generator/src/main/java/com/microsoft/typespec/http/client/generator/fluent/TypeSpecFluentMapperFactory.java b/packages/http-client-java/generator/http-client-generator/src/main/java/com/microsoft/typespec/http/client/generator/fluent/TypeSpecFluentMapperFactory.java index 43ba72a224..2e5093e799 100644 --- a/packages/http-client-java/generator/http-client-generator/src/main/java/com/microsoft/typespec/http/client/generator/fluent/TypeSpecFluentMapperFactory.java +++ b/packages/http-client-java/generator/http-client-generator/src/main/java/com/microsoft/typespec/http/client/generator/fluent/TypeSpecFluentMapperFactory.java @@ -5,6 +5,7 @@ import com.microsoft.typespec.http.client.generator.core.mapper.ChoiceMapper; import com.microsoft.typespec.http.client.generator.core.mapper.SealedChoiceMapper; +import com.microsoft.typespec.http.client.generator.mapper.TypeSpecChoiceMapper; import com.microsoft.typespec.http.client.generator.mgmt.mapper.FluentMapperFactory; import com.microsoft.typespec.http.client.generator.core.mapper.ClientMapper; import com.microsoft.typespec.http.client.generator.core.mapper.ModelMapper; @@ -42,7 +43,7 @@ public ModelPropertyMapper getModelPropertyMapper() { @Override public ChoiceMapper getChoiceMapper() { - return ChoiceMapper.getInstance(); + return TypeSpecChoiceMapper.getInstance(); } @Override diff --git a/packages/http-client-java/generator/http-client-generator/src/main/java/com/microsoft/typespec/http/client/generator/fluent/TypeSpecFluentPlugin.java b/packages/http-client-java/generator/http-client-generator/src/main/java/com/microsoft/typespec/http/client/generator/fluent/TypeSpecFluentPlugin.java index 059a9a874e..1272b060f4 100644 --- a/packages/http-client-java/generator/http-client-generator/src/main/java/com/microsoft/typespec/http/client/generator/fluent/TypeSpecFluentPlugin.java +++ b/packages/http-client-java/generator/http-client-generator/src/main/java/com/microsoft/typespec/http/client/generator/fluent/TypeSpecFluentPlugin.java @@ -8,6 +8,7 @@ import com.microsoft.typespec.http.client.generator.core.extension.model.Message; import com.microsoft.typespec.http.client.generator.core.extension.model.codemodel.CodeModel; import com.microsoft.typespec.http.client.generator.core.extension.plugin.JavaSettings; +import com.microsoft.typespec.http.client.generator.core.template.Templates; import com.microsoft.typespec.http.client.generator.mgmt.FluentGen; import com.microsoft.typespec.http.client.generator.mgmt.mapper.FluentMapper; import com.microsoft.typespec.http.client.generator.mgmt.model.javamodel.FluentJavaPackage; @@ -89,6 +90,7 @@ public void writeFile(String fileName, String content, List sourceMap) { protected FluentMapper getFluentMapper() { FluentMapper fluentMapper = super.getFluentMapper(); Mappers.setFactory(new TypeSpecFluentMapperFactory()); + Templates.setFactory(new TypeSpecFluentTemplateFactory()); return fluentMapper; } diff --git a/packages/http-client-java/generator/http-client-generator/src/main/java/com/microsoft/typespec/http/client/generator/fluent/TypeSpecFluentTemplateFactory.java b/packages/http-client-java/generator/http-client-generator/src/main/java/com/microsoft/typespec/http/client/generator/fluent/TypeSpecFluentTemplateFactory.java new file mode 100644 index 0000000000..9ea6e42b03 --- /dev/null +++ b/packages/http-client-java/generator/http-client-generator/src/main/java/com/microsoft/typespec/http/client/generator/fluent/TypeSpecFluentTemplateFactory.java @@ -0,0 +1,18 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +package com.microsoft.typespec.http.client.generator.fluent; + +import com.microsoft.typespec.http.client.generator.core.template.EnumTemplate; +import com.microsoft.typespec.http.client.generator.mgmt.template.FluentTemplateFactory; +import com.microsoft.typespec.http.client.generator.template.TypeSpecEnumTemplate; + +/** + * TypeSpec mgmt template factory. + */ +public class TypeSpecFluentTemplateFactory extends FluentTemplateFactory { + @Override + public EnumTemplate getEnumTemplate() { + return TypeSpecEnumTemplate.getInstance(); + } +} diff --git a/packages/http-client-java/generator/http-client-generator/src/main/java/com/microsoft/typespec/http/client/generator/mapper/TypeSpecChoiceMapper.java b/packages/http-client-java/generator/http-client-generator/src/main/java/com/microsoft/typespec/http/client/generator/mapper/TypeSpecChoiceMapper.java new file mode 100644 index 0000000000..a9b7b15782 --- /dev/null +++ b/packages/http-client-java/generator/http-client-generator/src/main/java/com/microsoft/typespec/http/client/generator/mapper/TypeSpecChoiceMapper.java @@ -0,0 +1,29 @@ +package com.microsoft.typespec.http.client.generator.mapper; + +import com.microsoft.typespec.http.client.generator.core.extension.model.codemodel.ChoiceSchema; +import com.microsoft.typespec.http.client.generator.core.mapper.ChoiceMapper; +import com.microsoft.typespec.http.client.generator.core.mapper.MapperUtils; +import com.microsoft.typespec.http.client.generator.core.mapper.Mappers; +import com.microsoft.typespec.http.client.generator.core.model.clientmodel.ClassType; +import com.microsoft.typespec.http.client.generator.core.model.clientmodel.IType; + +public class TypeSpecChoiceMapper extends ChoiceMapper { + private static TypeSpecChoiceMapper INSTANCE = new TypeSpecChoiceMapper(); + + public static TypeSpecChoiceMapper getInstance() { + return INSTANCE; + } + private TypeSpecChoiceMapper() { + } + + @Override + public IType map(ChoiceSchema enumType) { + IType elementType = Mappers.getSchemaMapper().map(enumType.getChoiceType()); + boolean isStringEnum = elementType == ClassType.STRING; + if (isStringEnum) { + return MapperUtils.createEnumType(enumType, true, true); + } else { + return MapperUtils.createEnumType(enumType, true, true, "getValue", "fromValue"); + } + } +} diff --git a/packages/http-client-java/generator/http-client-generator/src/main/java/com/microsoft/typespec/http/client/generator/mapper/TypeSpecMapperFactory.java b/packages/http-client-java/generator/http-client-generator/src/main/java/com/microsoft/typespec/http/client/generator/mapper/TypeSpecMapperFactory.java index 2552fbcba9..74df5b6eff 100644 --- a/packages/http-client-java/generator/http-client-generator/src/main/java/com/microsoft/typespec/http/client/generator/mapper/TypeSpecMapperFactory.java +++ b/packages/http-client-java/generator/http-client-generator/src/main/java/com/microsoft/typespec/http/client/generator/mapper/TypeSpecMapperFactory.java @@ -3,6 +3,7 @@ package com.microsoft.typespec.http.client.generator.mapper; +import com.microsoft.typespec.http.client.generator.core.mapper.ChoiceMapper; import com.microsoft.typespec.http.client.generator.core.mapper.ClientMapper; import com.microsoft.typespec.http.client.generator.core.mapper.DefaultMapperFactory; import com.microsoft.typespec.http.client.generator.core.mapper.PrimitiveMapper; @@ -18,4 +19,9 @@ public ClientMapper getClientMapper() { public PrimitiveMapper getPrimitiveMapper() { return TypeSpecPrimitiveMapper.getInstance(); } + + @Override + public ChoiceMapper getChoiceMapper() { + return TypeSpecChoiceMapper.getInstance(); + } } diff --git a/packages/http-client-java/generator/http-client-generator/src/main/java/com/microsoft/typespec/http/client/generator/template/TypeSpecEnumTemplate.java b/packages/http-client-java/generator/http-client-generator/src/main/java/com/microsoft/typespec/http/client/generator/template/TypeSpecEnumTemplate.java new file mode 100644 index 0000000000..f16af85edb --- /dev/null +++ b/packages/http-client-java/generator/http-client-generator/src/main/java/com/microsoft/typespec/http/client/generator/template/TypeSpecEnumTemplate.java @@ -0,0 +1,135 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +package com.microsoft.typespec.http.client.generator.template; + +import com.azure.core.util.CoreUtils; +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.ClientEnumValue; +import com.microsoft.typespec.http.client.generator.core.model.clientmodel.EnumType; +import com.microsoft.typespec.http.client.generator.core.model.clientmodel.IType; +import com.microsoft.typespec.http.client.generator.core.model.javamodel.JavaFile; +import com.microsoft.typespec.http.client.generator.core.model.javamodel.JavaModifier; +import com.microsoft.typespec.http.client.generator.core.model.javamodel.JavaVisibility; +import com.microsoft.typespec.http.client.generator.core.template.EnumTemplate; +import com.microsoft.typespec.http.client.generator.core.util.CodeNamer; + +import java.util.HashSet; +import java.util.Set; + +/** + * TypeSpec implementation for EnumTemplate. + */ +public class TypeSpecEnumTemplate extends EnumTemplate { + private static final TypeSpecEnumTemplate INSTANCE = new TypeSpecEnumTemplate(); + + public static TypeSpecEnumTemplate getInstance() { + return INSTANCE; + } + + @Override + protected void writeBrandedExpandableEnum(EnumType enumType, JavaFile javaFile, JavaSettings settings) { + if (enumType.getElementType() == ClassType.STRING) { + // if String, use ExpandableStringEnum implementation + super.writeBrandedExpandableEnum(enumType, javaFile, settings); + } else { + Set imports = new HashSet<>(); + imports.add("java.util.Collection"); + imports.add("java.lang.IllegalArgumentException"); + imports.add("java.util.Map"); + imports.add("java.util.concurrent.ConcurrentHashMap"); + imports.add("java.util.ArrayList"); + imports.add(ClassType.EXPANDABLE_ENUM.getFullName()); + if (!settings.isStreamStyleSerialization()) { + imports.add("com.fasterxml.jackson.annotation.JsonCreator"); + } + + addGeneratedImport(imports); + + javaFile.declareImport(imports); + javaFile.javadocComment(comment -> comment.description(enumType.getDescription())); + + String enumName = enumType.getName(); + IType elementType = enumType.getElementType(); + String typeName = elementType.getClientType().asNullable().toString(); + String pascalTypeName = CodeNamer.toPascalCase(typeName); + String declaration = enumName + " implements ExpandableEnum<" + pascalTypeName + ">"; + javaFile.publicFinalClass(declaration, classBlock -> { + classBlock.privateStaticFinalVariable(String.format("Map<%1$s, %2$s> VALUES = new ConcurrentHashMap<>()", pascalTypeName, enumName)); + + for (ClientEnumValue enumValue : enumType.getValues()) { + String value = enumValue.getValue(); + classBlock.javadocComment(CoreUtils.isNullOrEmpty(enumValue.getDescription()) + ? "Static value " + value + " for " + enumName + "." + : enumValue.getDescription()); + addGeneratedAnnotation(classBlock); + classBlock.publicStaticFinalVariable(String.format("%1$s %2$s = fromValue(%3$s)", enumName, + enumValue.getName(), elementType.defaultValueExpression(value))); + } + + classBlock.variable(pascalTypeName + " value", JavaVisibility.Private, JavaModifier.Final); + classBlock.privateConstructor(enumName + "(" + pascalTypeName + " value)", ctor -> { + ctor.line("this.value = value;"); + }); + + // fromValue(typeName) + classBlock.javadocComment(comment -> { + comment.description("Creates or finds a " + enumName); + comment.param("value", "a value to look for"); + comment.methodReturns("the corresponding " + enumName); + }); + + addGeneratedAnnotation(classBlock); + if (!settings.isStreamStyleSerialization()) { + classBlock.annotation("JsonCreator"); + } + + classBlock.publicStaticMethod(String.format("%1$s fromValue(%2$s value)", enumName, pascalTypeName), + function -> { + function.ifBlock("value == null", ifAction -> ifAction.line("throw new IllegalArgumentException(\"value can't be null\");")); + function.line(enumName + " member = VALUES.get(value);"); + function.ifBlock("member != null", ifAction -> { + ifAction.line("return member;"); + }); + function.methodReturn("VALUES.computeIfAbsent(value, key -> new " + enumName + "(key))"); + }); + + // values + classBlock.javadocComment(comment -> { + comment.description("Gets known " + enumName + " values."); + comment.methodReturns("Known " + enumName + " values."); + }); + addGeneratedAnnotation(classBlock); + classBlock.publicStaticMethod(String.format("Collection<%s> values()", enumName), + function -> function.methodReturn("new ArrayList<>(VALUES.values())")); + + // getValue + classBlock.javadocComment(comment -> { + comment.description("Gets the value of the " + enumName + " instance."); + comment.methodReturns("the value of the " + enumName + " instance."); + }); + + addGeneratedAnnotation(classBlock); + classBlock.annotation("Override"); + classBlock.publicMethod(pascalTypeName + " getValue()", + function -> function.methodReturn("this.value")); + + // toString + addGeneratedAnnotation(classBlock); + classBlock.annotation("Override"); + classBlock.method(JavaVisibility.Public, null, "String toString()", function -> function.methodReturn("getValue().toString()")); + + // equals + addGeneratedAnnotation(classBlock); + classBlock.annotation("Override"); + classBlock.method(JavaVisibility.Public, null, "boolean equals(Object obj)", function -> function.methodReturn(String.format("(obj instanceof %1$s) && ((%1$s) obj).getValue().equals(getValue())", enumName))); + + // hashcode + addGeneratedAnnotation(classBlock); + classBlock.annotation("Override"); + classBlock.method(JavaVisibility.Public, null, "int hashCode()", function -> function.methodReturn("getValue().hashCode()")); + }); + } + } +} diff --git a/packages/http-client-java/generator/http-client-generator/src/main/java/com/microsoft/typespec/http/client/generator/template/TypeSpecTemplateFactory.java b/packages/http-client-java/generator/http-client-generator/src/main/java/com/microsoft/typespec/http/client/generator/template/TypeSpecTemplateFactory.java new file mode 100644 index 0000000000..bb2a6443cb --- /dev/null +++ b/packages/http-client-java/generator/http-client-generator/src/main/java/com/microsoft/typespec/http/client/generator/template/TypeSpecTemplateFactory.java @@ -0,0 +1,17 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +package com.microsoft.typespec.http.client.generator.template; + +import com.microsoft.typespec.http.client.generator.core.template.DefaultTemplateFactory; +import com.microsoft.typespec.http.client.generator.core.template.EnumTemplate; + +/** + * TypeSpec template factory. + */ +public class TypeSpecTemplateFactory extends DefaultTemplateFactory { + @Override + public EnumTemplate getEnumTemplate() { + return TypeSpecEnumTemplate.getInstance(); + } +} From 41673da687f6c8bce47ef23a78eb04488bdb1e32 Mon Sep 17 00:00:00 2001 From: xiaofeicao Date: Thu, 19 Sep 2024 18:04:56 +0800 Subject: [PATCH 05/18] fix mapper bug --- .../http/client/generator/core/mapper/SchemaMapper.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/mapper/SchemaMapper.java b/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/mapper/SchemaMapper.java index 573947b0d4..5bfa5a3814 100644 --- a/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/mapper/SchemaMapper.java +++ b/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/mapper/SchemaMapper.java @@ -50,10 +50,10 @@ public IType map(Schema value) { private IType createSchemaType(Schema value) { if (value instanceof PrimitiveSchema) { return Mappers.getPrimitiveMapper().map((PrimitiveSchema) value); - } else if (value instanceof ChoiceSchema) { - return Mappers.getChoiceMapper().map((ChoiceSchema) value); } else if (value instanceof SealedChoiceSchema) { return Mappers.getSealedChoiceMapper().map((SealedChoiceSchema) value); + } else if (value instanceof ChoiceSchema) { + return Mappers.getChoiceMapper().map((ChoiceSchema) value); } else if (value instanceof ArraySchema) { return Mappers.getArrayMapper().map((ArraySchema) value); } else if (value instanceof DictionarySchema) { From cea7b57575641bbf6523ae4586b6a52f426bc67e Mon Sep 17 00:00:00 2001 From: xiaofeicao Date: Thu, 19 Sep 2024 18:07:53 +0800 Subject: [PATCH 06/18] regen regen arm --- .../CustomTemplateResourceProperties.java | 32 +++++++ .../models/PriorityModel.java | 88 +++++++++++++++++ .../enumservice/EnumServiceAsyncClient.java | 8 ++ .../cadl/enumservice/EnumServiceClient.java | 8 ++ .../implementation/EnumServiceClientImpl.java | 16 ++++ .../models/OlympicRecordModel.java | 95 +++++++++++++++++++ .../cadl/enumservice/models/Operation.java | 67 +++++++++++++ .../enumservice/models/PriorityModel.java | 72 ++++++++++---- .../enumdiscriminator/models/Snake.java | 2 +- .../cadl-enumservice_apiview_properties.json | 1 + 10 files changed, 371 insertions(+), 18 deletions(-) create mode 100644 packages/http-client-java/generator/http-client-generator-test/src/main/java/com/cadl/armresourceprovider/models/PriorityModel.java create mode 100644 packages/http-client-java/generator/http-client-generator-test/src/main/java/com/cadl/enumservice/models/OlympicRecordModel.java diff --git a/packages/http-client-java/generator/http-client-generator-test/src/main/java/com/cadl/armresourceprovider/fluent/models/CustomTemplateResourceProperties.java b/packages/http-client-java/generator/http-client-generator-test/src/main/java/com/cadl/armresourceprovider/fluent/models/CustomTemplateResourceProperties.java index f64a539f5b..80d38de157 100644 --- a/packages/http-client-java/generator/http-client-generator-test/src/main/java/com/cadl/armresourceprovider/fluent/models/CustomTemplateResourceProperties.java +++ b/packages/http-client-java/generator/http-client-generator-test/src/main/java/com/cadl/armresourceprovider/fluent/models/CustomTemplateResourceProperties.java @@ -9,6 +9,7 @@ import com.cadl.armresourceprovider.models.CustomTemplateResourcePropertiesAnonymousEmptyModel; import com.cadl.armresourceprovider.models.Dog; import com.cadl.armresourceprovider.models.EmptyModel; +import com.cadl.armresourceprovider.models.PriorityModel; import com.cadl.armresourceprovider.models.ProvisioningState; import com.fasterxml.jackson.annotation.JsonProperty; @@ -41,6 +42,12 @@ public final class CustomTemplateResourceProperties { @JsonProperty(value = "anonymousEmptyModel", required = true) private CustomTemplateResourcePropertiesAnonymousEmptyModel anonymousEmptyModel; + /* + * The priority property. + */ + @JsonProperty(value = "priority", required = true) + private PriorityModel priority; + /** * Creates an instance of CustomTemplateResourceProperties class. */ @@ -117,6 +124,26 @@ public CustomTemplateResourcePropertiesAnonymousEmptyModel anonymousEmptyModel() return this; } + /** + * Get the priority property: The priority property. + * + * @return the priority value. + */ + public PriorityModel priority() { + return this.priority; + } + + /** + * Set the priority property: The priority property. + * + * @param priority the priority value to set. + * @return the CustomTemplateResourceProperties object itself. + */ + public CustomTemplateResourceProperties withPriority(PriorityModel priority) { + this.priority = priority; + return this; + } + /** * Validates the instance. * @@ -144,6 +171,11 @@ public void validate() { } else { anonymousEmptyModel().validate(); } + if (priority() == null) { + throw LOGGER.atError() + .log(new IllegalArgumentException( + "Missing required property priority in model CustomTemplateResourceProperties")); + } } private static final ClientLogger LOGGER = new ClientLogger(CustomTemplateResourceProperties.class); diff --git a/packages/http-client-java/generator/http-client-generator-test/src/main/java/com/cadl/armresourceprovider/models/PriorityModel.java b/packages/http-client-java/generator/http-client-generator-test/src/main/java/com/cadl/armresourceprovider/models/PriorityModel.java new file mode 100644 index 0000000000..8b3b8ad177 --- /dev/null +++ b/packages/http-client-java/generator/http-client-generator-test/src/main/java/com/cadl/armresourceprovider/models/PriorityModel.java @@ -0,0 +1,88 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. +// Code generated by Microsoft (R) TypeSpec Code Generator. + +package com.cadl.armresourceprovider.models; + +import com.azure.core.util.ExpandableEnum; +import com.fasterxml.jackson.annotation.JsonCreator; +import java.lang.IllegalArgumentException; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; + +/** + * Defines values for PriorityModel. + */ +public final class PriorityModel implements ExpandableEnum { + private static final Map VALUES = new ConcurrentHashMap<>(); + + /** + * Static value 0 for PriorityModel. + */ + public static final PriorityModel HIGH = fromValue(0); + + /** + * Static value 1 for PriorityModel. + */ + public static final PriorityModel LOW = fromValue(1); + + private final Integer value; + + private PriorityModel(Integer value) { + this.value = value; + } + + /** + * Creates or finds a PriorityModel. + * + * @param value a value to look for. + * @return the corresponding PriorityModel. + */ + @JsonCreator + public static PriorityModel fromValue(Integer value) { + if (value == null) { + throw new IllegalArgumentException("value can't be null"); + } + PriorityModel member = VALUES.get(value); + if (member != null) { + return member; + } + return VALUES.computeIfAbsent(value, key -> new PriorityModel(key)); + } + + /** + * Gets known PriorityModel values. + * + * @return Known PriorityModel values. + */ + public static Collection values() { + return new ArrayList<>(VALUES.values()); + } + + /** + * Gets the value of the PriorityModel instance. + * + * @return the value of the PriorityModel instance. + */ + @Override + public Integer getValue() { + return this.value; + } + + @Override + public String toString() { + return getValue().toString(); + } + + @Override + public boolean equals(Object obj) { + return (obj instanceof PriorityModel) && ((PriorityModel) obj).getValue().equals(getValue()); + } + + @Override + public int hashCode() { + return getValue().hashCode(); + } +} diff --git a/packages/http-client-java/generator/http-client-generator-test/src/main/java/com/cadl/enumservice/EnumServiceAsyncClient.java b/packages/http-client-java/generator/http-client-generator-test/src/main/java/com/cadl/enumservice/EnumServiceAsyncClient.java index 51b5c4cfcf..6c98eecc6e 100644 --- a/packages/http-client-java/generator/http-client-generator-test/src/main/java/com/cadl/enumservice/EnumServiceAsyncClient.java +++ b/packages/http-client-java/generator/http-client-generator-test/src/main/java/com/cadl/enumservice/EnumServiceAsyncClient.java @@ -106,6 +106,8 @@ public Mono> getColorModelWithResponse(RequestOptions reque * colorValue: String(Red/Blue/Green) (Required) * colorModelValue: String(Red/Blue/Green) (Required) * unitValue: String(1/0.001/1000) (Optional) + * olympicRecord: String(9.58/19.3) (Optional) + * olympicRecordValue: String(9.58/19.3) (Optional) * } * } * @@ -139,6 +141,8 @@ public Mono> setColorModelWithResponse(String color, Reques * colorValue: String(Red/Blue/Green) (Required) * colorModelValue: String(Red/Blue/Green) (Required) * unitValue: String(1/0.001/1000) (Optional) + * olympicRecord: String(9.58/19.3) (Optional) + * olympicRecordValue: String(9.58/19.3) (Optional) * } * } * @@ -172,6 +176,8 @@ public Mono> setPriorityWithResponse(String priority, Reque * colorValue: String(Red/Blue/Green) (Required) * colorModelValue: String(Red/Blue/Green) (Required) * unitValue: String(1/0.001/1000) (Optional) + * olympicRecord: String(9.58/19.3) (Optional) + * olympicRecordValue: String(9.58/19.3) (Optional) * } * } * @@ -204,6 +210,8 @@ public Mono> getRunningOperationWithResponse(RequestOptions * colorValue: String(Red/Blue/Green) (Required) * colorModelValue: String(Red/Blue/Green) (Required) * unitValue: String(1/0.001/1000) (Optional) + * olympicRecord: String(9.58/19.3) (Optional) + * olympicRecordValue: String(9.58/19.3) (Optional) * } * } * diff --git a/packages/http-client-java/generator/http-client-generator-test/src/main/java/com/cadl/enumservice/EnumServiceClient.java b/packages/http-client-java/generator/http-client-generator-test/src/main/java/com/cadl/enumservice/EnumServiceClient.java index 97b21c8f17..512689a85f 100644 --- a/packages/http-client-java/generator/http-client-generator-test/src/main/java/com/cadl/enumservice/EnumServiceClient.java +++ b/packages/http-client-java/generator/http-client-generator-test/src/main/java/com/cadl/enumservice/EnumServiceClient.java @@ -104,6 +104,8 @@ public Response getColorModelWithResponse(RequestOptions requestOpti * colorValue: String(Red/Blue/Green) (Required) * colorModelValue: String(Red/Blue/Green) (Required) * unitValue: String(1/0.001/1000) (Optional) + * olympicRecord: String(9.58/19.3) (Optional) + * olympicRecordValue: String(9.58/19.3) (Optional) * } * } * @@ -137,6 +139,8 @@ public Response setColorModelWithResponse(String color, RequestOptio * colorValue: String(Red/Blue/Green) (Required) * colorModelValue: String(Red/Blue/Green) (Required) * unitValue: String(1/0.001/1000) (Optional) + * olympicRecord: String(9.58/19.3) (Optional) + * olympicRecordValue: String(9.58/19.3) (Optional) * } * } * @@ -170,6 +174,8 @@ public Response setPriorityWithResponse(String priority, RequestOpti * colorValue: String(Red/Blue/Green) (Required) * colorModelValue: String(Red/Blue/Green) (Required) * unitValue: String(1/0.001/1000) (Optional) + * olympicRecord: String(9.58/19.3) (Optional) + * olympicRecordValue: String(9.58/19.3) (Optional) * } * } * @@ -202,6 +208,8 @@ public Response getRunningOperationWithResponse(RequestOptions reque * colorValue: String(Red/Blue/Green) (Required) * colorModelValue: String(Red/Blue/Green) (Required) * unitValue: String(1/0.001/1000) (Optional) + * olympicRecord: String(9.58/19.3) (Optional) + * olympicRecordValue: String(9.58/19.3) (Optional) * } * } * diff --git a/packages/http-client-java/generator/http-client-generator-test/src/main/java/com/cadl/enumservice/implementation/EnumServiceClientImpl.java b/packages/http-client-java/generator/http-client-generator-test/src/main/java/com/cadl/enumservice/implementation/EnumServiceClientImpl.java index 12b6c27112..f4d3bd00df 100644 --- a/packages/http-client-java/generator/http-client-generator-test/src/main/java/com/cadl/enumservice/implementation/EnumServiceClientImpl.java +++ b/packages/http-client-java/generator/http-client-generator-test/src/main/java/com/cadl/enumservice/implementation/EnumServiceClientImpl.java @@ -526,6 +526,8 @@ public Response getColorModelWithResponse(RequestOptions requestOpti * colorValue: String(Red/Blue/Green) (Required) * colorModelValue: String(Red/Blue/Green) (Required) * unitValue: String(1/0.001/1000) (Optional) + * olympicRecord: String(9.58/19.3) (Optional) + * olympicRecordValue: String(9.58/19.3) (Optional) * } * } * @@ -560,6 +562,8 @@ public Mono> setColorModelWithResponseAsync(String color, R * colorValue: String(Red/Blue/Green) (Required) * colorModelValue: String(Red/Blue/Green) (Required) * unitValue: String(1/0.001/1000) (Optional) + * olympicRecord: String(9.58/19.3) (Optional) + * olympicRecordValue: String(9.58/19.3) (Optional) * } * } * @@ -593,6 +597,8 @@ public Response setColorModelWithResponse(String color, RequestOptio * colorValue: String(Red/Blue/Green) (Required) * colorModelValue: String(Red/Blue/Green) (Required) * unitValue: String(1/0.001/1000) (Optional) + * olympicRecord: String(9.58/19.3) (Optional) + * olympicRecordValue: String(9.58/19.3) (Optional) * } * } * @@ -627,6 +633,8 @@ public Mono> setPriorityWithResponseAsync(String priority, * colorValue: String(Red/Blue/Green) (Required) * colorModelValue: String(Red/Blue/Green) (Required) * unitValue: String(1/0.001/1000) (Optional) + * olympicRecord: String(9.58/19.3) (Optional) + * olympicRecordValue: String(9.58/19.3) (Optional) * } * } * @@ -660,6 +668,8 @@ public Response setPriorityWithResponse(String priority, RequestOpti * colorValue: String(Red/Blue/Green) (Required) * colorModelValue: String(Red/Blue/Green) (Required) * unitValue: String(1/0.001/1000) (Optional) + * olympicRecord: String(9.58/19.3) (Optional) + * olympicRecordValue: String(9.58/19.3) (Optional) * } * } * @@ -694,6 +704,8 @@ public Mono> getRunningOperationWithResponseAsync(RequestOp * colorValue: String(Red/Blue/Green) (Required) * colorModelValue: String(Red/Blue/Green) (Required) * unitValue: String(1/0.001/1000) (Optional) + * olympicRecord: String(9.58/19.3) (Optional) + * olympicRecordValue: String(9.58/19.3) (Optional) * } * } * @@ -727,6 +739,8 @@ public Response getRunningOperationWithResponse(RequestOptions reque * colorValue: String(Red/Blue/Green) (Required) * colorModelValue: String(Red/Blue/Green) (Required) * unitValue: String(1/0.001/1000) (Optional) + * olympicRecord: String(9.58/19.3) (Optional) + * olympicRecordValue: String(9.58/19.3) (Optional) * } * } * @@ -761,6 +775,8 @@ public Mono> getOperationWithResponseAsync(String state, Re * colorValue: String(Red/Blue/Green) (Required) * colorModelValue: String(Red/Blue/Green) (Required) * unitValue: String(1/0.001/1000) (Optional) + * olympicRecord: String(9.58/19.3) (Optional) + * olympicRecordValue: String(9.58/19.3) (Optional) * } * } * diff --git a/packages/http-client-java/generator/http-client-generator-test/src/main/java/com/cadl/enumservice/models/OlympicRecordModel.java b/packages/http-client-java/generator/http-client-generator-test/src/main/java/com/cadl/enumservice/models/OlympicRecordModel.java new file mode 100644 index 0000000000..24c16510f3 --- /dev/null +++ b/packages/http-client-java/generator/http-client-generator-test/src/main/java/com/cadl/enumservice/models/OlympicRecordModel.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 com.cadl.enumservice.models; + +import com.azure.core.annotation.Generated; +import com.azure.core.util.ExpandableEnum; +import java.lang.IllegalArgumentException; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; + +/** + * Defines values for OlympicRecordModel. + */ +public final class OlympicRecordModel implements ExpandableEnum { + private static final Map VALUES = new ConcurrentHashMap<>(); + + /** + * Static value 9.58 for OlympicRecordModel. + */ + @Generated + public static final OlympicRecordModel OLYMPIC_100_METERS = fromValue(9.58); + + /** + * Static value 19.3 for OlympicRecordModel. + */ + @Generated + public static final OlympicRecordModel OLYMPIC_200_METERS = fromValue(19.3); + + private final Double value; + + private OlympicRecordModel(Double value) { + this.value = value; + } + + /** + * Creates or finds a OlympicRecordModel. + * + * @param value a value to look for. + * @return the corresponding OlympicRecordModel. + */ + @Generated + public static OlympicRecordModel fromValue(Double value) { + if (value == null) { + throw new IllegalArgumentException("value can't be null"); + } + OlympicRecordModel member = VALUES.get(value); + if (member != null) { + return member; + } + return VALUES.computeIfAbsent(value, key -> new OlympicRecordModel(key)); + } + + /** + * Gets known OlympicRecordModel values. + * + * @return Known OlympicRecordModel values. + */ + @Generated + public static Collection values() { + return new ArrayList<>(VALUES.values()); + } + + /** + * Gets the value of the OlympicRecordModel instance. + * + * @return the value of the OlympicRecordModel instance. + */ + @Generated + @Override + public Double getValue() { + return this.value; + } + + @Generated + @Override + public String toString() { + return getValue().toString(); + } + + @Generated + @Override + public boolean equals(Object obj) { + return (obj instanceof OlympicRecordModel) && ((OlympicRecordModel) obj).getValue().equals(getValue()); + } + + @Generated + @Override + public int hashCode() { + return getValue().hashCode(); + } +} diff --git a/packages/http-client-java/generator/http-client-generator-test/src/main/java/com/cadl/enumservice/models/Operation.java b/packages/http-client-java/generator/http-client-generator-test/src/main/java/com/cadl/enumservice/models/Operation.java index ccadb18682..9d7002f5a5 100644 --- a/packages/http-client-java/generator/http-client-generator-test/src/main/java/com/cadl/enumservice/models/Operation.java +++ b/packages/http-client-java/generator/http-client-generator-test/src/main/java/com/cadl/enumservice/models/Operation.java @@ -77,6 +77,18 @@ public final class Operation implements JsonSerializable { @Generated private Unit unitValue; + /* + * The olympicRecord property. + */ + @Generated + private OlympicRecordModel olympicRecord; + + /* + * The olympicRecordValue property. + */ + @Generated + private OlympicRecordModel olympicRecordValue; + /** * Creates an instance of Operation class. * @@ -205,6 +217,50 @@ public Operation setUnitValue(Unit unitValue) { return this; } + /** + * Get the olympicRecord property: The olympicRecord property. + * + * @return the olympicRecord value. + */ + @Generated + public OlympicRecordModel getOlympicRecord() { + return this.olympicRecord; + } + + /** + * Set the olympicRecord property: The olympicRecord property. + * + * @param olympicRecord the olympicRecord value to set. + * @return the Operation object itself. + */ + @Generated + public Operation setOlympicRecord(OlympicRecordModel olympicRecord) { + this.olympicRecord = olympicRecord; + return this; + } + + /** + * Get the olympicRecordValue property: The olympicRecordValue property. + * + * @return the olympicRecordValue value. + */ + @Generated + public OlympicRecordModel getOlympicRecordValue() { + return this.olympicRecordValue; + } + + /** + * Set the olympicRecordValue property: The olympicRecordValue property. + * + * @param olympicRecordValue the olympicRecordValue value to set. + * @return the Operation object itself. + */ + @Generated + public Operation setOlympicRecordValue(OlympicRecordModel olympicRecordValue) { + this.olympicRecordValue = olympicRecordValue; + return this; + } + /** * {@inheritDoc} */ @@ -223,6 +279,9 @@ public JsonWriter toJson(JsonWriter jsonWriter) throws IOException { jsonWriter.writeStringField("colorModelValue", this.colorModelValue == null ? null : this.colorModelValue.toString()); jsonWriter.writeNumberField("unitValue", this.unitValue == null ? null : this.unitValue.toDouble()); + jsonWriter.writeNumberField("olympicRecord", this.olympicRecord == null ? null : this.olympicRecord.getValue()); + jsonWriter.writeNumberField("olympicRecordValue", + this.olympicRecordValue == null ? null : this.olympicRecordValue.getValue()); return jsonWriter.writeEndObject(); } @@ -243,6 +302,8 @@ public static Operation fromJson(JsonReader jsonReader) throws IOException { ColorModel color = null; Unit unit = null; Unit unitValue = null; + OlympicRecordModel olympicRecord = null; + OlympicRecordModel olympicRecordValue = null; while (reader.nextToken() != JsonToken.END_OBJECT) { String fieldName = reader.getFieldName(); reader.nextToken(); @@ -257,12 +318,18 @@ public static Operation fromJson(JsonReader jsonReader) throws IOException { unit = Unit.fromDouble(reader.getDouble()); } else if ("unitValue".equals(fieldName)) { unitValue = Unit.fromDouble(reader.getDouble()); + } else if ("olympicRecord".equals(fieldName)) { + olympicRecord = OlympicRecordModel.fromValue(reader.getDouble()); + } else if ("olympicRecordValue".equals(fieldName)) { + olympicRecordValue = OlympicRecordModel.fromValue(reader.getDouble()); } else { reader.skipChildren(); } } Operation deserializedOperation = new Operation(name, priority, color, unit); deserializedOperation.unitValue = unitValue; + deserializedOperation.olympicRecord = olympicRecord; + deserializedOperation.olympicRecordValue = olympicRecordValue; return deserializedOperation; }); diff --git a/packages/http-client-java/generator/http-client-generator-test/src/main/java/com/cadl/enumservice/models/PriorityModel.java b/packages/http-client-java/generator/http-client-generator-test/src/main/java/com/cadl/enumservice/models/PriorityModel.java index edfd132508..d3137131fe 100644 --- a/packages/http-client-java/generator/http-client-generator-test/src/main/java/com/cadl/enumservice/models/PriorityModel.java +++ b/packages/http-client-java/generator/http-client-generator-test/src/main/java/com/cadl/enumservice/models/PriorityModel.java @@ -5,53 +5,91 @@ package com.cadl.enumservice.models; import com.azure.core.annotation.Generated; -import com.azure.core.util.ExpandableStringEnum; +import com.azure.core.util.ExpandableEnum; +import java.lang.IllegalArgumentException; +import java.util.ArrayList; import java.util.Collection; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; /** * Defines values for PriorityModel. */ -public final class PriorityModel extends ExpandableStringEnum { +public final class PriorityModel implements ExpandableEnum { + private static final Map VALUES = new ConcurrentHashMap<>(); + /** * Static value 100 for PriorityModel. */ @Generated - public static final PriorityModel HIGH = fromInt(100); + public static final PriorityModel HIGH = fromValue(100); /** * Static value 0 for PriorityModel. */ @Generated - public static final PriorityModel LOW = fromInt(0); + public static final PriorityModel LOW = fromValue(0); + + private final Integer value; + + private PriorityModel(Integer value) { + this.value = value; + } /** - * Creates a new instance of PriorityModel value. + * Creates or finds a PriorityModel. * - * @deprecated Use the {@link #fromInt(int)} factory method. + * @param value a value to look for. + * @return the corresponding PriorityModel. */ @Generated - @Deprecated - public PriorityModel() { + public static PriorityModel fromValue(Integer value) { + if (value == null) { + throw new IllegalArgumentException("value can't be null"); + } + PriorityModel member = VALUES.get(value); + if (member != null) { + return member; + } + return VALUES.computeIfAbsent(value, key -> new PriorityModel(key)); } /** - * Creates or finds a PriorityModel from its string representation. + * Gets known PriorityModel values. * - * @param name a name to look for. - * @return the corresponding PriorityModel. + * @return Known PriorityModel values. */ @Generated - public static PriorityModel fromInt(int name) { - return fromString(String.valueOf(name), PriorityModel.class); + public static Collection values() { + return new ArrayList<>(VALUES.values()); } /** - * Gets known PriorityModel values. + * Gets the value of the PriorityModel instance. * - * @return known PriorityModel values. + * @return the value of the PriorityModel instance. */ @Generated - public static Collection values() { - return values(PriorityModel.class); + @Override + public Integer getValue() { + return this.value; + } + + @Generated + @Override + public String toString() { + return getValue().toString(); + } + + @Generated + @Override + public boolean equals(Object obj) { + return (obj instanceof PriorityModel) && ((PriorityModel) obj).getValue().equals(getValue()); + } + + @Generated + @Override + public int hashCode() { + return getValue().hashCode(); } } diff --git a/packages/http-client-java/generator/http-client-generator-test/src/main/java/com/type/model/inheritance/enumdiscriminator/models/Snake.java b/packages/http-client-java/generator/http-client-generator-test/src/main/java/com/type/model/inheritance/enumdiscriminator/models/Snake.java index cea781f69a..062f4a8d3c 100644 --- a/packages/http-client-java/generator/http-client-generator-test/src/main/java/com/type/model/inheritance/enumdiscriminator/models/Snake.java +++ b/packages/http-client-java/generator/http-client-generator-test/src/main/java/com/type/model/inheritance/enumdiscriminator/models/Snake.java @@ -21,7 +21,7 @@ public class Snake implements JsonSerializable { * discriminator property */ @Generated - private SnakeKind kind = SnakeKind.fromString("Snake"); + private SnakeKind kind; /* * Length of the snake diff --git a/packages/http-client-java/generator/http-client-generator-test/src/main/resources/META-INF/cadl-enumservice_apiview_properties.json b/packages/http-client-java/generator/http-client-generator-test/src/main/resources/META-INF/cadl-enumservice_apiview_properties.json index c1425196a7..aa079535b9 100644 --- a/packages/http-client-java/generator/http-client-generator-test/src/main/resources/META-INF/cadl-enumservice_apiview_properties.json +++ b/packages/http-client-java/generator/http-client-generator-test/src/main/resources/META-INF/cadl-enumservice_apiview_properties.json @@ -66,6 +66,7 @@ "com.cadl.enumservice.EnumServiceClientBuilder": "Cadl.EnumService.EnumOp", "com.cadl.enumservice.models.Color": "Cadl.EnumService.Color", "com.cadl.enumservice.models.ColorModel": "Cadl.EnumService.ColorModel", + "com.cadl.enumservice.models.OlympicRecordModel": "Cadl.EnumService.OlympicRecordModel", "com.cadl.enumservice.models.Operation": "Cadl.EnumService.Operation", "com.cadl.enumservice.models.OperationName": "Operation.name.anonymous", "com.cadl.enumservice.models.OperationStateValues": "Cadl.EnumService.OperationStateValues", From 4e7cab1eabee155d614b44948b49f8d3d236739f Mon Sep 17 00:00:00 2001 From: xiaofeicao Date: Fri, 20 Sep 2024 15:19:20 +0800 Subject: [PATCH 07/18] fix swagger mgmt --- .../http/client/generator/mgmt/mapper/FluentChoiceMapper.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/http-client-java/generator/http-client-generator-mgmt/src/main/java/com/microsoft/typespec/http/client/generator/mgmt/mapper/FluentChoiceMapper.java b/packages/http-client-java/generator/http-client-generator-mgmt/src/main/java/com/microsoft/typespec/http/client/generator/mgmt/mapper/FluentChoiceMapper.java index e6268fb02f..d31edb6306 100644 --- a/packages/http-client-java/generator/http-client-generator-mgmt/src/main/java/com/microsoft/typespec/http/client/generator/mgmt/mapper/FluentChoiceMapper.java +++ b/packages/http-client-java/generator/http-client-generator-mgmt/src/main/java/com/microsoft/typespec/http/client/generator/mgmt/mapper/FluentChoiceMapper.java @@ -18,6 +18,6 @@ public static FluentChoiceMapper getInstance() { @Override public IType map(ChoiceSchema enumType) { - return MapperUtils.createEnumType(enumType, true, false); + return MapperUtils.createEnumType(enumType, true, false, "getValue", null); } } From e8454cf826a228abcfcf402f1f5579d67c9ba99b Mon Sep 17 00:00:00 2001 From: xiaofeicao Date: Fri, 20 Sep 2024 16:26:36 +0800 Subject: [PATCH 08/18] fix swagger serialization --- .../generator/core/mapper/ChoiceMapper.java | 3 +- .../generator/core/mapper/MapperUtils.java | 6 ++-- .../core/model/clientmodel/EnumType.java | 33 ++++++++++++++----- .../mgmt/mapper/FluentChoiceMapper.java | 3 +- .../mapper/TypeSpecChoiceMapper.java | 2 +- 5 files changed, 33 insertions(+), 14 deletions(-) diff --git a/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/mapper/ChoiceMapper.java b/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/mapper/ChoiceMapper.java index 285bab88aa..e4454febb6 100644 --- a/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/mapper/ChoiceMapper.java +++ b/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/mapper/ChoiceMapper.java @@ -4,6 +4,7 @@ package com.microsoft.typespec.http.client.generator.core.mapper; import com.microsoft.typespec.http.client.generator.core.extension.model.codemodel.ChoiceSchema; +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.IType; @@ -47,6 +48,6 @@ public IType map(ChoiceSchema enumType) { } private IType createChoiceType(ChoiceSchema enumType) { - return MapperUtils.createEnumType(enumType, true, true); + return MapperUtils.createEnumType(enumType, true, true, "getValue", null, ClassType.STRING); } } diff --git a/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/mapper/MapperUtils.java b/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/mapper/MapperUtils.java index eab48abe10..24f9bbba0d 100644 --- a/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/mapper/MapperUtils.java +++ b/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/mapper/MapperUtils.java @@ -37,7 +37,7 @@ public final class MapperUtils { * @return enum client type */ public static IType createEnumType(ChoiceSchema enumType, boolean expandable, boolean useCodeModelNameForEnumMember) { - return createEnumType(enumType, expandable, useCodeModelNameForEnumMember, null, null); + return createEnumType(enumType, expandable, useCodeModelNameForEnumMember, null, null, null); } /** @@ -48,9 +48,10 @@ public static IType createEnumType(ChoiceSchema enumType, boolean expandable, bo * @param useCodeModelNameForEnumMember whether to use code model enum member name for client enum member name * @param serializationMethodName method name for serialization * @param deserializationMethodName method name for deserialization + * @param wireType wire type for serialization, if null, will default to element type of the enum * @return enum client type */ - public static IType createEnumType(ChoiceSchema enumType, boolean expandable, boolean useCodeModelNameForEnumMember, String serializationMethodName, String deserializationMethodName) { + public static IType createEnumType(ChoiceSchema enumType, boolean expandable, boolean useCodeModelNameForEnumMember, String serializationMethodName, String deserializationMethodName, IType wireType) { JavaSettings settings = JavaSettings.getInstance(); String enumTypeName = enumType.getLanguage().getJava().getName(); @@ -109,6 +110,7 @@ public static IType createEnumType(ChoiceSchema enumType, boolean expandable, bo .crossLanguageDefinitionId(enumType.getCrossLanguageDefinitionId()) .fromMethodName(deserializationMethodName) .toMethodName(serializationMethodName) + .wireType(wireType) .build(); } } diff --git a/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/model/clientmodel/EnumType.java b/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/model/clientmodel/EnumType.java index 21140eea9c..4414dc4496 100644 --- a/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/model/clientmodel/EnumType.java +++ b/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/model/clientmodel/EnumType.java @@ -38,22 +38,24 @@ public class EnumType implements IType { private String crossLanguageDefinitionId; private final String fromMethodName; private final String toMethodName; + private final IType wireType; /** * Create a new Enum with the provided properties. * - * @param name The name of the new Enum. - * @param description The description of the Enum. - * @param expandable Whether this will be an ExpandableStringEnum type. - * @param values The values of the Enum. - * @param fromMethodName The method name used to convert JSON to the enum type. - * @param toMethodName The method name used to convert the enum type to JSON. + * @param name The name of the new Enum. + * @param description The description of the Enum. + * @param expandable Whether this will be an ExpandableStringEnum type. + * @param values The values of the Enum. + * @param fromMethodName The method name used to convert JSON to the enum type. + * @param toMethodName The method name used to convert the enum type to JSON. + * @param wireType The actual wire type in JSON form. */ private EnumType(String packageKeyword, String name, String description, boolean expandable, List values, IType elementType, ImplementationDetails implementationDetails, - String crossLanguageDefinitionId, String fromMethodName, String toMethodName) { + String crossLanguageDefinitionId, String fromMethodName, String toMethodName, IType wireType) { this.name = name; this.packageName = packageKeyword; this.description = description; @@ -64,6 +66,7 @@ private EnumType(String packageKeyword, String name, String description, this.crossLanguageDefinitionId = crossLanguageDefinitionId; this.fromMethodName = fromMethodName; this.toMethodName = toMethodName; + this.wireType = wireType; } public String getCrossLanguageDefinitionId() { @@ -202,7 +205,7 @@ public String jsonSerializationMethodCall(String jsonWriterName, String fieldNam ? valueGetter + "." + getToMethodName() + "()" : valueGetter + " == null ? null : " + valueGetter + "." + getToMethodName() + "()"; - return elementType.asNullable().jsonSerializationMethodCall(jsonWriterName, fieldName, actualValueGetter, + return wireType.asNullable().jsonSerializationMethodCall(jsonWriterName, fieldName, actualValueGetter, jsonMergePatch); } @@ -245,6 +248,7 @@ public static class Builder { private String crossLanguageDefinitionId; private String fromMethodName; private String toMethodName; + private IType wireType; /** * Sets the name of the Enum. @@ -333,10 +337,19 @@ public Builder toMethodName(String toMethodName) { return this; } + public Builder wireType(IType wireType) { + this.wireType = wireType; + return this; + } + /** * @return an immutable EnumType instance with the configurations on this builder. */ public EnumType build() { + IType wireType = this.wireType; + if (wireType == null) { + wireType = elementType; + } return new EnumType( packageName, name, @@ -346,7 +359,9 @@ public EnumType build() { elementType, implementationDetails, crossLanguageDefinitionId, - fromMethodName, toMethodName); + fromMethodName, + toMethodName, + wireType); } } } diff --git a/packages/http-client-java/generator/http-client-generator-mgmt/src/main/java/com/microsoft/typespec/http/client/generator/mgmt/mapper/FluentChoiceMapper.java b/packages/http-client-java/generator/http-client-generator-mgmt/src/main/java/com/microsoft/typespec/http/client/generator/mgmt/mapper/FluentChoiceMapper.java index d31edb6306..79080d63f0 100644 --- a/packages/http-client-java/generator/http-client-generator-mgmt/src/main/java/com/microsoft/typespec/http/client/generator/mgmt/mapper/FluentChoiceMapper.java +++ b/packages/http-client-java/generator/http-client-generator-mgmt/src/main/java/com/microsoft/typespec/http/client/generator/mgmt/mapper/FluentChoiceMapper.java @@ -6,6 +6,7 @@ import com.microsoft.typespec.http.client.generator.core.extension.model.codemodel.ChoiceSchema; import com.microsoft.typespec.http.client.generator.core.mapper.ChoiceMapper; import com.microsoft.typespec.http.client.generator.core.mapper.MapperUtils; +import com.microsoft.typespec.http.client.generator.core.model.clientmodel.ClassType; import com.microsoft.typespec.http.client.generator.core.model.clientmodel.IType; public class FluentChoiceMapper extends ChoiceMapper { @@ -18,6 +19,6 @@ public static FluentChoiceMapper getInstance() { @Override public IType map(ChoiceSchema enumType) { - return MapperUtils.createEnumType(enumType, true, false, "getValue", null); + return MapperUtils.createEnumType(enumType, true, false, "getValue", null, ClassType.STRING); } } diff --git a/packages/http-client-java/generator/http-client-generator/src/main/java/com/microsoft/typespec/http/client/generator/mapper/TypeSpecChoiceMapper.java b/packages/http-client-java/generator/http-client-generator/src/main/java/com/microsoft/typespec/http/client/generator/mapper/TypeSpecChoiceMapper.java index a9b7b15782..8200440010 100644 --- a/packages/http-client-java/generator/http-client-generator/src/main/java/com/microsoft/typespec/http/client/generator/mapper/TypeSpecChoiceMapper.java +++ b/packages/http-client-java/generator/http-client-generator/src/main/java/com/microsoft/typespec/http/client/generator/mapper/TypeSpecChoiceMapper.java @@ -23,7 +23,7 @@ public IType map(ChoiceSchema enumType) { if (isStringEnum) { return MapperUtils.createEnumType(enumType, true, true); } else { - return MapperUtils.createEnumType(enumType, true, true, "getValue", "fromValue"); + return MapperUtils.createEnumType(enumType, true, true, "getValue", "fromValue", null); } } } From 0ff5db124970d6d37cd1cfcb19e90743e10c1007 Mon Sep 17 00:00:00 2001 From: XiaofeiCao Date: Mon, 23 Sep 2024 17:38:30 +0800 Subject: [PATCH 09/18] fix diff --- .../models/CustomTemplateResourceInner.java | 24 +++++++++++++++++++ .../CustomTemplateResourceImpl.java | 10 ++++++++ .../models/CustomTemplateResource.java | 22 ++++++++++++++++- .../reflect-config.json | 2 +- 4 files changed, 56 insertions(+), 2 deletions(-) diff --git a/packages/http-client-java/generator/http-client-generator-test/src/main/java/com/cadl/armresourceprovider/fluent/models/CustomTemplateResourceInner.java b/packages/http-client-java/generator/http-client-generator-test/src/main/java/com/cadl/armresourceprovider/fluent/models/CustomTemplateResourceInner.java index 5848e6a531..77bc1b99a8 100644 --- a/packages/http-client-java/generator/http-client-generator-test/src/main/java/com/cadl/armresourceprovider/fluent/models/CustomTemplateResourceInner.java +++ b/packages/http-client-java/generator/http-client-generator-test/src/main/java/com/cadl/armresourceprovider/fluent/models/CustomTemplateResourceInner.java @@ -11,6 +11,7 @@ import com.cadl.armresourceprovider.models.Dog; import com.cadl.armresourceprovider.models.EmptyModel; import com.cadl.armresourceprovider.models.ManagedServiceIdentity; +import com.cadl.armresourceprovider.models.PriorityModel; import com.cadl.armresourceprovider.models.ProvisioningState; import com.fasterxml.jackson.annotation.JsonProperty; import java.util.Map; @@ -179,6 +180,29 @@ public CustomTemplateResourcePropertiesAnonymousEmptyModel anonymousEmptyModel() return this; } + /** + * Get the priority property: The priority property. + * + * @return the priority value. + */ + public PriorityModel priority() { + return this.innerProperties() == null ? null : this.innerProperties().priority(); + } + + /** + * Set the priority property: The priority property. + * + * @param priority the priority value to set. + * @return the CustomTemplateResourceInner object itself. + */ + public CustomTemplateResourceInner withPriority(PriorityModel priority) { + if (this.innerProperties() == null) { + this.innerProperties = new CustomTemplateResourceProperties(); + } + this.innerProperties().withPriority(priority); + return this; + } + /** * Validates the instance. * diff --git a/packages/http-client-java/generator/http-client-generator-test/src/main/java/com/cadl/armresourceprovider/implementation/CustomTemplateResourceImpl.java b/packages/http-client-java/generator/http-client-generator-test/src/main/java/com/cadl/armresourceprovider/implementation/CustomTemplateResourceImpl.java index 7e155b1660..5a372fa11b 100644 --- a/packages/http-client-java/generator/http-client-generator-test/src/main/java/com/cadl/armresourceprovider/implementation/CustomTemplateResourceImpl.java +++ b/packages/http-client-java/generator/http-client-generator-test/src/main/java/com/cadl/armresourceprovider/implementation/CustomTemplateResourceImpl.java @@ -14,6 +14,7 @@ import com.cadl.armresourceprovider.models.Dog; import com.cadl.armresourceprovider.models.EmptyModel; import com.cadl.armresourceprovider.models.ManagedServiceIdentity; +import com.cadl.armresourceprovider.models.PriorityModel; import com.cadl.armresourceprovider.models.ProvisioningState; import java.util.Collections; import java.util.Map; @@ -73,6 +74,10 @@ public CustomTemplateResourcePropertiesAnonymousEmptyModel anonymousEmptyModel() return this.innerModel().anonymousEmptyModel(); } + public PriorityModel priority() { + return this.innerModel().priority(); + } + public Region region() { return Region.fromName(this.regionName()); } @@ -201,6 +206,11 @@ public CustomTemplateResourceImpl withNamedEmptyModel(EmptyModel namedEmptyModel return this; } + public CustomTemplateResourceImpl withPriority(PriorityModel priority) { + this.innerModel().withPriority(priority); + return this; + } + public CustomTemplateResourceImpl withIfMatch(String ifMatch) { this.createIfMatch = ifMatch; return this; diff --git a/packages/http-client-java/generator/http-client-generator-test/src/main/java/com/cadl/armresourceprovider/models/CustomTemplateResource.java b/packages/http-client-java/generator/http-client-generator-test/src/main/java/com/cadl/armresourceprovider/models/CustomTemplateResource.java index 5414d91d9e..189c955ee1 100644 --- a/packages/http-client-java/generator/http-client-generator-test/src/main/java/com/cadl/armresourceprovider/models/CustomTemplateResource.java +++ b/packages/http-client-java/generator/http-client-generator-test/src/main/java/com/cadl/armresourceprovider/models/CustomTemplateResource.java @@ -91,6 +91,13 @@ public interface CustomTemplateResource { */ CustomTemplateResourcePropertiesAnonymousEmptyModel anonymousEmptyModel(); + /** + * Gets the priority property: The priority property. + * + * @return the priority value. + */ + PriorityModel priority(); + /** * Gets the region of the resource. * @@ -176,7 +183,7 @@ interface WithResourceGroup { */ interface WithCreate extends DefinitionStages.WithTags, DefinitionStages.WithIdentity, DefinitionStages.WithDog, DefinitionStages.WithNamedEmptyModel, DefinitionStages.WithAnonymousEmptyModel, - DefinitionStages.WithIfMatch, DefinitionStages.WithIfNoneMatch { + DefinitionStages.WithPriority, DefinitionStages.WithIfMatch, DefinitionStages.WithIfNoneMatch { /** * Executes the create request. * @@ -258,6 +265,19 @@ interface WithAnonymousEmptyModel { WithCreate withAnonymousEmptyModel(CustomTemplateResourcePropertiesAnonymousEmptyModel anonymousEmptyModel); } + /** + * The stage of the CustomTemplateResource definition allowing to specify priority. + */ + interface WithPriority { + /** + * Specifies the priority property: The priority property.. + * + * @param priority The priority property. + * @return the next definition stage. + */ + WithCreate withPriority(PriorityModel priority); + } + /** * The stage of the CustomTemplateResource definition allowing to specify ifMatch. */ diff --git a/packages/http-client-java/generator/http-client-generator-test/src/main/resources/META-INF/native-image/com.azure.resourcemanager/azure-resourcemanager-armresourceprovider-generated/reflect-config.json b/packages/http-client-java/generator/http-client-generator-test/src/main/resources/META-INF/native-image/com.azure.resourcemanager/azure-resourcemanager-armresourceprovider-generated/reflect-config.json index 95474aab06..3e2f29f312 100644 --- a/packages/http-client-java/generator/http-client-generator-test/src/main/resources/META-INF/native-image/com.azure.resourcemanager/azure-resourcemanager-armresourceprovider-generated/reflect-config.json +++ b/packages/http-client-java/generator/http-client-generator-test/src/main/resources/META-INF/native-image/com.azure.resourcemanager/azure-resourcemanager-armresourceprovider-generated/reflect-config.json @@ -1 +1 @@ -[{"name":"com.cadl.armresourceprovider.fluent.models.ChildExtensionResourceInner","allDeclaredConstructors":true,"allDeclaredFields":true,"allDeclaredMethods":true},{"name":"com.cadl.armresourceprovider.fluent.models.ChildResourceInner","allDeclaredConstructors":true,"allDeclaredFields":true,"allDeclaredMethods":true},{"name":"com.cadl.armresourceprovider.fluent.models.ChildResourceProperties","allDeclaredConstructors":true,"allDeclaredFields":true,"allDeclaredMethods":true},{"name":"com.cadl.armresourceprovider.fluent.models.CustomTemplateResourceInner","allDeclaredConstructors":true,"allDeclaredFields":true,"allDeclaredMethods":true},{"name":"com.cadl.armresourceprovider.fluent.models.CustomTemplateResourceProperties","allDeclaredConstructors":true,"allDeclaredFields":true,"allDeclaredMethods":true},{"name":"com.cadl.armresourceprovider.fluent.models.OperationInner","allDeclaredConstructors":true,"allDeclaredFields":true,"allDeclaredMethods":true},{"name":"com.cadl.armresourceprovider.fluent.models.ResultInner","allDeclaredConstructors":true,"allDeclaredFields":true,"allDeclaredMethods":true},{"name":"com.cadl.armresourceprovider.fluent.models.TopLevelArmResourceInner","allDeclaredConstructors":true,"allDeclaredFields":true,"allDeclaredMethods":true},{"name":"com.cadl.armresourceprovider.fluent.models.TopLevelArmResourceProperties","allDeclaredConstructors":true,"allDeclaredFields":true,"allDeclaredMethods":true},{"name":"com.cadl.armresourceprovider.fluent.models.TopLevelArmResourceUpdateProperties","allDeclaredConstructors":true,"allDeclaredFields":true,"allDeclaredMethods":true},{"name":"com.cadl.armresourceprovider.implementation.models.ChildExtensionResourceListResult","allDeclaredConstructors":true,"allDeclaredFields":true,"allDeclaredMethods":true},{"name":"com.cadl.armresourceprovider.implementation.models.ChildResourceListResult","allDeclaredConstructors":true,"allDeclaredFields":true,"allDeclaredMethods":true},{"name":"com.cadl.armresourceprovider.implementation.models.OperationListResult","allDeclaredConstructors":true,"allDeclaredFields":true,"allDeclaredMethods":true},{"name":"com.cadl.armresourceprovider.implementation.models.TopLevelArmResourceListResult","allDeclaredConstructors":true,"allDeclaredFields":true,"allDeclaredMethods":true},{"name":"com.cadl.armresourceprovider.models.ActionType","allDeclaredConstructors":true,"allDeclaredFields":true,"allDeclaredMethods":true},{"name":"com.cadl.armresourceprovider.models.ChildExtensionResourceProperties","allDeclaredConstructors":true,"allDeclaredFields":true,"allDeclaredMethods":true},{"name":"com.cadl.armresourceprovider.models.ChildExtensionResourceUpdate","allDeclaredConstructors":true,"allDeclaredFields":true,"allDeclaredMethods":true},{"name":"com.cadl.armresourceprovider.models.ChildResourceUpdate","allDeclaredConstructors":true,"allDeclaredFields":true,"allDeclaredMethods":true},{"name":"com.cadl.armresourceprovider.models.CustomTemplateResourcePatch","allDeclaredConstructors":true,"allDeclaredFields":true,"allDeclaredMethods":true},{"name":"com.cadl.armresourceprovider.models.CustomTemplateResourcePropertiesAnonymousEmptyModel","allDeclaredConstructors":true,"allDeclaredFields":true,"allDeclaredMethods":true},{"name":"com.cadl.armresourceprovider.models.Dog","allDeclaredConstructors":true,"allDeclaredFields":true,"allDeclaredMethods":true},{"name":"com.cadl.armresourceprovider.models.DogKind","allDeclaredConstructors":true,"allDeclaredFields":true,"allDeclaredMethods":true},{"name":"com.cadl.armresourceprovider.models.EmptyModel","allDeclaredConstructors":true,"allDeclaredFields":true,"allDeclaredMethods":true},{"name":"com.cadl.armresourceprovider.models.Golden","allDeclaredConstructors":true,"allDeclaredFields":true,"allDeclaredMethods":true},{"name":"com.cadl.armresourceprovider.models.ManagedServiceIdentity","allDeclaredConstructors":true,"allDeclaredFields":true,"allDeclaredMethods":true},{"name":"com.cadl.armresourceprovider.models.ManagedServiceIdentityType","allDeclaredConstructors":true,"allDeclaredFields":true,"allDeclaredMethods":true},{"name":"com.cadl.armresourceprovider.models.OperationDisplay","allDeclaredConstructors":true,"allDeclaredFields":true,"allDeclaredMethods":true},{"name":"com.cadl.armresourceprovider.models.Origin","allDeclaredConstructors":true,"allDeclaredFields":true,"allDeclaredMethods":true},{"name":"com.cadl.armresourceprovider.models.ProvisioningState","allDeclaredConstructors":true,"allDeclaredFields":true,"allDeclaredMethods":true},{"name":"com.cadl.armresourceprovider.models.TopLevelArmResourceUpdate","allDeclaredConstructors":true,"allDeclaredFields":true,"allDeclaredMethods":true},{"name":"com.cadl.armresourceprovider.models.UserAssignedIdentity","allDeclaredConstructors":true,"allDeclaredFields":true,"allDeclaredMethods":true}] \ No newline at end of file +[{"name":"com.cadl.armresourceprovider.fluent.models.ChildExtensionResourceInner","allDeclaredConstructors":true,"allDeclaredFields":true,"allDeclaredMethods":true},{"name":"com.cadl.armresourceprovider.fluent.models.ChildResourceInner","allDeclaredConstructors":true,"allDeclaredFields":true,"allDeclaredMethods":true},{"name":"com.cadl.armresourceprovider.fluent.models.ChildResourceProperties","allDeclaredConstructors":true,"allDeclaredFields":true,"allDeclaredMethods":true},{"name":"com.cadl.armresourceprovider.fluent.models.CustomTemplateResourceInner","allDeclaredConstructors":true,"allDeclaredFields":true,"allDeclaredMethods":true},{"name":"com.cadl.armresourceprovider.fluent.models.CustomTemplateResourceProperties","allDeclaredConstructors":true,"allDeclaredFields":true,"allDeclaredMethods":true},{"name":"com.cadl.armresourceprovider.fluent.models.OperationInner","allDeclaredConstructors":true,"allDeclaredFields":true,"allDeclaredMethods":true},{"name":"com.cadl.armresourceprovider.fluent.models.ResultInner","allDeclaredConstructors":true,"allDeclaredFields":true,"allDeclaredMethods":true},{"name":"com.cadl.armresourceprovider.fluent.models.TopLevelArmResourceInner","allDeclaredConstructors":true,"allDeclaredFields":true,"allDeclaredMethods":true},{"name":"com.cadl.armresourceprovider.fluent.models.TopLevelArmResourceProperties","allDeclaredConstructors":true,"allDeclaredFields":true,"allDeclaredMethods":true},{"name":"com.cadl.armresourceprovider.fluent.models.TopLevelArmResourceUpdateProperties","allDeclaredConstructors":true,"allDeclaredFields":true,"allDeclaredMethods":true},{"name":"com.cadl.armresourceprovider.implementation.models.ChildExtensionResourceListResult","allDeclaredConstructors":true,"allDeclaredFields":true,"allDeclaredMethods":true},{"name":"com.cadl.armresourceprovider.implementation.models.ChildResourceListResult","allDeclaredConstructors":true,"allDeclaredFields":true,"allDeclaredMethods":true},{"name":"com.cadl.armresourceprovider.implementation.models.OperationListResult","allDeclaredConstructors":true,"allDeclaredFields":true,"allDeclaredMethods":true},{"name":"com.cadl.armresourceprovider.implementation.models.TopLevelArmResourceListResult","allDeclaredConstructors":true,"allDeclaredFields":true,"allDeclaredMethods":true},{"name":"com.cadl.armresourceprovider.models.ActionType","allDeclaredConstructors":true,"allDeclaredFields":true,"allDeclaredMethods":true},{"name":"com.cadl.armresourceprovider.models.ChildExtensionResourceProperties","allDeclaredConstructors":true,"allDeclaredFields":true,"allDeclaredMethods":true},{"name":"com.cadl.armresourceprovider.models.ChildExtensionResourceUpdate","allDeclaredConstructors":true,"allDeclaredFields":true,"allDeclaredMethods":true},{"name":"com.cadl.armresourceprovider.models.ChildResourceUpdate","allDeclaredConstructors":true,"allDeclaredFields":true,"allDeclaredMethods":true},{"name":"com.cadl.armresourceprovider.models.CustomTemplateResourcePatch","allDeclaredConstructors":true,"allDeclaredFields":true,"allDeclaredMethods":true},{"name":"com.cadl.armresourceprovider.models.CustomTemplateResourcePropertiesAnonymousEmptyModel","allDeclaredConstructors":true,"allDeclaredFields":true,"allDeclaredMethods":true},{"name":"com.cadl.armresourceprovider.models.Dog","allDeclaredConstructors":true,"allDeclaredFields":true,"allDeclaredMethods":true},{"name":"com.cadl.armresourceprovider.models.DogKind","allDeclaredConstructors":true,"allDeclaredFields":true,"allDeclaredMethods":true},{"name":"com.cadl.armresourceprovider.models.EmptyModel","allDeclaredConstructors":true,"allDeclaredFields":true,"allDeclaredMethods":true},{"name":"com.cadl.armresourceprovider.models.Golden","allDeclaredConstructors":true,"allDeclaredFields":true,"allDeclaredMethods":true},{"name":"com.cadl.armresourceprovider.models.ManagedServiceIdentity","allDeclaredConstructors":true,"allDeclaredFields":true,"allDeclaredMethods":true},{"name":"com.cadl.armresourceprovider.models.ManagedServiceIdentityType","allDeclaredConstructors":true,"allDeclaredFields":true,"allDeclaredMethods":true},{"name":"com.cadl.armresourceprovider.models.OperationDisplay","allDeclaredConstructors":true,"allDeclaredFields":true,"allDeclaredMethods":true},{"name":"com.cadl.armresourceprovider.models.Origin","allDeclaredConstructors":true,"allDeclaredFields":true,"allDeclaredMethods":true},{"name":"com.cadl.armresourceprovider.models.PriorityModel","allDeclaredConstructors":true,"allDeclaredFields":true,"allDeclaredMethods":true},{"name":"com.cadl.armresourceprovider.models.ProvisioningState","allDeclaredConstructors":true,"allDeclaredFields":true,"allDeclaredMethods":true},{"name":"com.cadl.armresourceprovider.models.TopLevelArmResourceUpdate","allDeclaredConstructors":true,"allDeclaredFields":true,"allDeclaredMethods":true},{"name":"com.cadl.armresourceprovider.models.UserAssignedIdentity","allDeclaredConstructors":true,"allDeclaredFields":true,"allDeclaredMethods":true}] \ No newline at end of file From 458bf9f916de7630ff325242ca8f479e91dfe3a4 Mon Sep 17 00:00:00 2001 From: xiaofeicao Date: Thu, 26 Sep 2024 17:18:49 +0800 Subject: [PATCH 10/18] format --- .../generator/core/mapper/MapperUtils.java | 29 +++++++------- .../core/model/clientmodel/ClassType.java | 5 +-- .../core/model/clientmodel/EnumType.java | 38 +++++++------------ .../generator/core/template/EnumTemplate.java | 2 +- .../fluent/TypeSpecFluentMapperFactory.java | 4 +- .../fluent/TypeSpecFluentPlugin.java | 2 +- .../mapper/TypeSpecChoiceMapper.java | 1 + .../template/TypeSpecEnumTemplate.java | 17 ++++++--- 8 files changed, 44 insertions(+), 54 deletions(-) diff --git a/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/mapper/MapperUtils.java b/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/mapper/MapperUtils.java index 40b44a2928..f052289bdb 100644 --- a/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/mapper/MapperUtils.java +++ b/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/mapper/MapperUtils.java @@ -30,27 +30,29 @@ public final class MapperUtils { /** * Create enum client type from code model. * - * @param enumType code model schema for enum - * @param expandable whether it's expandable enum + * @param enumType code model schema for enum + * @param expandable whether it's expandable enum * @param useCodeModelNameForEnumMember whether to use code model enum member name for client enum member name * @return enum client type */ - public static IType createEnumType(ChoiceSchema enumType, boolean expandable, boolean useCodeModelNameForEnumMember) { + public static IType createEnumType(ChoiceSchema enumType, boolean expandable, + boolean useCodeModelNameForEnumMember) { return createEnumType(enumType, expandable, useCodeModelNameForEnumMember, null, null, null); } /** * Create enum client type from code model. * - * @param enumType code model schema for enum - * @param expandable whether it's expandable enum + * @param enumType code model schema for enum + * @param expandable whether it's expandable enum * @param useCodeModelNameForEnumMember whether to use code model enum member name for client enum member name - * @param serializationMethodName method name for serialization - * @param deserializationMethodName method name for deserialization - * @param wireType wire type for serialization, if null, will default to element type of the enum + * @param serializationMethodName method name for serialization + * @param deserializationMethodName method name for deserialization + * @param wireType wire type for serialization, if null, will default to element type of the enum * @return enum client type */ - public static IType createEnumType(ChoiceSchema enumType, boolean expandable, boolean useCodeModelNameForEnumMember, String serializationMethodName, String deserializationMethodName, IType wireType) { + public static IType createEnumType(ChoiceSchema enumType, boolean expandable, boolean useCodeModelNameForEnumMember, + String serializationMethodName, String deserializationMethodName, IType wireType) { JavaSettings settings = JavaSettings.getInstance(); String enumTypeName = enumType.getLanguage().getJava().getName(); @@ -102,16 +104,15 @@ public static IType createEnumType(ChoiceSchema enumType, boolean expandable, bo } } - return new EnumType.Builder() - .packageName(enumPackage) + return new EnumType.Builder().packageName(enumPackage) .name(enumTypeName) .description(description) .expandable(expandable) .values(enumValues) .elementType(Mappers.getSchemaMapper().map(enumType.getChoiceType())) - .implementationDetails(new ImplementationDetails.Builder() - .usages(SchemaUtil.mapSchemaContext(enumType.getUsage())) - .build()) + .implementationDetails( + new ImplementationDetails.Builder().usages(SchemaUtil.mapSchemaContext(enumType.getUsage())) + .build()) .crossLanguageDefinitionId(enumType.getCrossLanguageDefinitionId()) .fromMethodName(deserializationMethodName) .toMethodName(serializationMethodName) diff --git a/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/model/clientmodel/ClassType.java b/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/model/clientmodel/ClassType.java index 16d6851383..f4f3982f1d 100644 --- a/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/model/clientmodel/ClassType.java +++ b/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/model/clientmodel/ClassType.java @@ -3,10 +3,6 @@ package com.microsoft.typespec.http.client.generator.core.model.clientmodel; -import com.azure.core.util.ExpandableEnum; -import com.microsoft.typespec.http.client.generator.core.extension.model.extensionmodel.XmsExtensions; -import com.microsoft.typespec.http.client.generator.core.extension.plugin.JavaSettings; -import com.microsoft.typespec.http.client.generator.core.util.TemplateUtil; import com.azure.core.client.traits.ConfigurationTrait; import com.azure.core.client.traits.EndpointTrait; import com.azure.core.client.traits.HttpTrait; @@ -49,6 +45,7 @@ import com.azure.core.util.Context; import com.azure.core.util.CoreUtils; import com.azure.core.util.DateTimeRfc1123; +import com.azure.core.util.ExpandableEnum; import com.azure.core.util.ExpandableStringEnum; import com.azure.core.util.logging.ClientLogger; import com.azure.core.util.logging.LogLevel; diff --git a/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/model/clientmodel/EnumType.java b/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/model/clientmodel/EnumType.java index 3e05d18aee..4630c7bbe9 100644 --- a/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/model/clientmodel/EnumType.java +++ b/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/model/clientmodel/EnumType.java @@ -42,19 +42,17 @@ public class EnumType implements IType { /** * Create a new Enum with the provided properties. * - * @param name The name of the new Enum. - * @param description The description of the Enum. - * @param expandable Whether this will be an ExpandableStringEnum type. - * @param values The values of the Enum. + * @param name The name of the new Enum. + * @param description The description of the Enum. + * @param expandable Whether this will be an ExpandableStringEnum type. + * @param values The values of the Enum. * @param fromMethodName The method name used to convert JSON to the enum type. - * @param toMethodName The method name used to convert the enum type to JSON. - * @param wireType The actual wire type in JSON form. + * @param toMethodName The method name used to convert the enum type to JSON. + * @param wireType The actual wire type in JSON form. */ - private EnumType(String packageKeyword, String name, String description, - boolean expandable, List values, - IType elementType, - ImplementationDetails implementationDetails, - String crossLanguageDefinitionId, String fromMethodName, String toMethodName, IType wireType) { + private EnumType(String packageKeyword, String name, String description, boolean expandable, + List values, IType elementType, ImplementationDetails implementationDetails, + String crossLanguageDefinitionId, String fromMethodName, String toMethodName, IType wireType) { this.name = name; this.packageName = packageKeyword; this.description = description; @@ -204,8 +202,8 @@ public String jsonSerializationMethodCall(String jsonWriterName, String fieldNam ? valueGetter + "." + getToMethodName() + "()" : valueGetter + " == null ? null : " + valueGetter + "." + getToMethodName() + "()"; - return wireType.asNullable().jsonSerializationMethodCall(jsonWriterName, fieldName, actualValueGetter, - jsonMergePatch); + return wireType.asNullable() + .jsonSerializationMethodCall(jsonWriterName, fieldName, actualValueGetter, jsonMergePatch); } @Override @@ -356,18 +354,8 @@ public EnumType build() { if (wireType == null) { wireType = elementType; } - return new EnumType( - packageName, - name, - description, - expandable, - values, - elementType, - implementationDetails, - crossLanguageDefinitionId, - fromMethodName, - toMethodName, - wireType); + return new EnumType(packageName, name, description, expandable, values, elementType, implementationDetails, + crossLanguageDefinitionId, fromMethodName, toMethodName, wireType); } } } diff --git a/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/template/EnumTemplate.java b/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/template/EnumTemplate.java index 9a7ea519e9..e2d849bb33 100644 --- a/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/template/EnumTemplate.java +++ b/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/template/EnumTemplate.java @@ -38,7 +38,7 @@ public final void write(EnumType enumType, JavaFile javaFile) { JavaSettings settings = JavaSettings.getInstance(); if (enumType.getExpandable()) { - if(settings.isBranded()) { + if (settings.isBranded()) { writeBrandedExpandableEnum(enumType, javaFile, settings); } else { writeExpandableStringEnumInterface(enumType, javaFile, settings); diff --git a/packages/http-client-java/generator/http-client-generator/src/main/java/com/microsoft/typespec/http/client/generator/fluent/TypeSpecFluentMapperFactory.java b/packages/http-client-java/generator/http-client-generator/src/main/java/com/microsoft/typespec/http/client/generator/fluent/TypeSpecFluentMapperFactory.java index f9176b2317..9cc3234c39 100644 --- a/packages/http-client-java/generator/http-client-generator/src/main/java/com/microsoft/typespec/http/client/generator/fluent/TypeSpecFluentMapperFactory.java +++ b/packages/http-client-java/generator/http-client-generator/src/main/java/com/microsoft/typespec/http/client/generator/fluent/TypeSpecFluentMapperFactory.java @@ -4,15 +4,13 @@ package com.microsoft.typespec.http.client.generator.fluent; import com.microsoft.typespec.http.client.generator.core.mapper.ChoiceMapper; -import com.microsoft.typespec.http.client.generator.core.mapper.SealedChoiceMapper; -import com.microsoft.typespec.http.client.generator.mapper.TypeSpecChoiceMapper; -import com.microsoft.typespec.http.client.generator.mgmt.mapper.FluentMapperFactory; import com.microsoft.typespec.http.client.generator.core.mapper.ClientMapper; import com.microsoft.typespec.http.client.generator.core.mapper.ModelMapper; import com.microsoft.typespec.http.client.generator.core.mapper.ModelPropertyMapper; import com.microsoft.typespec.http.client.generator.core.mapper.ObjectMapper; import com.microsoft.typespec.http.client.generator.core.mapper.PrimitiveMapper; import com.microsoft.typespec.http.client.generator.core.mapper.SealedChoiceMapper; +import com.microsoft.typespec.http.client.generator.mapper.TypeSpecChoiceMapper; import com.microsoft.typespec.http.client.generator.mapper.TypeSpecClientMapper; import com.microsoft.typespec.http.client.generator.mapper.TypeSpecPrimitiveMapper; import com.microsoft.typespec.http.client.generator.mgmt.mapper.FluentMapperFactory; diff --git a/packages/http-client-java/generator/http-client-generator/src/main/java/com/microsoft/typespec/http/client/generator/fluent/TypeSpecFluentPlugin.java b/packages/http-client-java/generator/http-client-generator/src/main/java/com/microsoft/typespec/http/client/generator/fluent/TypeSpecFluentPlugin.java index 78cfb224f4..4bc22aa168 100644 --- a/packages/http-client-java/generator/http-client-generator/src/main/java/com/microsoft/typespec/http/client/generator/fluent/TypeSpecFluentPlugin.java +++ b/packages/http-client-java/generator/http-client-generator/src/main/java/com/microsoft/typespec/http/client/generator/fluent/TypeSpecFluentPlugin.java @@ -11,9 +11,9 @@ import com.microsoft.typespec.http.client.generator.core.extension.model.Message; import com.microsoft.typespec.http.client.generator.core.extension.model.codemodel.CodeModel; import com.microsoft.typespec.http.client.generator.core.extension.plugin.JavaSettings; -import com.microsoft.typespec.http.client.generator.core.template.Templates; import com.microsoft.typespec.http.client.generator.core.mapper.Mappers; import com.microsoft.typespec.http.client.generator.core.model.clientmodel.Client; +import com.microsoft.typespec.http.client.generator.core.template.Templates; import com.microsoft.typespec.http.client.generator.mgmt.FluentGen; import com.microsoft.typespec.http.client.generator.mgmt.FluentNamer; import com.microsoft.typespec.http.client.generator.mgmt.mapper.FluentMapper; diff --git a/packages/http-client-java/generator/http-client-generator/src/main/java/com/microsoft/typespec/http/client/generator/mapper/TypeSpecChoiceMapper.java b/packages/http-client-java/generator/http-client-generator/src/main/java/com/microsoft/typespec/http/client/generator/mapper/TypeSpecChoiceMapper.java index 8200440010..234a3b448c 100644 --- a/packages/http-client-java/generator/http-client-generator/src/main/java/com/microsoft/typespec/http/client/generator/mapper/TypeSpecChoiceMapper.java +++ b/packages/http-client-java/generator/http-client-generator/src/main/java/com/microsoft/typespec/http/client/generator/mapper/TypeSpecChoiceMapper.java @@ -13,6 +13,7 @@ public class TypeSpecChoiceMapper extends ChoiceMapper { public static TypeSpecChoiceMapper getInstance() { return INSTANCE; } + private TypeSpecChoiceMapper() { } diff --git a/packages/http-client-java/generator/http-client-generator/src/main/java/com/microsoft/typespec/http/client/generator/template/TypeSpecEnumTemplate.java b/packages/http-client-java/generator/http-client-generator/src/main/java/com/microsoft/typespec/http/client/generator/template/TypeSpecEnumTemplate.java index f16af85edb..58272f7f3d 100644 --- a/packages/http-client-java/generator/http-client-generator/src/main/java/com/microsoft/typespec/http/client/generator/template/TypeSpecEnumTemplate.java +++ b/packages/http-client-java/generator/http-client-generator/src/main/java/com/microsoft/typespec/http/client/generator/template/TypeSpecEnumTemplate.java @@ -14,7 +14,6 @@ import com.microsoft.typespec.http.client.generator.core.model.javamodel.JavaVisibility; import com.microsoft.typespec.http.client.generator.core.template.EnumTemplate; import com.microsoft.typespec.http.client.generator.core.util.CodeNamer; - import java.util.HashSet; import java.util.Set; @@ -56,7 +55,8 @@ protected void writeBrandedExpandableEnum(EnumType enumType, JavaFile javaFile, String pascalTypeName = CodeNamer.toPascalCase(typeName); String declaration = enumName + " implements ExpandableEnum<" + pascalTypeName + ">"; javaFile.publicFinalClass(declaration, classBlock -> { - classBlock.privateStaticFinalVariable(String.format("Map<%1$s, %2$s> VALUES = new ConcurrentHashMap<>()", pascalTypeName, enumName)); + classBlock.privateStaticFinalVariable( + String.format("Map<%1$s, %2$s> VALUES = new ConcurrentHashMap<>()", pascalTypeName, enumName)); for (ClientEnumValue enumValue : enumType.getValues()) { String value = enumValue.getValue(); @@ -87,7 +87,8 @@ protected void writeBrandedExpandableEnum(EnumType enumType, JavaFile javaFile, classBlock.publicStaticMethod(String.format("%1$s fromValue(%2$s value)", enumName, pascalTypeName), function -> { - function.ifBlock("value == null", ifAction -> ifAction.line("throw new IllegalArgumentException(\"value can't be null\");")); + function.ifBlock("value == null", + ifAction -> ifAction.line("throw new IllegalArgumentException(\"value can't be null\");")); function.line(enumName + " member = VALUES.get(value);"); function.ifBlock("member != null", ifAction -> { ifAction.line("return member;"); @@ -118,17 +119,21 @@ protected void writeBrandedExpandableEnum(EnumType enumType, JavaFile javaFile, // toString addGeneratedAnnotation(classBlock); classBlock.annotation("Override"); - classBlock.method(JavaVisibility.Public, null, "String toString()", function -> function.methodReturn("getValue().toString()")); + classBlock.method(JavaVisibility.Public, null, "String toString()", + function -> function.methodReturn("getValue().toString()")); // equals addGeneratedAnnotation(classBlock); classBlock.annotation("Override"); - classBlock.method(JavaVisibility.Public, null, "boolean equals(Object obj)", function -> function.methodReturn(String.format("(obj instanceof %1$s) && ((%1$s) obj).getValue().equals(getValue())", enumName))); + classBlock.method(JavaVisibility.Public, null, "boolean equals(Object obj)", + function -> function.methodReturn(String + .format("(obj instanceof %1$s) && ((%1$s) obj).getValue().equals(getValue())", enumName))); // hashcode addGeneratedAnnotation(classBlock); classBlock.annotation("Override"); - classBlock.method(JavaVisibility.Public, null, "int hashCode()", function -> function.methodReturn("getValue().hashCode()")); + classBlock.method(JavaVisibility.Public, null, "int hashCode()", + function -> function.methodReturn("getValue().hashCode()")); }); } } From b80d30e3f5fdc3555007802f696121bddae85cdb Mon Sep 17 00:00:00 2001 From: xiaofeicao Date: Thu, 26 Sep 2024 17:27:32 +0800 Subject: [PATCH 11/18] Swagger non-string use ExpandableEnum --- .../generator/core/mapper/ChoiceMapper.java | 3 +- .../generator/core/mapper/MapperUtils.java | 1 - .../core/model/clientmodel/EnumType.java | 19 +-- .../generator/core/template/EnumTemplate.java | 107 ++++++++++++- .../mgmt/mapper/FluentChoiceMapper.java | 3 +- .../http/client/generator/TypeSpecPlugin.java | 3 - .../fluent/TypeSpecFluentPlugin.java | 2 - .../fluent/TypeSpecFluentTemplateFactory.java | 18 --- .../template/TypeSpecEnumTemplate.java | 140 ------------------ .../template/TypeSpecTemplateFactory.java | 17 --- 10 files changed, 111 insertions(+), 202 deletions(-) delete mode 100644 packages/http-client-java/generator/http-client-generator/src/main/java/com/microsoft/typespec/http/client/generator/fluent/TypeSpecFluentTemplateFactory.java delete mode 100644 packages/http-client-java/generator/http-client-generator/src/main/java/com/microsoft/typespec/http/client/generator/template/TypeSpecEnumTemplate.java delete mode 100644 packages/http-client-java/generator/http-client-generator/src/main/java/com/microsoft/typespec/http/client/generator/template/TypeSpecTemplateFactory.java diff --git a/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/mapper/ChoiceMapper.java b/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/mapper/ChoiceMapper.java index 8372eec954..361813a75e 100644 --- a/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/mapper/ChoiceMapper.java +++ b/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/mapper/ChoiceMapper.java @@ -4,7 +4,6 @@ package com.microsoft.typespec.http.client.generator.core.mapper; import com.microsoft.typespec.http.client.generator.core.extension.model.codemodel.ChoiceSchema; -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.IType; import java.util.Map; @@ -47,6 +46,6 @@ public IType map(ChoiceSchema enumType) { } private IType createChoiceType(ChoiceSchema enumType) { - return MapperUtils.createEnumType(enumType, true, true, "getValue", null, ClassType.STRING); + return MapperUtils.createEnumType(enumType, true, true); } } diff --git a/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/mapper/MapperUtils.java b/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/mapper/MapperUtils.java index f052289bdb..3747ff1899 100644 --- a/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/mapper/MapperUtils.java +++ b/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/mapper/MapperUtils.java @@ -116,7 +116,6 @@ public static IType createEnumType(ChoiceSchema enumType, boolean expandable, bo .crossLanguageDefinitionId(enumType.getCrossLanguageDefinitionId()) .fromMethodName(deserializationMethodName) .toMethodName(serializationMethodName) - .wireType(wireType) .build(); } } diff --git a/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/model/clientmodel/EnumType.java b/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/model/clientmodel/EnumType.java index 4630c7bbe9..b4728d0413 100644 --- a/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/model/clientmodel/EnumType.java +++ b/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/model/clientmodel/EnumType.java @@ -37,7 +37,6 @@ public class EnumType implements IType { private String crossLanguageDefinitionId; private final String fromMethodName; private final String toMethodName; - private final IType wireType; /** * Create a new Enum with the provided properties. @@ -48,11 +47,10 @@ public class EnumType implements IType { * @param values The values of the Enum. * @param fromMethodName The method name used to convert JSON to the enum type. * @param toMethodName The method name used to convert the enum type to JSON. - * @param wireType The actual wire type in JSON form. */ private EnumType(String packageKeyword, String name, String description, boolean expandable, List values, IType elementType, ImplementationDetails implementationDetails, - String crossLanguageDefinitionId, String fromMethodName, String toMethodName, IType wireType) { + String crossLanguageDefinitionId, String fromMethodName, String toMethodName) { this.name = name; this.packageName = packageKeyword; this.description = description; @@ -63,7 +61,6 @@ private EnumType(String packageKeyword, String name, String description, boolean this.crossLanguageDefinitionId = crossLanguageDefinitionId; this.fromMethodName = fromMethodName; this.toMethodName = toMethodName; - this.wireType = wireType; } public String getCrossLanguageDefinitionId() { @@ -202,7 +199,7 @@ public String jsonSerializationMethodCall(String jsonWriterName, String fieldNam ? valueGetter + "." + getToMethodName() + "()" : valueGetter + " == null ? null : " + valueGetter + "." + getToMethodName() + "()"; - return wireType.asNullable() + return elementType.asNullable() .jsonSerializationMethodCall(jsonWriterName, fieldName, actualValueGetter, jsonMergePatch); } @@ -245,7 +242,6 @@ public static class Builder { private String crossLanguageDefinitionId; private String fromMethodName; private String toMethodName; - private IType wireType; /** * Sets the name of the Enum. @@ -341,21 +337,12 @@ public Builder toMethodName(String toMethodName) { return this; } - public Builder wireType(IType wireType) { - this.wireType = wireType; - return this; - } - /** * @return an immutable EnumType instance with the configurations on this builder. */ public EnumType build() { - IType wireType = this.wireType; - if (wireType == null) { - wireType = elementType; - } return new EnumType(packageName, name, description, expandable, values, elementType, implementationDetails, - crossLanguageDefinitionId, fromMethodName, toMethodName, wireType); + crossLanguageDefinitionId, fromMethodName, toMethodName); } } } diff --git a/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/template/EnumTemplate.java b/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/template/EnumTemplate.java index e2d849bb33..cde4a04842 100644 --- a/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/template/EnumTemplate.java +++ b/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/template/EnumTemplate.java @@ -56,7 +56,112 @@ public final void write(EnumType enumType, JavaFile javaFile) { * @param settings {@link JavaSettings} instance */ protected void writeBrandedExpandableEnum(EnumType enumType, JavaFile javaFile, JavaSettings settings) { - writeExpandableStringEnum(enumType, javaFile, settings); + if (enumType.getElementType() == ClassType.STRING) { + writeExpandableStringEnum(enumType, javaFile, settings); + } else { + Set imports = new HashSet<>(); + imports.add("java.util.Collection"); + imports.add("java.lang.IllegalArgumentException"); + imports.add("java.util.Map"); + imports.add("java.util.concurrent.ConcurrentHashMap"); + imports.add("java.util.ArrayList"); + imports.add(ClassType.EXPANDABLE_ENUM.getFullName()); + if (!settings.isStreamStyleSerialization()) { + imports.add("com.fasterxml.jackson.annotation.JsonCreator"); + } + + addGeneratedImport(imports); + + javaFile.declareImport(imports); + javaFile.javadocComment(comment -> comment.description(enumType.getDescription())); + + String enumName = enumType.getName(); + IType elementType = enumType.getElementType(); + String typeName = elementType.getClientType().asNullable().toString(); + String pascalTypeName = CodeNamer.toPascalCase(typeName); + String declaration = enumName + " implements ExpandableEnum<" + pascalTypeName + ">"; + javaFile.publicFinalClass(declaration, classBlock -> { + classBlock.privateStaticFinalVariable( + String.format("Map<%1$s, %2$s> VALUES = new ConcurrentHashMap<>()", pascalTypeName, enumName)); + + for (ClientEnumValue enumValue : enumType.getValues()) { + String value = enumValue.getValue(); + classBlock.javadocComment(CoreUtils.isNullOrEmpty(enumValue.getDescription()) + ? "Static value " + value + " for " + enumName + "." + : enumValue.getDescription()); + addGeneratedAnnotation(classBlock); + classBlock.publicStaticFinalVariable(String.format("%1$s %2$s = fromValue(%3$s)", enumName, + enumValue.getName(), elementType.defaultValueExpression(value))); + } + + classBlock.variable(pascalTypeName + " value", JavaVisibility.Private, JavaModifier.Final); + classBlock.privateConstructor(enumName + "(" + pascalTypeName + " value)", ctor -> { + ctor.line("this.value = value;"); + }); + + // fromValue(typeName) + classBlock.javadocComment(comment -> { + comment.description("Creates or finds a " + enumName); + comment.param("value", "a value to look for"); + comment.methodReturns("the corresponding " + enumName); + }); + + addGeneratedAnnotation(classBlock); + if (!settings.isStreamStyleSerialization()) { + classBlock.annotation("JsonCreator"); + } + + classBlock.publicStaticMethod(String.format("%1$s fromValue(%2$s value)", enumName, pascalTypeName), + function -> { + function.ifBlock("value == null", + ifAction -> ifAction.line("throw new IllegalArgumentException(\"value can't be null\");")); + function.line(enumName + " member = VALUES.get(value);"); + function.ifBlock("member != null", ifAction -> { + ifAction.line("return member;"); + }); + function.methodReturn("VALUES.computeIfAbsent(value, key -> new " + enumName + "(key))"); + }); + + // values + classBlock.javadocComment(comment -> { + comment.description("Gets known " + enumName + " values."); + comment.methodReturns("Known " + enumName + " values."); + }); + addGeneratedAnnotation(classBlock); + classBlock.publicStaticMethod(String.format("Collection<%s> values()", enumName), + function -> function.methodReturn("new ArrayList<>(VALUES.values())")); + + // getValue + classBlock.javadocComment(comment -> { + comment.description("Gets the value of the " + enumName + " instance."); + comment.methodReturns("the value of the " + enumName + " instance."); + }); + + addGeneratedAnnotation(classBlock); + classBlock.annotation("Override"); + classBlock.publicMethod(pascalTypeName + " getValue()", + function -> function.methodReturn("this.value")); + + // toString + addGeneratedAnnotation(classBlock); + classBlock.annotation("Override"); + classBlock.method(JavaVisibility.Public, null, "String toString()", + function -> function.methodReturn("getValue().toString()")); + + // equals + addGeneratedAnnotation(classBlock); + classBlock.annotation("Override"); + classBlock.method(JavaVisibility.Public, null, "boolean equals(Object obj)", + function -> function.methodReturn(String + .format("(obj instanceof %1$s) && ((%1$s) obj).getValue().equals(getValue())", enumName))); + + // hashcode + addGeneratedAnnotation(classBlock); + classBlock.annotation("Override"); + classBlock.method(JavaVisibility.Public, null, "int hashCode()", + function -> function.methodReturn("getValue().hashCode()")); + }); + } } private void writeExpandableStringEnumInterface(EnumType enumType, JavaFile javaFile, JavaSettings settings) { diff --git a/packages/http-client-java/generator/http-client-generator-mgmt/src/main/java/com/microsoft/typespec/http/client/generator/mgmt/mapper/FluentChoiceMapper.java b/packages/http-client-java/generator/http-client-generator-mgmt/src/main/java/com/microsoft/typespec/http/client/generator/mgmt/mapper/FluentChoiceMapper.java index b411a19814..624292a683 100644 --- a/packages/http-client-java/generator/http-client-generator-mgmt/src/main/java/com/microsoft/typespec/http/client/generator/mgmt/mapper/FluentChoiceMapper.java +++ b/packages/http-client-java/generator/http-client-generator-mgmt/src/main/java/com/microsoft/typespec/http/client/generator/mgmt/mapper/FluentChoiceMapper.java @@ -6,7 +6,6 @@ import com.microsoft.typespec.http.client.generator.core.extension.model.codemodel.ChoiceSchema; import com.microsoft.typespec.http.client.generator.core.mapper.ChoiceMapper; import com.microsoft.typespec.http.client.generator.core.mapper.MapperUtils; -import com.microsoft.typespec.http.client.generator.core.model.clientmodel.ClassType; import com.microsoft.typespec.http.client.generator.core.model.clientmodel.IType; public class FluentChoiceMapper extends ChoiceMapper { @@ -21,6 +20,6 @@ public static FluentChoiceMapper getInstance() { @Override public IType map(ChoiceSchema enumType) { - return MapperUtils.createEnumType(enumType, true, false, "getValue", null, ClassType.STRING); + return MapperUtils.createEnumType(enumType, true, false, "getValue", null, null); } } diff --git a/packages/http-client-java/generator/http-client-generator/src/main/java/com/microsoft/typespec/http/client/generator/TypeSpecPlugin.java b/packages/http-client-java/generator/http-client-generator/src/main/java/com/microsoft/typespec/http/client/generator/TypeSpecPlugin.java index aadb7bd7d5..04254fc314 100644 --- a/packages/http-client-java/generator/http-client-generator/src/main/java/com/microsoft/typespec/http/client/generator/TypeSpecPlugin.java +++ b/packages/http-client-java/generator/http-client-generator/src/main/java/com/microsoft/typespec/http/client/generator/TypeSpecPlugin.java @@ -19,11 +19,9 @@ import com.microsoft.typespec.http.client.generator.core.model.javamodel.JavaPackage; import com.microsoft.typespec.http.client.generator.core.preprocessor.Preprocessor; import com.microsoft.typespec.http.client.generator.core.preprocessor.tranformer.Transformer; -import com.microsoft.typespec.http.client.generator.core.template.Templates; import com.microsoft.typespec.http.client.generator.core.util.ClientModelUtil; import com.microsoft.typespec.http.client.generator.mapper.TypeSpecMapperFactory; import com.microsoft.typespec.http.client.generator.model.EmitterOptions; -import com.microsoft.typespec.http.client.generator.template.TypeSpecTemplateFactory; import com.microsoft.typespec.http.client.generator.util.FileUtil; import com.microsoft.typespec.http.client.generator.util.ModelUtil; import java.io.File; @@ -301,7 +299,6 @@ public TypeSpecPlugin(EmitterOptions options, boolean sdkIntegration) { LOGGER.info("Namespace: {}", JavaSettings.getInstance().getPackage()); Mappers.setFactory(new TypeSpecMapperFactory()); - Templates.setFactory(new TypeSpecTemplateFactory()); } @SuppressWarnings("unchecked") diff --git a/packages/http-client-java/generator/http-client-generator/src/main/java/com/microsoft/typespec/http/client/generator/fluent/TypeSpecFluentPlugin.java b/packages/http-client-java/generator/http-client-generator/src/main/java/com/microsoft/typespec/http/client/generator/fluent/TypeSpecFluentPlugin.java index 4bc22aa168..635e301742 100644 --- a/packages/http-client-java/generator/http-client-generator/src/main/java/com/microsoft/typespec/http/client/generator/fluent/TypeSpecFluentPlugin.java +++ b/packages/http-client-java/generator/http-client-generator/src/main/java/com/microsoft/typespec/http/client/generator/fluent/TypeSpecFluentPlugin.java @@ -13,7 +13,6 @@ import com.microsoft.typespec.http.client.generator.core.extension.plugin.JavaSettings; import com.microsoft.typespec.http.client.generator.core.mapper.Mappers; import com.microsoft.typespec.http.client.generator.core.model.clientmodel.Client; -import com.microsoft.typespec.http.client.generator.core.template.Templates; import com.microsoft.typespec.http.client.generator.mgmt.FluentGen; import com.microsoft.typespec.http.client.generator.mgmt.FluentNamer; import com.microsoft.typespec.http.client.generator.mgmt.mapper.FluentMapper; @@ -89,7 +88,6 @@ public void writeFile(String fileName, String content, List sourceMap) { protected FluentMapper getFluentMapper() { FluentMapper fluentMapper = super.getFluentMapper(); Mappers.setFactory(new TypeSpecFluentMapperFactory()); - Templates.setFactory(new TypeSpecFluentTemplateFactory()); return fluentMapper; } diff --git a/packages/http-client-java/generator/http-client-generator/src/main/java/com/microsoft/typespec/http/client/generator/fluent/TypeSpecFluentTemplateFactory.java b/packages/http-client-java/generator/http-client-generator/src/main/java/com/microsoft/typespec/http/client/generator/fluent/TypeSpecFluentTemplateFactory.java deleted file mode 100644 index 9ea6e42b03..0000000000 --- a/packages/http-client-java/generator/http-client-generator/src/main/java/com/microsoft/typespec/http/client/generator/fluent/TypeSpecFluentTemplateFactory.java +++ /dev/null @@ -1,18 +0,0 @@ -// Copyright (c) Microsoft Corporation. All rights reserved. -// Licensed under the MIT License. - -package com.microsoft.typespec.http.client.generator.fluent; - -import com.microsoft.typespec.http.client.generator.core.template.EnumTemplate; -import com.microsoft.typespec.http.client.generator.mgmt.template.FluentTemplateFactory; -import com.microsoft.typespec.http.client.generator.template.TypeSpecEnumTemplate; - -/** - * TypeSpec mgmt template factory. - */ -public class TypeSpecFluentTemplateFactory extends FluentTemplateFactory { - @Override - public EnumTemplate getEnumTemplate() { - return TypeSpecEnumTemplate.getInstance(); - } -} diff --git a/packages/http-client-java/generator/http-client-generator/src/main/java/com/microsoft/typespec/http/client/generator/template/TypeSpecEnumTemplate.java b/packages/http-client-java/generator/http-client-generator/src/main/java/com/microsoft/typespec/http/client/generator/template/TypeSpecEnumTemplate.java deleted file mode 100644 index 58272f7f3d..0000000000 --- a/packages/http-client-java/generator/http-client-generator/src/main/java/com/microsoft/typespec/http/client/generator/template/TypeSpecEnumTemplate.java +++ /dev/null @@ -1,140 +0,0 @@ -// Copyright (c) Microsoft Corporation. All rights reserved. -// Licensed under the MIT License. - -package com.microsoft.typespec.http.client.generator.template; - -import com.azure.core.util.CoreUtils; -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.ClientEnumValue; -import com.microsoft.typespec.http.client.generator.core.model.clientmodel.EnumType; -import com.microsoft.typespec.http.client.generator.core.model.clientmodel.IType; -import com.microsoft.typespec.http.client.generator.core.model.javamodel.JavaFile; -import com.microsoft.typespec.http.client.generator.core.model.javamodel.JavaModifier; -import com.microsoft.typespec.http.client.generator.core.model.javamodel.JavaVisibility; -import com.microsoft.typespec.http.client.generator.core.template.EnumTemplate; -import com.microsoft.typespec.http.client.generator.core.util.CodeNamer; -import java.util.HashSet; -import java.util.Set; - -/** - * TypeSpec implementation for EnumTemplate. - */ -public class TypeSpecEnumTemplate extends EnumTemplate { - private static final TypeSpecEnumTemplate INSTANCE = new TypeSpecEnumTemplate(); - - public static TypeSpecEnumTemplate getInstance() { - return INSTANCE; - } - - @Override - protected void writeBrandedExpandableEnum(EnumType enumType, JavaFile javaFile, JavaSettings settings) { - if (enumType.getElementType() == ClassType.STRING) { - // if String, use ExpandableStringEnum implementation - super.writeBrandedExpandableEnum(enumType, javaFile, settings); - } else { - Set imports = new HashSet<>(); - imports.add("java.util.Collection"); - imports.add("java.lang.IllegalArgumentException"); - imports.add("java.util.Map"); - imports.add("java.util.concurrent.ConcurrentHashMap"); - imports.add("java.util.ArrayList"); - imports.add(ClassType.EXPANDABLE_ENUM.getFullName()); - if (!settings.isStreamStyleSerialization()) { - imports.add("com.fasterxml.jackson.annotation.JsonCreator"); - } - - addGeneratedImport(imports); - - javaFile.declareImport(imports); - javaFile.javadocComment(comment -> comment.description(enumType.getDescription())); - - String enumName = enumType.getName(); - IType elementType = enumType.getElementType(); - String typeName = elementType.getClientType().asNullable().toString(); - String pascalTypeName = CodeNamer.toPascalCase(typeName); - String declaration = enumName + " implements ExpandableEnum<" + pascalTypeName + ">"; - javaFile.publicFinalClass(declaration, classBlock -> { - classBlock.privateStaticFinalVariable( - String.format("Map<%1$s, %2$s> VALUES = new ConcurrentHashMap<>()", pascalTypeName, enumName)); - - for (ClientEnumValue enumValue : enumType.getValues()) { - String value = enumValue.getValue(); - classBlock.javadocComment(CoreUtils.isNullOrEmpty(enumValue.getDescription()) - ? "Static value " + value + " for " + enumName + "." - : enumValue.getDescription()); - addGeneratedAnnotation(classBlock); - classBlock.publicStaticFinalVariable(String.format("%1$s %2$s = fromValue(%3$s)", enumName, - enumValue.getName(), elementType.defaultValueExpression(value))); - } - - classBlock.variable(pascalTypeName + " value", JavaVisibility.Private, JavaModifier.Final); - classBlock.privateConstructor(enumName + "(" + pascalTypeName + " value)", ctor -> { - ctor.line("this.value = value;"); - }); - - // fromValue(typeName) - classBlock.javadocComment(comment -> { - comment.description("Creates or finds a " + enumName); - comment.param("value", "a value to look for"); - comment.methodReturns("the corresponding " + enumName); - }); - - addGeneratedAnnotation(classBlock); - if (!settings.isStreamStyleSerialization()) { - classBlock.annotation("JsonCreator"); - } - - classBlock.publicStaticMethod(String.format("%1$s fromValue(%2$s value)", enumName, pascalTypeName), - function -> { - function.ifBlock("value == null", - ifAction -> ifAction.line("throw new IllegalArgumentException(\"value can't be null\");")); - function.line(enumName + " member = VALUES.get(value);"); - function.ifBlock("member != null", ifAction -> { - ifAction.line("return member;"); - }); - function.methodReturn("VALUES.computeIfAbsent(value, key -> new " + enumName + "(key))"); - }); - - // values - classBlock.javadocComment(comment -> { - comment.description("Gets known " + enumName + " values."); - comment.methodReturns("Known " + enumName + " values."); - }); - addGeneratedAnnotation(classBlock); - classBlock.publicStaticMethod(String.format("Collection<%s> values()", enumName), - function -> function.methodReturn("new ArrayList<>(VALUES.values())")); - - // getValue - classBlock.javadocComment(comment -> { - comment.description("Gets the value of the " + enumName + " instance."); - comment.methodReturns("the value of the " + enumName + " instance."); - }); - - addGeneratedAnnotation(classBlock); - classBlock.annotation("Override"); - classBlock.publicMethod(pascalTypeName + " getValue()", - function -> function.methodReturn("this.value")); - - // toString - addGeneratedAnnotation(classBlock); - classBlock.annotation("Override"); - classBlock.method(JavaVisibility.Public, null, "String toString()", - function -> function.methodReturn("getValue().toString()")); - - // equals - addGeneratedAnnotation(classBlock); - classBlock.annotation("Override"); - classBlock.method(JavaVisibility.Public, null, "boolean equals(Object obj)", - function -> function.methodReturn(String - .format("(obj instanceof %1$s) && ((%1$s) obj).getValue().equals(getValue())", enumName))); - - // hashcode - addGeneratedAnnotation(classBlock); - classBlock.annotation("Override"); - classBlock.method(JavaVisibility.Public, null, "int hashCode()", - function -> function.methodReturn("getValue().hashCode()")); - }); - } - } -} diff --git a/packages/http-client-java/generator/http-client-generator/src/main/java/com/microsoft/typespec/http/client/generator/template/TypeSpecTemplateFactory.java b/packages/http-client-java/generator/http-client-generator/src/main/java/com/microsoft/typespec/http/client/generator/template/TypeSpecTemplateFactory.java deleted file mode 100644 index bb2a6443cb..0000000000 --- a/packages/http-client-java/generator/http-client-generator/src/main/java/com/microsoft/typespec/http/client/generator/template/TypeSpecTemplateFactory.java +++ /dev/null @@ -1,17 +0,0 @@ -// Copyright (c) Microsoft Corporation. All rights reserved. -// Licensed under the MIT License. - -package com.microsoft.typespec.http.client.generator.template; - -import com.microsoft.typespec.http.client.generator.core.template.DefaultTemplateFactory; -import com.microsoft.typespec.http.client.generator.core.template.EnumTemplate; - -/** - * TypeSpec template factory. - */ -public class TypeSpecTemplateFactory extends DefaultTemplateFactory { - @Override - public EnumTemplate getEnumTemplate() { - return TypeSpecEnumTemplate.getInstance(); - } -} From 0259090de1ebfaa4947fcb127ab4b2a01ce57fa7 Mon Sep 17 00:00:00 2001 From: xiaofeicao Date: Thu, 26 Sep 2024 17:36:08 +0800 Subject: [PATCH 12/18] regen --- .../java/com/cadl/armresourceprovider/models/PriorityModel.java | 1 - .../java/com/cadl/enumservice/models/OlympicRecordModel.java | 1 - .../src/main/java/com/cadl/enumservice/models/PriorityModel.java | 1 - 3 files changed, 3 deletions(-) diff --git a/packages/http-client-java/generator/http-client-generator-test/src/main/java/com/cadl/armresourceprovider/models/PriorityModel.java b/packages/http-client-java/generator/http-client-generator-test/src/main/java/com/cadl/armresourceprovider/models/PriorityModel.java index 8b3b8ad177..91dd555f09 100644 --- a/packages/http-client-java/generator/http-client-generator-test/src/main/java/com/cadl/armresourceprovider/models/PriorityModel.java +++ b/packages/http-client-java/generator/http-client-generator-test/src/main/java/com/cadl/armresourceprovider/models/PriorityModel.java @@ -6,7 +6,6 @@ import com.azure.core.util.ExpandableEnum; import com.fasterxml.jackson.annotation.JsonCreator; -import java.lang.IllegalArgumentException; import java.util.ArrayList; import java.util.Collection; import java.util.Map; diff --git a/packages/http-client-java/generator/http-client-generator-test/src/main/java/com/cadl/enumservice/models/OlympicRecordModel.java b/packages/http-client-java/generator/http-client-generator-test/src/main/java/com/cadl/enumservice/models/OlympicRecordModel.java index 24c16510f3..c841889639 100644 --- a/packages/http-client-java/generator/http-client-generator-test/src/main/java/com/cadl/enumservice/models/OlympicRecordModel.java +++ b/packages/http-client-java/generator/http-client-generator-test/src/main/java/com/cadl/enumservice/models/OlympicRecordModel.java @@ -6,7 +6,6 @@ import com.azure.core.annotation.Generated; import com.azure.core.util.ExpandableEnum; -import java.lang.IllegalArgumentException; import java.util.ArrayList; import java.util.Collection; import java.util.Map; diff --git a/packages/http-client-java/generator/http-client-generator-test/src/main/java/com/cadl/enumservice/models/PriorityModel.java b/packages/http-client-java/generator/http-client-generator-test/src/main/java/com/cadl/enumservice/models/PriorityModel.java index d3137131fe..2d1032e56d 100644 --- a/packages/http-client-java/generator/http-client-generator-test/src/main/java/com/cadl/enumservice/models/PriorityModel.java +++ b/packages/http-client-java/generator/http-client-generator-test/src/main/java/com/cadl/enumservice/models/PriorityModel.java @@ -6,7 +6,6 @@ import com.azure.core.annotation.Generated; import com.azure.core.util.ExpandableEnum; -import java.lang.IllegalArgumentException; import java.util.ArrayList; import java.util.Collection; import java.util.Map; From 10fb725512f5ead02c434c1e1c2f80695bde8d69 Mon Sep 17 00:00:00 2001 From: xiaofeicao Date: Thu, 26 Sep 2024 17:59:50 +0800 Subject: [PATCH 13/18] fix swagger --- .../generator/core/mapper/ChoiceMapper.java | 13 +++++++- .../generator/core/mapper/MapperUtils.java | 5 ++-- .../mgmt/mapper/FluentChoiceMapper.java | 7 ++--- .../fluent/TypeSpecFluentMapperFactory.java | 3 +- .../mapper/TypeSpecChoiceMapper.java | 30 ------------------- .../mapper/TypeSpecMapperFactory.java | 6 ---- 6 files changed, 17 insertions(+), 47 deletions(-) delete mode 100644 packages/http-client-java/generator/http-client-generator/src/main/java/com/microsoft/typespec/http/client/generator/mapper/TypeSpecChoiceMapper.java diff --git a/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/mapper/ChoiceMapper.java b/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/mapper/ChoiceMapper.java index 361813a75e..5433e6f68b 100644 --- a/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/mapper/ChoiceMapper.java +++ b/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/mapper/ChoiceMapper.java @@ -4,6 +4,7 @@ package com.microsoft.typespec.http.client.generator.core.mapper; import com.microsoft.typespec.http.client.generator.core.extension.model.codemodel.ChoiceSchema; +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.IType; import java.util.Map; @@ -45,7 +46,17 @@ public IType map(ChoiceSchema enumType) { return choiceType; } + protected boolean useCodeModelNameForEnumMember() { + return true; + } + private IType createChoiceType(ChoiceSchema enumType) { - return MapperUtils.createEnumType(enumType, true, true); + IType elementType = Mappers.getSchemaMapper().map(enumType.getChoiceType()); + boolean isStringEnum = elementType == ClassType.STRING; + if (isStringEnum) { + return MapperUtils.createEnumType(enumType, true, useCodeModelNameForEnumMember()); + } else { + return MapperUtils.createEnumType(enumType, true, useCodeModelNameForEnumMember(), "getValue", "fromValue"); + } } } diff --git a/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/mapper/MapperUtils.java b/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/mapper/MapperUtils.java index 3747ff1899..b625e2fc58 100644 --- a/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/mapper/MapperUtils.java +++ b/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/mapper/MapperUtils.java @@ -37,7 +37,7 @@ public final class MapperUtils { */ public static IType createEnumType(ChoiceSchema enumType, boolean expandable, boolean useCodeModelNameForEnumMember) { - return createEnumType(enumType, expandable, useCodeModelNameForEnumMember, null, null, null); + return createEnumType(enumType, expandable, useCodeModelNameForEnumMember, null, null); } /** @@ -48,11 +48,10 @@ public static IType createEnumType(ChoiceSchema enumType, boolean expandable, * @param useCodeModelNameForEnumMember whether to use code model enum member name for client enum member name * @param serializationMethodName method name for serialization * @param deserializationMethodName method name for deserialization - * @param wireType wire type for serialization, if null, will default to element type of the enum * @return enum client type */ public static IType createEnumType(ChoiceSchema enumType, boolean expandable, boolean useCodeModelNameForEnumMember, - String serializationMethodName, String deserializationMethodName, IType wireType) { + String serializationMethodName, String deserializationMethodName) { JavaSettings settings = JavaSettings.getInstance(); String enumTypeName = enumType.getLanguage().getJava().getName(); diff --git a/packages/http-client-java/generator/http-client-generator-mgmt/src/main/java/com/microsoft/typespec/http/client/generator/mgmt/mapper/FluentChoiceMapper.java b/packages/http-client-java/generator/http-client-generator-mgmt/src/main/java/com/microsoft/typespec/http/client/generator/mgmt/mapper/FluentChoiceMapper.java index 624292a683..7933acee1a 100644 --- a/packages/http-client-java/generator/http-client-generator-mgmt/src/main/java/com/microsoft/typespec/http/client/generator/mgmt/mapper/FluentChoiceMapper.java +++ b/packages/http-client-java/generator/http-client-generator-mgmt/src/main/java/com/microsoft/typespec/http/client/generator/mgmt/mapper/FluentChoiceMapper.java @@ -3,10 +3,7 @@ package com.microsoft.typespec.http.client.generator.mgmt.mapper; -import com.microsoft.typespec.http.client.generator.core.extension.model.codemodel.ChoiceSchema; import com.microsoft.typespec.http.client.generator.core.mapper.ChoiceMapper; -import com.microsoft.typespec.http.client.generator.core.mapper.MapperUtils; -import com.microsoft.typespec.http.client.generator.core.model.clientmodel.IType; public class FluentChoiceMapper extends ChoiceMapper { private static final FluentChoiceMapper INSTANCE = new FluentChoiceMapper(); @@ -19,7 +16,7 @@ public static FluentChoiceMapper getInstance() { } @Override - public IType map(ChoiceSchema enumType) { - return MapperUtils.createEnumType(enumType, true, false, "getValue", null, null); + protected boolean useCodeModelNameForEnumMember() { + return false; } } diff --git a/packages/http-client-java/generator/http-client-generator/src/main/java/com/microsoft/typespec/http/client/generator/fluent/TypeSpecFluentMapperFactory.java b/packages/http-client-java/generator/http-client-generator/src/main/java/com/microsoft/typespec/http/client/generator/fluent/TypeSpecFluentMapperFactory.java index 9cc3234c39..23ff984b09 100644 --- a/packages/http-client-java/generator/http-client-generator/src/main/java/com/microsoft/typespec/http/client/generator/fluent/TypeSpecFluentMapperFactory.java +++ b/packages/http-client-java/generator/http-client-generator/src/main/java/com/microsoft/typespec/http/client/generator/fluent/TypeSpecFluentMapperFactory.java @@ -10,7 +10,6 @@ import com.microsoft.typespec.http.client.generator.core.mapper.ObjectMapper; import com.microsoft.typespec.http.client.generator.core.mapper.PrimitiveMapper; import com.microsoft.typespec.http.client.generator.core.mapper.SealedChoiceMapper; -import com.microsoft.typespec.http.client.generator.mapper.TypeSpecChoiceMapper; import com.microsoft.typespec.http.client.generator.mapper.TypeSpecClientMapper; import com.microsoft.typespec.http.client.generator.mapper.TypeSpecPrimitiveMapper; import com.microsoft.typespec.http.client.generator.mgmt.mapper.FluentMapperFactory; @@ -43,7 +42,7 @@ public ModelPropertyMapper getModelPropertyMapper() { @Override public ChoiceMapper getChoiceMapper() { - return TypeSpecChoiceMapper.getInstance(); + return ChoiceMapper.getInstance(); } @Override diff --git a/packages/http-client-java/generator/http-client-generator/src/main/java/com/microsoft/typespec/http/client/generator/mapper/TypeSpecChoiceMapper.java b/packages/http-client-java/generator/http-client-generator/src/main/java/com/microsoft/typespec/http/client/generator/mapper/TypeSpecChoiceMapper.java deleted file mode 100644 index 234a3b448c..0000000000 --- a/packages/http-client-java/generator/http-client-generator/src/main/java/com/microsoft/typespec/http/client/generator/mapper/TypeSpecChoiceMapper.java +++ /dev/null @@ -1,30 +0,0 @@ -package com.microsoft.typespec.http.client.generator.mapper; - -import com.microsoft.typespec.http.client.generator.core.extension.model.codemodel.ChoiceSchema; -import com.microsoft.typespec.http.client.generator.core.mapper.ChoiceMapper; -import com.microsoft.typespec.http.client.generator.core.mapper.MapperUtils; -import com.microsoft.typespec.http.client.generator.core.mapper.Mappers; -import com.microsoft.typespec.http.client.generator.core.model.clientmodel.ClassType; -import com.microsoft.typespec.http.client.generator.core.model.clientmodel.IType; - -public class TypeSpecChoiceMapper extends ChoiceMapper { - private static TypeSpecChoiceMapper INSTANCE = new TypeSpecChoiceMapper(); - - public static TypeSpecChoiceMapper getInstance() { - return INSTANCE; - } - - private TypeSpecChoiceMapper() { - } - - @Override - public IType map(ChoiceSchema enumType) { - IType elementType = Mappers.getSchemaMapper().map(enumType.getChoiceType()); - boolean isStringEnum = elementType == ClassType.STRING; - if (isStringEnum) { - return MapperUtils.createEnumType(enumType, true, true); - } else { - return MapperUtils.createEnumType(enumType, true, true, "getValue", "fromValue", null); - } - } -} diff --git a/packages/http-client-java/generator/http-client-generator/src/main/java/com/microsoft/typespec/http/client/generator/mapper/TypeSpecMapperFactory.java b/packages/http-client-java/generator/http-client-generator/src/main/java/com/microsoft/typespec/http/client/generator/mapper/TypeSpecMapperFactory.java index 74df5b6eff..2552fbcba9 100644 --- a/packages/http-client-java/generator/http-client-generator/src/main/java/com/microsoft/typespec/http/client/generator/mapper/TypeSpecMapperFactory.java +++ b/packages/http-client-java/generator/http-client-generator/src/main/java/com/microsoft/typespec/http/client/generator/mapper/TypeSpecMapperFactory.java @@ -3,7 +3,6 @@ package com.microsoft.typespec.http.client.generator.mapper; -import com.microsoft.typespec.http.client.generator.core.mapper.ChoiceMapper; import com.microsoft.typespec.http.client.generator.core.mapper.ClientMapper; import com.microsoft.typespec.http.client.generator.core.mapper.DefaultMapperFactory; import com.microsoft.typespec.http.client.generator.core.mapper.PrimitiveMapper; @@ -19,9 +18,4 @@ public ClientMapper getClientMapper() { public PrimitiveMapper getPrimitiveMapper() { return TypeSpecPrimitiveMapper.getInstance(); } - - @Override - public ChoiceMapper getChoiceMapper() { - return TypeSpecChoiceMapper.getInstance(); - } } From 52e2f796c7c409e971ab35bc22c59e1ad9c5241f Mon Sep 17 00:00:00 2001 From: xiaofeicao Date: Sun, 29 Sep 2024 14:19:56 +0800 Subject: [PATCH 14/18] fix format --- .../generator/http-client-generator-test/tsp/arm.tsp | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/http-client-java/generator/http-client-generator-test/tsp/arm.tsp b/packages/http-client-java/generator/http-client-generator-test/tsp/arm.tsp index 3d714542cc..933006b6c6 100644 --- a/packages/http-client-java/generator/http-client-generator-test/tsp/arm.tsp +++ b/packages/http-client-java/generator/http-client-generator-test/tsp/arm.tsp @@ -147,7 +147,6 @@ model CustomTemplateResourceProperties { dog: Dog; namedEmptyModel: EmptyModel; anonymousEmptyModel: {}; - priority: PriorityModel; } From bb63f21499d738b441008202545a0bbb4b1856a4 Mon Sep 17 00:00:00 2001 From: XiaofeiCao Date: Mon, 30 Sep 2024 11:26:59 +0800 Subject: [PATCH 15/18] refactor, reuse SealedChoiceMapper cache --- .../generator/core/mapper/SealedChoiceMapper.java | 6 +++++- .../generator/core/template/EnumTemplate.java | 15 ++++++--------- .../mgmt/mapper/FluentSealedChoiceMapper.java | 7 ++----- 3 files changed, 13 insertions(+), 15 deletions(-) diff --git a/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/mapper/SealedChoiceMapper.java b/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/mapper/SealedChoiceMapper.java index 522f86a6b4..63a5342a9d 100644 --- a/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/mapper/SealedChoiceMapper.java +++ b/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/mapper/SealedChoiceMapper.java @@ -36,7 +36,11 @@ public IType map(SealedChoiceSchema enumType) { return sealedChoiceType; } + protected boolean useCodeModelNameForEnumMember() { + return true; + } + private IType createSealedChoiceType(SealedChoiceSchema enumType) { - return MapperUtils.createEnumType(enumType, false, true); + return MapperUtils.createEnumType(enumType, false, useCodeModelNameForEnumMember()); } } diff --git a/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/template/EnumTemplate.java b/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/template/EnumTemplate.java index cde4a04842..dc057207fb 100644 --- a/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/template/EnumTemplate.java +++ b/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/template/EnumTemplate.java @@ -65,6 +65,7 @@ protected void writeBrandedExpandableEnum(EnumType enumType, JavaFile javaFile, imports.add("java.util.Map"); imports.add("java.util.concurrent.ConcurrentHashMap"); imports.add("java.util.ArrayList"); + imports.add("java.util.Objects"); imports.add(ClassType.EXPANDABLE_ENUM.getFullName()); if (!settings.isStreamStyleSerialization()) { imports.add("com.fasterxml.jackson.annotation.JsonCreator"); @@ -113,12 +114,9 @@ protected void writeBrandedExpandableEnum(EnumType enumType, JavaFile javaFile, classBlock.publicStaticMethod(String.format("%1$s fromValue(%2$s value)", enumName, pascalTypeName), function -> { - function.ifBlock("value == null", - ifAction -> ifAction.line("throw new IllegalArgumentException(\"value can't be null\");")); + function.line("Objects.requireNonNull(value, \"'value' cannot be null.\");"); function.line(enumName + " member = VALUES.get(value);"); - function.ifBlock("member != null", ifAction -> { - ifAction.line("return member;"); - }); + function.ifBlock("member != null", ifAction -> ifAction.line("return member;")); function.methodReturn("VALUES.computeIfAbsent(value, key -> new " + enumName + "(key))"); }); @@ -146,20 +144,19 @@ protected void writeBrandedExpandableEnum(EnumType enumType, JavaFile javaFile, addGeneratedAnnotation(classBlock); classBlock.annotation("Override"); classBlock.method(JavaVisibility.Public, null, "String toString()", - function -> function.methodReturn("getValue().toString()")); + function -> function.methodReturn("Objects.toString(this.value)")); // equals addGeneratedAnnotation(classBlock); classBlock.annotation("Override"); classBlock.method(JavaVisibility.Public, null, "boolean equals(Object obj)", - function -> function.methodReturn(String - .format("(obj instanceof %1$s) && ((%1$s) obj).getValue().equals(getValue())", enumName))); + function -> function.methodReturn(String.format("Objects.equals(this.value, %s)", enumName))); // hashcode addGeneratedAnnotation(classBlock); classBlock.annotation("Override"); classBlock.method(JavaVisibility.Public, null, "int hashCode()", - function -> function.methodReturn("getValue().hashCode()")); + function -> function.methodReturn("Objects.hashCode(this.value)")); }); } } diff --git a/packages/http-client-java/generator/http-client-generator-mgmt/src/main/java/com/microsoft/typespec/http/client/generator/mgmt/mapper/FluentSealedChoiceMapper.java b/packages/http-client-java/generator/http-client-generator-mgmt/src/main/java/com/microsoft/typespec/http/client/generator/mgmt/mapper/FluentSealedChoiceMapper.java index 3ba15239ed..412e981fd2 100644 --- a/packages/http-client-java/generator/http-client-generator-mgmt/src/main/java/com/microsoft/typespec/http/client/generator/mgmt/mapper/FluentSealedChoiceMapper.java +++ b/packages/http-client-java/generator/http-client-generator-mgmt/src/main/java/com/microsoft/typespec/http/client/generator/mgmt/mapper/FluentSealedChoiceMapper.java @@ -3,10 +3,7 @@ package com.microsoft.typespec.http.client.generator.mgmt.mapper; -import com.microsoft.typespec.http.client.generator.core.extension.model.codemodel.SealedChoiceSchema; -import com.microsoft.typespec.http.client.generator.core.mapper.MapperUtils; import com.microsoft.typespec.http.client.generator.core.mapper.SealedChoiceMapper; -import com.microsoft.typespec.http.client.generator.core.model.clientmodel.IType; public class FluentSealedChoiceMapper extends SealedChoiceMapper { private static final FluentSealedChoiceMapper INSTANCE = new FluentSealedChoiceMapper(); @@ -19,7 +16,7 @@ public static FluentSealedChoiceMapper getInstance() { } @Override - public IType map(SealedChoiceSchema enumType) { - return MapperUtils.createEnumType(enumType, false, false); + protected boolean useCodeModelNameForEnumMember() { + return false; } } From 584fa6ff387ad96a72ee2f2b1088ab9497ec2d4d Mon Sep 17 00:00:00 2001 From: XiaofeiCao Date: Mon, 30 Sep 2024 14:08:32 +0800 Subject: [PATCH 16/18] regen --- .../armresourceprovider/models/PriorityModel.java | 11 +++++------ .../cadl/enumservice/models/OlympicRecordModel.java | 11 +++++------ .../com/cadl/enumservice/models/PriorityModel.java | 11 +++++------ 3 files changed, 15 insertions(+), 18 deletions(-) diff --git a/packages/http-client-java/generator/http-client-generator-test/src/main/java/com/cadl/armresourceprovider/models/PriorityModel.java b/packages/http-client-java/generator/http-client-generator-test/src/main/java/com/cadl/armresourceprovider/models/PriorityModel.java index 91dd555f09..5ea46a9777 100644 --- a/packages/http-client-java/generator/http-client-generator-test/src/main/java/com/cadl/armresourceprovider/models/PriorityModel.java +++ b/packages/http-client-java/generator/http-client-generator-test/src/main/java/com/cadl/armresourceprovider/models/PriorityModel.java @@ -9,6 +9,7 @@ import java.util.ArrayList; import java.util.Collection; import java.util.Map; +import java.util.Objects; import java.util.concurrent.ConcurrentHashMap; /** @@ -41,9 +42,7 @@ private PriorityModel(Integer value) { */ @JsonCreator public static PriorityModel fromValue(Integer value) { - if (value == null) { - throw new IllegalArgumentException("value can't be null"); - } + Objects.requireNonNull(value, "'value' cannot be null."); PriorityModel member = VALUES.get(value); if (member != null) { return member; @@ -72,16 +71,16 @@ public Integer getValue() { @Override public String toString() { - return getValue().toString(); + return Objects.toString(this.value); } @Override public boolean equals(Object obj) { - return (obj instanceof PriorityModel) && ((PriorityModel) obj).getValue().equals(getValue()); + return Objects.equals(this.value, PriorityModel); } @Override public int hashCode() { - return getValue().hashCode(); + return Objects.hashCode(this.value); } } diff --git a/packages/http-client-java/generator/http-client-generator-test/src/main/java/com/cadl/enumservice/models/OlympicRecordModel.java b/packages/http-client-java/generator/http-client-generator-test/src/main/java/com/cadl/enumservice/models/OlympicRecordModel.java index c841889639..6e8c0251d4 100644 --- a/packages/http-client-java/generator/http-client-generator-test/src/main/java/com/cadl/enumservice/models/OlympicRecordModel.java +++ b/packages/http-client-java/generator/http-client-generator-test/src/main/java/com/cadl/enumservice/models/OlympicRecordModel.java @@ -9,6 +9,7 @@ import java.util.ArrayList; import java.util.Collection; import java.util.Map; +import java.util.Objects; import java.util.concurrent.ConcurrentHashMap; /** @@ -43,9 +44,7 @@ private OlympicRecordModel(Double value) { */ @Generated public static OlympicRecordModel fromValue(Double value) { - if (value == null) { - throw new IllegalArgumentException("value can't be null"); - } + Objects.requireNonNull(value, "'value' cannot be null."); OlympicRecordModel member = VALUES.get(value); if (member != null) { return member; @@ -77,18 +76,18 @@ public Double getValue() { @Generated @Override public String toString() { - return getValue().toString(); + return Objects.toString(this.value); } @Generated @Override public boolean equals(Object obj) { - return (obj instanceof OlympicRecordModel) && ((OlympicRecordModel) obj).getValue().equals(getValue()); + return Objects.equals(this.value, OlympicRecordModel); } @Generated @Override public int hashCode() { - return getValue().hashCode(); + return Objects.hashCode(this.value); } } diff --git a/packages/http-client-java/generator/http-client-generator-test/src/main/java/com/cadl/enumservice/models/PriorityModel.java b/packages/http-client-java/generator/http-client-generator-test/src/main/java/com/cadl/enumservice/models/PriorityModel.java index 2d1032e56d..a63fe0fdeb 100644 --- a/packages/http-client-java/generator/http-client-generator-test/src/main/java/com/cadl/enumservice/models/PriorityModel.java +++ b/packages/http-client-java/generator/http-client-generator-test/src/main/java/com/cadl/enumservice/models/PriorityModel.java @@ -9,6 +9,7 @@ import java.util.ArrayList; import java.util.Collection; import java.util.Map; +import java.util.Objects; import java.util.concurrent.ConcurrentHashMap; /** @@ -43,9 +44,7 @@ private PriorityModel(Integer value) { */ @Generated public static PriorityModel fromValue(Integer value) { - if (value == null) { - throw new IllegalArgumentException("value can't be null"); - } + Objects.requireNonNull(value, "'value' cannot be null."); PriorityModel member = VALUES.get(value); if (member != null) { return member; @@ -77,18 +76,18 @@ public Integer getValue() { @Generated @Override public String toString() { - return getValue().toString(); + return Objects.toString(this.value); } @Generated @Override public boolean equals(Object obj) { - return (obj instanceof PriorityModel) && ((PriorityModel) obj).getValue().equals(getValue()); + return Objects.equals(this.value, PriorityModel); } @Generated @Override public int hashCode() { - return getValue().hashCode(); + return Objects.hashCode(this.value); } } From 30eca954a68671d2044384a2bb8856509322462f Mon Sep 17 00:00:00 2001 From: XiaofeiCao Date: Mon, 30 Sep 2024 16:43:24 +0800 Subject: [PATCH 17/18] fix --- .../http/client/generator/core/template/EnumTemplate.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/template/EnumTemplate.java b/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/template/EnumTemplate.java index dc057207fb..d7fbac8ba4 100644 --- a/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/template/EnumTemplate.java +++ b/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/template/EnumTemplate.java @@ -150,7 +150,7 @@ protected void writeBrandedExpandableEnum(EnumType enumType, JavaFile javaFile, addGeneratedAnnotation(classBlock); classBlock.annotation("Override"); classBlock.method(JavaVisibility.Public, null, "boolean equals(Object obj)", - function -> function.methodReturn(String.format("Objects.equals(this.value, %s)", enumName))); + function -> function.methodReturn("Objects.equals(this.value, obj)")); // hashcode addGeneratedAnnotation(classBlock); From fbabf4e616345d28363f94bf39c82f883af3bae9 Mon Sep 17 00:00:00 2001 From: XiaofeiCao Date: Mon, 30 Sep 2024 16:50:52 +0800 Subject: [PATCH 18/18] regen --- .../java/com/cadl/armresourceprovider/models/PriorityModel.java | 2 +- .../java/com/cadl/enumservice/models/OlympicRecordModel.java | 2 +- .../main/java/com/cadl/enumservice/models/PriorityModel.java | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/http-client-java/generator/http-client-generator-test/src/main/java/com/cadl/armresourceprovider/models/PriorityModel.java b/packages/http-client-java/generator/http-client-generator-test/src/main/java/com/cadl/armresourceprovider/models/PriorityModel.java index 5ea46a9777..3819610085 100644 --- a/packages/http-client-java/generator/http-client-generator-test/src/main/java/com/cadl/armresourceprovider/models/PriorityModel.java +++ b/packages/http-client-java/generator/http-client-generator-test/src/main/java/com/cadl/armresourceprovider/models/PriorityModel.java @@ -76,7 +76,7 @@ public String toString() { @Override public boolean equals(Object obj) { - return Objects.equals(this.value, PriorityModel); + return Objects.equals(this.value, obj); } @Override diff --git a/packages/http-client-java/generator/http-client-generator-test/src/main/java/com/cadl/enumservice/models/OlympicRecordModel.java b/packages/http-client-java/generator/http-client-generator-test/src/main/java/com/cadl/enumservice/models/OlympicRecordModel.java index 6e8c0251d4..4d11d95a20 100644 --- a/packages/http-client-java/generator/http-client-generator-test/src/main/java/com/cadl/enumservice/models/OlympicRecordModel.java +++ b/packages/http-client-java/generator/http-client-generator-test/src/main/java/com/cadl/enumservice/models/OlympicRecordModel.java @@ -82,7 +82,7 @@ public String toString() { @Generated @Override public boolean equals(Object obj) { - return Objects.equals(this.value, OlympicRecordModel); + return Objects.equals(this.value, obj); } @Generated diff --git a/packages/http-client-java/generator/http-client-generator-test/src/main/java/com/cadl/enumservice/models/PriorityModel.java b/packages/http-client-java/generator/http-client-generator-test/src/main/java/com/cadl/enumservice/models/PriorityModel.java index a63fe0fdeb..040b9ecbe8 100644 --- a/packages/http-client-java/generator/http-client-generator-test/src/main/java/com/cadl/enumservice/models/PriorityModel.java +++ b/packages/http-client-java/generator/http-client-generator-test/src/main/java/com/cadl/enumservice/models/PriorityModel.java @@ -82,7 +82,7 @@ public String toString() { @Generated @Override public boolean equals(Object obj) { - return Objects.equals(this.value, PriorityModel); + return Objects.equals(this.value, obj); } @Generated