Skip to content

Fix JsonSchemaExporter decimal pattern to support exponent notation - #130880

Open
Youssef1313 with Copilot wants to merge 2 commits into
mainfrom
copilot/fix-jsonschema-exporter-decimal-pattern
Open

Youssef1313 with Copilot wants to merge 2 commits into
mainfrom
copilot/fix-jsonschema-exporter-decimal-pattern

Conversation

Copilot AI commented Jul 16, 2026 •

Copy link
Copy Markdown
Contributor

JsonSchemaExporter generated an incorrect pattern for decimal with string number handling — the pattern ^-?(?:0|[1-9]\d*)(?:\.\d+)?$ rejected valid inputs like "1E4" even though JsonSerializer accepts them fine.

Changes

  • JsonPrimitiveConverter.cs: Renamed isIeeeFloatingPoint → supportsExponentNotation to reflect its actual role (pattern selection). The AllowNamedFloatingPointLiterals anyOf (NaN/Infinity) is now gated by a direct typeof(T) check instead of the parameter, since decimal doesn't support those values.
  • DecimalConverter.cs: Pass supportsExponentNotation: true so decimal gets the exponent-aware pattern ^-?(?:0|[1-9]\d*)(?:\.\d+)?(?:[eE][+-]?\d+)?$.
  • DoubleConverter.cs, SingleConverter.cs, HalfConverter.cs: Updated named argument to supportsExponentNotation: true.
  • JsonSchemaExporterTests.TestTypes.cs: Updated expected decimal patterns to include the (?:[eE][+-]?\d+)? group.
var opts = new JsonSerializerOptions { NumberHandling = JsonNumberHandling.AllowReadingFromString | JsonNumberHandling.WriteAsString };
// Before: pattern "^-?(?:0|[1-9]\d*)(?:\.\d+)?$"  — rejects "1E4"
// After:  pattern "^-?(?:0|[1-9]\d*)(?:\.\d+)?(?:[eE][+-]?\d+)?$"  — accepts "1E4"
JsonSchemaExporter.GetJsonSchemaAsNode(opts, typeof(decimal));

Copilot AI self-assigned this Jul 16, 2026
Copilot AI review requested due to automatic review settings July 16, 2026 15:10
Copilot AI removed the request for review from Copilot July 16, 2026 15:10
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
15 pipeline(s) were filtered out due to trigger conditions.
There may be pipelines that require an authorized user to comment /azp run to run.

Copilot AI linked an issue Jul 16, 2026 that may be closed by this pull request
@github-actions github-actions Bot added the needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners label Jul 16, 2026
- Rename isIeeeFloatingPoint to supportsExponentNotation in GetSchemaForNumericType
- Use typeof(T) check to gate AllowNamedFloatingPointLiterals (NaN/Infinity) anyOf
- Pass supportsExponentNotation: true for decimal to include exponent pattern
- Update test expectations for decimal pattern to include (?:[eE][+-]?\d+)?

Fixes: #130868

Co-authored-by: Youssef1313 <[email protected]>
Copilot AI requested review from Copilot and removed request for Copilot July 16, 2026 15:52
Copilot AI changed the title [WIP] Fix JsonSchemaExporter to handle decimal pattern correctly Fix JsonSchemaExporter decimal pattern to support exponent notation Jul 16, 2026
Copilot AI requested a review from Youssef1313 July 16, 2026 15:53
@Youssef1313
Youssef1313 marked this pull request as ready for review July 16, 2026 15:56
Copilot AI review requested due to automatic review settings July 16, 2026 15:56
Copilot AI temporarily deployed to copilot-pat-pool July 16, 2026 15:57 Inactive
Copilot AI temporarily deployed to copilot-pat-pool July 16, 2026 15:57 Inactive
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 3 pipeline(s).
12 pipeline(s) were filtered out due to trigger conditions.
There may be pipelines that require an authorized user to comment /azp run to run.

@github-actions github-actions Bot added area-System.Text.Json and removed needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners labels Jul 16, 2026
@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Tagging subscribers to this area: @dotnet/area-system-text-json
See info in area-owners.md if you want to be subscribed.

@Youssef1313

Copy link
Copy Markdown
Member

@eiriktsarpalis Let me know if that sounds reasonable. The PR separates the two concepts of "is IEEE" and "does it support exponent notation". The individual converters now pass whether they support exponent notation, and the base converter special cases some types to determine isIeeeFloatingPoint. The type check was based on the Debug.Assert that previously existed.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR updates System.Text.Json’s JSON Schema generation for numeric primitives so that decimal schemas produced under string number handling accept exponent notation (e.g., "1E4"), aligning the schema pattern with the actual parsing behavior of Utf8JsonReader / Utf8Parser for decimal.

Changes:

  • Extend the numeric string regex used for decimal schemas to include an optional exponent part.
  • Rename the schema-selection boolean parameter in GetSchemaForNumericType to better reflect its purpose, and gate named floating-point literal schema generation based on typeof(T) rather than the parameter.
  • Update schema exporter test expectations for decimal patterns accordingly.

Reviewed changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated no comments.

Show a summary per file
File Description
src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Converters/Value/JsonPrimitiveConverter.cs Renames the schema pattern switch parameter to supportsExponentNotation and derives IEEE floating-point behavior from typeof(T) for named literal schema generation.
src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Converters/Value/DecimalConverter.cs Opts decimal schema generation into exponent-aware numeric string patterns.
src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Converters/Value/DoubleConverter.cs Updates named argument to supportsExponentNotation: true for schema generation.
src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Converters/Value/SingleConverter.cs Updates named argument to supportsExponentNotation: true for schema generation.
src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Converters/Value/HalfConverter.cs Updates named argument to supportsExponentNotation: true for schema generation.
src/libraries/System.Text.Json/tests/Common/JsonSchemaExporterTests.TestTypes.cs Updates expected decimal regex patterns to include the optional exponent group.

pattern = schemaType is JsonSchemaType.Integer
? @"^-?(?:0|[1-9]\d*)$"
: isIeeeFloatingPoint
: supportsExponentNotation

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are there decimal numbers that do not support exponent notation? We expect the string contains a value in the JSON number format and that includes exponentials.

@github-actions

github-actions Bot commented Jul 19, 2026 •

Copy link
Copy Markdown
Contributor

Workflow state for the Holistic Review Orchestrator.

{
  "version": 5,
  "last_dispatched_commit": "41344e4556d4cd7ba2f22c0787e871c7982d0096",
  "last_dispatched_base_ref": "main",
  "last_dispatched_base_sha": "d92624eaf2fde2f8bff010893bde13663cd0791b",
  "last_reviewed_commit": "41344e4556d4cd7ba2f22c0787e871c7982d0096",
  "last_reviewed_base_ref": "main",
  "last_reviewed_base_sha": "d92624eaf2fde2f8bff010893bde13663cd0791b",
  "last_recorded_worker_run_id": "29684910794",
  "review_attempt_commit": "",
  "review_attempt_base_ref": "",
  "review_attempt_count": 0,
  "max_review_attempts": 5,
  "review_history_format": "holistic-review-disclosure-v1",
  "review_history": [
    {
      "commit": "41344e4556d4cd7ba2f22c0787e871c7982d0096",
      "review_id": 4730668983
    }
  ]
}

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Holistic Review

Motivation: JsonSchemaExporter emitted an incorrect pattern for decimal when string number handling (AllowReadingFromString/WriteAsString) was enabled. The generated pattern ^-?(?:0|[1-9]\d*)(?:\.\d+)?$ rejected exponent-notation strings such as "1E4", even though JsonSerializer deserializes those into decimal without issue. This produces schemas that are stricter than the serializer's actual acceptance behavior, so a valid payload can fail schema validation.

Approach: The shared helper GetSchemaForNumericType previously used a single isIeeeFloatingPoint parameter to drive two independent concerns: which regex pattern to emit, and whether to emit the NaN/Infinity anyOf for AllowNamedFloatingPointLiterals. The PR separates these. The parameter is renamed to supportsExponentNotation, and decimal now passes true so it gets the exponent-aware pattern. The named-floating-point-literals branch is instead gated by a direct typeof(T) check for double/float/Half, correctly keeping decimal out of the NaN/Infinity enum (which it does not support). Test expectations in JsonSchemaExporterTests.TestTypes.cs are updated for the three affected decimal properties.

Summary: This is a small, well-scoped, and correct fix. The decoupling of pattern selection from named-literal handling is a clean improvement over the overloaded boolean, and the resulting decimal pattern now matches serializer behavior while continuing to exclude NaN/Infinity. The float/Half/double call sites are updated to the renamed argument with unchanged semantics. The Debug asserts remain meaningful, and the typeof(T) check preserves the #if NET handling for Half. Test data updates align exactly with the new patterns and are consistent with the existing double expectations. I found no correctness, performance, or convention concerns. LGTM.

Detailed Findings

No actionable findings. The change is minimal and internally consistent; the only behavioral change is the intended broadening of the decimal string pattern to permit exponent notation, matching JsonSerializer acceptance.

Note

This review was generated by this repository's Holistic Review agentic workflow to complement the built-in Copilot review.

Generated by Holistic Review · 48.7 AIC · ⌖ 10.3 AIC · ⊞ 10K

This branch was previously deployed

1 inactive deployment
copilot-pat-pool — 41344e45 Deployed Jul 16, 2026 by Copilot via pat_pool #15407
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.

Does JsonSchemaExporter handle decimal pattern correctly?

4 participants