ory / sdk

The place where ORY's SDKs are being auto-generated
Apache License 2.0
137 stars 85 forks source link

UiNodeAttributes tag value generates with incorrect casing #276

Closed cbzehner closed 3 weeks ago

cbzehner commented 1 year ago

Preflight checklist

Describe the bug

When generating the Rust SDK, the value nodeType serializes to nodetype (incorrect) rather than node_type (correct).

This leads to the following error when attempting to use the SDK to deserialize UINodes into Rust structs: reqwest::Error { kind: Decode, source: Error("missing fieldnodetype", line: 1, column: 585) }

Changing this line locally to use the correct casing fixes the issue

This affects at least SDK versions corresponding to 0.11 & 0.13 - I haven't checked back further than that.

Reproducing the bug

Create a new rust project with

  1. cargo new --bin test-node-type-issue
  2. Add ory-kratos-client, serde & serde_json as dependencies
  3. Add the following code (below) to your main.rs file
  4. Run cargo test to run the test and reproduce the issue
#[cfg(test)]
mod tests {
    use super::*;
    use serde_json::json;
    use ory_kratos_client::models::LoginFlow;

    #[test]
    fn test_deserialize_login_flow() {
        let example = r##"
        {
            "id": "2e386b63-7fa1-4d1e-94df-446f95b70450",
            "type": "browser",
            "expires_at": "2023-06-11T03:30:35.827119Z",
            "issued_at": "2023-06-11T03:20:35.827119Z",
            "request_url": "http://127.0.0.1:4433/self-service/registration/browser",
            "ui": {
              "action": "http://127.0.0.1:4433/self-service/registration?flow=2e386b63-7fa1-4d1e-94df-446f95b70450",
              "method": "POST",
              "nodes": [
                {
                  "type": "input",
                  "group": "default",
                  "attributes": {
                    "name": "csrf_token",
                    "type": "hidden",
                    "value": "YA6fD6VsPTE1XU3WCtbDBdY+y/KfKfY/cdm6ypr2t7RCVGqsO4TLR/USSFl86ULJoZEhsaAYBtS12w5Gip9EpQ==",
                    "required": true,
                    "disabled": false,
                    "node_type": "input"
                  },
                  "messages": [],
                  "meta": {}
                },
                {
                  "type": "input",
                  "group": "password",
                  "attributes": {
                    "name": "traits.email",
                    "type": "email",
                    "required": true,
                    "disabled": false,
                    "node_type": "input"
                  },
                  "messages": [],
                  "meta": {
                    "label": {
                      "id": 1070002,
                      "text": "E-Mail",
                      "type": "info"
                    }
                  }
                },
                {
                  "type": "input",
                  "group": "password",
                  "attributes": {
                    "name": "password",
                    "type": "password",
                    "required": true,
                    "disabled": false,
                    "node_type": "input"
                  },
                  "messages": [],
                  "meta": {
                    "label": {
                      "id": 1070001,
                      "text": "Password",
                      "type": "info"
                    }
                  }
                },
                {
                  "type": "input",
                  "group": "password",
                  "attributes": {
                    "name": "traits.name.first",
                    "type": "text",
                    "disabled": false,
                    "node_type": "input"
                  },
                  "messages": [],
                  "meta": {
                    "label": {
                      "id": 1070002,
                      "text": "First Name",
                      "type": "info"
                    }
                  }
                },
                {
                  "type": "input",
                  "group": "password",
                  "attributes": {
                    "name": "traits.name.last",
                    "type": "text",
                    "disabled": false,
                    "node_type": "input"
                  },
                  "messages": [],
                  "meta": {
                    "label": {
                      "id": 1070002,
                      "text": "Last Name",
                      "type": "info"
                    }
                  }
                },
                {
                  "type": "input",
                  "group": "password",
                  "attributes": {
                    "name": "traits.name.preferred",
                    "type": "text",
                    "disabled": false,
                    "node_type": "input"
                  },
                  "messages": [],
                  "meta": {
                    "label": {
                      "id": 1070002,
                      "text": "Preferred Name",
                      "type": "info"
                    }
                  }
                },
                {
                  "type": "input",
                  "group": "password",
                  "attributes": {
                    "name": "method",
                    "type": "submit",
                    "value": "password",
                    "disabled": false,
                    "node_type": "input"
                  },
                  "messages": [],
                  "meta": {
                    "label": {
                      "id": 1040001,
                      "text": "Sign up",
                      "type": "info",
                      "context": {}
                    }
                  }
                }
              ]
            }
          }
        "##;

        serde_json::from_str::<LoginFlow>(example).unwrap();
    }
}

Relevant log output

No response

Relevant configuration

No response

Version

v0.11.1 & v0.13

On which operating system are you observing this issue?

macOS

In which environment are you deploying?

Binary

Additional Context

No response

RobDavenport commented 1 year ago

Related to #275 I assume. Would love to know how to fix this but have no idea where to start. Any ideas?

RobDavenport commented 11 months ago

Seems like this struct in particular is quite difficult according to #72 - however it looks like the it's being properly generated now as an enum. Unfortunately, tag is still being incorrectly generated as "nodetype" instead of "node_type" which the spec states. Is this an issue with OpenAPI's generator? I was browsing over there and came accross this PR https://github.com/OpenAPITools/openapi-generator/pull/16205 but unfortunately am not too familiar with the generator to understand if this is truly related or not.

Edit: Nevermind it looks related to this issue: https://github.com/OpenAPITools/openapi-generator/issues/14745 which apparently fixes this - but the issue still persists. Do we need to regenerate the rust client code or something here?

akkie commented 9 months ago

This problem is fixed with the latest openapi-generator.

As example generated with latest version:

#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct UiNodeTextAttributes {
    /// A unique identifier
    #[serde(rename = "id")]
    pub id: String,
    /// NodeType represents this node's types. It is a mirror of `node.type` and is primarily used to allow compatibility with OpenAPI 3.0.  In this struct it technically always is \"text\".
    #[serde(rename = "node_type")]
    pub node_type: String,
    #[serde(rename = "text")]
    pub text: Box<crate::models::UiText>,
}

impl UiNodeTextAttributes {
    pub fn new(id: String, node_type: String, text: crate::models::UiText) -> UiNodeTextAttributes {
        UiNodeTextAttributes {
            id,
            node_type,
            text: Box::new(text),
        }
    }
}

The problem is that we cannot switch to the openapi-generator provided templates, because this issue is still open: https://github.com/OpenAPITools/openapi-generator/issues/13257

aeneasr commented 3 weeks ago

This should be resolved with the new versions of the SDK!