Skip to content

[BUG] [RUST] Missing lifetime when using mockall feature and optional query parameter #24912

Description

@ThaddeusTreloar

Bug Report Checklist

  • [*] Have you provided a full/minimal spec to reproduce the issue?
  • [*] Have you validated the input using an OpenAPI validator?
  • [*] 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

When generating using the reqwest-trait library and mockall feature (uses the mockall::automock procedural macro), the lifetime annotations in the function signature causing compilation to fail.

openapi-generator version

docker: openapitools/openapi-generator-cli:latest

OpenAPI declaration file content or url
openapi: 3.0.3
info:
  title: Mockall lifetime bu
  description: >-
    An optional string query parameter causes the generator to declare a named
    lifetime (e.g. 'run_id) in the async fn generics but emit Option<&str>
    (anonymous lifetime) instead of Option<&'run_id str>. The #[automock]
    macro cannot elide the lifetime.
  version: 1.0.0
servers:
  - url: /api/v1
paths:
  /widgets/{id}/items:
    parameters:
      - name: id
        in: path
        required: true
        schema:
          type: string
          format: uuid
    get:
      operationId: listWidgetItems
      summary: List items for a widget, optionally filtered by run_id
      tags:
        - widget
      parameters:
        - name: run_id
          in: query
          required: false
          description: Optional string query param — triggers the lifetime bug when mockall=true
          schema:
            type: string
            format: uuid
      responses:
        "200":
          description: Item list
          content:
            application/json:
              schema:
                type: object
                required:
                  - items
                properties:
                  items:
                    type: array
                    items:
                      $ref: "#/components/schemas/Item"
components:
  schemas:
    Item:
      type: object
      required:
        - id
      properties:
        id:
          type: string
          format: uuid
Generation Details
podman run --rm -v "$PWD":/local openapitools/openapi-generator-cli:latest generate \
	    -i /local/bug/v1/spec.yaml \
	    -g rust \
	    -o /local/target/spec-lib-rs-client-bug \
	    --additional-properties packageName=spec-lib-rs-client-bug,packageVersion=1.0.0,supportMiddleware=true,reqwestDefaultFeatures=rustls,library=reqwest-trait,mockall=true
Steps to reproduce

Run the provided command with the provided spec as the target. Then attempt to build the library using:

cargo build --features mockall

The output should be:

error[E0106]: missing lifetime specifier
  --> src/apis/widget_api.rs:29:78
   |
29 |     async fn list_widget_items<'id, 'run_id>(&self, id: &str, run_id: Option<&str>) -> Result<models::ListWidgetItems200Resp...
   |                                                                              ^ expected named lifetime parameter
   |
   = note: for more information on higher-ranked polymorphism, visit https://doc.rust-lang.org/nomicon/hrtb.html
help: consider making the bound lifetime-generic with a new `'a` lifetime
   |
22 ~ #[cfg_attr(feature = "mockall", for<'a> automock)]
23 | #[async_trait]
...
28 |     ///
29 ~     async fn list_widget_items<'id, 'run_id>(&self, id: &str, run_id: Option<&'a str>) -> Result<models::ListWidgetItems200Response, Error<ListWidgetItemsError>>;
   |
help: consider introducing a named lifetime parameter
   |
22 ~ #[cfg_attr(feature = "mockall", automock<'a>)]
23 | #[async_trait]
...
28 |     ///
29 ~     async fn list_widget_items<'id, 'run_id>(&self, id: &str, run_id: Option<&'a str>) -> Result<models::ListWidgetItems200Response, Error<ListWidgetItemsError>>;
   |

error[E0637]: `&` without an explicit lifetime name cannot be used here
  --> src/apis/widget_api.rs:29:78
   |
29 |     async fn list_widget_items<'id, 'run_id>(&self, id: &str, run_id: Option<&str>) -> Result<models::ListWidgetItems200Resp...
   |                                                                              ^ explicit lifetime name needed here
   |
help: consider introducing a higher-ranked lifetime here
   |
22 ~ #[cfg_attr(feature = "mockall", for<'a> automock)]
23 | #[async_trait]
...
28 |     ///
29 ~     async fn list_widget_items<'id, 'run_id>(&self, id: &str, run_id: Option<&'a str>) -> Result<models::ListWidgetItems200Response, Error<ListWidgetItemsError>>;
   |

Some errors have detailed explanations: E0106, E0637.
For more information about an error, try `rustc --explain E0106`.
error: could not compile `spec-lib-rs-client-bug` (lib) due to 2 previous errors

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions