openapi icon indicating copy to clipboard operation
openapi copied to clipboard

Add Builder pattern to structs

Open ju6ge opened this issue 3 years ago • 0 comments

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)

ju6ge avatar Jul 13 '22 15:07 ju6ge