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

MR
2d ago
+3564-3504

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.