(GH-538) Define transforms for Option<T> - #1702
Merged
Steve Lee (SteveL-MSFT) merged 3 commits intoSep 3, 2026
Merged
Conversation
Prior to this change we had no (relatively) convenient way to make the
emitted schemas for structs with `Option<T>` fields more idiomatic.
As of the `v1.0.0` release of schemars, the schema generator _always_
wraps handling for `Option<T>` fields. The emitted schema depends on
`T`:
- When `T` is for a primitive type, like `String` or `bool`, the emitted
schema looks like:
```json
{
"type": "object",
"properties": {
"string_field": {
"type": ["string", "null"]
},
"boolean_field": {
"type": ["boolean", "null"]
}
}
}
```
- When `T` is for an inlined string enum, the emitted schema looks like:
```json
{
"type": "object",
"properties": {
"enum_field": {
"type": ["string", "null"],
"enum": ["Variant1", "Variant2", "Variant3", null]
}
}
}
```
- When `T` is for an inlined struct, the emitted schema looks like:
```json
{
"type": "object",
"properties": {
"struct_field": {
"type": ["string", "null"],
"pattern": "^[a-zA-Z0-9_]+$"
}
}
}
```
- When `T` is for an enum or struct that isn't inlined, the emitted
schema looks like:
```json
{
"type": "object",
"properties": {
"non_inlined_field": {
"anyOf": [
{ "$ref": "#/$defs/NonInlinedType" },
{ "type": "null" }
]
}
},
"$defs": {
"NonInlinedType": {
"type": "object",
"properties": {
"inner_field": {
"type": "string"
}
}
}
}
}
```
All of these representations are non-idiomatic. In JSON Schema,
explicitly defining a field as `null` is **_not_** equivalent to not
specifying the field. We should only permit specifying a field as
`null` when this is semantically accurate, not as shorthand for
"not defined."
We control whether a property is mandatory in the schema with other
keywords, like `required` and `dependentRequired`.
This change:
- Adds the `idiomaticize_option_field` transform to address this
limitation of schemars for a single field or variant.
- Adds the `idiomaticize_optional_properties` transform to address this
limitation of schemars at the top level of a struct, applying the
`idiomaticize_option_field` transform to every optional field in the
struct.
- Adds new helpers to `SchemaUtilityExtensions` for ease of use in the
implementation of the new transforms:
- `get_defined_keywords` to return every keyword defined at the top
level of the schema.
- `get_properties_keys` to return the keys of the `properties` object
at the top level of the schema.
- `get_required_property_names` to return the names of the properties
defined in the `required` keyword at the top level of the schema.
- Includes documentation and integration testing
Mikey Lombardi (He/Him) (michaeltlombardi)
marked this pull request as ready for review
September 2, 2026 21:10
Mikey Lombardi (He/Him) (michaeltlombardi)
requested review from
Steve Lee (SteveL-MSFT) and
Tess Gauthier (tgauth)
and
a lite review from Copilot
September 2, 2026 21:10
Copilot started reviewing on behalf of
Mikey Lombardi (He/Him) (michaeltlombardi)
September 2, 2026 21:11
View session
Contributor
There was a problem hiding this comment.
🟡 Changes recommended
The new public transforms have a documentation/behavior mismatch and can panic unexpectedly when applied to non-schemars “optional but non-nullable” schemas.
Once you've addressed the issues Copilot identified, you can request another Copilot review.
Pull request overview
Adds schema transforms in dsc-lib-jsonschema to “idiomaticize” Option<T> handling from schemars by removing explicit null branches and instead relying on required to express optionality, plus new schema utility helpers and integration tests.
Changes:
- Introduces
idiomaticize_option_field(field-level) andidiomaticize_optional_properties(struct-level) transforms. - Adds
SchemaUtilityExtensionshelpers for top-level keyword discovery and property/required key enumeration. - Adds integration tests and localized panic messages for the new transform behavior.
File summaries
| File | Description |
|---|---|
| lib/dsc-lib-jsonschema/tests/integration/transforms/mod.rs | Registers new transform integration test modules. |
| lib/dsc-lib-jsonschema/tests/integration/transforms/idiomaticize_optional_properties.rs | Adds integration coverage for struct-level optional-property idiomaticization. |
| lib/dsc-lib-jsonschema/tests/integration/transforms/idiomaticize_option_field.rs | Adds integration coverage for field-level Option<T> idiomaticization patterns. |
| lib/dsc-lib-jsonschema/src/transforms/mod.rs | Exposes the two new transforms from the transforms module. |
| lib/dsc-lib-jsonschema/src/transforms/idiomaticize_optional_properties.rs | Implements struct-level transform over non-required properties. |
| lib/dsc-lib-jsonschema/src/transforms/idiomaticize_option_field.rs | Implements field-level transform to remove null branches from schemars Option<T> schemas. |
| lib/dsc-lib-jsonschema/src/schema_utility_extensions.rs | Adds new schema helper methods used by the transforms. |
| lib/dsc-lib-jsonschema/locales/en-us.toml | Adds i18n strings for new transform panic messages. |
Review details
- Files reviewed: 8/8 changed files
- Comments generated: 2
- Review effort level: Lite
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Steve Lee (SteveL-MSFT)
approved these changes
Sep 3, 2026
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.
PR Summary
This change:
Adds the
idiomaticize_option_fieldtransform to address this limitation of schemars for a single field or variant.Adds the
idiomaticize_optional_propertiestransform to address this limitation of schemars at the top level of a struct, applying theidiomaticize_option_fieldtransform to every optional field in the struct.Adds new helpers to
SchemaUtilityExtensionsfor ease of use in the implementation of the new transforms:get_defined_keywordsto return every keyword defined at the top level of the schema.get_properties_keysto return the keys of thepropertiesobject at the top level of the schema.get_required_property_namesto return the names of the properties defined in therequiredkeyword at the top level of the schema.Includes documentation and integration testing
PR Context
Prior to this change we had no (relatively) convenient way to make the emitted schemas for structs with
Option<T>fields more idiomatic.As of the
v1.0.0release of schemars, the schema generator always wraps handling forOption<T>fields. The emitted schema depends onT:When
Tis for a primitive type, likeStringorbool, the emitted schema looks like:{ "type": "object", "properties": { "string_field": { "type": ["string", "null"] }, "boolean_field": { "type": ["boolean", "null"] } } }When
Tis for an inlined string enum, the emitted schema looks like:{ "type": "object", "properties": { "enum_field": { "type": ["string", "null"], "enum": ["Variant1", "Variant2", "Variant3", null] } } }When
Tis for an inlined struct, the emitted schema looks like:{ "type": "object", "properties": { "struct_field": { "type": ["string", "null"], "pattern": "^[a-zA-Z0-9_]+$" } } }When
Tis for an enum or struct that isn't inlined, the emitted schema looks like:{ "type": "object", "properties": { "non_inlined_field": { "anyOf": [ { "$ref": "#/$defs/NonInlinedType" }, { "type": "null" } ] } }, "$defs": { "NonInlinedType": { "type": "object", "properties": { "inner_field": { "type": "string" } } } } }All of these representations are non-idiomatic. In JSON Schema, explicitly defining a field as
nullis not equivalent to not specifying the field. We should only permit specifying a field asnullwhen this is semantically accurate, not as shorthand for "not defined."We control whether a property is mandatory in the schema with other keywords, like
requiredanddependentRequired.