Conversation
When OmitNullFields=true, optional fields with value None are omitted from the generated JSON rather than serialized as null. This addresses the request in issue #1245: users integrating with strict APIs that reject null fields can now use: type ColorProvider = JsonProvider<"...", SampleIsList=true, OmitNullFields=true> let value = ColorProvider.Root(color = "Blue", code = None) // Serializes as: {"color": "Blue"} (no "code": null) Changes: - JsonRuntime.fs: Add CreateRecordOmitNulls() that filters null fields - JsonGenerator.fs: Add OmitNullFields field to JsonGenerationContext; use CreateRecordOmitNulls when OmitNullFields=true - JsonProvider.fs: Add OmitNullFields static parameter (default false) - TypeProviderInstantiation.fs: Update test helper with new param - JsonProvider.fs (tests): Add 3 tests covering new behavior Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
3 tasks
dsyme
approved these changes
Feb 24, 2026
github-actions bot
pushed a commit
that referenced
this pull request
Feb 24, 2026
…* PR Main merged the OmitNullFields PR (#1638) after this branch was created. Resolving conflicts by integrating both sets of changes: - JsonGenerator.fs: add OmitNullFields param + keep With* method generation - JsonProvider.fs: accept new OmitNullFields static parameter - JsonRuntime.fs: add both WithRecordProperty and CreateRecordOmitNulls - TypeProviderInstantiation.fs: update test struct with OmitNullFields - JsonProvider tests: include OmitNullFields tests alongside With* tests Build: 0 errors. Tests: 268/268 passed. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
dsyme
added a commit
that referenced
this pull request
Feb 24, 2026
…ecord types (closes #1431) (#1639) * Add With* methods to CsvProvider Row and JsonProvider record types (closes #1431) - CsvGenerator.fs: Add With<PropName>(newValue) methods to Row type that return a new row with one field replaced, using tuple construction for multi-column CSVs or direct value return for single-column CSVs - JsonRuntime.fs: Add WithRecordProperty(doc, name, value, cultureStr) helper that updates a named property in a JSON record document, returning a new IJsonDocument with the modified field - JsonGenerator.fs: Generate With<PropName> methods for each JSON record property, calling JsonRuntime.WithRecordProperty with the original raw property name. Methods are generated in the same block as constructors (GenerateConstructors flag) - Tests: Added 6 tests covering With* methods for both CsvProvider and JsonProvider, verifying correct value updates and immutability of the original record Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * ci: trigger CI checks * Resolve merge conflicts with main: integrate OmitNullFields into With* PR Main merged the OmitNullFields PR (#1638) after this branch was created. Resolving conflicts by integrating both sets of changes: - JsonGenerator.fs: add OmitNullFields param + keep With* method generation - JsonProvider.fs: accept new OmitNullFields static parameter - JsonRuntime.fs: add both WithRecordProperty and CreateRecordOmitNulls - TypeProviderInstantiation.fs: update test struct with OmitNullFields - JsonProvider tests: include OmitNullFields tests alongside With* tests Build: 0 errors. Tests: 268/268 passed. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * ci: trigger CI checks * Fix blank line before CreateRecordOmitNulls in JsonRuntime.fs Add missing blank line to match main branch formatting and ensure GitHub merge conflict evaluation resolves cleanly. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * ci: trigger CI checks * Update design-time signature expected files for With* methods Regenerate 112 expected files to include the With* methods added to CsvProvider Row types and JsonProvider record types. Fixes the design-time signature test failures reported in issue #1644. Root cause: the expected files were not updated when With* methods were added to CsvGenerator.fs and JsonGenerator.fs. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * ci: trigger CI checks --------- Co-authored-by: Repo Assist <repo-assist@github.com> Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com> Co-authored-by: Don Syme <dsyme@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
🤖 This is an automated draft PR from Repo Assist, an AI assistant.
Summary
Adds an
OmitNullFieldsstatic parameter toJsonProviderso users can opt out of serializing optional fields asnull— instead omitting them entirely from the generated JSON.Addresses #1245. Requested by @dsyme in this comment.
Usage
Root Cause
JsonRuntime.CreateRecordalways serialized every field in the constructor args array, convertingNoneoptions toJsonValue.Null. There was no way to tell it to skip null-valued fields.Fix
JsonRuntime.fs: AddedCreateRecordOmitNulls(properties, cultureStr)— same asCreateRecordbut filters out entries where the value converts toJsonValue.Null.JsonGenerator.fs: AddedOmitNullFields: booltoJsonGenerationContext. The record constructor code generation now callsCreateRecordOmitNullswhenOmitNullFields = true.JsonProvider.fs: AddedOmitNullFields: boolstatic parameter (defaultfalse) at index 13.TypeProviderInstantiation.fs: Updated test helper to include the new parameter.JsonProvider.fs(tests): Added 3 tests verifying OmitNullFields behavior.Design Notes
false— no change in behavior for existing users; fully backward-compatible.OmitNullFields=trueapplies to all record constructors in the generated type hierarchy.Test Status
FSharp.Data.Tests— 262/262 pass (259 existing + 3 new OmitNullFields tests)FSharp.Data.DesignTime.Tests— 486/487 pass (1 skipped: network-blocked test, pre-existing)fantomasformatting verified — no changes needed