Repository navigation
Fix JsonSchemaExporter decimal pattern to support exponent notation - #130880
Youssef1313 with Copilot wants to merge 2 commits into
Conversation
|
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. |
- 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]>
|
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. |
|
Tagging subscribers to this area: @dotnet/area-system-text-json |
|
@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 |
There was a problem hiding this comment.
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
decimalschemas to include an optional exponent part. - Rename the schema-selection boolean parameter in
GetSchemaForNumericTypeto better reflect its purpose, and gate named floating-point literal schema generation based ontypeof(T)rather than the parameter. - Update schema exporter test expectations for
decimalpatterns 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 |
There was a problem hiding this comment.
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.
|
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
}
]
} |
There was a problem hiding this comment.
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
JsonSchemaExportergenerated an incorrect pattern fordecimalwith string number handling — the pattern^-?(?:0|[1-9]\d*)(?:\.\d+)?$rejected valid inputs like"1E4"even thoughJsonSerializeraccepts them fine.Changes
JsonPrimitiveConverter.cs: RenamedisIeeeFloatingPoint→supportsExponentNotationto reflect its actual role (pattern selection). TheAllowNamedFloatingPointLiteralsanyOf (NaN/Infinity) is now gated by a directtypeof(T)check instead of the parameter, sincedecimaldoesn't support those values.DecimalConverter.cs: PasssupportsExponentNotation: trueso decimal gets the exponent-aware pattern^-?(?:0|[1-9]\d*)(?:\.\d+)?(?:[eE][+-]?\d+)?$.DoubleConverter.cs,SingleConverter.cs,HalfConverter.cs: Updated named argument tosupportsExponentNotation: true.JsonSchemaExporterTests.TestTypes.cs: Updated expected decimal patterns to include the(?:[eE][+-]?\d+)?group.