diff --git a/sdk/typespec/typespec_client_core/src/http/response.rs b/sdk/typespec/typespec_client_core/src/http/response.rs index c4ac8efbc2..2b51687444 100644 --- a/sdk/typespec/typespec_client_core/src/http/response.rs +++ b/sdk/typespec/typespec_client_core/src/http/response.rs @@ -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, + #[serde(rename = "nextLink")] + next_link: Option, + } + /// A sample service client function. fn get_secret() -> Response { 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 { + Response::from_bytes( + StatusCode::Ok, + Headers::new(), + r#"{"value":[{"name":"my_secret","value":"my_value"}],"nextLink":"?page=2"}"#, ) } @@ -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 = + 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")]