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

[BUG] [Rust] fails to compile discriminator if multiple oneOf definitions use the same key names #13257

Open
6 tasks done
aeneasr opened this issue Aug 23, 2022 · 5 comments · May be fixed by #13259
Open
6 tasks done

Comments

@aeneasr
Copy link
Contributor

aeneasr commented Aug 23, 2022

Bug Report Checklist

  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator (example)?
  • Have you tested with the latest master to confirm the issue still exists?
  • Have you searched for related issues/PRs?
  • What's the actual output vs expected output?
  • [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description

When using a spec which has a type that is a discriminator with multiple types, and at least two of those types have equal property names, the Rust generator fails to generate compilable code for that discriminator in cases where the conflicting key is also a type (e.g. an enum):

      "uiNodeAttributes": {
        "discriminator": {
          "mapping": {
            "a": "#/components/schemas/uiNodeAnchorAttributes",
            "img": "#/components/schemas/uiNodeImageAttributes",
            "input": "#/components/schemas/uiNodeInputAttributes",
            "script": "#/components/schemas/uiNodeScriptAttributes",
            "text": "#/components/schemas/uiNodeTextAttributes"
          },
          "propertyName": "node_type"
        },
        "oneOf": [
          {
            "$ref": "#/components/schemas/uiNodeInputAttributes"
          },
          {
            "$ref": "#/components/schemas/uiNodeTextAttributes"
          },
          {
            "$ref": "#/components/schemas/uiNodeImageAttributes"
          },
          {
            "$ref": "#/components/schemas/uiNodeAnchorAttributes"
          },
          {
            "$ref": "#/components/schemas/uiNodeScriptAttributes"
          }
        ],
        "title": "Attributes represents a list of attributes (e.g. `href=\"foo\"` for links)."
      },
      "uiNodeScriptAttributes": {
        "properties": {
          "async": {
            "description": "The script async type",
            "type": "boolean"
          },
          "crossorigin": {
            "description": "The script cross origin policy",
            "type": "string"
          },
          "id": {
            "description": "A unique identifier",
            "type": "string"
          },
          "integrity": {
            "description": "The script's integrity hash",
            "type": "string"
          },
          "node_type": {
            "description": "NodeType represents this node's types. It is a mirror of `node.type` and\nis primarily used to allow compatibility with OpenAPI 3.0. In this struct it technically always is \"script\".",
            "type": "string"
          },
          "nonce": {
            "description": "Nonce for CSP\n\nA nonce you may want to use to improve your Content Security Policy.\nYou do not have to use this value but if you want to improve your CSP\npolicies you may use it. You can also choose to use your own nonce value!",
            "type": "string"
          },
          "referrerpolicy": {
            "description": "The script referrer policy",
            "type": "string"
          },
          "src": {
            "description": "The script source",
            "type": "string"
          },
          "type": {
            "description": "The script MIME type",
            "type": "string"
          }
        },
        "required": [
          "src",
          "async",
          "referrerpolicy",
          "crossorigin",
          "integrity",
          "type",
          "id",
          "nonce",
          "node_type"
        ],
        "title": "ScriptAttributes represent script nodes which load javascript.",
        "type": "object"
      },
      "uiNodeInputAttributes": {
        "description": "InputAttributes represents the attributes of an input node",
        "properties": {
          "type": {
            "description": "The input's element type.",
            "enum": [
              "text",
              "password",
              "number",
              "checkbox",
              "hidden",
              "email",
              "tel",
              "submit",
              "button",
              "datetime-local",
              "date",
              "url"
            ],
            "type": "string"
          },
          "typeenum": {
            "description": "The input's element type.",
            "enum": [
              "text",
              "password",
              "number",
              "checkbox",
              "hidden",
              "email",
              "tel",
              "submit",
              "button",
              "datetime-local",
              "date",
              "url"
            ],
            "type": "string"
          }
        },
        "required": [
          "type",
          "typeenum"
        ],
        "type": "object"
      },

In the example above, the file ui_node_attributes.rs contains a large enum definition for the discriminator but is missing TypeEnum:



#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(tag = "nodetype")]
pub enum UiNodeAttributes {
    #[serde(rename="a")]
    UiNodeAnchorAttributes {
        // ...
    },
    #[serde(rename="img")]
    UiNodeImageAttributes {
        // ...
    },
    #[serde(rename="input")]
    UiNodeInputAttributes {
        // ...

        /// The input's element type.
        #[serde(rename = "type")]
        // true, false, TypeEnum, String, false
        _type: TypeEnum,
        /// The input's element type.
        #[serde(rename = "typeenum")]
        // true, false, TypeenumEnum, String, false
        typeenum: TypeenumEnum,

        // ..
    },
    // ..
}

/// The autocomplete attribute for the input.
#[derive(Clone, Copy, Debug, Eq, PartialEq, Ord, PartialOrd, Hash, Serialize, Deserialize)]
pub enum AutocompleteEnum {
    #[serde(rename = "email")]
    Email,
    #[serde(rename = "tel")]
    Tel,
    #[serde(rename = "url")]
    Url,
    #[serde(rename = "current-password")]
    CurrentPassword,
    #[serde(rename = "new-password")]
    NewPassword,
    #[serde(rename = "one-time-code")]
    OneTimeCode,
}

#[derive(Clone, Copy, Debug, Eq, PartialEq, Ord, PartialOrd, Hash, Serialize, Deserialize)]
pub enum TypeenumEnum {
    #[serde(rename = "text")]
    Text,
    #[serde(rename = "password")]
    Password,
    #[serde(rename = "number")]
    Number,
    #[serde(rename = "checkbox")]
    Checkbox,
    #[serde(rename = "hidden")]
    Hidden,
    #[serde(rename = "email")]
    Email,
    #[serde(rename = "tel")]
    Tel,
    #[serde(rename = "submit")]
    Submit,
    #[serde(rename = "button")]
    Button,
    #[serde(rename = "datetime-local")]
    DatetimeLocal,
    #[serde(rename = "date")]
    Date,
    #[serde(rename = "url")]
    Url,
}

As you can see above, the two enums TypeenumEnum and AutocompleteEnum are correctly marked as "enums" and included, TypeEnum however is missing. This happens because the uiNodeScriptAttributes also has a key called type:

          "type": {
            "description": "The script MIME type",
            "type": "string"
          }

which is clashing with the type field from uiNodeInputAttributes:

          "type": {
            "description": "The input's element type.",
            "enum": [
              "text",
              "password",
              "number",
              "checkbox",
              "hidden",
              "email",
              "tel",
              "submit",
              "button",
              "datetime-local",
              "date",
              "url"
            ],
            "type": "string"
          },

I was able to identify this by adding the following debug statement to model.mustache of the Rust generator:

{{!-- for properties that are of enum type --}}
{{#vars}}
+/*
+{{{.}}}
+*/
{{#isEnum}}
/// {{{description}}}
#[derive(Clone, Copy, Debug, Eq, PartialEq, Ord, PartialOrd, Hash, Serialize, Deserialize)]
pub enum {{{enumName}}} {
{{#allowableValues}}
{{#enumVars}}
    #[serde(rename = "{{{value}}}")]
    {{{name}}},
{{/enumVars}}
{{/allowableValues}}
}
{{/isEnum}}
{{/vars}}

which then showed that there is only one variable called type and it is coming from the script definition:

https://gist.github.com/aeneasr/83cbb9013e589904332cab0482a765c1#file-ui_node_attributes-rs-L216-L219

If I change the order and move #/components/schemas/uiNodeInputAttributes below #/components/schemas/uiNodeScriptAttributes, the enum TypeEnum is correctly generated. However, this is only a temporary solution as it would still clashes with the other property definition.

openapi-generator version

All versions of 5.x and 6.x are affected

OpenAPI declaration file content or url

https://gist.github.com/aeneasr/c201992378c87943dfdbe559adc32bae

Generation Details
rust.yml
packageName: ory-client
packageVersion: v0.2.0-alpha.13
library: reqwest
supportAsync: true
enumNameSuffix: Enum
Steps to reproduce

Use spec file https://gist.github.com/aeneasr/c201992378c87943dfdbe559adc32bae

  openapi-generator-cli version-manager set 6.0.1
  openapi-generator-cli generate -i "${SPEC_FILE}" \
    -g rust \
    -o "$dir" \
    --git-user-id ory \
    --git-repo-id sdk \
    --git-host github.com \
    -c ./config/client/rust.yml
Related issues/PRs

None

Suggest a fix

Looks like this has to be fixed outside of the templates and in the Java code base?

@wing328
Copy link
Member

wing328 commented Aug 24, 2022

I don't think rust client/server generator supports oneOf/anyOf at the moment.

May I know if you've time to make the contribution?

I can show you some reference implementations in other languages as a starting point.

@aeneasr
Copy link
Contributor Author

aeneasr commented Aug 24, 2022

It does :)

I fixed the problem here: #13259

a-liashenko pushed a commit to a-liashenko/openapi-generator that referenced this issue Aug 10, 2023
@akkie
Copy link
Contributor

akkie commented Oct 9, 2023

@wing328 Is there a chance that this gets fixed? The PR #13259 is ready since a year. Maybe a comment why the PR might not be suitable so that we can work on it?

@humb1t
Copy link
Contributor

humb1t commented Nov 1, 2023

+1 - also waiting for it to be merged and can try to contribute additional fixes if needed

@wing328
Copy link
Member

wing328 commented Nov 1, 2023

sorry for the delay in reviewing the fix.

I tested with

java -jar modules/openapi-generator-cli/target/openapi-generator-cli.jar generate -g rust -i https://gist.githubusercontent.com/aeneasr/c201992378c87943dfdbe559adc32bae/raw/bdd23b17707b67c55b0ca7a3e128b198f5bbf0f3/openapi.json -o /tmp/rust2/

but couldn't repeat the issue by running cargo test in /tmp/rust2

   Compiling reqwest v0.11.22
   Compiling openapi v0.2.0-alpha.13 (/private/tmp/rust2)
    Finished test [unoptimized + debuginfo] target(s) in 1m 13s
     Running unittests src/lib.rs (target/debug/deps/openapi-8cac114e773fb363)

running 0 tests

test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s

   Doc-tests openapi

running 0 tests

test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s

Can you tell me more how to repeat the issue to confirm the fix?

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

Successfully merging a pull request may close this issue.

4 participants