OpenAPITools / openapi-generator

OpenAPI Generator allows generation of API client libraries (SDK generation), server stubs, documentation and configuration automatically given an OpenAPI Spec (v2, v3)
https://openapi-generator.tech
Apache License 2.0
21.91k stars 6.58k forks source link

[BUG][RUST-SERVER] doesn't produce compilable code with simple example API #2424

Open piersfinlayson opened 5 years ago

piersfinlayson commented 5 years ago
Description

I have written a simple, sample API, and rust-server generates code which doesn't compile.

Full cargo build output here, but there are three problems:

error[E0308]: match arms have incompatible types
  --> src/models.rs:42:9
   |
42 | /         match s {
43 | |             "0" => Ok(Arg::_0),
   | |                    ----------- match arm with an incompatible type
44 | |             "1" => Ok(Arg::_1),
45 | |             "2" => Ok(Arg::_2),
46 | |             _ => Err(()),
47 | |         }
   | |_________^ expected enum `std::result::Result`, found struct `models::Ok`
   |
   = note: expected type `std::result::Result<models::Arg, ()>`
              found type `models::Ok`

This might be a little harsh! The problem is that Ok here is models::Ok not Result::Ok.

Commit fixing the compilation errors here.

There are also similar errors with the client and server examples, plus also an extraneous use swagger::auth::Authorization; causing the server example to fail to compile:

error: unused import: `swagger::auth::Authorization`
  --> examples/server_lib/mod.rs:16:5
   |
16 | use swagger::auth::Authorization;
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   |
   = note: `-D unused-imports` implied by `-D warnings`

Fixes for the client and server examples are in this commit.

openapi-generator version

4.0.0-SNAPSHOT

Using container openapitools/openapi-generator-cli:latest

OpenAPI declaration file content or url

openapi.yaml

Command line used for generation
docker run --rm \
           -v ~/openapi-generator-rust-server-test/openapi_client:/local \
           openapitools/openapi-generator-cli \
           generate \
           -g rust-server \
           -i /local/openapi.yaml \
           -o /local
Steps to reproduce
cd ~
git clone https://github.com/piersfinlayson/openapi-generator-rust-server-test
cd openapi-generator-rust-server-test
docker pull openapitools/openapi-generator-cli
docker run --rm \
           -v ~/openapi-generator-rust-server-test/openapi_client:/local \
           openapitools/openapi-generator-cli \
           generate \
           -g rust-server \
           -i /local/openapi.yaml \
           -o /local
cd openapi_client
cargo build
cargo build --example client
cargo build --example server
Related issues/PRs

None that I can see

Suggest a fix

See above

auto-labeler[bot] commented 5 years ago

👍 Thanks for opening this issue! 🏷 I have applied any labels matching special text in your issue.

The team will review the labels and make any necessary changes.

richardwhiuk commented 4 years ago

wrong variable used to log parameter

This is fixed by https://github.com/OpenAPITools/openapi-generator/pull/5490 - the variable's correct, but we should be providing a type that does implement Display so we can provide some detail on why the arg failed to parse.

extraneous use swagger::auth::Authorization;

This is fixed by https://github.com/OpenAPITools/openapi-generator/pull/2504 - specifically https://github.com/OpenAPITools/openapi-generator/pull/2504/files#diff-222a21abd6957cc26b6f8b495c327837R16-R18

models::models::models::TYPE used instead of models::TYPE, e.g.

This was fixed by some general tidy up in https://github.com/OpenAPITools/openapi-generator/pull/2504

invalid FromStr implementation e.g.

This is being caused by the clashing Ok model.

https://github.com/OpenAPITools/openapi-generator/pull/5557 makes some progress to fix this sort of error, but didn't consider those types imported by the prelude such as Result.

I'll add something to that MR to fix this as well.