fix(csharp): send union-typed fields in form-urlencoded and multipart request bodies

Merged
2Y
1h ago
+772-80

Why

In C# SDKs, a union-typed (oneOf/anyOf) property of an application/x-www-form-urlencoded request body was sent under an empty key (=abc), so servers dropped it. Each variant property of the generated union class carries form metadata with an empty name, and RequestBodySerializer walked the union class like a regular object.

Union values that are not form fields themselves were converted with Utilities.ValueToString, which sent the union class's type name (e.g. Openapi.Models.Shared.FormUnionStringOrInteger):

  • items of an array whose items are a union
  • items of a union variant that is an array of unions
  • union-typed properties of the object variant of a whole-body union
  • union-typed values of the map variant of a whole-body union
  • scalar and array union parts in multipart/form-data bodies

Properties of a whole-body union's object variant that are generated as JSON-encoded (a nested object, an array of objects, or a union with an object variant) were also converted with ValueToString, so an object was sent as its type name and a string was sent unquoted.

What changed

  • RequestBodySerializer unwraps a union to its active variant before serializing it, then serializes the variant under the field's own name and explode setting. This applies to form fields, object properties, list items, map values, and non-JSON multipart parts.
  • Properties of a nested object that are marked as JSON-encoded are serialized with SerializeJSON, as top-level form fields already are.
  • A union field with no active variant (null, or an open union holding an unknown value) is omitted from the body.
  • Multipart parts with contentType: application/json keep the union class, so its JSON converter still writes the value.
  • IsUnionWrapper moves from UnionDeserializer to Utilities. UnionDeserializer is only emitted with smart unions enabled, while form serialization needs the check in every SDK. Deserializer behavior is unchanged.
  • New fragment tests/specs/fragments/primary/form-union-fields.yaml and runtime tests FormUnionFieldsAdditionalShould cover scalar, string-or-array, enum-or-boolean, array-of-union, nullable, optional and JSON-encoded union fields; a whole-body union whose object variant holds a plain and a JSON-encoded union property, and whose map variant holds union values; and scalar, integer, array and JSON-encoded multipart union parts, plus a nullable multipart union holding null, which is omitted.
  • Review SDK zSDKs/sdk-csharp regenerated: only the Utils files change.

Known limitations:

  • A union-typed form field with an object or map variant is always JSON-encoded, and a union field with an encoding entry that has no contentType is rejected. explode: false on a union field can't be expressed from a spec today.
  • A union with no active variant inside a list or map is sent as an empty value, like a null item.
  • An item of an array of unions whose active variant is itself an array is sent as the list's type name. Form encoding has no standard representation for nested arrays.
  • A multipart union with a binary variant is sent as a text part holding the file model's type name, as it was before this change.

Testing

  • TARGET=primary make test-csharp: 631 passed, 0 failed, 34 skipped
  • TARGET=review make test-csharp: 31 passed, 0 failed, 10 skipped; the regenerated review SDK matches the committed one
  • TARGET=quaternary, tertiary and secondary make test-csharp: 191, 178 and 177 passed, 0 failed (netstandard2.0, NodaTime, and SDKs without smart unions or presence-aware serialization)
  • The fragment feeds every target's primary spec, and CI runs only the C# jobs for this change, so the primary variant was run locally for the other targets:
    • test-typescriptv2 (904 passed), test-pythonv2 (800 passed), test-go (773 passed), test-ruby (393 run, 0 failures), test-mcp-typescript (385 passed)
    • build-postman and build-terraform generate successfully
    • test-javav2: the SDK compiles and 726 of 743 tests pass; the 2 failures are in TimeoutAdditionalTest and 15 are skipped
    • test-cli: 2536 of 2540 tests pass; the 4 failures are the TestConfigure* tests
    • test-php could not run: Composer's roave/security-advisories rejects the local PHP version
    • The Java and CLI failures do not involve the new operations.
  • make check-template-csharp, make lint
  • The new tests fail without the serializer change: the two whole-body JSON-property tests fail with the nested JSON branch disabled, the multipart test fails with the multipart JSON branch disabled, and it fails again with an empty nullableUnion part when the multipart null-variant check is removed.
  • A separate spec with date, date-time, number, enum, open-enum, nullable and nested union fields, generated before and after this change with the primary and quaternary configurations, produces identical form and multipart output for every non-union field of an object body, and the variant's value for every union field.
  • No cross-target test ID is registered: the runtime tests are C#-specific.

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.