Repository navigation
Add opt-in nil_fields setting to encode missing values as null - #201
Merged
Merged
Conversation
andreashasse
changed the base branch from
andreashasse/schema-and-error-fixes
to
main
October 30, 2026 22:22
A field whose type can be missing (T | undefined, T | nil) is left out
of the JSON when it holds the missing value. Some clients want every
field present. The new application setting
{spectra, [{nil_fields, null}]} writes such fields as JSON null instead.
The setting is read once per call into #sp_config{}, which both the
encoder and the schema generator use, so they cannot disagree. With
null, the field is listed in required and its schema allows null
(type: [T, "null"] for a plain type, anyOf otherwise). Optional (=>)
map keys absent from the data stay absent and stay out of required.
Typed map values that are missing also become null. The default, omit,
leaves output unchanged; an unknown value raises
{invalid_config, nil_fields, Value}.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
With nil_fields = null the encoder writes a missing typed map value as null, but additionalProperties was built without the missing member and rejected it. Build the value schema through field_schema so it allows null, as record and map fields do. Unchanged under omit. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
andreashasse
force-pushed
the
andreashasse/nil-fields-as-null
branch
from
October 30, 2026 22:23
4225140 to
9997e1a
Compare
Give add_missing_field/3 an unconstrained key type so a binary key list stays binary, and skip nil_fields_test in eqwalizer like the other test modules. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Merged
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.
Stacked on the schema fixes PR. The base is
andreashasse/schema-and-error-fixes, and GitHub retargets this PR to main once that PR merges.A field whose type can be missing (
T | undefined, or ElixirT | nil) is left out when it is encoded, and its schema does not allownull. The new application setting{spectra, [{nil_fields, null}]}(in Elixir,config :spectra, :nil_fields, :null) does three things:null.type: [T, "null"]oranyOf: [..., {type: null}].additionalProperties) allow null.The encoder and the schema generator read the setting from the same
#sp_config{}per call, so they cannot disagree. Fields whose type cannot be missing are unaffected. With the default,omit, the output is exactly as before. An invalid value raises{invalid_config, nil_fields, Value}.Tests cover both settings, records and
:=/=>maps, typed map values, decodingnull, and encoded output validated against the generated schema (with jesse and openapi-spec-validator).make testpasses (803 tests), and so do dialyzer, hank, xref, the format check and proper.🤖 Generated with Claude Code