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

add AutoRust support for x-ms-enum modelAsString #768

Closed
cataggar opened this issue May 25, 2022 · 4 comments · Fixed by #769
Closed

add AutoRust support for x-ms-enum modelAsString #768

cataggar opened this issue May 25, 2022 · 4 comments · Fixed by #769

Comments

@cataggar
Copy link
Member

#413 is for x-ms-enum in general, but this is specifically modelAsString support. When an enumeration is markes as "modelAsString": true, it is extensible.

Currently, the cargo run --package azure_mgmt_compute --example vm_list example fails for me because one of my VMs is a size not in the enum.

An snippet from a spec: Microsoft.Compute\stable\2021-11-01\compute.json

    "HardwareProfile": {
      "properties": {
        "vmSize": {
          "type": "string",
          "enum": [
            "Basic_A0",
            "Basic_A1",
            "Basic_A2",
            "Basic_A3",
            "Basic_A4",
            "Standard_A0",
            "Standard_A1",
            "Standard_A2",

            "Standard_ND6s",
            "Standard_ND12s",
            "Standard_ND24s",
            "Standard_ND24rs",
            "Standard_NV6",
            "Standard_NV12",
            "Standard_NV24"
          ],
          "x-ms-enum": {
            "name": "VirtualMachineSizeTypes",
            "modelAsString": true
          }
        },

The generated code looks like this:

pub mod hardware_profile {
    use super::*;

    #[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
    pub enum VmSize {
        #[serde(rename = "Basic_A0")]
        BasicA0,
        #[serde(rename = "Basic_A1")]
        BasicA1,
        #[serde(rename = "Basic_A2")]

We need a way to deserialize and deserialize other string values using serde.

@johnbatty
Copy link
Contributor

johnbatty commented May 25, 2022

I looked at this a while back and concluded that it wasn't possible with the standard serde features, and that we'd probably need to generate explicit serializer/deserializer code. See this serde issue for details:
serde-rs/serde#912

However, I just looked again, and found the serde-enum-str crate which looks promising:
https://stackoverflow.com/questions/57469527/how-can-i-support-an-unknown-or-other-value-for-a-serde-enum
https://docs.rs/serde-enum-str/latest/serde_enum_str/

Questions:

  • Are you happy to pull in the additional dependency serde-enum-str?
    • I notice that there are currently a relatively small number of dependencies, so not sure whether you want to avoid adding new ones
  • Would you be happy for the additional enum field to be Other(String)?

@johnbatty
Copy link
Contributor

johnbatty commented May 25, 2022

Bah... I just implemented this, only to find that:

  • there are some enums that have an Other value, which obviously clashes with Other(String)
  • there are some enums that have an Error value with clashes with an Error type used within the serde-enum-str macros!

@johnbatty
Copy link
Contributor

Think I'll try replacing serde-enum-str with the workaround at the end of: serde-rs/serde#912.

That will fix my Error error. Then we just need to decide how to resolve the clash between Other and the new field Other(String). Possibly alternative names for the new field...

  • OtherValue(String)
  • Unknown(String)
  • Unrecognized(String)

Any alternative suggestions welcome.

I haven't yet checked whether any of these clash with existing enum values! I guess OtherValue(String) has least chance of clashing.

@cataggar
Copy link
Member Author

cataggar commented May 27, 2022

#769 used UnknownValue. That works for me.

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 a pull request may close this issue.

2 participants