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] oneOf with value and array results in invalid code #18984

Open
5 of 6 tasks
jath03 opened this issue Jun 20, 2024 · 0 comments
Open
5 of 6 tasks

[BUG][RUST] oneOf with value and array results in invalid code #18984

jath03 opened this issue Jun 20, 2024 · 0 comments

Comments

@jath03
Copy link

jath03 commented Jun 20, 2024

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

Basically, using a oneof in rust generates an enum. When actually using that enum to make a request, there is an error because the
enum cannot be converted to a string because it doesn't implement the display trait.

To fix this, there are two options:

  1. Implement the display trait for the enum so that it converts itself into a string correctly (not ideal because you'd have to deal with url encoding when you implement the trait).
  2. Handle each enum variant when you are building the request (which is what I implemented - see below)
Full Error from output error[E0599]: the method `to_string` exists for reference `&GetSystemWaypointsTraitsParameter`, but its trait bounds were not satisfied --> src/apis/systems_api.rs:256:89 | 256 | local_var_req_builder = local_var_req_builder.query(&[("traits", &local_var_str.to_string())]); | ^^^^^^^^^ method cannot be called on `&GetSystemWaypointsTraitsParameter` due to unsatisfied trait bounds | ::: src/models/get_system_waypoints_traits_parameter.rs:16:1 | 16 | pub enum GetSystemWaypointsTraitsParameter { | ------------------------------------------ doesn't satisfy `GetSystemWaypointsTraitsParameter: ToString` or `_: Display` | = note: the following trait bounds were not satisfied: `GetSystemWaypointsTraitsParameter: std::fmt::Display` which is required by `GetSystemWaypointsTraitsParameter: ToString` `&GetSystemWaypointsTraitsParameter: std::fmt::Display` which is required by `&GetSystemWaypointsTraitsParameter: ToString` note: the trait `std::fmt::Display` must be implemented --> /home/jack/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/fmt/mod.rs:705:1 | 705 | pub trait Display { | ^^^^^^^^^^^^^^^^^ = help: items from traits can only be used if the trait is implemented and in scope = note: the following trait defines an item `to_string`, perhaps you need to implement it: candidate #1: `ToString`

For more information about this error, try rustc --explain E0599.
error: could not compile openapi (lib) due to 1 previous error

openapi-generator version

7.6.0
also tested with latest snapshot

OpenAPI declaration file content or url

https://raw.githubusercontent.com/SpaceTradersAPI/api-docs/main/reference/SpaceTraders.json
Specific offending portion:
https://github.com/SpaceTradersAPI/api-docs/blob/3ff410cbfe2ab9f05b73a7d1d92cca0593480018/reference/SpaceTraders.json#L513C11-L529C14

Generation Details

This is the command I was using

openapi-generator-cli generate -i spacetraders-api-docs-spacedust-patch/reference/SpaceTraders.json -g rust -o client-dist --additional-properties=packageName=spacedust,supportAsync=true,supportMiddleware=true
Steps to reproduce
git clone https://github.com/SpaceTradersAPI/api-docs
openapi-generator-cli generate -i api-docs/reference/SpaceTraders.json -g rust -o client-dist
cd client-dist
cargo check
Related issues/PRs

Seems to be somewhat related to this previously fixed issue: #17896
Searching the issue list for rust oneof comes up with a few that seems to be somewhat similar but not the same:
#18527
#17210
#13257
#9497
#7802
#3297

Suggest a fix

This is what I did to fix the generated code, but I don't know how I would fix the generator.

$ diff client-dist4/src/apis/systems_api.rs client-dist2/src/apis/systems_api.rs 
255,256c255,267
<     if let Some(ref local_var_str) = traits {
<         local_var_req_builder = local_var_req_builder.query(&[("traits", &local_var_str.to_string())]);
---
>     if let Some(ref local_var_enum) = traits {
>         if let models::GetSystemWaypointsTraitsParameter::Array(local_str_array) = local_var_enum {
>             for local_var_str in local_str_array {
>                 local_var_req_builder =
>                     local_var_req_builder.query(&[("traits", &local_var_str.to_string())]);
>             }
>         } else if let models::GetSystemWaypointsTraitsParameter::WaypointTraitSymbol(
>             local_var_str,
>         ) = local_var_enum
>         {
>             local_var_req_builder =
>                 local_var_req_builder.query(&[("traits", &local_var_str.to_string())]);
>         }

This is just something that works. I'm not necessarily recommending this because I don't know the conventions for generated code or what you have available in the generator.

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

No branches or pull requests

1 participant