Skip to content
Merged
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
3 changes: 3 additions & 0 deletions CHANGELOG.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@

https://github.com/oxidecomputer/typify/compare/v0.10.2\...HEAD[Full list of commits]

* Distinguish optional fields that may be `null` from those that may not (#1122)
** Note this may bring in `json-serde` as a dependency

== 0.10.2 (released 2026-11-25)

https://github.com/oxidecomputer/typify/compare/v0.10.1\...v0.10.2[Full list of commits]
Expand Down
38 changes: 25 additions & 13 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ expectorate = "1.2.0"
glob = "0.3.3"
heck = "0.5.0"
ipnetwork = { version = "0.21.1", features = ["schemars"] }
json-serde = "1.1.1"
log = "0.4.31"
newline-converter = "0.3.0"
paste = "1.0.15"
Expand All @@ -49,3 +50,6 @@ trybuild = "1.0.116"
typespace = "0.1.4"
unicode-ident = "1.0.24"
uuid = "1.16.0"

#[patch."crates-io"]
#typespace = { path = "../typespace/typespace" }
11 changes: 7 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,13 @@ properties, Typify emits a `HashMap<String, T>` if the `additionalProperties`
schema specifies `T` or a `HashMap<String, serde_json::Value>` otherwise.

Properties of generated `struct` that are not in the `required` set are
typically represented as an `Option<T>` with the `#[serde(default)]` attribute
applied. Non-required properties with types that already have a default value
(such as a `Vec<T>`) simply get the `#[serde(default)]` attribute (so you won't
see e.g. `Option<Vec<T>>`).
typically represented as an `Option<T>`. An `Option` that may not have a value
of `null` will also carry the attribute `#[serde(default, deserialize_with =
"::json_serde::deserialize_some")]`, requiring a dependency on the
[`json-serde`](https://crates.io/crates/json-serde) crate. Non-required
properties with types that already have a default value (such as a `Vec<T>`)
simply get the `#[serde(default)]` attribute (so you won't see e.g.
`Option<Vec<T>>`).

#### Alternate Map types

Expand Down
17 changes: 3 additions & 14 deletions typify-impl/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -887,22 +887,11 @@ impl TypeSpace {
Ok((type_id, metadata))
}

/// Create an Option<T> from a pre-assigned TypeId and assign it an ID.
///
/// typespace renders exactly the types it is given, so we avoid
/// constructing Option<Option<T>>. (A forward reference won't have an
/// entry yet, but references always name named types, never raw Options,
/// so wrapping is correct in that case.)
fn id_to_option(&mut self, id: &TypeId) -> TypeId {
if let Some(TypeEntry::Type(typespace::build::Type::Option(_))) = self.id_to_entry.get(id) {
return id.clone();
}
self.assign_type(typespace::build::Type::Option(id.clone()).into())
}

// Create an Option<T> from a TypeEntry by assigning it type.
fn type_to_option(&mut self, ty: TypeEntry) -> TypeEntry {
// As with id_to_option, don't nest Options.
// Nesting options doesn't make a ton of sense since this is just for
// values that accept `null`--if the inner type is already an option
// then it already accepts `null`.
if matches!(&ty, TypeEntry::Type(typespace::build::Type::Option(_))) {
return ty;
}
Expand Down
32 changes: 9 additions & 23 deletions typify-impl/src/structs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,28 +121,19 @@ impl TypeSpace {
Some(name) => Name::Suggested(name),
None => Name::Unknown,
};
let (mut type_id, metadata) = self.id_for_schema(sub_type_name, schema)?;
let (type_id, metadata) = self.id_for_schema(sub_type_name, schema)?;

// Determine the property's volitionality.
let state = if required.contains(prop_name) {
StructPropertyState::Required
} else {
// Examine the type to determine the proper handling for an
// optional property.
match optional_property_state(
optional_property_state(
self,
&type_id,
metadata.as_ref().and_then(|m| m.default.as_ref()),
) {
StructPropertyState::Required => {
// TODO this is legacy behavior, but it isn't accurate.
// We'll keep this to minimize churn during the typespace
// migration, but modify it afterwards.
type_id = self.id_to_option(&type_id);
StructPropertyState::Optional
}
state => state,
}
)
};

let (name, rename) = recase(prop_name, Case::Snake);
Expand Down Expand Up @@ -308,20 +299,14 @@ fn optional_property_state(
.map(|type_entry| type_entry.as_type()),
default,
) {
// No default specified.
(Some(Type::Option(_)), None) => StructPropertyState::Optional,
(Some(Type::Vec(_)), None) => StructPropertyState::Default,
(Some(Type::Map(..)), None) => StructPropertyState::Default,
(Some(Type::Unit), None) => StructPropertyState::Default,

// TODO the caller converts this to Optional after inserting an Option
// type; this is for legacy compatibility.
(_, None) => StructPropertyState::Required,
(_, None) => StructPropertyState::Optional,

// Default specified is the same as the implicit default: null
(Some(Type::Option(_)), Some(serde_json::Value::Null)) => StructPropertyState::Optional,
(Some(Type::Option(_) | Type::Unit), Some(serde_json::Value::Null)) => {
StructPropertyState::Default
}
// Default specified is the same as the implicit default: []
(Some(Type::Vec(_)), Some(serde_json::Value::Array(a))) if a.is_empty() => {
(Some(Type::Vec(_) | Type::Set(_)), Some(serde_json::Value::Array(a))) if a.is_empty() => {
StructPropertyState::Default
}
// Default specified is the same as the implicit default: {}
Expand All @@ -345,6 +330,7 @@ fn optional_property_state(

// This is a reference that will resolve to this type id later.
(None, Some(default)) => StructPropertyState::DefaultValue(JsonValue(default.clone())),

// All other types as well as types with intrinsic defaults that have
// been explicitly overridden.
(Some(_), Some(default)) => StructPropertyState::DefaultValue(JsonValue(default.clone())),
Expand Down
Loading
Loading