Use properties' declared defaults when rendering whole-type defaults#1041
Open
danieleades wants to merge 1 commit into
Open
Use properties' declared defaults when rendering whole-type defaults#1041danieleades wants to merge 1 commit into
danieleades wants to merge 1 commit into
Conversation
When rendering a whole-type default value (used for a type's impl
Default and for parent properties' serde default functions), properties
absent from the default JSON object were filled in with
`Default::default()`, ignoring the properties' own declared schema
defaults. This made the rendered default disagree with deserialization,
which fills in absent properties via their serde default functions.
For example, given a property with `"default": 42` and a whole-type
default of `{}`, the rendered default produced `0` where deserializing
`{}` produces `42`.
Render absent properties using their declared default value when one
exists, falling back to `Default::default()` otherwise. Also fully
qualify that fallback as `::std::default::Default::default()` so it
cannot collide with a schema-defined type named `Default`.
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 #662
When typify renders a whole-type default value — a struct type's
defaultused for itsimpl Default, or a parent property's default expression — properties absent from the default JSON object were filled in withDefault::default(), ignoring those properties' own declareddefaultvalues from the schema.Using the schema from #662:
previously generated
which disagrees with what deserializing the declared default value
{}produces (the per-property serde default functions yieldSome("#B2000000")/1). So the rendered default did not mean what the schema says it means.With this change, a property absent from the whole-type default is rendered from its own declared default when it has one:
falling back to
Default::default()only when the property declares no default. While here, the fallback is fully qualified as::std::default::Default::default(), since a schema can legitimately define a type namedDefault.The
types-with-defaults.jsongolden test is extended with theSeparatorConfig/SeparatorHolderexample from the issue, covering both adefault: {}(all properties filled from their declared defaults) and a partialdefault: { "lineThickness": 5 }(the remaining properties filled from their declared defaults).