-
Notifications
You must be signed in to change notification settings - Fork 332
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
feat: schema: feature-gate deny_unknown_fields #2017
feat: schema: feature-gate deny_unknown_fields #2017
Conversation
Thanks for bringing this up. I think the only reason deny_unknown_fields was ever introduced is the generated schema and the generated TypeScript types. At decoding level we don't even want the behaviour. I wonder if there is a better fix for it, e.g. by tweaking the schema/code generation only without affecting serde itself. |
interesting... do you have more info for how |
See #1307 |
oh wait - this PR isn't quite right... it requires that the caller have the feature, since it's just generated from the proc macro... not ready for merging yet, will ping for re-review |
@webmaster128 - it's fixed and ready for review It's a bit too time consuming for me to dig deeper on "try to get rid of deny_unknown_fields altogether" right now. What do you think about merging this in as a stop-gap, and opening a new tracking issue to link here if someone wants to pick it up and fix more holistically? No pressure ofc, we can point at our fork for now if need-be (just for this, not any other package) :) |
I was thinking about a different short term solution: Create an optional argument like this: |
sorry, not following what you mean there.. the compiler didn't like this when I tested it
Ok, I've made it additive in 0837c36 to test now it's with imho this is a bit harder to reason about since it has to control it by setting default-features = false in the dependency... I could imagine a future dev wondering what that's about - but not a biggie. If it lands with this approach, all good. If you'd like to close this PR and just note it as some sortof request for the future, also all good :) |
I'd also vote for the
Works fine for me: #[cw_serde(deny_unknown_fields = false)]
pub struct State {
pub count: i32,
} Of course, at the moment it is just ignored. |
ahhh, I see what you're saying now. Yes, I agree, that's much nicer :) |
I'm going to go ahead and close this PR... not sure if I'll get around to opening one with that other approach, but if so, I'll link here for reference |
I'll open an issue to track it though |
This is a backwards-compatible change to the
schema
package which makesserde(deny_unknown_fields)
optional.It's feature-gated and not set by default.
cargo test --features allow-unknown-fields
will run the newtest_allow_unknown_fields()
.I believe this test illustrates the use-case clearly in code, but a bit more explanation: this has been a real-world problem for us (Levana) - we have bots which are written in Rust and consume messages from contracts. They do share the same "message" library code, but oftentimes we need to stagger their deployment (e.g. deploy contracts with new fields for the frontend, and we do not want to immediately deploy new bots or indexer services).
deny_unknown_fields
causes the bots to then break, since they are unaware of the new fields - but they don't need or use those new fields at all. We want to allow it.Tbh I'm a bit surprised this hasn't come up more in the wild, but that may be due to most clients using languages other than Rust which are not bound by the serde constraints (such as Typescript which does not error out on unknown fields.)