Marwes / schemafy

Crate for generating rust types from a json schema
MIT License
242 stars 51 forks source link

Add a command line flag to prevent writing 'deny_unknown_fields' #65

Open ehiggs opened 1 year ago

ehiggs commented 1 year ago

Problem: A server can add backwards compatible optional fields which will break the client if it fails when we parse a response because we deny unknown fields.

Solution: Rather than remove that altogether (I assume you put it in for a good reason), I added a command line flag to prevent writing deny_unknown_fields to files. Plumbed this through in a more general 'options' field so further fields can be added without changing signatures in the future, or adding a large number of arguments to functions.

Without flag

❯ ./target/release/schemafy ActorInput.json --root ActorInput 
#[derive(Clone, PartialEq, Debug, Default, Deserialize, Serialize)]
#[serde(deny_unknown_fields)]
pub struct ActorInput {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub group: Option<Vec<String>>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub user: Option<Vec<String>>,
}

With flag:

schemafy on  fix/allow-unknown-parameters is 📦 v0.6.0 via 🦀 v1.65.0 
❯ ./target/release/schemafy ActorInput.json --root ActorInput --allow-unknown-fields
#[derive(Clone, PartialEq, Debug, Default, Deserialize, Serialize)]
pub struct ActorInput {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub group: Option<Vec<String>>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub user: Option<Vec<String>>,
}