feat(openapi): automatically generate OpenAPI spec from code (#318)

Co-authored-by: bernard eugine <bernard.eugine@bernard.eugine-MacBookPro>
Co-authored-by: Sanchith Hegde <sanchith.hegde@juspay.in>
This commit is contained in:
bernard-eugine
2023-01-09 16:09:49 +05:30
committed by GitHub
parent c807713a6f
commit 3fe60b5a48
15 changed files with 1401 additions and 14 deletions

View File

@ -1,5 +1,5 @@
use router::{
configs::settings::{CmdLineConf, Settings},
configs::settings::{CmdLineConf, Settings, Subcommand},
core::errors::{BachError, BachResult},
logger,
};
@ -10,6 +10,20 @@ async fn main() -> BachResult<()> {
// get commandline config before initializing config
let cmd_line = CmdLineConf::from_args();
if let Some(Subcommand::GenerateOpenapiSpec) = cmd_line.subcommand {
let file_path = "openapi/generated.json";
#[allow(clippy::expect_used)]
std::fs::write(
file_path,
<router::openapi::ApiDoc as utoipa::OpenApi>::openapi()
.to_pretty_json()
.expect("Failed to generate serialize OpenAPI specification as JSON"),
)
.expect("Failed to write OpenAPI specification to file");
println!("Successfully saved OpenAPI specification file at '{file_path}'");
return Ok(());
}
#[allow(clippy::expect_used)]
let conf = Settings::with_config_path(cmd_line.config_path)
.expect("Unable to construct application configuration");