-
Notifications
You must be signed in to change notification settings - Fork 270
Description
Is your feature request related to a problem? Please describe.
The JsonSchemaType class is intended to represent a JSON Schema type, but its ToString() method currently produces a Pascal-cased string, e.g.:
JsonSchemaType.Object.ToString() == "Object"
but the actual JSON schema type is lowercase "object". So if a user wants to obtain the real JSON Schema type from a JsonSchemaType object, they currently must lowercase it themselves, and must use case-insensitive compares for type values in actual JSON schemas.
And there is the reverse problem, which is that EnumParse of a correctly-cased JSON Schema type currently throws an exception rather than returning the appropriate JsonSchemaType object.
Enum.Parse<JsonSchemaType>("object")Unhandled exception. System.ArgumentException: Requested value 'object' was not found.
at System.Enum.TryParseByName[TStorage](RuntimeType enumType, ReadOnlySpan`1 value, Boolean ignoreCase, Boolean throwOnFailure, TStorage& result)
at System.Enum.TryParseByValueOrName[TUnderlying,TStorage](RuntimeType enumType, ReadOnlySpan`1 value, Boolean ignoreCase, Boolean throwOnFailure, TUnderlying& result)
at System.Enum.Parse[TEnum](String value, Boolean ignoreCase)
at System.Enum.Parse[TEnum](String value)
at Program.<Main>$(String[] args)
Command terminated by signal 6
Describe the solution you'd like
I think the JsonSchemaType class should provide a method that returns the correctly-cased JSON Schema type.
In addition, Enum.Parse of a correctly-cased JSON schema type should return the appropriate JsonSchemaType object.
Describe alternatives you've considered
One alternative is to change the ToString() method to return a correctly cased JSON Schema type value.
Another alternative is to provide a new method, such as ToIdentifier(), that returns the correctly cased value.
For EnumParse, it should accept either the Pascal-cased or correctly-cased JSON Schema type and return the appropriate JsonSchemaType object.
Additional context
Discussion on aspnetcore PR 59480 that motivated this issue.