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

Add test for deserializing pageable body #1897

Merged
merged 1 commit into from
Nov 5, 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
42 changes: 41 additions & 1 deletion sdk/typespec/typespec_client_core/src/http/response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -341,12 +341,30 @@ mod tests {
value: String,
}

/// An example JSON-serialized list response type.
#[derive(Model, Deserialize)]
#[typespec(crate = "crate")]
struct GetSecretListResponse {
value: Vec<GetSecretResponse>,
#[serde(rename = "nextLink")]
next_link: Option<String>,
}

/// A sample service client function.
fn get_secret() -> Response<GetSecretResponse> {
Response::from_bytes(
StatusCode::Ok,
Headers::new(),
"{\"name\":\"my_secret\",\"value\":\"my_value\"}",
r#"{"name":"my_secret","value":"my_value"}"#,
)
}

/// A sample service client function to return a list of secrets.
fn list_secrets() -> Response<GetSecretListResponse> {
Response::from_bytes(
StatusCode::Ok,
Headers::new(),
r#"{"value":[{"name":"my_secret","value":"my_value"}],"nextLink":"?page=2"}"#,
)
}

Expand Down Expand Up @@ -374,6 +392,28 @@ mod tests {
assert_eq!(secret.yon_name, "my_secret");
assert_eq!(secret.yon_value, "my_value");
}

#[tokio::test]
async fn deserialize_pageable_from_body() {
// We need to efficiently deserialize the body twice to get the "nextLink" but return it to the caller.
let response = list_secrets();
let (status, headers, body) = response.deconstruct();
let bytes = body.collect().await.expect("collect response");
let model: GetSecretListResponse =
crate::json::from_json(bytes.clone()).expect("deserialize GetSecretListResponse");
assert_eq!(status, StatusCode::Ok);
assert_eq!(model.value.len(), 1);
assert_eq!(model.next_link, Some("?page=2".to_string()));

let response: Response<GetSecretListResponse> =
Response::from_bytes(status, headers, bytes);
assert_eq!(response.status(), StatusCode::Ok);
let model = response
.deserialize_body()
.await
.expect("deserialize GetSecretListResponse again");
assert_eq!(model.next_link, Some("?page=2".to_string()));
}
}

#[cfg(feature = "xml")]
Expand Down