Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions typify-impl/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -706,6 +706,12 @@ impl TypeSpace {

fn convert_ref_type(&mut self, type_name: Name, schema: Schema, type_id: TypeId) -> Result<()> {
let (mut type_entry, metadata) = self.convert_schema(type_name.clone(), &schema)?;
// Note that the conversion may have already recorded a default drawn
// from the definition's own metadata (e.g. structs record it in
// TypeEntryStruct::from_metadata and return no metadata). Only
// override that value if the metadata returned by the conversion
// actually specifies a default--such as one declared at the reference
// site--rather than clobbering it with `None`.
let default = metadata
.as_ref()
.and_then(|m| m.default.as_ref())
Expand All @@ -714,15 +720,21 @@ impl TypeSpace {
let type_entry = match &mut type_entry.details {
// The types that are already named are good to go.
TypeEntryDetails::Enum(details) => {
details.default = default;
if default.is_some() {
details.default = default;
}
type_entry
}
TypeEntryDetails::Struct(details) => {
details.default = default;
if default.is_some() {
details.default = default;
}
type_entry
}
TypeEntryDetails::Newtype(details) => {
details.default = default;
if default.is_some() {
details.default = default;
}
type_entry
}

Expand Down
48 changes: 48 additions & 0 deletions typify/tests/schemas/ref-with-default.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
{
"$comment": "definition-level defaults should survive $ref resolution",
"$schema": "http://json-schema.org/draft-07/schema#",
"definitions": {
"DefaultedStruct": {
"type": "object",
"properties": {
"a": {
"type": "string"
}
},
"additionalProperties": false,
"default": {
"a": "hello"
}
},
"DefaultedEnum": {
"type": "string",
"enum": [
"alpha",
"beta",
"gamma"
],
"default": "beta"
},
"Root": {
"type": "object",
"properties": {
"via_ref": {
"$ref": "#/definitions/DefaultedStruct"
},
"enum_via_ref": {
"$ref": "#/definitions/DefaultedEnum"
},
"override_at_ref": {
"default": {
"a": "override"
},
"allOf": [
{
"$ref": "#/definitions/DefaultedStruct"
}
]
}
}
}
}
}
Loading