Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The JsonSchemaType should offer a method to produce the JSON Schema type value #2078

Open
mikekistler opened this issue Jan 22, 2025 · 0 comments

Comments

@mikekistler
Copy link
Member

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

Recreate on dotnetfiddle

Discussion on aspnetcore PR 59480 that motivated this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant