Skip to content

[Repo Assist] Add OmitNullFields static parameter to JsonProvider (closes #1245)#1638

Merged
dsyme merged 2 commits intomainfrom
repo-assist/fix-issue-1245-omit-null-fields-8211027df61f08bd
Feb 24, 2026
Merged

[Repo Assist] Add OmitNullFields static parameter to JsonProvider (closes #1245)#1638
dsyme merged 2 commits intomainfrom
repo-assist/fix-issue-1245-omit-null-fields-8211027df61f08bd

Conversation

@github-actions
Copy link
Contributor

🤖 This is an automated draft PR from Repo Assist, an AI assistant.

Summary

Adds an OmitNullFields static parameter to JsonProvider so users can opt out of serializing optional fields as null — instead omitting them entirely from the generated JSON.

Addresses #1245. Requested by @dsyme in this comment.

Usage

// Default (backward-compatible): optional fields are serialized as null
type ColorProvider = JsonProvider<"""[{"color": "Red", "code": 15}, {"color": "Green"}]""", SampleIsList = true>
let v1 = ColorProvider.Root(color = "Blue", code = None)
// v1.ToString() → {"color": "Blue", "code": null}

// With OmitNullFields=true: None fields are omitted entirely
type ColorProviderOmit = JsonProvider<"""[{"color": "Red", "code": 15}, {"color": "Green"}]""", SampleIsList = true, OmitNullFields = true>
let v2 = ColorProviderOmit.Root(color = "Blue", code = None)
// v2.ToString() → {"color": "Blue"}

Root Cause

JsonRuntime.CreateRecord always serialized every field in the constructor args array, converting None options to JsonValue.Null. There was no way to tell it to skip null-valued fields.

Fix

  • JsonRuntime.fs: Added CreateRecordOmitNulls(properties, cultureStr) — same as CreateRecord but filters out entries where the value converts to JsonValue.Null.
  • JsonGenerator.fs: Added OmitNullFields: bool to JsonGenerationContext. The record constructor code generation now calls CreateRecordOmitNulls when OmitNullFields = true.
  • JsonProvider.fs: Added OmitNullFields: bool static parameter (default false) at index 13.
  • TypeProviderInstantiation.fs: Updated test helper to include the new parameter.
  • JsonProvider.fs (tests): Added 3 tests verifying OmitNullFields behavior.

Design Notes

  • Default is false — no change in behavior for existing users; fully backward-compatible.
  • OmitNullFields=true applies to all record constructors in the generated type hierarchy.
  • Non-optional fields are never null, so they are unaffected by this parameter.

Test Status

  • FSharp.Data.Tests262/262 pass (259 existing + 3 new OmitNullFields tests)
  • FSharp.Data.DesignTime.Tests486/487 pass (1 skipped: network-blocked test, pre-existing)
  • fantomas formatting verified — no changes needed

Generated by Repo Assist

To install this workflow, run gh aw add githubnext/agentics/workflows/repo-assist.md@b87234850bf9664d198f28a02df0f937d0447295. View source at https://github.com/githubnext/agentics/tree/b87234850bf9664d198f28a02df0f937d0447295/workflows/repo-assist.md.

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>
@dsyme dsyme marked this pull request as ready for review February 24, 2026 21:11
@dsyme dsyme merged commit d6599df into main Feb 24, 2026
2 checks passed
@dsyme dsyme deleted the repo-assist/fix-issue-1245-omit-null-fields-8211027df61f08bd branch February 24, 2026 21:12
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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant