Skip to content

[csharp] Fix compile errors when schema names collide with framework types - #25272

Open
MrD4rkne wants to merge 2 commits into
OpenAPITools:masterfrom
MrD4rkne:fix/csharp-model-name-collisions
Open

MrD4rkne wants to merge 2 commits into
OpenAPITools:masterfrom
MrD4rkne:fix/csharp-model-name-collisions

Conversation

@MrD4rkne

@MrD4rkne MrD4rkne commented Oct 16, 2026 •

Copy link
Copy Markdown
Contributor

A schema named Parameter, ParameterType, Task, RestRequest, … broke the restsharp ApiClient.cs with CS0104, because it imported the model namespace only to reach AbstractOpenAPISchema. A schema named ValidationResult hid System.ComponentModel.DataAnnotations.ValidationResult inside the model namespace, so every IValidatableObject.Validate implementation stopped compiling.

  • restsharp ApiClient.mustache / ApiClient.v790.mustache: drop using {{packageName}}.{{modelPackage}}; and qualify AbstractOpenAPISchema, as the httpclient ApiClient already does.
  • validatable.mustache: qualify System.ComponentModel.DataAnnotations.ValidationResult, as ValidateRegex, modelOneOf and modelAnyOf already do. generichost shares this template, so its samples change too; that is most of the sample diff.
  • Added 3_0/csharp/model-name-collisions.yaml and two tests in CSharpClientCodegenTest. restsharp and httpclient clients generated from that spec now compile under net8; on master they fail with CS0104 (Parameter, ParameterType, Task) and a Validate return-type mismatch (ValidationResult).

Related: #16123

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
    
    Commit all changed files.
  • If your PR is targeting a particular programming language, @mention the technical committee members, so they are more likely to review the pull request.

cc @mandrean @shibayan @Blackclaws @lucamazzanti


Summary by cubic

Fixes C# code generation so schemas named like framework types no longer produce code that fails to compile. Previously, a schema named Parameter, ParameterType, Task, or ValidationResult broke the generated restsharp client with CS0104, because ApiClient.cs imported the model namespace and models hid System.ComponentModel.DataAnnotations.ValidationResult.

  • Removed the model namespace import from the restsharp ApiClient templates and qualified all AbstractOpenAPISchema references.
  • Qualified ValidationResult as System.ComponentModel.DataAnnotations.ValidationResult in the validatable.mustache template shared by restsharp and generichost.
  • Added a regression spec and two tests that generate clients from a schema containing these colliding names.

Written for commit 9204dfc. Summary will update on new commits.

View guided diff

ApiClient.cs imported the model namespace only to reach
AbstractOpenAPISchema, so a schema named like a type ApiClient uses
unqualified (Parameter, ParameterType, Task, RestRequest, DateTime, ...)
made the generated client fail to compile with CS0104. Qualify
AbstractOpenAPISchema as the httpclient ApiClient already does and drop
the using.
A schema named ValidationResult generates a model that hides
System.ComponentModel.DataAnnotations.ValidationResult inside the model
namespace, so every IValidatableObject.Validate implementation stopped
compiling. Qualify it as ValidateRegex, modelOneOf and modelAnyOf already do.
@MrD4rkne
MrD4rkne marked this pull request as ready for review October 16, 2026 11:03

@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.

No issues found across 2598 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

@MrD4rkne

Copy link
Copy Markdown
Contributor Author

PR got kinda big (because it touches a lot of *Model.cs and ApiClient.cs. If it is too big, I'm open to suggestions

This branch has not been deployed

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant