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

Type aliases not being resolved correctly #429

Closed
dob9601 opened this issue Jan 7, 2023 · 3 comments · Fixed by #1048
Closed

Type aliases not being resolved correctly #429

dob9601 opened this issue Jan 7, 2023 · 3 comments · Fixed by #1048
Labels
Aliases Work for global aliases

Comments

@dob9601
Copy link

dob9601 commented Jan 7, 2023

Using the type alias

pub type DateTimeUtc = chrono::DateTime<chrono::Utc>;

Leads to any serialized struct field having the type:

{recursive: DateTimeUtc}

Directly specifying the type as DateTime<Utc> in the struct leads to the type being correctly resolved. (type shows as date-time)

@juhaku
Copy link
Owner

juhaku commented Mar 13, 2023

Hey,

This is because during parsing the AST we do not know what is the actual type of DateTimeUtc but we only know the type definition of the alias which in this case is the DateTimeUtc. This is then used to compare against know set of types to determine the the type and format for the field in the schema. That being said DateTimeUtc naturally will not match to any of predefined types.

One alternative to type aliases here would be using a new type pattern, but that also adds another layer of indirection to the types. This would introduce another layer of boilerplate that one might need to implement for the new type to be able to access to the underlying type properties more elegantly.

Type aliases in general are quite hard thing in the utoipa world.

@juhaku
Copy link
Owner

juhaku commented Sep 9, 2024

This same topic has been discussed here #751, #790 and #766

The Rust aliases would need shared memory to read the possible alias representations. I have some plans but still in early stages.

@juhaku juhaku added the Aliases Work for global aliases label Sep 9, 2024
juhaku added a commit that referenced this issue Sep 12, 2024
This PR adds global config for `utiopa` which allows defining global
Rust type aliases that can be recognized by `utoipa`.

```rust
 let _ = Config::new()
     .alias_for("MyType", "i32");
     .alias_for("Json", "Value");
     .alias_for("NullableString", "Option<String>");
```

Closes #984 Closes #787 Closes #766 Closes #429
@juhaku juhaku moved this to In Progress in utoipa kanban Sep 12, 2024
@juhaku
Copy link
Owner

juhaku commented Sep 12, 2024

@dob9601 There is now coming a new PR which adds global type aliases for utoipa #1048.

@juhaku juhaku closed this as completed in e66b4ed Sep 12, 2024
@github-project-automation github-project-automation bot moved this from In Progress to Done in utoipa kanban Sep 12, 2024
@juhaku juhaku moved this from Done to Released in utoipa kanban Oct 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Aliases Work for global aliases
Projects
Status: Released
Development

Successfully merging a pull request may close this issue.

2 participants