Skip to content

[RUST] Excess structs generated when using descriminators #10089

@denumerate

Description

@denumerate
Description

When generating rust code based on an openapi spec that uses discriminators and mapped objects, excess structs are created that are confusing to the user.

openapi-generator version

Version 5.1.0

OpenAPI declaration file content or url

The openapi spec below

components:
  schemas:
    Data:
      description: The event payload.
      type: object
      discriminator:
        propertyName: type_name
        mapping:
          UserAuthentication: UserAuthentication

    UserAuthentication:
      required:
        - type_name
      description: User's authentication data.
      type: object
      properties:
        type_name:
          description: The name of this type.
          type: string
          example: UserAuthentication
        certificate:
          type: string

results in both of the following files
data.rs:

#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(tag = "typename")]
pub enum Data {
  rename="UserAuthentication")]
    UserAuthentication {
        #[serde(rename = "certificate", skip_serializing_if = "Option::is_none")]
        certificate: Option<String>,
    },
}

user_authentication.rs:

#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct UserAuthentication {
    /// The name of this type.
    #[serde(rename = "type_name")]
    pub type_name: String,
    #[serde(rename = "certificate", skip_serializing_if = "Option::is_none")]
    pub certificate: Option<String>,
}
Command line used for generation
	docker run --rm \
		-v ".:." \
		openapitools/openapi-generator-cli:v5.1.0 generate \
		-i api/openapi.yaml \
		-g rust \
		-p packageName=dataexport-models \
		-p packageVersion='0.1.0' \
		-o crates/dataexport-models \
		--ignore-file-override=openapi-generator-ignore-rust
Steps to reproduce
  • Put the open api spec in an api directory
  • mkdir crates
  • run the above docker run command.
Related issues/PRs

This spec is based on the work in #3895.

Suggest a fix/enhancement

If either the structs were not generated or used inside the enums the resulting code would be much easier to use.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions