OpenAPITools / OpenAPITools/openapi-generator

[BUG][RUST] deepObject parameters don't compile

Open
#15,324 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Issue: Bug
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

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

When using deepObjects in query strings the Rust client no longer compiles, this is for scenarios such as;

/mypath?page[size]=20&page[offset]=100

This is supported by the spec but, the struct that gets created can't be returned back as a string for the URI.

openapi-generator version
  • 6.5.0

I have also tested in prior versions and it also failed, I check the mustache files and it looks like this never worked

OpenAPI declaration file content or url
openapi: 3.0.0
info:
  title: Pets
  version: '1.0'
  description: Example of spec with an object query param
paths:
  '/pets':
    get:
      summary: List pets
      description: Paginated list of pets
      operationId: get-pets-list
      parameters:
        - $ref: '#/components/parameters/pagination-page'
      responses:
        '200':
          description: List pets
          content:
            application/json:
              schema:
                description: Pet schema
                type: object
                properties:
                  meta:
                    type: object
                    properties:
                      results:
                        type: number
                    required:
                      - results
                  data:
                    type: array
                    items:
                      type: object
                      properties:
                        name:
                          type: string
                      required:
                        - name
              examples:
                paginated-pets:
                  value:
                    meta:
                      results: 20
                      data:
                        - name: pet1
                        - name: pet2
      tags:
        - test

components:
  parameters:
    pagination-page:
      in: query
      name: page
      schema:
        type: object
        properties:
          before:
            type: integer
            example: 20
            description: Offset
          after:
            type: integer
            example: 20
            description: Offset
          size:
            type: integer
            example: 20
            description: Limit
      style: deepObject
      explode: true
      description: Query parameters for pagination


tags:
  - name: test
Generation Details

Place the above yaml in a file named object-params.yaml;

mkdir -p ./out/test-rust
docker run --rm -v "${PWD}:/local" openapitools/openapi-generator-cli:v6.5.0 generate \
  -i /local/object-params.yaml \
  -g rust \
  --library=hyper \
  --additional-properties=packageName=openapi-test-client,packageVersion=1.0.0 \
  -o /local/out/test-rust
Steps to reproduce

Running cargo build on the output produces;

   Compiling openapi-test-client v1.0.0 (/home/user/openapi-gen-rust-test/out/test-rust)
error[E0599]: the method `to_string` exists for reference `&GetPetsListPageParameter`, but its trait bounds were not satisfied
   --> src/apis/test_api.rs:48:33
    |
48  |             let query_value = s.to_string();
    |                                 ^^^^^^^^^ method cannot be called on `&GetPetsListPageParameter` due to unsatisfied trait bounds
    |
   ::: src/models/get_pets_list_page_parameter.rs:15:1
    |
15  | pub struct GetPetsListPageParameter {
    | -----------------------------------
    | |
    | doesn't satisfy `GetPetsListPageParameter: ToString`
    | doesn't satisfy `GetPetsListPageParameter: std::fmt::Display`
    |
    = note: the following trait bounds were not satisfied:
            `GetPetsListPageParameter: std::fmt::Display`
            which is required by `GetPetsListPageParameter: ToString`
            `&GetPetsListPageParameter: std::fmt::Display`
            which is required by `&GetPetsListPageParameter: ToString`
note: the trait `std::fmt::Display` must be implemented
   --> /home/andrew-s/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/fmt/mod.rs:786:1
    |
786 | pub trait Display {
    | ^^^^^^^^^^^^^^^^^

For more information about this error, try `rustc --explain E0599`.
error: could not compile `openapi-test-client` due to previous error
warning: build failed, waiting for other jobs to finish...
Suggest a fix

It looks like the issue is within the model mustache file;

https://github.com/OpenAPITools/openapi-generator/blob/master/modules/openapi-generator/src/main/resources/rust/model.mustache

There's a toString for enums but not a way to put the struct back into a URI string as is needed by deep object parameters in the query string.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with modules/openapi-generator/src/main/resources/rust/model.mustache and the generated src/apis/test_api.rs and src/models/get_pets_list_page_parameter.rs from the supplied object-params.yaml. Run cargo build on the generated client to reproduce the failure. Done means the deepObject parameter can be converted into its URI query-string representation and the generated Rust client compiles.

Written by the indexing model from the issue text.

Assessment

Tech stack
openapi, rust
Domain
api, tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.