fix(csharp): use full type names to avoid collision with standard library#23374
fix(csharp): use full type names to avoid collision with standard library#23374Neokil wants to merge 1 commit intoOpenAPITools:masterfrom
Conversation
…tandard library name collisions
When a model class shares a name with a .NET standard library type (e.g.
`Attributes`), the generated `Assert.IsType<Attributes>(model)` call is
ambiguous and fails to compile. Use the fully-qualified type name
`{{packageName}}.{{modelPackage}}.{{{.}}}` for model return types so the
generated test code is unambiguous regardless of model naming.
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
|
FYI @mandrean (2017/08) @shibayan (2020/02) @Blackclaws (2021/03) @lucamazzanti (2021/05) @iBicha (2023/07) |
There was a problem hiding this comment.
No issues found across 2 files
Since this is your first cubic review, here's how it works:
- cubic automatically reviews your code and comments on bugs and improvements
- Teach cubic by replying to its comments. cubic learns from your replies and gets better over time
- Add one-off context when rerunning by tagging
@cubic-dev-aiwith guidance or docs links (includingllms.txt) - Ask questions if you need clarification on any suggestion
|
Thanks for this. I don't think we need a distinct yaml sample for this either, the petstore probably covers it already. Other than that, no real issues. Though we probably just shouldn't import System at all to avoid this. |
|
Not a C# Dev really (Java). My two cents: I think this makes the most sense for all languages and generators. Of course, this begs the question: does the generators know what class names are "reserved", in order to apply FQN when needed? (So, doing fully-qualified on custom models - when they conflict - makes the most sense, and does not cause clutter except for those cases.) |
|
I like that better. You wouldn't do fully qualified when they conflict, you would do it always. |
Description
When a model class shares a name with a type in the .NET standard library (for
example
Attributes, which conflicts withSystem.Attribute), the generatedtest code fails to compile because the unqualified type assertion is ambiguous.
Before (
api_test.mustache):After:
The fix conditionally uses the fully-qualified type name (
{{packageName}}.{{modelPackage}}.{{{.}}}) when the response type is a model class. Primitive and other non-model return types are left as-is.Changes
modules/openapi-generator/src/main/resources/csharp/libraries/generichost/api_test.mustache— use
{{packageName}}.{{modelPackage}}.{{{.}}}for model return types insideAssert.IsType<>modules/openapi-generator/src/test/resources/3_0/csharp/model-name-collision.yaml— new OpenAPI spec with a model named
Attributesto reproduce the collisionHow to reproduce
Generate a C# client from an OpenAPI spec that contains a model whose name
matches a .NET standard library class (e.g.
Attributes,Exception,Task).The generated
*Testsfiles will fail to compile due to an ambiguous type referencein
Assert.IsType<>.Generator
csharp)generichostlibraries/generichost/api_test.mustachePR checklist
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.
master(upcoming7.x.0minor release - breaking changes with fallbacks),8.0.x(breaking changes without fallbacks)"fixes #123"present in the PR description)Summary by cubic
Qualifies model return types in generated C# tests to avoid .NET type name collisions. Fixes compile errors when models share names with standard library types (e.g.,
Attributes).csharp/libraries/generichost/api_test.mustache, use{{packageName}}.{{modelPackage}}.{{{.}}}inAssert.IsType<>for model return types; non-model types unchanged.3_0/csharp/model-name-collision.yamlto reproduce and validate the fix.Written for commit 08ef04a. Summary will update on new commits.