Repository navigation
fix(csharp): send union-typed fields in form-urlencoded and multipart request bodies - #240
Conversation
There was a problem hiding this comment.
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
4568b36 to
2315530
Compare
There was a problem hiding this comment.
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
Generator Snapshot TestingSnapshot tests are running. Snapshot results: https://github.com/speakeasy-api/openapi-generation-snapshots/issues/179#issuecomment-7140524352 |
AshGodfrey
left a comment
There was a problem hiding this comment.
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 toValueToString(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
IsClasswalk inURLBuilderlikely has the same gap for union-typed query params. Worth its own issue.
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.
2315530 to
340d655
Compare
|
Addressed the first two points in 340d655:
The third point is confirmed: |
There was a problem hiding this comment.
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
Why
In C# SDKs, a union-typed (
oneOf/anyOf) property of anapplication/x-www-form-urlencodedrequest 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, andRequestBodySerializerwalked 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):multipart/form-databodiesProperties 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
RequestBodySerializerunwraps a union to its active variant before serializing it, then serializes the variant under the field's own name andexplodesetting. This applies to form fields, object properties, list items, map values, and non-JSON multipart parts.SerializeJSON, as top-level form fields already are.contentType: application/jsonkeep the union class, so its JSON converter still writes the value.IsUnionWrappermoves fromUnionDeserializertoUtilities.UnionDeserializeris only emitted with smart unions enabled, while form serialization needs the check in every SDK. Deserializer behavior is unchanged.tests/specs/fragments/primary/form-union-fields.yamland runtime testsFormUnionFieldsAdditionalShouldcover 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.zSDKs/sdk-csharpregenerated: only theUtilsfiles change.Known limitations:
encodingentry that has nocontentTypeis rejected.explode: falseon a union field can't be expressed from a spec today.Testing
TARGET=primary make test-csharp: 631 passed, 0 failed, 34 skippedTARGET=review make test-csharp: 31 passed, 0 failed, 10 skipped; the regenerated review SDK matches the committed oneTARGET=quaternary,tertiaryandsecondarymake test-csharp: 191, 178 and 177 passed, 0 failed (netstandard2.0, NodaTime, and SDKs without smart unions or presence-aware serialization)test-typescriptv2(904 passed),test-pythonv2(800 passed),test-go(773 passed),test-ruby(393 run, 0 failures),test-mcp-typescript(385 passed)build-postmanandbuild-terraformgenerate successfullytest-javav2: the SDK compiles and 726 of 743 tests pass; the 2 failures are inTimeoutAdditionalTestand 15 are skippedtest-cli: 2536 of 2540 tests pass; the 4 failures are theTestConfigure*teststest-phpcould not run: Composer'sroave/security-advisoriesrejects the local PHP versionmake check-template-csharp,make lintnullableUnionpart when the multipart null-variant check is removed.Public-safety check
.claude/skills/public-repo-communication/SKILL.md).git diff --check.