Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[REQ] Derrive Default for generated Rust structs #7501

Open
ddaws opened this issue Sep 24, 2020 · 4 comments
Open

[REQ] Derrive Default for generated Rust structs #7501

ddaws opened this issue Sep 24, 2020 · 4 comments

Comments

@ddaws
Copy link

ddaws commented Sep 24, 2020

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 {
            // ...
        }
    }
}
@horacimacias
Copy link

I wasn't sure if Default can be derived for everything so I'm using some templates where Default is implemented when there are no variables needed.
In case anybody is interested, have a look here: https://github.com/horacimacias/openapi-generator-rust

I cleaned up some clippy findings too.

@ahirner
Copy link
Contributor

ahirner commented Oct 30, 2021

@horacimacias Your template is pretty amazing. It fixed our client generation to a large extent. I added a PR for more fixes, in case you are interested.

@horacimacias
Copy link

cool, thanks for the PR.
The templates are based on the "standard" templates so not really "mine" but anyway. Happy it worked well for you.

@ramosbugs
Copy link

Related: #10845

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants