Skip None fields when serializing generated input objects#227
Skip None fields when serializing generated input objects#227achitojha wants to merge 3 commits into
Conversation
There was a problem hiding this comment.
Pull request overview
This PR changes the generated Serialize implementation for GraphQL input objects so that optional fields (those generated as Option<T>) are omitted from the serialized output when None, rather than being written as explicit null values. Previously every field was written unconditionally, which led to unwanted explicit nulls in the wasm-API output buffer.
Changes:
- In
ShopifyFunctionCodeGenerator::additional_impls_for_input_object, split field emission byis_required(): required fields are always written, optional fields are wrapped in anif let Some(value) = ...and only written when present. - Compute the object's field count dynamically as
num_required_fields + Σ usize::from(self.<opt>.is_some())sowrite_objectreceives the correct size. - Add a
SerializationProbeinput to the example schema and three tests covering omission ofNoneoptional fields, inclusion ofSomefields, and@oneOfinput object serialization.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
shopify_function_macro/src/lib.rs |
Generates conditional emission of optional input-object fields and computes the field count from required + present-optional counts. |
example_with_targets/src/tests.rs |
Adds tests verifying that None fields are omitted, Some fields are included, and @oneOf variants serialize correctly. |
example_with_targets/schema.graphql |
Adds SerializationProbe input with a mix of optional, defaulted, and required fields to support the new tests. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
adampetro
left a comment
There was a problem hiding this comment.
Can we do some validation on how this affects instruction count in the worst case (when we don't skip serialization but have some extra checks to do)?
Co-authored-by: Adam Petro <adam.petro@shopify.com>
Co-authored-by: Adam Petro <adam.petro@shopify.com>
Issue : https://github.com/shop/issues-shopifyvm/issues/318.
This updates generated input-object serialization so generated
Option<T>fields that areNoneare omitted instead of serialized as explicitnullvalues.Tests
cargo fmt --checkcargo testcargo clippy --all-targets -- -D warnings