OpenAPITools / OpenAPITools/openapi-generator

[RUST] Excess structs generated when using descriminators

Open
#10,089 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

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 https://github.com/OpenAPITools/openapi-generator/pull/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.

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 by running the provided Docker generation command with the supplied OpenAPI declaration and compare data.rs with user_authentication.rs. Trace the Rust generator's discriminator and mapped-object handling, then add a regression check showing that generation no longer produces confusing excess structs or otherwise makes the enum use the mapped type; the expected output needs to be settled between these alternatives.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.