Skip to content

[BUG][RUST][CLIENT] Generated enum type used with format! but it doesn't impl Display #16975

Closed
@eliaperantoni

Description

@eliaperantoni

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

If an endpoint uses an enum as the path parameter, and the enum is defined in components rather than inline, then the generated default_api.rs creates the URL with the format! macro but the generated Rust enum doesn't implement Display (it does implement ToString instead).

error[E0277]: `Color` doesn't implement `std::fmt::Display`
  --> src/apis/default_api.rs:31:97
   |
31 |     let local_var_uri_str = format!("{}/demo/{color}", local_var_configuration.base_path, color=color);
   |                                                                                                 ^^^^^ `Color` cannot be formatted with the default formatter
   |
   = help: the trait `std::fmt::Display` is not implemented for `Color`
   = note: in format strings you may be able to use `{:?}` (or {:#?} for pretty-print) instead
   = note: this error originates in the macro `$crate::__export::format_args` which comes from the expansion of the macro `format` (in Nightly builds, run with -Z macro-backtrace for more info)

NOTE: You need to reference the enum like:

schema:
  allOf:
    - $ref: "#/components/schemas/Color"

otherwise you're going to run into issue #16974 first.

openapi-generator version

I'm using the Docker openapitools/openapi-generator-cli:latest image. openapi-generator-cli version prints 7.1.0-SNAPSHOT.

OpenAPI declaration file content or url
openapi: 3.0.3
info:
  title: Demo
  version: 0.0.0
paths:
  /demo/{color}:
    get:
      parameters:
        - name: color
          required: true
          in: path
          schema:
            $ref: "#/components/schemas/Color"
      responses:
        200:
          description: OK
components:
  schemas:
    Color:
      type: string
      enum:
        - RED
        - GREEN
        - BLUE
Generation Details
$ openapi-generator generate -i openapi.yaml -g rust -o demo

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