OpenAPITools / OpenAPITools/openapi-generator

[REQ] Derrive Default for generated Rust structs

Open
#7,501 4 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Is your feature request related to a problem? Please describe.

It would be useful to add Default to the list of derived traits here. This would make it simpler to create defaults for types. This would allow a consumer to create defaults of structs like

let default_pet = Pet::default();

This is really helpful when mocking things out, writing tests, etc, where you just need a valid instance to fulfill some list of arguments.

Describe the solution you'd like

Add Default to the list of derived attributes on generated struct models. I am assuming this is possible because all structs are serializable to and from JSON so they must eventually resolve to the set of primitive types that have defaults defined in the Rust standard library.

Describe alternatives you've considered

The alternative is to implement the Default trait per type which adds a lot of boiler plate. For example,

impl Default for Pet {
    fn default() -> Self { 
        Pet {
            // ...
        }
    }
}

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, especially the derived-trait list around line 47. Check how generated Rust struct models handle their fields, then verify that generated examples can use Pet::default() and that the relevant Rust generation tests or compilation checks pass.

Written by the indexing model from the issue text.

Assessment

Tech stack
openapi, rust
Domain
tooling
Issue type
Feature
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.