Skip to content

fix compilation error in rust when using enums as part of the path #11754

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

ctron
Copy link
Contributor

@ctron ctron commented Mar 1, 2022

When an enum is used as part of the path, the compilation will fail as the enum implements ToString but not Display.

However, implementing Display automatically implements ToString. So this PR implements Display in favor of ToString.

Also see: https://doc.rust-lang.org/std/string/trait.ToString.html

This trait is automatically implemented for any type which implements the Display trait. As such, ToString shouldn’t be implemented directly: Display should be implemented instead, and you get the ToString implementation for free.

PR checklist

  • Read the contribution guidelines.
  • Pull Request title clearly describes the work in the pull request and Pull Request description provides details about how to validate the work. Missing information here may result in delayed response from the community.
  • Run the following to build the project and update samples:
    ./mvnw clean package 
    ./bin/generate-samples.sh
    ./bin/utils/export_docs_generators.sh
    
    Commit all changed files.
    This is important, as CI jobs will verify all generator outputs of your HEAD commit as it would merge with master.
    These must match the expectations made by your contribution.
    You may regenerate an individual generator by passing the relevant config(s) as an argument to the script, for example ./bin/generate-samples.sh bin/configs/java*.
    For Windows users, please run the script in Git BASH.
  • File the PR against the correct branch: master (5.3.0), 6.0.x
  • If your PR is targeting a particular programming language, @mention the technical committee members, so they are more likely to review the pull request.

@frol @farcaller @richardwhiuk @paladinzh

@wing328
Copy link
Member

wing328 commented Jul 9, 2022

@ctron thanks for the PR. May I know if you can provide a spec to reproduce the issue?

I tried to repeat the issue locally but couldn't.

@ctron
Copy link
Contributor Author

ctron commented Jul 10, 2022

Sure, like this:

info:
  title: Sample Pet Store App
  version: 1.0.1
openapi: 3.0.3
paths:
  /pets/{type}:
    get:
      responses:
        '200':
          description: Some value
    parameters:
      - name: type
        in: path
        schema:
          $ref: '#/components/schemas/test'
components:
  schemas:
    test:
      type: string
      enum:
        - foo
        - bar
        - baz

@wing328
Copy link
Member

wing328 commented Jul 13, 2022

@ctron I tested your PR with the spec you provided but got errors when running cargo check:

    Checking reqwest v0.11.11
    Checking openapi v1.0.1 (/private/tmp/rust2)
error[E0412]: cannot find type `Test` in this scope
  --> src/apis/default_api.rs:26:88
   |
26 | pub async fn pets_type_get(configuration: &configuration::Configuration, _type: Option<Test>) -> Result<(), Error<PetsTypeGetError>> {
   |                                                                                        ^^^^ not found in this scope
   |
help: consider importing this enum
   |
12 | use crate::models::Test;
   |

For more information about this error, try `rustc --explain E0412`.
error: could not compile `openapi` due to previous error

Does cargo check work fine for you in your machine?

@ctron
Copy link
Contributor Author

ctron commented Jul 13, 2022

Yea, that seems to be another issue. You can import that manually though.

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

Successfully merging this pull request may close these issues.

2 participants