OpenAPITools / OpenAPITools/openapi-generator

[BUG] [RUST-SERVER] Unnecessary use of refence for parameter Vec in Api trait

Open
#22,626 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Description

I don't think it is necessary to pass a reference of the parameter Vec. The relevant part is in the Api trait in src/lib.rs:

/// API
#[async_trait]
#[allow(clippy::too_many_arguments, clippy::ptr_arg)]
pub trait Api<C: Send + Sync> {
    // [...]
    /// Add new users
    async fn users_put(
        &self,                                /* \/ Why is there a "&" here? */
        _users_put_request_inner: &Vec<models::UsersPutRequestInner>,
        context: &C) -> Result<UsersPutResponse, ApiError>;
    // [...]
}

In src/server/mod.rs it is definitively not needed - at least not with my config utilizing the "requestBody" for parameter passing:

let param_user_add = match param_user_add {
                                    Some(param_user_add) => param_user_add,
                                    None => return Ok(Response::builder()
                                                        .status(StatusCode::BAD_REQUEST)
                                                        .body(Body::from("Missing required body parameter user_add"))
                                                        .expect("Unable to create Bad Request response for missing body parameter user_add")),
                                };

                                let result = api_impl.users_put( // only usage of users_put in the whole library
                                            param_user_add.as_ref(), // this .as_ref() is not needed, as this is the only usage of param_user_add
                                        &context
                                    ).await;

I think it maybe a relict of the past and is not required (anymore). Just nobody looked into it. The reference symbol was conditionally present since the initial commit of the rust-server template .

So my question is: (When) Is the "&" (still) required in {#isArray}}&{{#exts.x-param-needs-lifetime}}'a {{/exts.x-param-needs-lifetime}}{{/isArray}}?

openapi-generator version

v7.7.0
I did not test with the master version as the code is still present in main.

OpenAPI declaration file content or url
openapi: 3.0.0
info:
  title: Example API
  version: 0.0.1
  description: This API allows communicating with my server.
paths:
  /users:
    put:
      summary: Add new users
      description: If one user can not be added, none is added.
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: array
              items: # NOTE: the following part was originally from another file (referenced). So this is the extract.
                type: object
                properties:
                  username:
                    type: string
                    description: The username of this user
Generation Details

I modified my run configuration of IntelliJ to use "rust-server" instead of rust template to generate me this code via the OpenAPI RustRover plugin.

Steps to reproduce

Run the code gen over the provided yaml file with the "rust-server" template.
Look into gen/src/lib.src.

Related issues/PRs

I don't think so

Suggest a fix

Check when a reference is required and either:

  1. Don't use just the isArray condition for checking if a "&" (with lifetimes) needs to be added
  2. Remove the {#isArray}}&{{#exts.x-param-needs-lifetime}}'a {{/exts.x-param-needs-lifetime}}{{/isArray}} part completely
  3. Lock referencing behind a config flag

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.

Research direction

Start with modules/openapi-generator/src/main/resources/rust-server/lib.mustache around the referenced parameter condition, then compare the generated signatures in src/lib.rs and the call in src/server/mod.rs. Reproduce the provided OpenAPI YAML with the rust-server template and determine when the reference and lifetime are required. Done means the template behavior is clarified and the generated output no longer adds an unnecessary reference, with the relevant generation behavior checked.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.