Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Search for discriminator children through multiple levels of allOf nesting #1460

Merged
merged 9 commits into from
Nov 17, 2023

Conversation

krishanjmistry
Copy link
Contributor

Attempts to fix #1458 - see the issue for more details.

I've tested this by modifying the codegen/examples/resources_mgmt.rs to point to:
azure-rest-api-specs/specification/recoveryservicesbackup/resource-manager/Microsoft.RecoveryServices/stable/2023-04-01/bms.json

This now produces the enum like so:

#[doc = "Type of the backup item."]
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(tag = "protectableItemType")]
pub enum WorkloadProtectableItemUnion {
    AzureFileShare(AzureFileShareProtectableItem),
    #[serde(rename = "Microsoft.ClassicCompute/virtualMachines")]
    MicrosoftClassicComputeVirtualMachines(AzureIaaSClassicComputeVmProtectableItem),
    #[serde(rename = "Microsoft.Compute/virtualMachines")]
    MicrosoftComputeVirtualMachines(AzureIaaSComputeVmProtectableItem),
    AzureVmWorkloadProtectableItem(AzureVmWorkloadProtectableItem),
    #[serde(rename = "SAPAseSystem")]
    SapAseSystem(AzureVmWorkloadSapAseSystemProtectableItem),
    #[serde(rename = "SAPHanaDBInstance")]
    SapHanaDbInstance(AzureVmWorkloadSapHanaDbInstance),
    #[serde(rename = "SAPHanaDatabase")]
    SapHanaDatabase(AzureVmWorkloadSapHanaDatabaseProtectableItem),
    #[serde(rename = "HanaHSRContainer")]
    HanaHsrContainer(AzureVmWorkloadSapHanaHsrProtectableItem),
    #[serde(rename = "SAPHanaSystem")]
    SapHanaSystem(AzureVmWorkloadSapHanaSystemProtectableItem),
    #[serde(rename = "SQLAvailabilityGroupContainer")]
    SqlAvailabilityGroupContainer(AzureVmWorkloadSqlAvailabilityGroupProtectableItem),
    #[serde(rename = "SQLDataBase")]
    SqlDataBase(AzureVmWorkloadSqlDatabaseProtectableItem),
    #[serde(rename = "SQLInstance")]
    SqlInstance(AzureVmWorkloadSqlInstanceProtectableItem),
    #[serde(rename = "IaaSVMProtectableItem")]
    IaaSvmProtectableItem(IaaSvmProtectableItem),
}

I'm unsure how to test this more generally across all the API's - happy to be steered on this

@demoray demoray requested a review from cataggar November 9, 2023 20:57
@krishanjmistry
Copy link
Contributor Author

krishanjmistry commented Nov 9, 2023

I'm starting to think it should be a breadth-first search rather than depth-first (what I've currently implemented). That way, we'd stop at the level where we find another "discriminator"...

@cataggar
Copy link
Member

Thanks @krishanjmistry! I ran the code generation. Here is how I ran it:

~/ms/azure-rest-api-specs> git checkout 7c906b15744aaf7b782dfb82ccefcbef05cbf20b
~/ms/azure-sdk-for-rust/services/autorust> cargo run -p azure-autorust --release

It fixed 7 control plane and 3 data place services.

Once a unit test is added, this should be good to merge.

Copy link
Member

@cataggar cataggar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks again! For extra credit, a unit test that used to fail deserialization, but now works would be nice. json from a specification example could probably be copied.

@krishanjmistry
Copy link
Contributor Author

I've added a test in the mgmt/recoveryservicesbackup crate which does just this. Based off of the example in REST API specs which handily used one of the previous problem cases

@demoray demoray merged commit 0d09ed3 into Azure:main Nov 17, 2023
19 checks passed
@krishanjmistry krishanjmistry deleted the discriminator-value-nested-allof branch November 17, 2023 17:16

let item = list.value.first().expect("There should be at least one item in the list");
let properties= item.properties.as_ref().expect("The properties should be present");
assert!(matches!(properties, &WorkloadProtectableItemUnion::MicrosoftClassicComputeVirtualMachines(_)));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perfect. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Data conversion error when using the azure_mgmt_recoveryservicesbackup crate
3 participants