2022-08-18 18:58:55 +00:00
|
|
|
// Copyright 2022 Oxide Computer Company
|
2021-10-17 17:40:22 +00:00
|
|
|
|
2023-01-14 16:58:57 +00:00
|
|
|
use std::{
|
|
|
|
fs::File,
|
|
|
|
path::{Path, PathBuf},
|
|
|
|
};
|
2021-10-17 17:40:22 +00:00
|
|
|
|
2022-07-03 02:09:38 +00:00
|
|
|
use progenitor_impl::{
|
2023-02-01 05:28:12 +00:00
|
|
|
GenerationSettings, Generator, InterfaceStyle, TagStyle, TypeImpl,
|
|
|
|
TypePatch,
|
2022-07-03 02:09:38 +00:00
|
|
|
};
|
2021-10-17 17:40:22 +00:00
|
|
|
|
2023-01-14 16:58:57 +00:00
|
|
|
use openapiv3::OpenAPI;
|
|
|
|
|
|
|
|
fn load_api<P>(p: P) -> OpenAPI
|
|
|
|
where
|
|
|
|
P: AsRef<Path> + std::clone::Clone + std::fmt::Debug,
|
|
|
|
{
|
|
|
|
let mut f = File::open(p.clone()).unwrap();
|
|
|
|
match serde_json::from_reader(f) {
|
|
|
|
Ok(json_value) => json_value,
|
|
|
|
_ => {
|
2023-02-01 05:28:12 +00:00
|
|
|
f = File::open(p).unwrap();
|
2023-01-14 16:58:57 +00:00
|
|
|
serde_yaml::from_reader(f).unwrap()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-17 17:40:22 +00:00
|
|
|
#[track_caller]
|
2022-07-03 02:09:38 +00:00
|
|
|
fn verify_apis(openapi_file: &str) {
|
2021-10-17 17:40:22 +00:00
|
|
|
let mut in_path = PathBuf::from("../sample_openapi");
|
2023-01-14 16:58:57 +00:00
|
|
|
in_path.push(openapi_file);
|
|
|
|
let openapi_stem = openapi_file.split('.').next().unwrap();
|
2021-10-17 17:40:22 +00:00
|
|
|
|
2023-01-14 16:58:57 +00:00
|
|
|
let spec = load_api(in_path);
|
2022-07-03 02:09:38 +00:00
|
|
|
|
2023-03-28 21:54:05 +00:00
|
|
|
// Positional generation.
|
2022-07-03 02:09:38 +00:00
|
|
|
let mut generator = Generator::default();
|
|
|
|
let output = generator.generate_text_normalize_comments(&spec).unwrap();
|
|
|
|
expectorate::assert_contents(
|
2023-01-14 16:58:57 +00:00
|
|
|
format!("tests/output/{}-positional.out", openapi_stem),
|
2022-07-03 02:09:38 +00:00
|
|
|
&output,
|
|
|
|
);
|
|
|
|
|
2023-03-28 21:54:05 +00:00
|
|
|
// Builder generation with derives and patches.
|
2022-07-03 02:09:38 +00:00
|
|
|
let mut generator = Generator::new(
|
|
|
|
GenerationSettings::default()
|
|
|
|
.with_interface(InterfaceStyle::Builder)
|
2022-07-03 05:28:06 +00:00
|
|
|
.with_tag(TagStyle::Merged)
|
2022-11-14 18:51:05 +00:00
|
|
|
.with_derive("JsonSchema")
|
2022-12-27 19:55:57 +00:00
|
|
|
.with_patch("Name", TypePatch::default().with_derive("Hash"))
|
|
|
|
.with_conversion(
|
|
|
|
schemars::schema::SchemaObject {
|
|
|
|
instance_type: Some(
|
|
|
|
schemars::schema::InstanceType::Integer.into(),
|
|
|
|
),
|
|
|
|
format: Some("int32".to_string()),
|
|
|
|
..Default::default()
|
|
|
|
},
|
|
|
|
"usize",
|
2023-02-01 05:28:12 +00:00
|
|
|
[TypeImpl::Display].into_iter(),
|
2022-12-27 19:55:57 +00:00
|
|
|
),
|
2022-07-03 02:09:38 +00:00
|
|
|
);
|
|
|
|
let output = generator.generate_text_normalize_comments(&spec).unwrap();
|
|
|
|
expectorate::assert_contents(
|
2023-01-14 16:58:57 +00:00
|
|
|
format!("tests/output/{}-builder.out", openapi_stem),
|
2022-07-03 02:09:38 +00:00
|
|
|
&output,
|
|
|
|
);
|
|
|
|
|
2023-03-28 21:54:05 +00:00
|
|
|
// Builder generation with tags.
|
2022-07-03 02:09:38 +00:00
|
|
|
let mut generator = Generator::new(
|
|
|
|
GenerationSettings::default()
|
|
|
|
.with_interface(InterfaceStyle::Builder)
|
|
|
|
.with_tag(TagStyle::Separate),
|
|
|
|
);
|
|
|
|
let output = generator.generate_text_normalize_comments(&spec).unwrap();
|
2021-10-17 17:40:22 +00:00
|
|
|
expectorate::assert_contents(
|
2023-01-14 16:58:57 +00:00
|
|
|
format!("tests/output/{}-builder-tagged.out", openapi_stem),
|
2021-10-17 17:40:22 +00:00
|
|
|
&output,
|
2022-07-03 02:09:38 +00:00
|
|
|
);
|
2023-03-28 21:54:05 +00:00
|
|
|
|
|
|
|
// CLI generation.
|
|
|
|
let output = generator.cli_text(&spec, "sdk").unwrap();
|
|
|
|
expectorate::assert_contents(
|
|
|
|
format!("tests/output/{}-cli.out", openapi_stem),
|
|
|
|
&output,
|
|
|
|
);
|
2023-04-25 02:03:33 +00:00
|
|
|
|
|
|
|
// httpmock generation.
|
|
|
|
let code = generator.httpmock(&spec, "sdk").unwrap();
|
|
|
|
|
|
|
|
// TODO pending #368
|
|
|
|
let output = rustfmt_wrapper::rustfmt_config(
|
|
|
|
rustfmt_wrapper::config::Config {
|
|
|
|
format_strings: Some(true),
|
|
|
|
..Default::default()
|
|
|
|
},
|
|
|
|
code,
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let output = progenitor_impl::space_out_items(output).unwrap();
|
|
|
|
expectorate::assert_contents(
|
|
|
|
format!("tests/output/{}-httpmock.out", openapi_stem),
|
|
|
|
&output,
|
|
|
|
);
|
2021-10-17 17:40:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_keeper() {
|
2023-01-14 16:58:57 +00:00
|
|
|
verify_apis("keeper.json");
|
2021-10-17 17:40:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_buildomat() {
|
2023-01-14 16:58:57 +00:00
|
|
|
verify_apis("buildomat.json");
|
2021-10-17 17:40:22 +00:00
|
|
|
}
|
|
|
|
|
2021-12-10 02:15:24 +00:00
|
|
|
#[test]
|
|
|
|
fn test_nexus() {
|
2023-01-14 16:58:57 +00:00
|
|
|
verify_apis("nexus.json");
|
2021-12-10 02:15:24 +00:00
|
|
|
}
|
|
|
|
|
2022-09-28 20:40:07 +00:00
|
|
|
#[test]
|
|
|
|
fn test_propolis_server() {
|
2023-01-14 16:58:57 +00:00
|
|
|
verify_apis("propolis-server.json");
|
2022-09-28 20:40:07 +00:00
|
|
|
}
|
|
|
|
|
2022-12-02 06:50:50 +00:00
|
|
|
#[test]
|
|
|
|
fn test_param_override() {
|
2023-01-14 16:58:57 +00:00
|
|
|
verify_apis("param-overrides.json");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_yaml() {
|
|
|
|
verify_apis("param-overrides.yaml");
|
2022-12-02 06:50:50 +00:00
|
|
|
}
|
|
|
|
|
2021-10-17 17:40:22 +00:00
|
|
|
// TODO this file is full of inconsistencies and incorrectly specified types.
|
|
|
|
// It's an interesting test to consider whether we try to do our best to
|
|
|
|
// interpret the intent or just fail.
|
|
|
|
#[ignore]
|
|
|
|
#[test]
|
|
|
|
fn test_github() {
|
2023-01-14 16:58:57 +00:00
|
|
|
verify_apis("api.github.com.json");
|
2021-10-17 17:40:22 +00:00
|
|
|
}
|