Skip to content

Commit

Permalink
fix for when runtime API field name is _ (#1191)
Browse files Browse the repository at this point in the history
* fix for when runtime API field name is _

* add a test

* formatting

---------

Co-authored-by: Tadeo hepperle <tadeo@do-mix.de>
  • Loading branch information
jsdw and tadeohepperle committed Oct 5, 2023
1 parent e91d0c7 commit 919b866
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 9 deletions.
10 changes: 8 additions & 2 deletions codegen/src/api/runtime_apis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,14 @@ fn generate_runtime_api(
.then_some(quote! { #( #[doc = #docs ] )* })
.unwrap_or_default();

let inputs: Vec<_> = method.inputs().map(|input| {
let name = format_ident!("{}", &input.name);
let inputs: Vec<_> = method.inputs().enumerate().map(|(idx, input)| {
// These are method names, which can just be '_', but struct field names can't
// just be an underscore, so fix any such names we find to work in structs.
let name = if input.name == "_" {
format_ident!("_{}", idx)
} else {
format_ident!("{}", &input.name)
};
let ty = type_gen.resolve_type_path(input.ty);

let param = quote!(#name: #ty);
Expand Down
6 changes: 3 additions & 3 deletions testing/ui-tests/src/dispatch_errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ use generate_custom_metadata::dispatch_error::{
use frame_metadata::RuntimeMetadataPrefixed;

pub fn metadata_array_dispatch_error() -> RuntimeMetadataPrefixed {
generate_metadata_from_pallets_custom_dispatch_error::<ArrayDispatchError>(vec![])
generate_metadata_from_pallets_custom_dispatch_error::<ArrayDispatchError>(vec![], vec![])
}

pub fn metadata_legacy_dispatch_error() -> RuntimeMetadataPrefixed {
generate_metadata_from_pallets_custom_dispatch_error::<LegacyDispatchError>(vec![])
generate_metadata_from_pallets_custom_dispatch_error::<LegacyDispatchError>(vec![], vec![])
}

pub fn metadata_named_field_dispatch_error() -> RuntimeMetadataPrefixed {
generate_metadata_from_pallets_custom_dispatch_error::<NamedFieldDispatchError>(vec![])
generate_metadata_from_pallets_custom_dispatch_error::<NamedFieldDispatchError>(vec![], vec![])
}
8 changes: 8 additions & 0 deletions testing/ui-tests/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
//! to automatically regenerate `stderr` files, but don't forget to check that new files make sense.

mod dispatch_errors;
mod runtime_apis;
mod storage;
mod utils;

Expand All @@ -34,6 +35,13 @@ fn ui_tests() {
.build(storage::metadata_storage_map_no_keys()),
);

// Check runtime APIs with _ in method names work
t.pass(
m.new_test_case()
.name("runtime_api_underscore_method_name")
.build(runtime_apis::metadata_runtime_api_underscore_method_name()),
);

// Test that the codegen can handle the different types of DispatchError.
t.pass(
m.new_test_case()
Expand Down
29 changes: 29 additions & 0 deletions testing/ui-tests/src/runtime_apis.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Copyright 2019-2023 Parity Technologies (UK) Ltd.
// This file is dual-licensed as Apache-2.0 or GPL-3.0.
// see LICENSE for license details.

use frame_metadata::{
v15::{RuntimeApiMetadata, RuntimeApiMethodMetadata, RuntimeApiMethodParamMetadata},
RuntimeMetadataPrefixed,
};

use crate::utils::generate_metadata_from_runtime_apis;

/// Generate metadata which contains a `Map` storage entry with no hashers/values.
/// This is a bit of an odd case, but it was raised in https://github.com/paritytech/subxt/issues/552,
/// and this test will fail before the fix and should pass once the fix is applied.
pub fn metadata_runtime_api_underscore_method_name() -> RuntimeMetadataPrefixed {
generate_metadata_from_runtime_apis(vec![RuntimeApiMetadata {
name: "MyApi".to_owned(),
docs: vec![],
methods: vec![RuntimeApiMethodMetadata {
name: "my_method".to_owned(),
inputs: vec![RuntimeApiMethodParamMetadata {
name: "_".to_owned(), // The important bit we're testing.
ty: 0.into(), // we don't care what type this is.
}],
output: 0.into(), // we don't care what type this is.
docs: vec![],
}],
}])
}
18 changes: 14 additions & 4 deletions testing/ui-tests/src/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ mod metadata_test_runner;
use frame_metadata::{
v15::{
CustomMetadata, ExtrinsicMetadata, OuterEnums, PalletMetadata, PalletStorageMetadata,
RuntimeMetadataV15, StorageEntryMetadata,
RuntimeApiMetadata, RuntimeMetadataV15, StorageEntryMetadata,
},
RuntimeMetadataPrefixed,
};
use generate_custom_metadata::dispatch_error::ArrayDispatchError;
use scale_info::{meta_type, IntoPortable, TypeInfo};
use scale_info::{form::PortableForm, meta_type, IntoPortable, TypeInfo};

pub use metadata_test_runner::MetadataTestRunner;

Expand All @@ -21,6 +21,7 @@ pub use metadata_test_runner::MetadataTestRunner;
/// type matching the generic type param provided.
pub fn generate_metadata_from_pallets_custom_dispatch_error<DispatchError: TypeInfo + 'static>(
pallets: Vec<PalletMetadata>,
runtime_apis: Vec<RuntimeApiMetadata<PortableForm>>,
) -> RuntimeMetadataPrefixed {
// We don't care about the extrinsic type.
let extrinsic = ExtrinsicMetadata {