Skip to content

Commit

Permalink
mgmt, fix SchemaCleanup for discriminator property (#2774)
Browse files Browse the repository at this point in the history
* fix SchemaCleanup for discriminator property

* re-generate test code

---------

Co-authored-by: actions-user <[email protected]>
  • Loading branch information
XiaofeiCao and actions-user authored May 17, 2024
1 parent 892c210 commit 74b9e9f
Show file tree
Hide file tree
Showing 11 changed files with 388 additions and 3 deletions.
52 changes: 52 additions & 0 deletions fluent-tests/swagger/schema-cleanup.json
Original file line number Diff line number Diff line change
Expand Up @@ -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."
}
}
},
Expand Down Expand Up @@ -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.",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,20 @@ private static boolean tryCleanup(CodeModel codeModel, Set<String> javaNamesForP
.collect(Collectors.toSet());
schemasNotInUse.removeAll(elementsInParentCollection);
choicesSchemasNotInUse.removeAll(elementsInParentCollection);

// discriminators
Set<Schema> 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,29 @@

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.
*/
@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.
*/
Expand All @@ -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);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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());
}
Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -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() {
}
}
Original file line number Diff line number Diff line change
@@ -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<DogKind> {
/**
* 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<DogKind> values() {
return values(DogKind.class);
}
}
Loading

0 comments on commit 74b9e9f

Please sign in to comment.