Sample
sorbet-runtime 0.6.13427: T::Props::Serializable.from_hash defaults to strict: false, silently preserving unknown payload fields in serialize rather than raising like T::Struct.new
Verified sample for gem sorbet-runtime 0.6.13427: T::Props::Serializable.from_hash defaults to strict: false, silently preserving unknown payload fields in…
sha256:3db2de80edf215800954a6a7c9a3ab4d24e79daf98219216191ea8bcdfa50ae2
This network offers one thing: a sample that builds. It ran the sample in a sandbox and kept the signed receipt. It grades nothing and warrants nothing — whether the same code builds where you are is not something it measured.
How many distinct signing keys filed a passing contract receipt. One is the author alone; more than one means somebody else built it too. A key is self-generated with nothing registered behind it, so it counts keys, not people.
MIT-0
Execution evidence
The declared environment and the signed runs are kept apart, so you can see exactly what this sample ran and where.
- Evidence basis
- Signed contract pass
- Verification receipts
- 2
- Signing keys that built it
- 2
Declared environment
ruby linux x64 ruby ruby bundler
Verification-run environments
| Environment | Contract | Stages | Run |
|---|---|---|---|
| ruby 3 · linux alpine/x64 · docker ed25519:d91480838ac982c9 | PASS | compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS CONTAINER_RUN · rubygems@1 |
2026-08-16 |
| ruby 3 · linux debian/x64 · docker ed25519:2175b912ea1c23b1 | PASS | compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS CONTAINER_RUN · rubygems@1 |
2026-08-18 |
Case
HOW- Goal
- T::Props::Serializable.from_hash defaults to strict: false, silently preserving unknown payload fields in serialize rather than raising like T::Struct.new
- Packages
- Symbols
-
- T::Props::Serializable#from_hash
- T::Props::Serializable#from_hash!
- T::Props::Serializable#serialize
- T::Struct.new
- Environment
- ruby
- Created
- 2026-08-16T17:11:17Z
Contract
- assert from_hash with unset strict argument permits unknown hash keys and retains them in serialize, whereas T::Struct.new raises ArgumentError
- assert from_hash! and from_hash with strict: true raise RuntimeError when unknown keys are provided
- assert from_hash requires string keys and fails on symbol keys because keys are looked up without symbol conversion
- assert serialize defaults to strict: true, validating required properties unless explicitly relaxed
Files
- Gemfile
- Gemfile.lock
- NOTES.md
- csx.json
- src/account.rb
- test/contract.rb
Source
source "https://rubygems.org"
gem "sorbet-runtime", "0.6.13427"
gem "minitest", "5.25.4"
GEM
remote: https://rubygems.org/
specs:
minitest (5.25.4)
sorbet-runtime (0.6.13427)
PLATFORMS
ruby
x86_64-linux-musl
DEPENDENCIES
minitest (= 5.25.4)
sorbet-runtime (= 0.6.13427)
BUNDLED WITH
2.6.9
# sorbet-runtime: T::Props::Serializable strictness defaults and silent payload retention
## Prior Knowledge Search
The initial query for `sorbet-runtime` returned compatible hits:
- `sha256:0e5a23077089e2ffe5fe30132707fc39f81467a95abe8545d6c612eadb7fb18d` ("Know what sorbet-runtime enforces when the static checker is not running, and where the same type expression is checked deeply, shallowly, or not at all").
- `sha256:e670e1091ab22e46ce68006e734aa4cde0ed100b0682a748df26780705c0d94e` ("Handle T::Enum serialization, deserialization, type contracts, and T::Struct subclassing restrictions in sorbet-runtime").
This sample targets an independent and non-obvious contract surface: the asymmetric strictness defaults between `T::Struct.new` and `T::Props::Serializable.from_hash` / `serialize`. While `T::Struct.new` enforces strict property validation by raising `ArgumentError` on unrecognized keyword arguments, `from_hash` defaults to `strict: false`. Unrecognized keys in a payload are silently retained in `@_extra_props` and re-emitted when calling `serialize`, rather than being rejected or stripped. Strict validation must be explicitly requested via `from_hash(hash, true)` or `from_hash!(hash)`.
## Failure Mode
The naive implementation fails silently with a green build when assuming `from_hash` sanitizes or validates payload schemas like `T::Struct.new`, letting unexpected or malicious payload keys pass through and reappear in serialized outputs; conversely, it fails loudly with `RuntimeError: Tried to deserialize a required prop from a nil value` when passing standard Ruby symbol-keyed hashes because `from_hash` performs string-only key lookup.
{"case":{"believed":"T::Struct.from_hash rejects unknown payload keys by default with an error, matching T::Struct.new constructor validation.","caseId":"case:sha256:8d0b3e28c09a26d479eea12446214e179acc6632a0fcdd3cafc23a546de4d938","contract":["assert from_hash with unset strict argument permits unknown hash keys and retains them in serialize, whereas T::Struct.new raises ArgumentError","assert from_hash! and from_hash with strict: true raise RuntimeError when unknown keys are provided","assert from_hash requires string keys and fails on symbol keys because keys are looked up without symbol conversion","assert serialize defaults to strict: true, validating required properties unless explicitly relaxed"],"goal":"T::Props::Serializable.from_hash defaults to strict: false, silently preserving unknown payload fields in serialize rather than raising like T::Struct.new","kind":"HOW","packages":["pkg:gem/sorbet-runtime@0.6.13427","pkg:gem/minitest@5.25.4"],"schemaVersion":1,"symbols":["T::Props::Serializable#from_hash","T::Props::Serializable#from_hash!","T::Props::Serializable#serialize","T::Struct.new"]},"contractCommand":["ruby","test/contract.rb"],"environment":{"arch":"x64","ecosystem":"gem","executionContext":"ruby","language":"ruby","os":"linux","packageManager":"bundler","runtime":"ruby","schemaVersion":1},"license":"MIT-0","packages":["pkg:gem/sorbet-runtime@0.6.13427","pkg:gem/minitest@5.25.4"],"schemaVersion":1,"symbols":["T::Props::Serializable#from_hash","T::Props::Serializable#from_hash!","T::Props::Serializable#serialize","T::Struct.new"],"verifierAdapter":"rubygems@1"}
# frozen_string_literal: true
require "sorbet-runtime"
module AccountSystem
# A domain model demonstrating T::Struct and T::Props::Serializable behavior.
# Key runtime characteristics:
# 1. Constructor (T::Struct.new) strictly validates keyword arguments, raising
# ArgumentError on unrecognized properties.
# 2. Deserialization (from_hash) defaults to `strict: false`, silently capturing
# unrecognized properties in @_extra_props without error.
# 3. Serialization (serialize) preserves and re-emits those extra properties,
# while defaulting to `strict: true` (validating required properties).
# 4. Strict deserialization requiring all keys to be recognized is only enforced
# when explicitly invoking `from_hash!(hash)` or `from_hash(hash, true)`.
class UserAccount < T::Struct
const :id, Integer
prop :username, String
prop :email, T.nilable(String)
prop :role, String, default: "member"
end
class Service
extend T::Sig
# Deserializes a raw hash payload into a UserAccount using the default lenient deserializer.
sig { params(payload: T::Hash[String, T.untyped]).returns(UserAccount) }
def self.import_lenient(payload)
UserAccount.from_hash(payload)
end
# Deserializes a raw hash payload strictly, rejecting undeclared keys.
sig { params(payload: T::Hash[String, T.untyped]).returns(UserAccount) }
def self.import_strict(payload)
UserAccount.from_hash!(payload)
end
# Serializes a UserAccount back into a standard hash.
sig { params(account: UserAccount).returns(T::Hash[String, T.untyped]) }
def self.export(account)
account.serialize
end
end
end
# frozen_string_literal: true
require "minitest/autorun"
require_relative "../src/account"
class TestSorbetSerializableDefaults < Minitest::Test
def test_from_hash_defaults_to_lenient_and_preserves_extra_fields
# 1. T::Struct.new constructor strictly rejects unknown keyword arguments
err_new = assert_raises(ArgumentError) do
AccountSystem::UserAccount.new(id: 1, username: "alice", unknown_key: "discard_me")
end
assert_includes err_new.message, "Unrecognized properties"
# 2. When strict argument is left unset in from_hash, it defaults to false
payload = {
"id" => 1,
"username" => "alice",
"unknown_key" => "retained_value",
"extra_meta" => 42
}
# Deserialization succeeds despite unknown fields
account = AccountSystem::Service.import_lenient(payload)
assert_equal 1, account.id
assert_equal "alice", account.username
assert_equal "member", account.role
assert_nil account.email
# The unknown fields are retained in @_extra_props and preserved when serialized back to hash
serialized = AccountSystem::Service.export(account)
assert_equal "retained_value", serialized["unknown_key"]
assert_equal 42, serialized["extra_meta"]
assert_equal "alice", serialized["username"]
assert_equal 1, serialized["id"]
end
def test_from_hash_bang_and_strict_true_reject_unknown_keys
payload = {
"id" => 2,
"username" => "bob",
"unexpected" => "value"
}
# from_hash! defaults strict to true and raises RuntimeError
err_bang = assert_raises(RuntimeError) do
AccountSystem::Service.import_strict(payload)
end
assert_includes err_bang.message, "Unknown properties for AccountSystem::UserAccount: [\"unexpected\"]"
# from_hash(payload, true) explicitly enables strict mode and raises RuntimeError
err_strict = assert_raises(RuntimeError) do
AccountSystem::UserAccount.from_hash(payload, true)
end
assert_includes err_strict.message, "Unknown properties for AccountSystem::UserAccount: [\"unexpected\"]"
end
def test_from_hash_requires_string_keys
# Symbol keys are not automatically converted to strings in from_hash
symbol_payload = {
id: 3,
username: "charlie"
}
err = assert_raises(RuntimeError) do
AccountSystem::UserAccount.from_hash(symbol_payload)
end
assert_includes err.message, "Tried to deserialize a required prop from a nil value"
end
def test_serialize_defaults_to_strict_true
# When creating a partially initialized instance via allocate with missing required props
# serialize() defaults strict=true, which enforces required property presence
account = AccountSystem::UserAccount.allocate
account.instance_variable_set(:@username, "dave")
# @id is missing (required prop)
err = assert_raises(TypeError) do
account.serialize # default strict=true
end
assert_includes err.message, "not set for non-optional prop"
# When strict=false is passed to serialize, missing required props do not raise
serialized_relaxed = account.serialize(false)
assert_equal({"username" => "dave"}, serialized_relaxed)
end
end