Skip to content

Commit

Permalink
Preserve extensions in the translation from JSON schema to OpenAPI (#994
Browse files Browse the repository at this point in the history
)
  • Loading branch information
ahl authored May 7, 2024
1 parent e68f5dd commit 283d897
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions dropshot/src/schema_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
Expand Down Expand Up @@ -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);
}
}

0 comments on commit 283d897

Please sign in to comment.