Skip to content

Commit

Permalink
workaround for json schemars bug
Browse files Browse the repository at this point in the history
  • Loading branch information
chenyukang committed Feb 26, 2024
1 parent 263d660 commit 264de22
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 63 deletions.
23 changes: 11 additions & 12 deletions devtools/doc/rpc-gen/src/gen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,23 +131,22 @@ impl RpcDocGenerator {
// sort rpc_methods accoring to title
rpc_methods.sort_by(|a, b| a.title.cmp(&b.title));

let mut types: Vec<(String, Value)> = vec![];
for map in all_types.iter() {
let mut pre_defined: Vec<(String, String)> = pre_defined_types().collect();
pre_defined.extend(visit_for_types());

let mut types: Vec<(String, Value)> = pre_defined
.iter()
.map(|(name, desc)| (name.clone(), Value::String(desc.clone())))
.collect();
for map in all_types {
for (name, ty) in map.iter() {
if !types.iter().any(|(n, _)| *n == *name) {
if !(types.iter().any(|(n, _)| *n == *name)
|| (name.starts_with("Either_for_") && name.ends_with("_JsonBytes")))
{
types.push((name.to_string(), ty.to_owned()));
}
}
}

let mut pre_defined: Vec<(String, String)> = pre_defined_types().collect();
pre_defined.extend(visit_for_types());
for (ty, desc) in pre_defined {
if types.iter().any(|t| t.0 == *ty) {
continue;
}
types.push((ty.to_owned(), Value::String(desc)));
}
types.sort_by(|(name1, _), (name2, _)| name1.cmp(name2));
Self {
rpc_methods,
Expand Down
2 changes: 1 addition & 1 deletion devtools/doc/rpc-gen/src/syn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ pub(crate) fn visit_for_types() -> Vec<(String, String)> {
let mut finder = CommentFinder {
type_comments: Default::default(),
current_type: None,
types: vec![],
types: vec!["JsonBytes".to_string()],
};
let dir = "util/jsonrpc-types";
for entry in WalkDir::new(dir).follow_links(true).into_iter() {
Expand Down
50 changes: 33 additions & 17 deletions rpc/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,10 @@ The crate `ckb-rpc`'s minimum supported rustc version is 1.71.1.

* [Type `Alert`](#type-alert)
* [Type `AlertId`](#type-alertid)
* [Type `AlertId`](#type-alertid)
* [Type `AlertMessage`](#type-alertmessage)
* [Type `AlertPriority`](#type-alertpriority)
* [Type `AlertPriority`](#type-alertpriority)
* [Type `AncestorsScoreSortKey`](#type-ancestorsscoresortkey)
* [Type `BannedAddr`](#type-bannedaddr)
* [Type `Block`](#type-block)
Expand Down Expand Up @@ -141,9 +143,7 @@ The crate `ckb-rpc`'s minimum supported rustc version is 1.71.1.
* [Type `DeploymentInfo`](#type-deploymentinfo)
* [Type `DeploymentState`](#type-deploymentstate)
* [Type `DeploymentsInfo`](#type-deploymentsinfo)
* [Type `Either_for_BlockView_and_JsonBytes`](#type-either_for_blockview_and_jsonbytes)
* [Type `Either_for_HeaderView_and_JsonBytes`](#type-either_for_headerview_and_jsonbytes)
* [Type `Either_for_TransactionView_and_JsonBytes`](#type-either_for_transactionview_and_jsonbytes)
* [Type `EpochNumber`](#type-epochnumber)
* [Type `EpochNumber`](#type-epochnumber)
* [Type `EpochNumberWithFraction`](#type-epochnumberwithfraction)
* [Type `EpochView`](#type-epochview)
Expand Down Expand Up @@ -4933,6 +4933,12 @@ The alert identifier that is used to filter duplicated alerts.

This is a 32-bit unsigned integer type encoded as the 0x-prefixed hex string in JSON. See examples of [Uint32](#type-uint32).

### Type `AlertId`

The alert identifier that is used to filter duplicated alerts.

This is a 32-bit unsigned integer type encoded as the 0x-prefixed hex string in JSON. See examples of [Uint32](type.Uint32.html#examples).

### Type `AlertMessage`
An alert sent by RPC `send_alert`.

Expand All @@ -4953,6 +4959,12 @@ Alerts are sorted by priority. Greater integers mean higher priorities.

This is a 32-bit unsigned integer type encoded as the 0x-prefixed hex string in JSON. See examples of [Uint32](#type-uint32).

### Type `AlertPriority`

Alerts are sorted by priority. Greater integers mean higher priorities.

This is a 32-bit unsigned integer type encoded as the 0x-prefixed hex string in JSON. See examples of [Uint32](type.Uint32.html#examples).

### Type `AncestorsScoreSortKey`
A struct as a sorted key for tx-pool

Expand Down Expand Up @@ -5162,7 +5174,7 @@ Represent soft fork deployments where the activation epoch is hard-coded into th
* `status`: [`SoftForkStatus`](#type-softforkstatus) - SoftFork status

### Type `Byte32`

The fixed-length 32 bytes binary encoded as a 0x-prefixed hex string in JSON.

### Type `Capacity`

Expand Down Expand Up @@ -5527,19 +5539,16 @@ Chain information.

* `hash`: [`H256`](#type-h256) - requested block hash

### Type `Either_for_BlockView_and_JsonBytes`
The enum `Either` with variants `Left` and `Right` is a general purpose sum type with two cases.

### Type `Either_for_HeaderView_and_JsonBytes`
The enum `Either` with variants `Left` and `Right` is a general purpose sum type with two cases.
### Type `EpochNumber`
Consecutive epoch number starting from 0.

### Type `Either_for_TransactionView_and_JsonBytes`
The enum `Either` with variants `Left` and `Right` is a general purpose sum type with two cases.
This is a 64-bit unsigned integer type encoded as the 0x-prefixed hex string in JSON. See examples of [Uint64](#type-uint64).

### Type `EpochNumber`

Consecutive epoch number starting from 0.

This is a 64-bit unsigned integer type encoded as the 0x-prefixed hex string in JSON. See examples of [Uint64](#type-uint64).
This is a 64-bit unsigned integer type encoded as the 0x-prefixed hex string in JSON. See examples of [Uint64](type.Uint64.html#examples).

### Type `EpochNumberWithFraction`

Expand Down Expand Up @@ -5635,11 +5644,7 @@ The fee_rate statistics information, includes mean and median, unit: shannons pe
* `median`: [`Uint64`](#type-uint64) - median
### Type `H256`
The 32-byte fixed-length binary data.
The name comes from the number of bits in the data.
In JSONRPC, it is encoded as a 0x-prefixed hex string.
The 256-bit binary data encoded as a 0x-prefixed hex string in JSON.
### Type `HardForkFeature`
The information about one hardfork feature.
Expand Down Expand Up @@ -5940,6 +5945,17 @@ Grouped Tx inner type

### Type `JsonBytes`

Variable-length binary encoded as a 0x-prefixed hex string in JSON.

###### Example

| JSON | Binary |
| ---------- | ------------------------------------ |
| "0x" | Empty binary |
| "0x00" | Single byte 0 |
| "0x636b62" | 3 bytes, UTF-8 encoding of ckb |
| "00" | Invalid, 0x is required |
| "0x0" | Invalid, each byte requires 2 digits |

### Type `LocalNode`
The information of the node itself.
Expand Down
48 changes: 15 additions & 33 deletions util/jsonrpc-types/src/json_schema.rs
Original file line number Diff line number Diff line change
@@ -1,41 +1,23 @@
use crate::{Byte32, Uint128, Uint32, Uint64};
use schemars::JsonSchema;

impl JsonSchema for Byte32 {
fn schema_name() -> String {
String::from("Byte32")
}
fn json_schema(gen: &mut schemars::gen::SchemaGenerator) -> schemars::schema::Schema {
gen.subschema_for::<[u8; 32]>().into_object().into()
}
macro_rules! impl_json_schema_for_type {
($type:ty, $inner_ty:ty, $name:expr) => {
impl JsonSchema for $type {
fn schema_name() -> String {
String::from($name)
}
fn json_schema(gen: &mut schemars::gen::SchemaGenerator) -> schemars::schema::Schema {
gen.subschema_for::<$inner_ty>().into_object().into()
}
}
};
}

impl JsonSchema for Uint32 {
fn schema_name() -> String {
String::from("Uint32")
}
fn json_schema(gen: &mut schemars::gen::SchemaGenerator) -> schemars::schema::Schema {
gen.subschema_for::<u32>().into_object().into()
}
}

impl JsonSchema for Uint64 {
fn schema_name() -> String {
String::from("Uint64")
}
fn json_schema(gen: &mut schemars::gen::SchemaGenerator) -> schemars::schema::Schema {
gen.subschema_for::<u64>().into_object().into()
}
}

impl JsonSchema for Uint128 {
fn schema_name() -> String {
String::from("Uint128")
}
fn json_schema(gen: &mut schemars::gen::SchemaGenerator) -> schemars::schema::Schema {
gen.subschema_for::<u128>().into_object().into()
}
}
impl_json_schema_for_type!(Byte32, [u8; 32], "Byte32");
impl_json_schema_for_type!(Uint32, u32, "Uint32");
impl_json_schema_for_type!(Uint64, u64, "Uint64");
impl_json_schema_for_type!(Uint128, u128, "Uint128");

pub fn u256_json_schema(
_schemars: &mut schemars::gen::SchemaGenerator,
Expand Down

0 comments on commit 264de22

Please sign in to comment.