[csharp][generichost] Use a single context

Merged
DE
11d ago
+1395-2856

Please merge these first:
https://github.com/OpenAPITools/openapi-generator/pull/25486
https://github.com/OpenAPITools/openapi-generator/pull/25548

Fixes https://github.com/OpenAPITools/openapi-generator/issues/20895
Fixes https://github.com/OpenAPITools/openapi-generator/issues/25158

Problem

The C# Generic Host generator created one JsonSerializerContext per model. This duplicated source-generated metadata across the client (#20895) and only masked collisions by spreading contracts across multiple contexts.

Nested enums expose the underlying correctness problem. Types such as ImageAsset.TypeEnum and DocumentAsset.TypeEnum are distinct CLR types, but System.Text.Json gives both metadata properties the name TypeEnum. This produces SYSLIB1031, omits one contract, and can result in incomplete fast-path serialization (#25158). Nullable enums and their Option or collection wrappers can collide in the same way.

Solution

This change generates one OpenApiSerializationContext for the client and registers it before the existing reflection fallback. Collision-prone enum, nullable, optional, and container contracts receive deterministic, globally unique TypeInfoPropertyName values, while every generated model remains a serialization root.

Removing the model-local contexts eliminates duplicated metadata and gives the generated client one consistent serialization boundary. The context, resolver registration, and model namespace import are all gated when a specification has no models, so no-model clients remain valid.

Generated tests now resolve every explicit contract through the shared context, and the source-generation samples and documentation reflect the new API.

PR checklist

  • Read the contribution guidelines.
  • Run the following to build the project and update samples:
    ./mvnw clean package || exit
    ./bin/generate-samples.sh ./bin/configs/*.yaml || exit
    ./bin/utils/export_docs_generators.sh || exit
    
    (For Windows users, please run the script in WSL)
    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

Consolidates the per-model source-generated JSON serialization contexts in the C# generichost library into a single shared OpenApiSerializationContext.

  • Replaces individual XxxSerializationContext classes generated inside each model file with one context in the Client namespace that registers all model types and inner enum types.
  • Uses unique TypeInfoPropertyName values for inner enums to avoid name collisions.
  • Updates HostConfiguration to combine the new single context with DefaultJsonTypeInfoResolver and adds DI tests verifying the shared context contains every generated model contract.

Migration

  • Code that referenced per-model contexts such as ActivitySerializationContext must use OpenApiSerializationContext instead.