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

Add Builder pattern to structs #60

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open

Conversation

ju6ge
Copy link

@ju6ge ju6ge commented Jul 13, 2022

This PR uses the derive_builder crate to derive builder classes for all structs. This enables writing an OpenApi Spec in a much more readable and straight forward fashion (No need to explicitly set parts of the struct to None or creating a mutable struct via default and then just editing the desired fileds). This is useful when using this crate to generate an openapi spec instead of parsing it from a file.

Example with Builders:

use openapi::OpenApi;
use openapi::v3_0::{ComponentsBuilder, ContactBuilder, InfoBuilder, OperationBuilder, ParameterBuilder, PathItemBuilder, ServerBuilder, SpecBuilder};

let spec = SpecBuilder::default()
	.openapi("3.0.1".to_string())
	.info(InfoBuilder::default()
			.title("Example Api".to_string())
			.version(clap::crate_version!().to_string())
			.contact(ContactBuilder::default()
					.name("Support".to_string())
					.email("[email protected]".to_string())
					.build().unwrap()
			)
			.build().unwrap()
	)
	.servers(vec![ ServerBuilder::default()
					.url("https://api.example.com")
					.build().unwrap()
			]
	)
	.paths(api_paths)
	.build().unwrap();

OpenApi::V3_0(spec)

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

Successfully merging this pull request may close these issues.

1 participant