[BUG][Rust] reqwest client does not compile for required nullable params, free-form object query params, inline object form fields and a param named configuration

未关闭
#25,020 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

还没有人认领这个 Issue。

评估

难度
4/5
预计耗时
3-5 天
新手友好度
55/100
Issue 类型
缺陷
描述清晰度
基本清楚
活跃度
活跃
技术栈
openapi, rust
领域
api, tooling

调研方向

Start by generating the client from the supplied spec.yaml with the shown OpenAPI Generator command, then run cargo check and inspect src/apis/default_api.rs. Trace the Rust generator templates for query parameters, multipart fields, model qualification, and parameter naming. Done means the four reduced cases generate compiling Rust without the reported errors.

由索引模型根据 Issue 内容生成。

描述

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?
Description

The rust generator (default reqwest library) emits code that doesn't compile for four parameter shapes. They came up generating a client for GitLab's description (doc/api/openapi/openapi_v3.yaml, 19.5), where they account for 214 of the 219 compile errors. Four more come from path parameters GitLab uses without declaring, and one from .to_string() on an array-of-objects form field, which I haven't reduced. The spec below reduces the four to three operations.

  1. A parameter that is required and nullable: true gets an Option type, but the template takes the required branch and uses it as a plain value. For a query array it iterates the Option and calls .to_string() on the whole Vec<String>; for a multipart file it passes Option<PathBuf> to TokioFile::open. This is 158 of GitLab's errors.
  2. A free-form object query parameter (type: object, no properties) is typed models::serde_json::Value.
  3. An inline object form field is typed models::models::CreateImportRequestConfiguration. Together with 2, that's 48 errors on GitLab.
  4. A form field named configuration becomes a second parameter named configuration, next to the &configuration::Configuration argument, and the rest of the function then fails to type-check (8 errors on GitLab).
openapi-generator version

7.25.0.

OpenAPI declaration file content or url
openapi: 3.0.3
info:
  title: rust reqwest compile errors
  version: "1"
paths:
  /variables:
    delete:
      operationId: deleteVariables
      parameters:
        - name: filter
          in: query
          schema:
            type: object
            nullable: true
        - name: paths
          in: query
          required: true
          schema:
            type: array
            nullable: true
            items:
              type: string
      responses:
        "204":
          description: deleted
  /imports:
    post:
      operationId: createImport
      requestBody:
        content:
          application/x-www-form-urlencoded:
            schema:
              type: object
              properties:
                configuration:
                  type: object
                  properties:
                    url:
                      type: string
      responses:
        "201":
          description: created
  /uploads:
    post:
      operationId: upload
      requestBody:
        content:
          multipart/form-data:
            schema:
              type: object
              required:
                - file
              properties:
                file:
                  type: string
                  format: binary
                  nullable: true
      responses:
        "201":
          description: created
Generation Details
java -jar openapi-generator-cli-7.25.0.jar generate -g rust -i spec.yaml -o out --additional-properties=packageName=repro
cd out && cargo check --message-format=short
Steps to reproduce
src/apis/default_api.rs:42:74  error[E0415]: identifier `configuration` is bound more than once in this parameter list
src/apis/default_api.rs:42:104  error[E0433]: cannot find `models` in `models`
src/apis/default_api.rs:72:128  error[E0433]: cannot find `serde_json` in `models`
src/apis/default_api.rs:84:96  error[E0599]: `Vec<std::string::String>` doesn't implement `std::fmt::Display`
src/apis/default_api.rs:116:16  error[E0277]: the trait bound `std::option::Option<PathBuf>: AsRef<Path>` is not satisfied
src/apis/default_api.rs:118:33  error[E0599]: no method named `file_name` found for enum `std::option::Option<T>`

(plus follow-on errors from the duplicate configuration). The generated lines:

pub async fn create_import(configuration: &configuration::Configuration, configuration: Option<models::models::CreateImportRequestConfiguration>) -> Result<(), Error<CreateImportError>> {
pub async fn delete_variables(configuration: &configuration::Configuration, paths: Option<Vec<String>>, filter: Option<models::serde_json::Value>) -> Result<(), Error<DeleteVariablesError>> {
        "multi" => req_builder.query(&p_query_paths.into_iter().map(|p| ("paths".to_owned(), p.to_string())).collect::<Vec<(std::string::String, std::string::String)>>()),
    let file = TokioFile::open(&p_form_file).await?;
Suggest a fix

Treat required nullable parameters like optional ones in the query and multipart templates, keep serde_json::Value and already-qualified model types free of the extra models:: prefix, and escape a parameter named configuration the way other colliding names are escaped.

主要语言
Java
星标
26.9k
派生
7.8k
平均合并
2 天 10 小时
30 天内合并 PR
180

环境准备

在 Codespaces 中打开

在浏览器里用你自己的 GitHub 账号启动这个项目的开发容器。

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

OpenAPITools/openapi-generator 的其他 Issue

查看 OpenAPITools/openapi-generator 的全部 Issue

相似的 Issue

更多 Java Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。