[csharp][generichost] Raise correct error on discriminator read failures
Summary
Ensure C# Generic Host clients report invalid discriminator token types as contextual JsonException values.
Generated converters previously called Utf8JsonReader.GetString() directly when reading discriminator values. A numeric, boolean, object, or array discriminator could therefore leak InvalidOperationException instead of following the generated client's invalid-JSON exception contract. A null discriminator would return null and continue the deserialization path.
This change:
- Adds a shared
ClientUtils.GetDiscriminatorValuehelper that requiresJsonTokenType.String. - Throws
JsonExceptioncontaining the discriminator property name and actual token type when the value is not a string. - Uses the helper in both discriminator-reading paths:
ClientUtils.GetDiscriminator- mapped-model discriminator scans in generated model converters
- Regenerates the maintained C# Generic Host samples.
- Adds generator assertions for both generated code paths and the contextual error message.
Valid string discriminators retain their existing behavior. Missing discriminators continue to produce the existing JsonException.
PR checklist
- Read the contribution guidelines.
- Run the following to build the project and update samples:
(For Windows users, please run the script in WSL)./mvnw clean package || exit ./bin/generate-samples.sh ./bin/configs/*.yaml || exit ./bin/utils/export_docs_generators.sh || exit
Commit all changed files.
This is important, as CI jobs will verify all generator outputs of your HEAD commit as it would merge with master.
These must match the expectations made by your contribution.
You may regenerate an individual generator by passing the relevant config(s) as an argument to the script, for example./bin/generate-samples.sh bin/configs/java*.
IMPORTANT: Do NOT purge/delete any folders/files (e.g. tests) when regenerating the samples as manually written tests may be removed. - If your PR is targeting a particular programming language, @mention the technical committee members, so they are more likely to review the pull request.
Summary by cubic
Raises a clear JsonException when a discriminator property is not a JSON string in C# generichost clients. Previously, GetString() on a non-string token produced an InvalidOperationException; now a shared helper validates the token type and reports the property name and actual token type in the error message.