Skip to content
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

[BUG][Rust] format! syntax error for path parameters with dashes in them #18666

Open
valpackett opened this issue May 14, 2024 · 2 comments
Open

Comments

@valpackett
Copy link
Contributor

Description

The Rust generator generates syntax errors when URI templates have parameter names with - dashes in them.

openapi-generator version

0e05cf2

OpenAPI declaration file content or url
openapi: '3.0.0'
info:
  version: 1.0.0
  title: test
paths:
  /test/{uwu-owo}:
    get:
      summary: sample
      parameters:
      - name: uwu-owo
        in: path
        required: true
        schema:
          type: string
      responses:
        '200':
          description: OK
          content:
            application/json:
              schema:
                type: object
                properties:
                  hello:
                    type: string
Generation Details
java -jar /home/val/.m2/repository/org/openapitools/openapi-generator-cli/7.6.0-SNAPSHOT/openapi-generator-cli-7.6.0-SNAPSHOT.jar generate -i dash.yaml -o rsbug/ -g rust
Steps to reproduce

generate the above, try to build, get syntax error:

error: invalid format string: expected `'}'`, found `'-'`
  --> src/apis/default_api.rs:31:50
   |
31 |     let local_var_uri_str = format!("{}/test/{uwu-owo}", local_var_configuration.base_path, uwu-owo=crate::api...
   |                                              -   ^ expected `'}'` in format string
   |                                              |
   |                                              because of this opening brace
   |
   = note: if you intended to print `{`, you can escape it using `{{`
Related issues/PRs

🤷‍♀️

Suggest a fix

Add dash replacement for format! key names

@Fefer-Ivan
Copy link

Fefer-Ivan commented Jun 14, 2024

Looks like the problem is caused by this line:

let local_var_uri_str = format!("{}{{{path}}}", local_var_configuration.base_path{{#pathParams}}, {{{baseName}}}={{#isString}}crate::apis::urlencode({{/isString}}{{{paramName}}}{{^required}}.unwrap(){{/required}}{{#required}}{{#isNullable}}.unwrap(){{/isNullable}}{{/required}}{{#isArray}}.join(",").as_ref(){{/isArray}}{{^isString}}{{^isUuid}}{{^isPrimitiveType}}{{^isContainer}}.to_string(){{/isContainer}}{{/isPrimitiveType}}{{/isUuid}}{{/isString}}{{#isString}}){{/isString}}{{/pathParams}});

Arguments {{path}} and {{baseName}} are used which are not sanitized for Rust.

For a workaround it is possible to use hyper library instead of reqwest.

@fdietze
Copy link

fdietze commented Nov 1, 2024

My workaround to fix the generated code:

# Enable globstar for recursive globbing in bash
shopt -s globstar

# 3 words kebab-case to snake_case
sed -i -E 's/\{([a-z]+)-([a-z]+)-([a-z]+)\}/{\1_\2_\3}/g' my-generated-api/**/*.rs
sed -i -E 's/([a-z]+)-([a-z]+)-([a-z]+)\=/\1_\2_\3=/g' my-generated-api/**/*.rs

# 2 words kebab-case to snake_case
sed -i -E 's/\{([a-z]+)-([a-z]+)\}/{\1_\2}/g' my-generated-api/**/*.rs
sed -i -E 's/([a-z]+)-([a-z]+)\=/\1_\2=/g' my-generated-api/**/*.rs

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

No branches or pull requests

3 participants