Skip to content

Support exponential notation in JsonSchemaExporter decimal pattern - #136876

Merged
eiriktsarpalis merged 1 commit into
dotnet:mainfrom
Manish-1kumar:users/Manish-1kumar/stj-decimal-schema-pattern
Nov 4, 2026
Merged

eiriktsarpalis merged 1 commit into
dotnet:mainfrom
Manish-1kumar:users/Manish-1kumar/stj-decimal-schema-pattern

Conversation

@Manish-1kumar

Copy link
Copy Markdown
Contributor

Fixes #130868

Description

When generating a JSON schema for decimal under JsonNumberHandling.AllowReadingFromString or WriteAsString, JsonPrimitiveConverter.GetSchemaForNumericType previously emitted @"^-?(?:0|[1-9]\d*)(?:\.\d+)?$" because isIeeeFloatingPoint was false.

However, decimal parsing in .NET and Utf8JsonReader support scientific / exponential notation (e.g. "1E4", "1e-4"), which meant that valid string-encoded decimals in scientific notation were rejected by the schema pattern despite deserializing without error.

This PR updates GetSchemaForNumericType so that all non-integer number types (double, float, Half, decimal, etc.) use the pattern @"^-?(?:0|[1-9]\d*)(?:\.\d+)?(?:[eE][+-]?\d+)?$", enabling exponential notation in the decimal schema.

Customer Impact

Consumers using JsonSchemaExporter for validation or OpenAPI generation with decimal properties and AllowReadingFromString will no longer have valid exponential representations rejected.

Regression?

No.

Risk

Low. Only extends the regex pattern for decimal when string number handling is active.

Testing

  • Updated schema expectations in JsonSchemaExporterTests.TestTypes.cs.
  • Added test Decimal_AllowReadingFromString_MatchesScientificNotation in JsonSchemaExporterTests.cs validating regex pattern match and roundtrip deserialization of scientific notation values (1E4, 1e-4, 1.23E+4, -1.23e-4).
  • All 434 tests in JsonSchemaExporterTests pass.

When exporting JSON schema for numbers under string reading/writing, the pattern regex for decimal omitted the optional exponent clause, causing valid string-encoded decimals in scientific notation (e.g., 1E4) to be rejected by the schema even though Utf8JsonReader and JsonSerializer deserialize them successfully. Update GetSchemaForNumericType so all floating/fixed-point number types include the exponent notation in their string pattern regex.

Fixes dotnet#130868
@dotnet-policy-service dotnet-policy-service Bot added the community-contribution Indicates that the PR has been added by a community member label Nov 2, 2026
@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.

@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.

@eiriktsarpalis eiriktsarpalis left a comment

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.

Thanks

@eiriktsarpalis
eiriktsarpalis merged commit f239bc8 into dotnet:main Nov 4, 2026
97 checks passed
@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 12.0-preview1 milestone Nov 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-System.Text.Json community-contribution Indicates that the PR has been added by a community member

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Does JsonSchemaExporter handle decimal pattern correctly?

2 participants