Skip to content

Fix deserialisation error on next_key #112

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

Merged
merged 2 commits into from
May 3, 2024
Merged
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
4 changes: 2 additions & 2 deletions packages/osmosis-std/src/types/cosmos/base/query/v1beta1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,12 @@ pub struct PageResponse {
/// next_key is the key to be passed to PageRequest.key to
/// query the next page most efficiently. It will be empty if
/// there are no more results.
#[prost(bytes = "vec", tag = "1")]
#[prost(bytes = "vec", optional, tag = "1")]
#[serde(
serialize_with = "crate::serde::as_base64_encoded_string::serialize",
deserialize_with = "crate::serde::as_base64_encoded_string::deserialize"
)]
pub next_key: ::prost::alloc::vec::Vec<u8>,
pub next_key: ::core::option::Option<::prost::alloc::vec::Vec<u8>>,
/// total is total number of results available if PageRequest.count_total
/// was set, its value is undefined otherwise
#[prost(uint64, tag = "2")]
Expand Down
1 change: 1 addition & 0 deletions packages/proto-build/src/transform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ fn transform_items(
let s = transformers::append_attrs_struct(src, &s, descriptor);
let s = transformers::serde_alias_id_with_uppercased(s);
let s = transformers::allow_serde_vec_u8_as_base64_encoded_string(s);
let s = transformers::make_next_key_optional(s);
let s = transformers::allow_serde_int_as_str(s);

transformers::allow_serde_vec_int_as_vec_str(s)
Expand Down
54 changes: 54 additions & 0 deletions packages/proto-build/src/transformers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,31 @@ pub fn serde_alias_id_with_uppercased(s: ItemStruct) -> ItemStruct {

syn::ItemStruct { fields, ..s }
}

pub fn make_next_key_optional(mut s: ItemStruct) -> ItemStruct {
if s.ident == "PageResponse" {
if let Fields::Named(ref mut fields_named) = s.fields {
for field in fields_named.named.iter_mut() {
if let Some(ident) = &field.ident {
if ident == "next_key" {
field.ty =
parse_quote!(::core::option::Option<::prost::alloc::vec::Vec<u8>>);
for attr in field.attrs.iter_mut() {
if attr.path.is_ident("prost") {
*attr = parse_quote! {
#[prost(bytes = "vec", optional, tag = "1")]
};
}
}
}
}
}
}
}

s
}

// ====== helpers ======

fn get_query_attr(
Expand Down Expand Up @@ -726,4 +751,33 @@ mod tests {

assert_ast_eq!(result, expected);
}

#[test]
fn test_make_next_key_optional() {
let input: ItemStruct = parse_quote! {
pub struct PageResponse {
#[prost(bytes = "vec", tag = "1")]
#[serde(
serialize_with = "crate::serde::as_base64_encoded_string::serialize",
deserialize_with = "crate::serde::as_base64_encoded_string::deserialize"
)]
pub next_key: ::prost::alloc::vec::Vec<u8>,
}
};

let result = make_next_key_optional(input);

let expected: ItemStruct = parse_quote! {
pub struct PageResponse {
#[prost(bytes = "vec", optional, tag = "1")]
#[serde(
serialize_with = "crate::serde::as_base64_encoded_string::serialize",
deserialize_with = "crate::serde::as_base64_encoded_string::deserialize"
)]
pub next_key: ::core::option::Option<::prost::alloc::vec::Vec<u8>>,
}
};

assert_ast_eq!(result, expected);
}
}