Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 16 additions & 5 deletions poem-openapi/src/openapi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -281,12 +281,8 @@ impl<T> OpenApiService<T, ()> {
_webhook: PhantomData,
info: MetaInfo {
title: title.into(),
summary: None,
description: None,
version: version.into(),
terms_of_service: None,
contact: None,
license: None,
..Default::default()
},
external_document: None,
servers: Vec::new(),
Expand Down Expand Up @@ -418,6 +414,21 @@ impl<T, W> OpenApiService<T, W> {
self
}

/// Add info extension
#[must_use]
pub fn info_extension<E>(mut self, extension: E, value: serde_json::Value) -> Self
where
E: Into<String>,
{
let extension: String = extension.into();
assert!(
extension.starts_with("x-"),
"Extensions need to begin with 'x-'"
);
self.info.extensions.insert(extension, value);
self
}

/// Sets the cookie key.
#[must_use]
#[cfg(feature = "cookie")]
Expand Down
2 changes: 2 additions & 0 deletions poem-openapi/src/registry/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -540,6 +540,8 @@ pub struct MetaInfo {
pub contact: Option<MetaContact>,
#[serde(skip_serializing_if = "Option::is_none")]
pub license: Option<MetaLicense>,
#[serde(flatten)]
pub extensions: BTreeMap<String, serde_json::Value>,
}

#[derive(Debug, Eq, PartialEq, Serialize, Clone)]
Expand Down
62 changes: 62 additions & 0 deletions poem-openapi/tests/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1084,3 +1084,65 @@ async fn parameter_style_none() {
let spec = OpenApiService::new(Api {}, "test", "1.0").spec();
assert!(!spec.contains("\"style\"") && !spec.contains("\"style\": null"));
}

#[tokio::test]
async fn info_extensions() {
use serde_json::Value as JsonVal;
use serde_yaml::Value as YamlVal;

#[allow(dead_code)]
struct Api;

#[OpenApi]
impl Api {
#[oai(path = "/hello", method = "get")]
#[allow(dead_code)]
async fn index(&self) -> PlainText<String> {
PlainText("hello, world!".to_string())
}
}

let example_string = JsonVal::String("Example".to_string());
let example_string_yaml = YamlVal::String("Example".to_string());
let example_array = JsonVal::Array(vec![
JsonVal::String("B2B".to_string()),
JsonVal::String("B2C".to_string()),
]);
let example_array_yaml = YamlVal::Sequence(vec![
YamlVal::String("B2B".to_string()),
YamlVal::String("B2C".to_string()),
]);

let api = OpenApiService::new(Api {}, "test", "1.0")
.info_extension("x-category", example_string.clone())
.info_extension("x-segment", example_array.clone());

// check JSON:
let spec_parsed: JsonVal = serde_json::from_str(&api.spec()).expect("Generated invalid JSON");
if let JsonVal::Object(spec_root) = spec_parsed {
let info = spec_root.get("info").expect("Spec has no info");
if let JsonVal::Object(spec_info) = info {
assert_eq!(spec_info.get("x-category"), Some(&example_string));
assert_eq!(spec_info.get("x-segment"), Some(&example_array));
} else {
panic!("Spec info isn't a JSON object");
}
} else {
panic!("Spec root isn't a JSON object");
}

// check YAML:
let spec_parsed: YamlVal =
serde_yaml::from_str(&api.spec_yaml()).expect("Generated invalid YAML");
if let YamlVal::Mapping(spec_root) = spec_parsed {
let info = spec_root.get("info").expect("YAML Spec has no info");
if let YamlVal::Mapping(spec_info) = info {
assert_eq!(spec_info.get("x-category"), Some(&example_string_yaml));
assert_eq!(spec_info.get("x-segment"), Some(&example_array_yaml));
} else {
panic!("Spec info isn't a YAML mapping");
}
} else {
panic!("Spec root isn't a YAML mapping");
}
}