Skip to content

[Repo Assist] Add ExceptionIfMissing static parameter to JsonProvider and XmlProvider#1680

Merged
dsyme merged 3 commits intomainfrom
repo-assist/feature-exception-if-missing-41dc95b94e954dbf
Mar 1, 2026
Merged

[Repo Assist] Add ExceptionIfMissing static parameter to JsonProvider and XmlProvider#1680
dsyme merged 3 commits intomainfrom
repo-assist/feature-exception-if-missing-41dc95b94e954dbf

Conversation

@github-actions
Copy link
Contributor

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

Closes #1241

Summary

This PR implements opt-in strict mode for missing field handling in JsonProvider and XmlProvider.

Problem: When a field is missing from JSON/XML data, the type provider silently returns a default value ("" for string, NaN for float, etc.), which can hide data quality issues.

Solution: A new ExceptionIfMissing static parameter (default false) that, when true, raises an exception when a non-optional field is absent from the data.

Usage

// Strict mode - throws if 'Name' field is missing
type PersonStrict = JsonProvider<"""{"Name":"Alice","Age":30}""", ExceptionIfMissing=true>
let p = PersonStrict.Parse("""{"Age": 30}""")
p.Name  // raises System.Exception: 'Name' is missing

// Default mode (backward compatible) - returns "" for missing string
type Person = JsonProvider<"""{"Name":"Alice","Age":30}""">
let p = Person.Parse("""{"Age": 30}""")
p.Name  // returns ""

Same works for XmlProvider:

type ItemStrict = XmlProvider<"(item)(name)foo(/name)(/item)", ExceptionIfMissing=true>

Changes

  • JsonRuntime.fs and TextRuntime.fs: Added GetNonOptionalValueStrict<'T> — like GetNonOptionalValue but without the string/float special cases that return "" / NaN.
  • JsonConversionsGenerator.fs: Added exceptionIfMissing parameter; selects GetNonOptionalValueStrict vs GetNonOptionalValue at code generation time.
  • ConversionsGenerator.fs: Added exceptionIfMissing parameter for XML/CSV/HTML string conversions.
  • JsonGenerator.fs, XmlGenerator.fs: Added ExceptionIfMissing: bool to context records; propagated to code generation.
  • JsonProvider.fs, XmlProvider.fs: Added ExceptionIfMissing static parameter (default false).
  • CsvGenerator.fs, HtmlGenerator.fs: Always pass false — these providers don't expose the parameter.
  • TypeProviderInstantiation.fs: Updated test helper to include new parameter.
  • Tests added in tests/FSharp.Data.Tests/JsonProvider.fs and XmlProvider.fs.

Bug Fix (incidental)

Fixes a pre-existing bug in XmlProvider.fs where args.[14] was used for both UseSchemaTypeNames and PreferDateTimeOffset (correct: args.[14] = UseSchemaTypeNames, args.[15] = PreferDateTimeOffset, args.[16] = ExceptionIfMissing).

Test Status

tests/FSharp.Data.Tests — 283 tests, all passed (includes 7 new ExceptionIfMissing tests)

⚠️ tests/FSharp.Data.DesignTime.Tests — 218 signature tests failing with pre-existing infrastructure error (CsvFile\1[RowType] not found in target reference assembly set). This is unrelated to these changes and present on main` as well.

Generated by Repo Assist for issue #1241

To install this agentic workflow, run

gh aw add githubnext/agentics/workflows/repo-assist.md@afb00b92a9514fee9a14c583f059a03d05738f70

Implements opt-in strict mode for missing field handling. When ExceptionIfMissing=true,
accessing a non-optional field that is absent in the data raises an exception instead of
silently returning a default value (empty string for string, NaN for float).

The default behavior (ExceptionIfMissing=false) is unchanged for backward compatibility.

- Added GetNonOptionalValueStrict<'T> to JsonRuntime and TextRuntime
- Added ExceptionIfMissing static parameter to JsonProvider and XmlProvider
- Updated code generation pipeline (JsonConversionsGenerator, ConversionsGenerator)
- CsvProvider and HtmlProvider always use non-strict mode (false)
- Updated TypeProviderInstantiation.fs test helper
- Added tests for ExceptionIfMissing behavior

Also fixes a pre-existing XmlProvider bug where args.[14] was used for both
UseSchemaTypeNames and PreferDateTimeOffset (should be args.[14] and args.[15]).

Closes #1241

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@dsyme dsyme marked this pull request as ready for review March 1, 2026 01:23
@dsyme dsyme merged commit f671f35 into main Mar 1, 2026
2 checks passed
@dsyme dsyme deleted the repo-assist/feature-exception-if-missing-41dc95b94e954dbf branch March 1, 2026 01:23
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.

JsonProvider accepts absurd input

1 participant