Skip to content

[csharp][generichost] Use a single context - #25593

Merged
wing328 merged 2 commits into
OpenAPITools:masterfrom
devhl-labs:devhl/20895-single-context
Nov 9, 2026
Merged

wing328 merged 2 commits into
OpenAPITools:masterfrom
devhl-labs:devhl/20895-single-context

Conversation

@devhl-labs

@devhl-labs devhl-labs commented Nov 7, 2026 •

Copy link
Copy Markdown
Contributor

Please merge these first:
#25486
#25548

Fixes #20895
Fixes #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.

Written for commit 98eddc8. Summary will update on new commits.

View guided diff

@cubic-dev-ai cubic-dev-ai Bot 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.

All reported issues were addressed across 331 files

Note: This PR contains a large number of files. cubic selects up to 200 of the highest-priority eligible files for this review, so some files may not have been reviewed.

View guided diff | Re-trigger cubic

@wing328
wing328 merged commit 0b72374 into OpenAPITools:master Nov 9, 2026
58 checks passed
@wing328 wing328 added this to the 7.28.0 milestone Nov 9, 2026
@devhl-labs
devhl-labs deleted the devhl/20895-single-context branch November 9, 2026 13:02
@wing328

wing328 commented Nov 9, 2026

Copy link
Copy Markdown
Member

FYI. Just filed #25597 to fixed duplicated methods in csharp client codegen.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

2 participants