From 74b9e9f55a461241450063262ba1384a3e0cbb6d Mon Sep 17 00:00:00 2001 From: Xiaofei Cao <92354331+XiaofeiCao@users.noreply.github.com> Date: Fri, 17 May 2024 14:56:32 +0800 Subject: [PATCH] mgmt, fix `SchemaCleanup` for discriminator property (#2774) * fix SchemaCleanup for discriminator property * re-generate test code --------- Co-authored-by: actions-user --- fluent-tests/swagger/schema-cleanup.json | 52 +++++++++++++ .../fluent/transformer/SchemaCleanup.java | 14 ++++ .../models/CustomTemplateResourceInner.java | 24 ++++++ .../CustomTemplateResourceProperties.java | 41 +++++++++- .../CustomTemplateResourceImpl.java | 10 +++ .../models/CustomTemplateResource.java | 22 +++++- .../cadl/armresourceprovider/models/Dog.java | 78 +++++++++++++++++++ .../armresourceprovider/models/DogKind.java | 48 ++++++++++++ .../armresourceprovider/models/Golden.java | 61 +++++++++++++++ .../reflect-config.json | 15 ++++ typespec-tests/tsp/arm.tsp | 26 +++++++ 11 files changed, 388 insertions(+), 3 deletions(-) create mode 100644 typespec-tests/src/main/java/com/cadl/armresourceprovider/models/Dog.java create mode 100644 typespec-tests/src/main/java/com/cadl/armresourceprovider/models/DogKind.java create mode 100644 typespec-tests/src/main/java/com/cadl/armresourceprovider/models/Golden.java diff --git a/fluent-tests/swagger/schema-cleanup.json b/fluent-tests/swagger/schema-cleanup.json index 134db5d851..61205b1adf 100644 --- a/fluent-tests/swagger/schema-cleanup.json +++ b/fluent-tests/swagger/schema-cleanup.json @@ -74,6 +74,10 @@ "userAssignedIdentities": { "$ref": "#/definitions/UserAssignedIdentities", "description": "The identities assigned to this resource by the user." + }, + "Dog": { + "$ref": "#/definitions/Dog", + "description": "A beautiful dog." } } }, @@ -108,6 +112,54 @@ } } }, + "Dog": { + "type": "object", + "description": "Test extensible enum type for discriminator", + "properties": { + "kind": { + "$ref": "#/definitions/DogKind", + "description": "discriminator property" + }, + "weight": { + "type": "integer", + "format": "int32", + "description": "Weight of the dog" + } + }, + "discriminator": "kind", + "required": [ + "kind", + "weight" + ] + }, + "DogKind": { + "type": "string", + "description": "extensible enum type for discriminator", + "enum": [ + "golden" + ], + "x-ms-enum": { + "name": "DogKind", + "modelAsString": true, + "values": [ + { + "name": "Golden", + "value": "golden", + "description": "Species golden" + } + ] + } + }, + "Golden": { + "type": "object", + "description": "Golden dog model", + "allOf": [ + { + "$ref": "#/definitions/Dog" + } + ], + "x-ms-discriminator-value": "golden" + }, "CloudError": { "x-ms-external": true, "description": "The object that defines the structure of an Azure Synapse error response.", diff --git a/fluentnamer/src/main/java/com/azure/autorest/fluent/transformer/SchemaCleanup.java b/fluentnamer/src/main/java/com/azure/autorest/fluent/transformer/SchemaCleanup.java index 9fe05bc800..28ecab68e1 100644 --- a/fluentnamer/src/main/java/com/azure/autorest/fluent/transformer/SchemaCleanup.java +++ b/fluentnamer/src/main/java/com/azure/autorest/fluent/transformer/SchemaCleanup.java @@ -147,6 +147,20 @@ private static boolean tryCleanup(CodeModel codeModel, Set javaNamesForP .collect(Collectors.toSet()); schemasNotInUse.removeAll(elementsInParentCollection); choicesSchemasNotInUse.removeAll(elementsInParentCollection); + + // discriminators + Set discriminators = schemasInUse.stream() + .map(s -> { + if (s instanceof ObjectSchema && ((ObjectSchema) s).getDiscriminator() != null) { + return ((ObjectSchema) s).getDiscriminator().getProperty().getSchema(); + } else { + return null; + } + }) + .filter(Objects::nonNull) + .collect(Collectors.toSet()); + schemasNotInUse.removeAll(discriminators); + choicesSchemasNotInUse.removeAll(discriminators); } AtomicBoolean codeModelModified = new AtomicBoolean(false); diff --git a/typespec-tests/src/main/java/com/cadl/armresourceprovider/fluent/models/CustomTemplateResourceInner.java b/typespec-tests/src/main/java/com/cadl/armresourceprovider/fluent/models/CustomTemplateResourceInner.java index 07529a31f9..dc900f4540 100644 --- a/typespec-tests/src/main/java/com/cadl/armresourceprovider/fluent/models/CustomTemplateResourceInner.java +++ b/typespec-tests/src/main/java/com/cadl/armresourceprovider/fluent/models/CustomTemplateResourceInner.java @@ -7,6 +7,7 @@ import com.azure.core.annotation.Fluent; import com.azure.core.management.Resource; import com.azure.core.management.SystemData; +import com.cadl.armresourceprovider.models.Dog; import com.cadl.armresourceprovider.models.ManagedServiceIdentity; import com.cadl.armresourceprovider.models.ProvisioningState; import com.fasterxml.jackson.annotation.JsonProperty; @@ -106,6 +107,29 @@ public ProvisioningState provisioningState() { return this.innerProperties() == null ? null : this.innerProperties().provisioningState(); } + /** + * Get the dog property: The dog property. + * + * @return the dog value. + */ + public Dog dog() { + return this.innerProperties() == null ? null : this.innerProperties().dog(); + } + + /** + * Set the dog property: The dog property. + * + * @param dog the dog value to set. + * @return the CustomTemplateResourceInner object itself. + */ + public CustomTemplateResourceInner withDog(Dog dog) { + if (this.innerProperties() == null) { + this.innerProperties = new CustomTemplateResourceProperties(); + } + this.innerProperties().withDog(dog); + return this; + } + /** * Validates the instance. * diff --git a/typespec-tests/src/main/java/com/cadl/armresourceprovider/fluent/models/CustomTemplateResourceProperties.java b/typespec-tests/src/main/java/com/cadl/armresourceprovider/fluent/models/CustomTemplateResourceProperties.java index 883381509e..2087b7cd3d 100644 --- a/typespec-tests/src/main/java/com/cadl/armresourceprovider/fluent/models/CustomTemplateResourceProperties.java +++ b/typespec-tests/src/main/java/com/cadl/armresourceprovider/fluent/models/CustomTemplateResourceProperties.java @@ -4,14 +4,16 @@ package com.cadl.armresourceprovider.fluent.models; -import com.azure.core.annotation.Immutable; +import com.azure.core.annotation.Fluent; +import com.azure.core.util.logging.ClientLogger; +import com.cadl.armresourceprovider.models.Dog; import com.cadl.armresourceprovider.models.ProvisioningState; import com.fasterxml.jackson.annotation.JsonProperty; /** * Top Level Arm Resource Properties. */ -@Immutable +@Fluent public final class CustomTemplateResourceProperties { /* * The status of the last operation. @@ -19,6 +21,12 @@ public final class CustomTemplateResourceProperties { @JsonProperty(value = "provisioningState", access = JsonProperty.Access.WRITE_ONLY) private ProvisioningState provisioningState; + /* + * The dog property. + */ + @JsonProperty(value = "dog", required = true) + private Dog dog; + /** * Creates an instance of CustomTemplateResourceProperties class. */ @@ -34,11 +42,40 @@ public ProvisioningState provisioningState() { return this.provisioningState; } + /** + * Get the dog property: The dog property. + * + * @return the dog value. + */ + public Dog dog() { + return this.dog; + } + + /** + * Set the dog property: The dog property. + * + * @param dog the dog value to set. + * @return the CustomTemplateResourceProperties object itself. + */ + public CustomTemplateResourceProperties withDog(Dog dog) { + this.dog = dog; + return this; + } + /** * Validates the instance. * * @throws IllegalArgumentException thrown if the instance is not valid. */ public void validate() { + if (dog() == null) { + throw LOGGER.atError() + .log(new IllegalArgumentException( + "Missing required property dog in model CustomTemplateResourceProperties")); + } else { + dog().validate(); + } } + + private static final ClientLogger LOGGER = new ClientLogger(CustomTemplateResourceProperties.class); } diff --git a/typespec-tests/src/main/java/com/cadl/armresourceprovider/implementation/CustomTemplateResourceImpl.java b/typespec-tests/src/main/java/com/cadl/armresourceprovider/implementation/CustomTemplateResourceImpl.java index f0bbe94ee1..765c3a80f9 100644 --- a/typespec-tests/src/main/java/com/cadl/armresourceprovider/implementation/CustomTemplateResourceImpl.java +++ b/typespec-tests/src/main/java/com/cadl/armresourceprovider/implementation/CustomTemplateResourceImpl.java @@ -10,6 +10,7 @@ import com.cadl.armresourceprovider.fluent.models.CustomTemplateResourceInner; import com.cadl.armresourceprovider.models.CustomTemplateResource; import com.cadl.armresourceprovider.models.CustomTemplateResourcePatch; +import com.cadl.armresourceprovider.models.Dog; import com.cadl.armresourceprovider.models.ManagedServiceIdentity; import com.cadl.armresourceprovider.models.ProvisioningState; import java.util.Collections; @@ -58,6 +59,10 @@ public ProvisioningState provisioningState() { return this.innerModel().provisioningState(); } + public Dog dog() { + return this.innerModel().dog(); + } + public Region region() { return Region.fromName(this.regionName()); } @@ -170,6 +175,11 @@ public CustomTemplateResourceImpl withIdentity(ManagedServiceIdentity identity) } } + public CustomTemplateResourceImpl withDog(Dog dog) { + this.innerModel().withDog(dog); + return this; + } + public CustomTemplateResourceImpl withIfMatch(String ifMatch) { this.createIfMatch = ifMatch; return this; diff --git a/typespec-tests/src/main/java/com/cadl/armresourceprovider/models/CustomTemplateResource.java b/typespec-tests/src/main/java/com/cadl/armresourceprovider/models/CustomTemplateResource.java index d5ddcab37c..6b4a790a38 100644 --- a/typespec-tests/src/main/java/com/cadl/armresourceprovider/models/CustomTemplateResource.java +++ b/typespec-tests/src/main/java/com/cadl/armresourceprovider/models/CustomTemplateResource.java @@ -70,6 +70,13 @@ public interface CustomTemplateResource { */ ProvisioningState provisioningState(); + /** + * Gets the dog property: The dog property. + * + * @return the dog value. + */ + Dog dog(); + /** * Gets the region of the resource. * @@ -153,7 +160,7 @@ interface WithResourceGroup { * The stage of the CustomTemplateResource definition which contains all the minimum required properties for the * resource to be created, but also allows for any other optional properties to be specified. */ - interface WithCreate extends DefinitionStages.WithTags, DefinitionStages.WithIdentity, + interface WithCreate extends DefinitionStages.WithTags, DefinitionStages.WithIdentity, DefinitionStages.WithDog, DefinitionStages.WithIfMatch, DefinitionStages.WithIfNoneMatch { /** * Executes the create request. @@ -197,6 +204,19 @@ interface WithIdentity { WithCreate withIdentity(ManagedServiceIdentity identity); } + /** + * The stage of the CustomTemplateResource definition allowing to specify dog. + */ + interface WithDog { + /** + * Specifies the dog property: The dog property.. + * + * @param dog The dog property. + * @return the next definition stage. + */ + WithCreate withDog(Dog dog); + } + /** * The stage of the CustomTemplateResource definition allowing to specify ifMatch. */ diff --git a/typespec-tests/src/main/java/com/cadl/armresourceprovider/models/Dog.java b/typespec-tests/src/main/java/com/cadl/armresourceprovider/models/Dog.java new file mode 100644 index 0000000000..ba55edbf3d --- /dev/null +++ b/typespec-tests/src/main/java/com/cadl/armresourceprovider/models/Dog.java @@ -0,0 +1,78 @@ +// 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.annotation.Fluent; +import com.fasterxml.jackson.annotation.JsonProperty; +import com.fasterxml.jackson.annotation.JsonSubTypes; +import com.fasterxml.jackson.annotation.JsonTypeId; +import com.fasterxml.jackson.annotation.JsonTypeInfo; +import com.fasterxml.jackson.annotation.JsonTypeName; + +/** + * Test extensible enum type for discriminator. + */ +@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "kind", defaultImpl = Dog.class, visible = true) +@JsonTypeName("Dog") +@JsonSubTypes({ @JsonSubTypes.Type(name = "golden", value = Golden.class) }) +@Fluent +public class Dog { + /* + * discriminator property + */ + @JsonTypeId + @JsonProperty(value = "kind", required = true) + private DogKind kind; + + /* + * Weight of the dog + */ + @JsonProperty(value = "weight", required = true) + private int weight; + + /** + * Creates an instance of Dog class. + */ + public Dog() { + this.kind = DogKind.fromString("Dog"); + } + + /** + * Get the kind property: discriminator property. + * + * @return the kind value. + */ + public DogKind kind() { + return this.kind; + } + + /** + * Get the weight property: Weight of the dog. + * + * @return the weight value. + */ + public int weight() { + return this.weight; + } + + /** + * Set the weight property: Weight of the dog. + * + * @param weight the weight value to set. + * @return the Dog object itself. + */ + public Dog withWeight(int weight) { + this.weight = weight; + return this; + } + + /** + * Validates the instance. + * + * @throws IllegalArgumentException thrown if the instance is not valid. + */ + public void validate() { + } +} diff --git a/typespec-tests/src/main/java/com/cadl/armresourceprovider/models/DogKind.java b/typespec-tests/src/main/java/com/cadl/armresourceprovider/models/DogKind.java new file mode 100644 index 0000000000..5346ed18c8 --- /dev/null +++ b/typespec-tests/src/main/java/com/cadl/armresourceprovider/models/DogKind.java @@ -0,0 +1,48 @@ +// 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.ExpandableStringEnum; +import com.fasterxml.jackson.annotation.JsonCreator; +import java.util.Collection; + +/** + * extensible enum type for discriminator. + */ +public final class DogKind extends ExpandableStringEnum { + /** + * Static value golden for DogKind. + */ + public static final DogKind GOLDEN = fromString("golden"); + + /** + * Creates a new instance of DogKind value. + * + * @deprecated Use the {@link #fromString(String)} factory method. + */ + @Deprecated + public DogKind() { + } + + /** + * Creates or finds a DogKind from its string representation. + * + * @param name a name to look for. + * @return the corresponding DogKind. + */ + @JsonCreator + public static DogKind fromString(String name) { + return fromString(name, DogKind.class); + } + + /** + * Gets known DogKind values. + * + * @return known DogKind values. + */ + public static Collection values() { + return values(DogKind.class); + } +} diff --git a/typespec-tests/src/main/java/com/cadl/armresourceprovider/models/Golden.java b/typespec-tests/src/main/java/com/cadl/armresourceprovider/models/Golden.java new file mode 100644 index 0000000000..79f6822549 --- /dev/null +++ b/typespec-tests/src/main/java/com/cadl/armresourceprovider/models/Golden.java @@ -0,0 +1,61 @@ +// 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.annotation.Fluent; +import com.fasterxml.jackson.annotation.JsonProperty; +import com.fasterxml.jackson.annotation.JsonTypeId; +import com.fasterxml.jackson.annotation.JsonTypeInfo; +import com.fasterxml.jackson.annotation.JsonTypeName; + +/** + * Golden dog model. + */ +@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "kind", defaultImpl = Golden.class, visible = true) +@JsonTypeName("golden") +@Fluent +public final class Golden extends Dog { + /* + * discriminator property + */ + @JsonTypeId + @JsonProperty(value = "kind", required = true) + private DogKind kind = DogKind.GOLDEN; + + /** + * Creates an instance of Golden class. + */ + public Golden() { + } + + /** + * Get the kind property: discriminator property. + * + * @return the kind value. + */ + @Override + public DogKind kind() { + return this.kind; + } + + /** + * {@inheritDoc} + */ + @Override + public Golden withWeight(int weight) { + super.withWeight(weight); + return this; + } + + /** + * Validates the instance. + * + * @throws IllegalArgumentException thrown if the instance is not valid. + */ + @Override + public void validate() { + super.validate(); + } +} diff --git a/typespec-tests/src/main/resources/META-INF/native-image/com.azure.resourcemanager/azure-resourcemanager-armresourceprovider-generated/reflect-config.json b/typespec-tests/src/main/resources/META-INF/native-image/com.azure.resourcemanager/azure-resourcemanager-armresourceprovider-generated/reflect-config.json index b124ee15cd..c32c5a6daf 100644 --- a/typespec-tests/src/main/resources/META-INF/native-image/com.azure.resourcemanager/azure-resourcemanager-armresourceprovider-generated/reflect-config.json +++ b/typespec-tests/src/main/resources/META-INF/native-image/com.azure.resourcemanager/azure-resourcemanager-armresourceprovider-generated/reflect-config.json @@ -63,6 +63,21 @@ "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.Golden", + "allDeclaredConstructors" : true, + "allDeclaredFields" : true, + "allDeclaredMethods" : true }, { "name" : "com.cadl.armresourceprovider.models.ManagedServiceIdentity", "allDeclaredConstructors" : true, diff --git a/typespec-tests/tsp/arm.tsp b/typespec-tests/tsp/arm.tsp index 4595ba48db..54e0d0682e 100644 --- a/typespec-tests/tsp/arm.tsp +++ b/typespec-tests/tsp/arm.tsp @@ -107,6 +107,30 @@ model CustomTemplateResource is TrackedResource