Skip to content

[BUG][Rust] oneOf with array produces invalid code #17896

@DDtKey

Description

@DDtKey

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

It's nice that basic support of oneOf for rust has landed as part of #13970, but there are some issues.
It generates broken code if one of elements is "array", because it uses type-name as variant name for enum without normalization.

It leads to structures like this:

#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(untagged)]
pub enum OneOfWithArray {
    SomeObject(Box<SomeObject>),
    Vec<crate::models::SomeObject>(Box<Vec<crate::models::SomeObject>>), // invalid rust code
}
openapi-generator version

latest master:

openapi-generator-cli 7.4.0-SNAPSHOT
  commit : 76d743b
OpenAPI declaration file content or url

Can be reproduced with existing modules/openapi-generator/src/test/resources/3_0/oneOfArrayMapImport.yaml

Or another MRE:

{
  "openapi": "3.0.3",
  "info": {
    "title": "Omitted",
    "description": "Omitted",
    "license": {
      "name": ""
    },
    "version": "0.1.0"
  },
  "servers": [
    {
      "url": "http://localhost:8080",
      "description": "Omitted"
    }
  ],
  "paths": {},
  "components": {
    "schemas": {
      "OneOfWithArray": {
        "type": "object",
        "oneOf": [
          {
            "$ref": "#/components/schemas/SomeObject"
          },
          {
            "type": "array",
            "items": {
              "$ref": "#/components/schemas/SomeObject"
            }
          }
        ]
      },
      "SomeObject": {
        "type": "object",
        "properties": {
          "a": {
            "type": "integer"
          }
        }
      }
    },
    "responses": {}
  }
}
Generation Details

Just
openapi-generator generate -i test-spec.json -g rust --package-name test-one-of --additional-properties=supportMiddleware=true -o ./test

Steps to reproduce

Just try to generate rust client for provided spec.

Related issues/PRs

#13970 - the behavior was introduced there, but it's initial version of oneOf support.

#17869 - related issue with mention of wrong String import

Suggest a fix

Instead of using type-name as enum variant name, for such cases it could be normalization. I.e replace special chars and paths, for example VecSomeObject instead of Vec<crate::models::SomeObject>(..) or even just SomeObjects. Not sure about complexity though.
And not really related to this case, but if there is a descriminator - its values can be used instead of type names

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions