Skip to content

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

Merged
2ynn merged 3 commits into
mainfrom
fix/csharp/form-union-fields
Nov 24, 2026
Merged

2ynn merged 3 commits into
mainfrom
fix/csharp/form-union-fields

Conversation

@2ynn

@2ynn 2ynn commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

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.

@github-actions github-actions Bot added the csharp Trigger (24) snapshot tests for csharp label Nov 24, 2026

@cubic-dev-ai cubic-dev-ai Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All reported issues were addressed across 11 files

Shadow auto-approve: would not auto-approve because issues were found.
Tip: Get a Slack DM when cubic finishes reviewing your PRs. Connect Slack

View guided diff | Turn on auto-fix | Re-trigger cubic

@2ynn
2ynn marked this pull request as ready for review November 24, 2026 12:06
@2ynn
2ynn requested a review from a team as a code owner November 24, 2026 12:06
@2ynn
2ynn requested a review from AshGodfrey November 24, 2026 12:06
@2ynn
2ynn force-pushed the fix/csharp/form-union-fields branch from 4568b36 to 2315530 Compare November 24, 2026 14:36
@linear-code

linear-code Bot commented Nov 24, 2026

Copy link
Copy Markdown

GEN-3236

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

0 issues found across 2 files (changes from recent commits).

Confidence score: 5/5

  • Automated review surfaced no issues in the provided summaries.
  • No files require special attention.

Shadow auto-approve: would require human review. The PR updates C# form and multipart serialization for union fields. The multipart JSON-union test’s encoding relies on generated metadata that is not visible in the truncated diff, so the dismissal cannot be verified.

View guided diff | Turn on auto-fix | Re-trigger cubic

@speakeasy-generator-bot

speakeasy-generator-bot Bot commented Nov 24, 2026 •

Copy link
Copy Markdown

Generator Snapshot Testing

Snapshot tests are running.

Snapshot results: https://github.com/speakeasy-api/openapi-generation-snapshots/issues/179#issuecomment-7140524352

@2ynn 2ynn added csharp Trigger (24) snapshot tests for csharp and removed csharp Trigger (24) snapshot tests for csharp labels Nov 24, 2026

@AshGodfrey AshGodfrey left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed against a probe SDK generated from this branch and a direct serializer harness. The unwrap is sound: no false positives from IsUnionWrapper on models, request classes or *Type classes; unions used only in multipart still unwrap via the form:explode=true variant annotation; date-time variants format the same as plain DateTime fields.

Non-blocking:

  • An open union holding an unknown value on a required field sends an empty body with no error. Omitting is documented and was allowed by the issue, but sending the raw token's string when it is a primitive would be friendlier.
  • SerializeForm's top-level dictionary branch still passes union values to ValueToString (emits the type name). Unreachable today since map form bodies are rejected, but the sibling list branch was unwrapped, so this one could be too.
  • The same IsClass walk in URLBuilder likely has the same gap for union-typed query params. Worth its own issue.

2ynn added 3 commits November 24, 2026 19:13
A form field whose schema is a union was sent under an empty key: the
union wrapper was walked like a regular object, and its variant
properties carry form metadata without a name. Servers dropped the field.

SerializeFormValue now detects a union wrapper and encodes its active
variant under the field's name, applying the field's explode setting. A
union with no active variant (null, or an open union holding an unknown
value) is omitted. IsUnionWrapper moves from UnionDeserializer, which is
only emitted for the populated-fields union strategy, to Utilities.

Adds a primary fragment covering scalar, enum, array, nullable, unset and
JSON-encoded union fields, with C# runtime tests against the echo server.
Union values that were not themselves a form field were still converted
with ValueToString, which sent the wrapper's type name:

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

RequestBodySerializer now unwraps union wrappers to their active variant
before converting a value to a string, and omits a value whose union has
no active variant. Multipart parts encoded as JSON keep the wrapper so
its converter still writes the value.

Properties of an object variant of a whole-body union that are generated
as JSON-encoded were converted with ValueToString as well, sending an
object as its type name and a string unquoted. They are now serialized
with SerializeJSON, as top-level form fields already are.

The fragment adds array-of-union fields, a whole-body union with a map
variant and an object variant holding plain and JSON-encoded union
properties, and a multipart operation with scalar, array and JSON-encoded
union parts. Tests also cover zero and false variants and single-element
arrays.
An open union holding an unknown primitive value is sent as that value
instead of being omitted. Union values of a dictionary form body are
unwrapped to their active variant.
@2ynn
2ynn force-pushed the fix/csharp/form-union-fields branch from 2315530 to 340d655 Compare November 24, 2026 18:15
@2ynn

2ynn commented Nov 24, 2026

Copy link
Copy Markdown
Contributor Author

Addressed the first two points in 340d655:

  • An open union holding an unknown primitive value now sends that value as-is. An unknown object, array or null value is still omitted. A new openUnion field (x-speakeasy-unknown-values: allow) in the fragment covers each case.
  • The top-level dictionary branch of SerializeForm now unwraps union values, like the list branch.

The third point is confirmed: formQueryParamsUnion sends the request without unionParam for both the string and the array variant. It spans URLBuilder and HeaderSerializer and needs new path and header test operations, so it will be fixed separately.

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

0 issues found across 4 reviewed files. 1 file intentionally excluded from review.

Confidence score: 5/5

  • Automated review surfaced no issues in the provided summaries.
  • No files require special attention.

Shadow auto-approve: would require human review. C# union-typed form/multipart serialization fix with tests. The multipart JSON-union behavior relies on generated metadata that is not visible in this diff, so the dismissed finding remains unverified.

View guided diff | Turn on auto-fix | Re-trigger cubic

@2ynn
2ynn added this pull request to the merge queue Nov 24, 2026
Merged via the queue into main with commit e91bcf8 Nov 24, 2026
151 of 152 checks passed
@2ynn
2ynn deleted the fix/csharp/form-union-fields branch November 24, 2026 18:43
@github-actions github-actions Bot locked and limited conversation to collaborators Nov 24, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

csharp Trigger (24) snapshot tests for csharp

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants