From 283d8978abfcfa9b720a155b18cc124c82cb29c9 Mon Sep 17 00:00:00 2001 From: Adam Leventhal Date: Tue, 7 May 2024 15:38:19 -0700 Subject: [PATCH] Preserve extensions in the translation from JSON schema to OpenAPI (#994) --- dropshot/src/schema_util.rs | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/dropshot/src/schema_util.rs b/dropshot/src/schema_util.rs index ecc90c6a9..f0cb76dcd 100644 --- a/dropshot/src/schema_util.rs +++ b/dropshot/src/schema_util.rs @@ -488,6 +488,14 @@ fn j2oas_schema_object( data.write_only = metadata.write_only; } + // Preserve extensions + data.extensions = obj + .extensions + .iter() + .filter(|(key, _)| key.starts_with("x-")) + .map(|(key, value)| (key.clone(), value.clone())) + .collect(); + if let Some(name) = name { data.title = Some(name.clone()); } @@ -1069,4 +1077,21 @@ mod test { let _ = j2oas_schema_object(None, &schema); } + + #[test] + fn test_extension_conversion() { + let j = serde_json::json!({ + "type": "object", + "x-stuff": { + "a": "b", + "c": [ "d", "e" ] + } + }); + + let pre: schemars::schema::Schema = + serde_json::from_value(j.clone()).unwrap(); + let post = j2oas_schema(None, &pre); + let v = serde_json::to_value(post.as_item().unwrap()).unwrap(); + assert_eq!(j, v); + } }