OpenAPITools / OpenAPITools/openapi-generator

[BUG][Default][Rust] Free-form model generation

Open
#14,514 0 comments 1 reaction 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

Free-form models without validation which are not allOf included in any composed schema are not generated, though valid OpenAPI documents exist which make use of these objects. In larger, specification-based OpenAPI documents (such as [DMTF's Redfish schema|https://redfish.dmtf.org/]), these objects are leveraged to specify implementation-defined objects, which can contain any number of properties required by a valid implementation of the specification. This allows, for example, me to use a different request body for some requests than that which is required for a Redfish-compliant implementation provided by IBM or HP Enterprise hardware (etc).

Actual output: The model User in the below schema is not generated, though it is referenced in the requestBody of an action on a URL.
Expected output: The model User is generated.

openapi-generator version

Version 6.3.0, HEAD of master. This is not a regression.

OpenAPI declaration file content or url
openapi: 3.0.0
info:
  description: "Optional multiline or single-line description in [CommonMark](http://commonmark.org/help/)\
    \ or HTML."
  title: Sample API
  version: 0.1.9
servers:
- description: "Optional server description, e.g. Main (production) server"
  url: http://api.example.com/v1
- description: "Optional server description, e.g. Internal staging server for testing"
  url: http://staging-api.example.com
paths:
  /users:
    get:
      description: Optional extended description in CommonMark or HTML.
      requestBody:
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/User'
      responses:
        "200":
          description: A JSON array of user names
      summary: Returns a list of users.
components:
  schemas:
    User:
      additionalProperties: false
      description: A user
      properties: {}
      type: object
Generation Details

See steps to reproduce

Steps to reproduce
  1. Attempt to generate the source using the rust-server generator.
openapi-generator -g rust-server -i openapi.yaml -o test-models
  1. Attempt to compile the source (observe compilation errors about missing struct definitions)
cargo build
   Compiling openapi_client v0.1.9 (/home/edtwardy/Git/redfish/small-test/test-models)
error[E0412]: cannot find type `User` in module `models`
  --> src/lib.rs:34:30
   |
34 |         body: Option<models::User>,
   |                              ^^^^ not found in `models`

error[E0412]: cannot find type `User` in module `models`
  --> src/lib.rs:51:30
   |
51 |         body: Option<models::User>,
   |                              ^^^^ not found in `models`

error[E0412]: cannot find type `User` in module `models`
  --> src/lib.rs:82:30
   |
82 |         body: Option<models::User>,
   |                              ^^^^ not found in `models`

error[E0412]: cannot find type `User` in module `models`
   --> src/client/mod.rs:385:36
    |
385 |         param_body: Option<models::User>,
    |                                    ^^^^ not found in `models`

error[E0412]: cannot find type `User` in module `models`
   --> src/server/mod.rs:151:64
    |
151 | ...                   let param_body: Option<models::User> = if !body.is_empty() {
    |                                                      ^^^^ not found in `models`

For more information about this error, try `rustc --explain E0412`.
error: could not compile `openapi_client` due to 5 previous errors
Related issues/PRs

#5969, #5970: The same issue, observed for the same generator, a few years ago. Presumably, the PR was closed because isFreeFormObject was still provided by the Mustache object model at the time. This is no longer the case since commit #d4b8ff60

#7361, #7373: Implementation of a subset of use cases for free-form objects in OpenAPI documents.

Suggest a fix

I have a commit which fixes the issue on my fork. This commit allows templates to once again use {{#isFreeFormObject}} to specially handle free-form objects. Additionally, the third case of free-form objects not handled by #7373 is enabled if global property generateFreeFormModels is true.

Since CONTRIBUTING.md requests that an issue be opened first to enable discussion, I did not open a PR. testFreeFormSchemas(DefaultCodegenTest.java:2526) is currently failing. As soon as the community feels a PR is appropriate, I would be happy to open one and update the tests!

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 the rust-server generator and DefaultCodegenTest.java:2526, which currently fails for free-form schemas. Review the linked fork commit and related issues #5969, #5970, #7361, and #7373 to understand the intended handling. Done means the User model is generated for the supplied schema, the rust-server output compiles, and the relevant test passes.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, openapi, rust
Domain
backend-api-design, testing-qa, tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.