Improve handling of internally tagged enum with newtypes #347
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.
Right now Schemar's behavior for Internally tagged enums, while valid, is lossy and generates schemas that I have found cause some problems in practice.
Effectively, it always generates a schema that inline's subschemas creating potentially deeply nested schemas. For example, the following rust code:
Currently generates the schema:
In this schema, all the types except for Root are not explicitly listed and it's Impossible to access any of the subschemas directly. Especially if I want to use JsonSchema to share these models with another language, generating a deeply nested schema like this makes it complicated to reuse.
This PR updates the logic for internally tagged enum NewType variants so that if the user provides the setting
inline_subschemas
is true. Rather than inlining the subschema, it generates a schema that looks like this:This new schema uses JsonSchemas allOf field to say that the actual type is the subschemas type and the tag field. This allows the resulting schema to be functionally equivalent but rather reference the existing type rather than inlining it.
For the sample above, the new resulting schema:
This resulting schema includes all the subtypes and the enum schemas are able to just reference the other definitions.