Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Preserve extensions in the translation from JSON schema to OpenAPI #994

Merged
merged 1 commit into from
May 7, 2024
Merged
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
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-"))
bnaecker marked this conversation as resolved.
Show resolved Hide resolved
.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);
}
}