fix(csharp): spread additionalProperties behind spreadAdditionalProperties fix flag

Merged
2Y
1d ago
+1289-44

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 fixFlags config field for C#, with spreadAdditionalProperties (on for new SDKs, off for existing ones). Flag off: generated output is unchanged.
  • Flag on:
    • The public Dictionary<string, T>? AdditionalProperties keeps its name and type but becomes [JsonIgnore].
    • A private [JsonExtensionData] property exposes it to Newtonsoft through a new internal AdditionalPropertiesAdapter<T> (Utils/AdditionalPropertiesAdapter.cs). The adapter works with any serializer settings, including JsonConvert call sites without SDK settings.
    • Reads convert each extra to T through JSON text. For decimal targets this keeps the double's round-trip digits, where JToken.ToObject<decimal> rounds to 15 significant digits. object values keep the existing AnyDeserializer shape (nested dictionaries and lists). With strict schema validation, an extra that cannot be converted to T now fails deserialization of the whole response, as a declared field does; values Newtonsoft can coerce (12 for a string, "12" or true for 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 (const fields) to extension data; they are skipped instead of being stored, or failing conversion to T, as extras.
    • Models with additional properties carry [JsonObject(MissingMemberHandling = global::Newtonsoft.Json.MissingMemberHandling.Ignore)] (qualified so a declared property named MissingMemberHandling does 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.None even without presenceAwareJsonSerialization, so date-like string extras are kept verbatim. This applies to every any-typed value in responses, not only extras: ISO-shaped strings now stay string instead of becoming DateTime.
    • SDK readers use MetadataPropertyHandling.Ignore, so $ref, $id, $type and $values keys 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 $id or $ref round-trip. Declared any-typed properties read through FlexibleObjectDeserializer with 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.
  • 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-csharp regenerated. The getUser response-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.

Known limitations with the flag on:

  • Bare-number decimal extras with more significant digits than a double holds lose precision, because Newtonsoft tokenizes extension data as double before 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, or populated-fields without presenceAwareJsonSerialization, 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 DateParseHandling is None, and gets Newtonsoft metadata handling on extra objects unless MetadataPropertyHandling is Ignore: an extra {"$ref":"..."} reads as null, and one with $ref plus sibling keys fails deserialization.

Testing

  • TARGET=primary make test-csharp: 654 passed, 34 skipped
  • TARGET=tertiary make test-csharp: 179 passed, 51 skipped
  • TARGET=review make test-csharp: 33 passed, 8 skipped
  • TARGET=secondary make test-csharp (flag off): 182 passed, 35 skipped
  • TARGET=quaternary make test-csharp (flag off): 192 passed, 51 skipped
  • make 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/$values keys 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 spreadAdditionalProperties default for a new SDK (on) and for an existing SDK whose config omits fixFlags (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;
  • $ref keys kept inside typed extras, and decimal extras 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.