Repository navigation
Support exponential notation in JsonSchemaExporter decimal pattern - #136876
Merged
eiriktsarpalis merged 1 commit intoNov 4, 2026
Merged
eiriktsarpalis merged 1 commit into
eiriktsarpalis merged 1 commit into
Conversation
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
|
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. |
Contributor
|
Tagging subscribers to this area: @dotnet/area-system-text-json |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #130868
Description
When generating a JSON schema for
decimalunderJsonNumberHandling.AllowReadingFromStringorWriteAsString,JsonPrimitiveConverter.GetSchemaForNumericTypepreviously emitted@"^-?(?:0|[1-9]\d*)(?:\.\d+)?$"becauseisIeeeFloatingPointwasfalse.However,
decimalparsing in .NET andUtf8JsonReadersupport 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
GetSchemaForNumericTypeso 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
JsonSchemaExporterfor validation or OpenAPI generation withdecimalproperties andAllowReadingFromStringwill no longer have valid exponential representations rejected.Regression?
No.
Risk
Low. Only extends the regex pattern for
decimalwhen string number handling is active.Testing
JsonSchemaExporterTests.TestTypes.cs.Decimal_AllowReadingFromString_MatchesScientificNotationinJsonSchemaExporterTests.csvalidating regex pattern match and roundtrip deserialization of scientific notation values (1E4,1e-4,1.23E+4,-1.23e-4).JsonSchemaExporterTestspass.