Skip to content

Commit

Permalink
Add extensions support for OpenApi (#1013)
Browse files Browse the repository at this point in the history
This commit adds extensions support for `OpenApi` struct as demonstrated
below.
```rust
let mut api = OpenApiBuilder::new().build();
let extensions = api.extensions.get_or_insert(HashMap::new());
extensions.insert(
    String::from("x-tagGroup"),
    String::from("anything that serializes to Json").into(),
);
```

Resolves #968
  • Loading branch information
juhaku committed Aug 31, 2024
1 parent 9c79ef2 commit 3225e4d
Showing 1 changed file with 30 additions and 1 deletion.
31 changes: 30 additions & 1 deletion utoipa/src/openapi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use serde::{
de::{Error, Expected, Visitor},
Deserialize, Deserializer, Serialize, Serializer,
};
use std::fmt::Formatter;
use std::{collections::HashMap, fmt::Formatter};

use self::path::PathsMap;
pub use self::{
Expand Down Expand Up @@ -124,6 +124,10 @@ builder! {
/// All the references and invidual files could use their own schema dialect.
#[serde(rename = "$schema", default, skip_serializing_if = "String::is_empty")]
pub schema: String,

/// Optional extensions "x-something".
#[serde(skip_serializing_if = "Option::is_none", flatten)]
pub extensions: Option<HashMap<String, serde_json::Value>>,
}
}

Expand Down Expand Up @@ -1012,4 +1016,29 @@ mod tests {
})
)
}

#[test]
fn openapi_custom_extension() {
let mut api = OpenApiBuilder::new().build();
let extensions = api.extensions.get_or_insert(HashMap::new());
extensions.insert(
String::from("x-tagGroup"),
String::from("anything that serializes to Json").into(),
);

let api_json = serde_json::to_value(api).expect("OpenApi must serialize to JSON");

assert_json_eq!(
api_json,
json!({
"info": {
"title": "",
"version": ""
},
"openapi": "3.1.0",
"paths": {},
"x-tagGroup": "anything that serializes to Json",
})
)
}
}

0 comments on commit 3225e4d

Please sign in to comment.