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

[feat] Generate OAS document for URLs with separate query parameters #78

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@
/web/dist
/web/node_modules
/web/wasm

tests/fixtures/*.oas.json
71 changes: 53 additions & 18 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,38 @@ impl<'a> Transpiler<'a> {
paths: &[postman::PathElement],
security_requirement: Option<Vec<SecurityRequirement>>,
) {
let resolved_segments = paths
let mut paths_with_query = paths.to_vec(); // Clone the elements of paths into a new vector
if url.query.is_some() {
let query_string = url
.query
.as_ref()
.unwrap()
.iter()
.filter_map(|param| {
let key = param.key.as_ref().map_or("", String::as_str);
let value = param.value.as_ref().map_or("", String::as_str);

if value.is_empty() {
None // Skip this parameter
} else {
Some(format!("{}={}", key, value))
}
})
.collect::<Vec<String>>()
.join("&");

if !query_string.is_empty() {
paths_with_query.push(postman::PathElement::String(format!("?{}", query_string)));
}
}

// Filter empty path elements
paths_with_query.retain(|segment| match segment {
postman::PathElement::PathClass(c) => c.value.is_some(),
postman::PathElement::String(c) => !c.is_empty(),
});

let resolved_segments = paths_with_query
.iter()
.map(|segment| {
let mut seg = match segment {
Expand Down Expand Up @@ -1508,18 +1539,18 @@ mod tests {
let spec: Spec = serde_json::from_str(get_fixture("echo.postman.json").as_ref()).unwrap();
let oas = Transpiler::transpile(spec);
let ordered_paths = [
"/get",
"/get/?foo1=bar1&foo2=bar2",
"/post",
"/put",
"/patch",
"/delete",
"/headers",
"/response-headers",
"/response-headers/?foo1=bar1&foo2=bar2",
"/basic-auth",
"/digest-auth",
"/auth/hawk",
"/oauth1",
"/cookies/set",
"/cookies/set/?foo1=bar1&foo2=bar2",
"/cookies",
"/cookies/delete",
"/status/200",
Expand All @@ -1530,23 +1561,27 @@ mod tests {
"/deflate",
"/ip",
"/time/now",
"/time/valid",
"/time/format",
"/time/unit",
"/time/add",
"/time/subtract",
"/time/start",
"/time/object",
"/time/before",
"/time/after",
"/time/between",
"/time/leap",
"/transform/collection",
"/{method}/hello",
"/time/valid/?timestamp=2016-10-10",
"/time/format/?timestamp=2016-10-10&format=mm",
"/time/unit/?timestamp=2016-10-10&unit=day",
"/time/add/?timestamp=2016-10-10&years=100",
"/time/subtract/?timestamp=2016-10-10&years=50",
"/time/start/?timestamp=2016-10-10&unit=month",
"/time/object/?timestamp=2016-10-10",
"/time/before/?timestamp=2016-10-10&target=2017-10-10",
"/time/after/?timestamp=2016-10-10&target=2017-10-10",
"/time/between/?timestamp=2016-10-10&start=2017-10-10&end=2019-10-10",
"/time/leap/?timestamp=2016-10-10",
"/transform/collection/?from=1&to=2",
"/transform/collection/?from=2&to=1",
];
let OpenApi::V3_0(s) = oas;
let keys = s.paths.keys().enumerate();
for (i, k) in keys {
if i >= ordered_paths.len() {
println!("Extra path: {}", k);
break;
}
assert_eq!(k, ordered_paths[i])
}
}
Expand Down Expand Up @@ -1679,7 +1714,7 @@ mod tests {
OpenApi::V3_0(oas) => {
let query_param_names = oas
.paths
.get("/v2/json-rpc/{site id}")
.get("/v2/json-rpc/{site id}/?apikey={v3 API key}&sig={sig}")
.unwrap()
.post
.as_ref()
Expand Down
Loading