OpenAPITools / OpenAPITools/openapi-generator

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

Open
#2,424 2 comments 0 reactions 1 assignee View on GitHub

@richardwhiuk is already working on this.

Since Mar 22, 2020.

Issue: Bug Server: Rust
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

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:

  • models::models::models::TYPE used instead of models::TYPE, e.g.
error[E0603]: module `models` is private
  --> src/lib.rs:77:35
   |
77 |     fn a_post(&self, arg: models::models::models::Arg) -> Box<Future<Item=APostResponse, Error=ApiError>>;
  • wrong variable used to log parameter e.g.
error[E0277]: `()` doesn't implement `std::fmt::Display`
   --> src/server/mod.rs:210:173
    |
210 |                         Err(e) => return Box::new(future::ok(Response::new().with_status(StatusCode::BadRequest).with_body(format!("Couldn't parse path parameter arg: {}", e)))),

In this case , e) should be , param_arg)

  • invalid FromStr implementation e.g.
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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.