-
Notifications
You must be signed in to change notification settings - Fork 184
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
fix(torii/graphql): handle empty metadata and schema change #2731
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -234,8 +234,14 @@ | |
let token_id = row.token_id.split(':').collect::<Vec<&str>>(); | ||
assert!(token_id.len() == 2); | ||
|
||
// skip the token if metadata is null | ||
if row.metadata.is_none() { | ||
continue; | ||
} | ||
Comment on lines
+238
to
+240
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Strengthen token_id parsing robustness, sensei The current token_id parsing using split and assert could panic if the format is unexpected. Consider using a more robust approach. -let token_id = row.token_id.split(':').collect::<Vec<&str>>();
-assert!(token_id.len() == 2);
+let token_id: Vec<&str> = row.token_id.split(':').collect();
+if token_id.len() != 2 {
+ warn!(
+ "Skipping token due to invalid token_id format: {}",
+ row.token_id
+ );
+ continue;
+}
|
||
let metadata_str = row.metadata.as_ref().unwrap(); | ||
|
||
let metadata: serde_json::Value = | ||
serde_json::from_str(&row.metadata).expect("metadata is always json"); | ||
serde_json::from_str(metadata_str).expect("metadata is always json"); | ||
let metadata_name = | ||
metadata.get("name").map(|v| v.to_string().trim_matches('"').to_string()); | ||
let metadata_description = metadata | ||
|
@@ -248,7 +254,7 @@ | |
|
||
let token_metadata = Erc721Token { | ||
name: row.name, | ||
metadata: row.metadata, | ||
metadata: metadata_str.to_owned(), | ||
contract_address: row.contract_address, | ||
symbol: row.symbol, | ||
token_id: token_id[1].to_string(), | ||
|
@@ -295,5 +301,5 @@ | |
pub token_id: String, | ||
pub balance: String, | ||
pub contract_type: String, | ||
pub metadata: String, | ||
pub metadata: Option<String>, | ||
} |
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -263,8 +263,14 @@ | |||||||||||||||||||||||||
let token_id = row.token_id.split(':').collect::<Vec<&str>>(); | ||||||||||||||||||||||||||
assert!(token_id.len() == 2); | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
// skip the token if metadata is null | ||||||||||||||||||||||||||
if row.metadata.is_none() { | ||||||||||||||||||||||||||
continue; | ||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
let metadata_str = row.metadata.as_ref().unwrap(); | ||||||||||||||||||||||||||
let metadata: serde_json::Value = | ||||||||||||||||||||||||||
serde_json::from_str(&row.metadata).expect("metadata is always json"); | ||||||||||||||||||||||||||
serde_json::from_str(metadata_str).expect("metadata is always json"); | ||||||||||||||||||||||||||
Comment on lines
+271
to
+273
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Enhance error handling for metadata JSON parsing, sensei! The current implementation uses -let metadata_str = row.metadata.as_ref().unwrap();
-let metadata: serde_json::Value =
- serde_json::from_str(metadata_str).expect("metadata is always json");
+let metadata_str = row.metadata.as_ref().unwrap();
+let metadata: serde_json::Value = match serde_json::from_str(metadata_str) {
+ Ok(value) => value,
+ Err(e) => {
+ warn!("Failed to parse metadata JSON for token_id {}: {}", row.token_id, e);
+ continue;
+ }
+}; 📝 Committable suggestion
Suggested change
|
||||||||||||||||||||||||||
let metadata_name = | ||||||||||||||||||||||||||
metadata.get("name").map(|v| v.to_string().trim_matches('"').to_string()); | ||||||||||||||||||||||||||
let metadata_description = metadata | ||||||||||||||||||||||||||
|
@@ -277,7 +283,7 @@ | |||||||||||||||||||||||||
|
||||||||||||||||||||||||||
let token_metadata = ErcTokenType::Erc721(Erc721Token { | ||||||||||||||||||||||||||
name: row.name, | ||||||||||||||||||||||||||
metadata: row.metadata, | ||||||||||||||||||||||||||
metadata: metadata_str.to_owned(), | ||||||||||||||||||||||||||
contract_address: row.contract_address, | ||||||||||||||||||||||||||
symbol: row.symbol, | ||||||||||||||||||||||||||
token_id: token_id[1].to_string(), | ||||||||||||||||||||||||||
|
@@ -333,7 +339,7 @@ | |||||||||||||||||||||||||
pub symbol: String, | ||||||||||||||||||||||||||
pub decimals: u8, | ||||||||||||||||||||||||||
pub contract_type: String, | ||||||||||||||||||||||||||
pub metadata: String, | ||||||||||||||||||||||||||
pub metadata: Option<String>, | ||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
#[derive(Debug, Clone)] | ||||||||||||||||||||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
💡 Codebase verification
Ohayo! The imagePath should indeed be nullable, sensei!
Looking at the codebase,
image_path
is constructed using a simple format string:format!("{}/{}", token_id.join("/"), "image")
. This is a static path construction that doesn't verify the actual existence of the image, while other metadata fields (name, description, attributes) are properly wrapped inOption
. For consistency and correctness, it should be nullable.crates/torii/graphql/src/object/erc/erc_token.rs
,image_path
is defined as a non-optionalString
crates/torii/graphql/src/mapping.rs
, it's mapped as non-nullableTypeRef::named_nn
token_transfer.rs
andtoken_balance.rs
doesn't validate image existence🔗 Analysis chain
Consider making imagePath nullable for consistency
While the metadata fields are now properly nullable,
imagePath
remains non-nullable (TypeRef::named_nn
). Should this also be nullable since it's part of the metadata that might not be available?Let's check if imagePath is always guaranteed to be present:
Consider making
imagePath
nullable for consistency with other metadata fields:🏁 Scripts executed
The following scripts were executed for the analysis:
Script:
Length of output: 79
Script:
Length of output: 1080
Script:
Length of output: 6109