-
Notifications
You must be signed in to change notification settings - Fork 51
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
bug: assuming snake_case breaks JSON Schema compatibility #73
Comments
@jleightcap Just to clarify: this part isn't directly relevant to this report, right? The fact that we're generating the JSON Schema from a protobuf isn't actually important to this crate; the part that we're actually reporting here is that |
Yes, thank you for the clarification -- we happen to be operating from Protobuf source, but this is true in general for this naming convention clash. I updated the issue to make that more clear. |
Ah, so this may be user error. With #[serde(skip_serializing_if = "Option::is_none")]
#[serde(rename = "publicKey")]
pub public_key: Option<...>, The |
For a concrete example, using a schema generated from some Protobuf: {
"$schema": "http://json-schema.org/draft-04/schema#",
"$ref": "#/definitions/LogId",
"definitions": {
"LogId": {
"properties": {
"keyId": {
"type": "string",
"description": "The unique id of the log, represented as the SHA-256 hash of the log's public key, calculated over the DER encoding of the key represented as SubjectPublicKeyInfo. See https://www.rfc-editor.org/rfc/rfc6962#section-3.2",
"format": "binary",
"binaryEncoding": "base64"
}
},
"additionalProperties": false,
"type": "object",
"title": "Log Id",
"description": "LogId captures the identity of a transparency log."
}
}
} with an associated file artifact: {"keyId": "foo"} Seems to work fine; use serde::{Deserialize, Serialize};
schemafy::schemafy!("LogId.schema.json");
fn main() {
let js = std::fs::read_to_string("LogId.json").unwrap();
assert_eq!(
serde_json::from_str::<LogId>(&js).unwrap(),
LogId {
key_id: Some(String::from("foo"))
}
);
} |
Current failing case: schema
JSON artifact
This artifact uses snake_case variable names, and as pasted deserializes correctly. This same artifact then fails if any field name is re-named to use camelCase. |
Okay, I think I might have just went down a rabbit hole based on some incorrect testing setup -- re-generating our schema to consistently use camelCase everywhere seems to (de)serialize just fine. Apologies for the noise -- I believe this was fully user error. |
For context, using JSON schemas generated from protobuf, which specifies that for field names (https://protobuf.dev/programming-guides/proto3/#json):
In this crate, all properties seem to be normalized to use and expect snake_case. A schema might define a property
generating a struct
meaning deserialization is locked to only parse snake_case artifacts, against the property definition.
(CC @woodruffw)
The text was updated successfully, but these errors were encountered: