Arnavion / k8s-openapi

Rust definitions of the resource types in the Kubernetes client API
Apache License 2.0
386 stars 41 forks source link

`k8s_openapi::ListResponse` should be serializable #142

Closed Nabushika closed 1 year ago

Nabushika commented 1 year ago

Currently working on mocking API calls to Kubernetes using tower_test. The responses need to be in a Vec<u8> form for sending back from the mock API.

This works, meaning I can mock individual GET methods:

serde_json::to_vec(&Pod::default()).unwrap()

This does not, meaning I can't mock api.list() calls (or rather, I can't use Rust objects, and instead have to send back my own json manually):

serde_json::to_vec(&ListResponse::Ok(List::<Pod>::default())).unwrap()

Can we add a serde::Serialize implementation for ListResponse, since we can serialize other objects?

Arnavion commented 1 year ago

The response enums are enums because their variants encode the response body corresponding to the HTTP status code. ie ListResponse::<Pod>::Ok(l) represents an HTTP response with status code OK and l: List<Pod> body, while ListResponse::<Pod>::Other(o) represents an HTTP response with all other status codes and o: serde_json::Value body. It doesn't make sense to me to serialize the response enum as a whole to JSON, because such an impl would only serialize its variant data and the status code information would be lost. For the same reason I don't understand how your mocking would work as you described it, since just a Vec<u8> by itself is not enough to deserialize the response. You need both status code and body to give to k8s_openapi::Response::try_from_parts.

Nabushika commented 1 year ago

Ah, sorry - I think I must have got a little mixed up. List<T> is definitely serializable, I was struggling with the trait bounds since I was dealing with a CRD and couldn't seem to construct a list.

Sorry for the clutter - closing this issue now.