fix(csharp): spread additionalProperties behind spreadAdditionalProperties fix flag
Why
C# models emitted the additionalProperties catch-all as a regular member bound to a literal "additionalProperties" JSON key:
- Outbound, extras were nested under
"additionalProperties": {...}instead of spread at the top level of the object. - Inbound, top-level extras were silently dropped and the dictionary stayed
null.
Go, Java, Python, TypeScript and Ruby already spread additional properties at the top level.
Existing SDKs can depend on the nested shape (serialized models persisted client-side, tests asserting JSON, servers ignoring unknown keys), so the fix is opt-in for them.
What changed
- New
fixFlagsconfig field for C#, withspreadAdditionalProperties(on for new SDKs, off for existing ones). Flag off: generated output is unchanged. - Flag on:
- The public
Dictionary<string, T>? AdditionalPropertieskeeps its name and type but becomes[JsonIgnore]. - A private
[JsonExtensionData]property exposes it to Newtonsoft through a new internalAdditionalPropertiesAdapter<T>(Utils/AdditionalPropertiesAdapter.cs). The adapter works with any serializer settings, includingJsonConvertcall sites without SDK settings. - Reads convert each extra to
Tthrough JSON text. Fordecimaltargets this keeps thedouble's round-trip digits, whereJToken.ToObject<decimal>rounds to 15 significant digits.objectvalues keep the existingAnyDeserializershape (nested dictionaries and lists). With strict schema validation, an extra that cannot be converted toTnow fails deserialization of the whole response, as a declared field does; values Newtonsoft can coerce (12for a string,"12"ortruefor an integer) are converted, also as for declared fields. Before, top-level extras were dropped whatever their type. - Writes hand values to the outer serializer, so reference loop detection and caller settings apply. Values whose type needs an item converter (
BigIntStrConverter,DecimalStrConverter) are converted with it first. - Keys matching a declared property's JSON name (case-insensitive) are neither read nor written. On write this matches Newtonsoft's case-insensitive routing of those keys to the declared property on read. On read, Newtonsoft hands keys of get-only declared properties (
constfields) to extension data; they are skipped instead of being stored, or failing conversion toT, as extras. - Models with additional properties carry
[JsonObject(MissingMemberHandling = global::Newtonsoft.Json.MissingMemberHandling.Ignore)](qualified so a declared property namedMissingMemberHandlingdoes not shadow the enum). Left-to-right union matching (MissingMemberHandling.Error) then accepts extras on those models only, and closed models in the same graph stay strict, except models nested inside typed extras (see known limitations). - SDK readers use
DateParseHandling.Noneeven withoutpresenceAwareJsonSerialization, so date-like string extras are kept verbatim. This applies to everyany-typed value in responses, not only extras: ISO-shaped strings now staystringinstead of becomingDateTime. - SDK readers use
MetadataPropertyHandling.Ignore, so$ref,$id,$typeand$valueskeys are kept as data instead of being consumed as Newtonsoft metadata. This matters most for extras carrying JSON Schema fragments, and also lets declared properties named$idor$refround-trip. Declaredany-typed properties read throughFlexibleObjectDeserializerwith default settings and are unchanged. The SDK uses neither type-name nor reference handling. - The smart-union scorer counts extras from the extension data member, and scores each extra against the dictionary's value type: an extra that arrives in a form the value type does not accept counts as an inexact match, as a mismatched declared field does, so an open member with typed extras no longer wins over one that accepts the value as is. String-encoded bigint/decimal extras match when they arrive as strings, closed integer-enum extras match only for a defined member, and date-time extras accept any RFC 3339 fractional precision. Matching extras do not add to the matched count, so they never outrank declared fields.
- Error payload models get the same members.
- The public
- Test variants:
- primary, review and tertiary: flag on.
- secondary and quaternary: flag explicitly off, covering the legacy shape with presence-aware serialization on and off respectively.
- Review SDK
zSDKs/sdk-csharpregenerated. ThegetUserresponse-body tests, previously skipped for C#, now run.- They are removed from the C# skip list outright rather than skipped only when the flag is off: they are defined only in the review test workflows (
tests/tests/review/tests.arazzo.yaml), and the review SDK runs with the flag on, so a flag-dependent skip would never apply.
- They are removed from the C# skip list outright rather than skipped only when the flag is off: they are defined only in the review test workflows (
Known limitations with the flag on:
- Bare-number
decimalextras with more significant digits than adoubleholds lose precision, because Newtonsoft tokenizes extension data asdoublebefore conversion. String-encoded decimals are unaffected. - Extra values are converted with the adapter's own settings, so neither lenient schema validation nor strict member matching reaches inside them. In lenient mode a wrong-typed extra is dropped and its siblings survive. In left-to-right union matching (
unionStrategy: left-to-right, orpopulated-fieldswithoutpresenceAwareJsonSerialization, which falls back to it), an unknown field inside a model-typed extra is ignored instead of rejecting the candidate. Before this change, any top-level extra rejected a candidate with additional properties. - Extras whose key matches a declared property's JSON name are not written.
- User code deserializing models with its own settings gets date-like string extras reformatted unless
DateParseHandlingisNone, and gets Newtonsoft metadata handling on extra objects unlessMetadataPropertyHandlingisIgnore: an extra{"$ref":"..."}reads asnull, and one with$refplus sibling keys fails deserialization.
Testing
TARGET=primary make test-csharp: 654 passed, 34 skippedTARGET=tertiary make test-csharp: 179 passed, 51 skippedTARGET=review make test-csharp: 33 passed, 8 skippedTARGET=secondary make test-csharp(flag off): 182 passed, 35 skippedTARGET=quaternary make test-csharp(flag off): 192 passed, 51 skippedmake check-template-csharp,make lint
The auth-focused variants (basic-http, client-credentials, client-credentials-basic, custom-http, oauth2-password, security-options) leave the flag unset; test SDKs count as new SDKs, so they run with it on.
Not covered by a variant: flag on with presenceAwareJsonSerialization off. Every flag-on variant enables presence-aware serialization, which already sets DateParseHandling.None.
New tests:
- top-level read;
- skipping declared JSON names on write, and get-only (
const) declared names on read; - keeping
$ref/$id/$valueskeys in extras; - strict member matching (left-to-right unions) accepting extras on an open model while a closed model still fails;
- the self-reference throwing
JsonSerializationException; - lenient drop of a wrong-typed extra;
- legacy nested-shape write, read and union-scorer assertions (secondary);
- the
spreadAdditionalPropertiesdefault for a new SDK (on) and for an existing SDK whose config omitsfixFlags(off); - strict rejection of an extra that cannot be converted to the value type;
- verbatim date-like strings, both as top-level string extras and nested inside typed extras;
$refkeys kept inside typed extras, anddecimalextras keeping their round-trip digits;- union scoring preferring an open member whose extras accept the value over one whose value type does not match, while declared fields still outrank extras.
Public-safety check
- This change contains no credentials, customer documents, private repository URLs, private filesystem paths, or unredacted private logs.
- Title, body, comments, and commit messages name no customers or customer-derived identifiers, private paths or trackers, or workflow provenance, and are understandable without private context (
.claude/skills/public-repo-communication/SKILL.md). - Generated fixtures and review SDK changes are public-safe.
- I reviewed
git diff --check.