Comments (6)
cc @itegulov @miraclx we might want to consider this issue now that we are using schema for ABI
from borsh-rs.
Good catch! Thanks for reporting it, but I believe borsh schema issues won't be addressed at the moment as it is not used in production and was just an experiment without a clear specification
from borsh-rs.
what is possible outline of fix for this? I have clash for same named struct in different parts of my big account struct.
from borsh-rs.
I see next:
- BorshSchema is used with solana contracts, even some in spl
- BorshSchema is used in near codebase in several types
- NearSchema with borsh attribute generates BorshSchema impl. as per code and documentation NearSchema does BorshSchema as default.
So there are some users, may be a lot if search on GH.
cc @frol
from borsh-rs.
Did quick tests on schemars
TLDR
For same named items schemars generates second declaration/definition under +1 suffixed name.
Long:
See #/definitions/A2
thread 'engine::tests::borsh_diff::serde_name_conflict' panicked at engine/src/engine/tests/borsh_diff.rs:68:5:
RootSchema { meta_schema: Some("http://json-schema.org/draft-07/schema#"), schema: SchemaObject { metadata: Some(Metadata { id: None, title: Some("D"), description: None, default: None, deprecated: false, read_only: false, write_only: false, examples: [] }), instance_type: Some(Single(Object)), format: None, enum_values: None, const_value: None, subschemas: None, number: None, string: None, array: None, object: Some(ObjectValidation { max_properties: None, min_properties: None, required: {"b", "c"}, properties: {"b": Object(SchemaObject { metadata: None, instance_type: None, format: None, enum_values: None, const_value: None, subschemas: None, number: None, string: None, array: None, object: None, reference: Some("#/definitions/B"), extensions: {} }), "c": Object(SchemaObject { metadata: None, instance_type: None, format: None, enum_values: None, const_value: None, subschemas: None, number: None, string: None, array: None, object: None, reference: Some("#/definitions/C"), extensions: {} })}, pattern_properties: {}, additional_properties: None, property_names: None }), reference: None, extensions: {} }, definitions: {"A": Object(SchemaObject { metadata: None, instance_type: Some(Single(Object)), format: None, enum_values: None, const_value: None, subschemas: None, number: None, string: None, array: None, object: Some(ObjectValidation { max_properties: None, min_properties: None, required: {"a"}, properties: {"a": Object(SchemaObject { metadata: None, instance_type: Some(Single(Integer)), format: Some("uint8"), enum_values: None, const_value: None, subschemas: None, number: Some(NumberValidation { multiple_of: None, maximum: None, exclusive_maximum: None, minimum: Some(0.0), exclusive_minimum: None }), string: None, array: None, object: None, reference: None, extensions: {} })}, pattern_properties: {}, additional_properties: None, property_names: None }), reference: None, extensions: {} }), "A2": Object(SchemaObject { metadata: None, instance_type: Some(Single(Object)), format: None, enum_values: None, const_value: None, subschemas: None, number: None, string: None, array: None, object: Some(ObjectValidation { max_properties: None, min_properties: None, required: {"a"}, properties: {"a": Object(SchemaObject { metadata: None, instance_type: Some(Single(Integer)), format: Some("uint8"), enum_values: None, const_value: None, subschemas: None, number: Some(NumberValidation { multiple_of: None, maximum: None, exclusive_maximum: None, minimum: Some(0.0), exclusive_minimum: None }), string: None, array: None, object: None, reference: None, extensions: {} })}, pattern_properties: {}, additional_properties: None, property_names: None }), reference: None, extensions: {} }), "B": Object(SchemaObject { metadata: None, instance_type: Some(Single(Object)), format: None, enum_values: None, const_value: None, subschemas: None, number: None, string: None, array: None, object: Some(ObjectValidation { max_properties: None, min_properties: None, required: {"a"}, properties: {"a": Object(SchemaObject { metadata: None, instance_type: None, format: None, enum_values: None, const_value: None, subschemas: None, number: None, string: None, array: None, object: None, reference: Some("#/definitions/A"), extensions: {} })}, pattern_properties: {}, additional_properties: None, property_names: None }), reference: None, extensions: {} }), "C": Object(SchemaObject { metadata: None, instance_type: Some(Single(Object)), format: None, enum_values: None, const_value: None, subschemas: None, number: None, string: None, array: None, object: Some(ObjectValidation { max_properties: None, min_properties: None, required: {"a"}, properties: {"a": Object(SchemaObject { metadata: None, instance_type: None, format: None, enum_values: None, const_value: None, subschemas: None, number: None, string: None, array: None, object: None, reference: Some("#/definitions/A2"), extensions: {} })}, pattern_properties: {}, additional_properties: None, property_names: None }), reference: None, extensions: {} })} }
mod a {
use borsh::{de, BorshDeserialize, BorshSchema, BorshSerialize};
#[derive(Default, Debug, BorshSerialize, BorshSchema, arbitrary::Arbitrary, schemars::JsonSchema, serde::Serialize)]
pub struct A {
a: u8,
}
#[derive(Default, Debug, BorshSerialize, BorshSchema, arbitrary::Arbitrary, schemars::JsonSchema, serde::Serialize)]
pub struct B {
a: A,
}
}
mod b {
use borsh::{de, BorshDeserialize, BorshSchema, BorshSerialize};
#[derive(Default, Debug, BorshSerialize, BorshSchema, arbitrary::Arbitrary, schemars::JsonSchema, serde::Serialize)]
pub struct A {
a: u8,
}
#[derive(Default, Debug, BorshSerialize, BorshSchema, arbitrary::Arbitrary, schemars::JsonSchema, serde::Serialize)]
pub struct C {
a: A,
}
}
use a::B;
use b::C;
#[derive(Default, Debug, BorshSerialize, BorshSchema, arbitrary::Arbitrary, schemars::JsonSchema, serde::Serialize)]
struct D {
b: B,
c: C,
}
#[test]
fn serde_name_conflict() {
let schema = schemars::gen::SchemaGenerator::default()
.into_root_schema_for::<D>();
panic!("{:?}", schema);
}
from borsh-rs.
@frol @itegulov @miraclx @austinabell
would PR which does exact same bump of number suffix in declaration accepted as fix as in schemars?
solution
So for type A
and A
in definitions, second one will become A2
possible improvement
- improvement can be if definitions are 100% same, do not bump, but use existing one (not conflict panic)
- store in schema name as it was given as is, and reference name with bumped id
alternative
Prefix definition with parent
struct, so if A
and A
in B
and C
, name of second one will be B_A
, which will require need to propagate parent info (hopefully it is already).
In case A
and A
in same struct one will have alias or mod prefix, so use alias or mod prefix.
Alternative seems complicated.
Not sure if one can get full Rust compile time info about modules in proc macro.
from borsh-rs.
Related Issues (20)
- Include discriminant number in `BorshSchema::Enum::variants`
- Rename declarations (tuples, `nil`, `string`, `nonzero_xx` and arrays) HOT 5
- add implementation for `BorshSerialize`, `BorshDeserialize`, `BorshSchema` for `char` HOT 3
- Security Policy violation Branch Protection HOT 2
- Security Policy violation SECURITY.md HOT 2
- Restriction on Serializing Zero-Sized Types Affects Marker Component Usage HOT 7
- derive BoshSerialize fails if the type already uses `W` generic name HOT 4
- BorshSerialize derive fails for structs with packed attribute HOT 4
- Should `std` feature imply `rc` feature and vice-versa? HOT 4
- Read/Write mutable reference in `serialize` and `deserialize_reader` is unnecessary HOT 8
- `derive` macros require `cargo` to be present HOT 4
- Old NEAR contracts won't compile with the new `borsh` re-exported from `near-sdk-rs` ("Could not find `borsh`") HOT 5
- equivalent of `#[serde(default)]` HOT 2
- Borsh 2.0? Not planned HOT 2
- Extract `alloc` feature (2.0 candidate) HOT 1
- `bytes::Bytes` and `bytes::BytesMut` can be complemented with `BorshSchema`
- `bson::oid::ObjectId` can be complemented with `BorshSchema`
- Add struct/enum attr to relax bounds
- feat(schema): for chars HOT 4
- feat(schema): support serde of HashMap with S hasher
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from borsh-rs.