Skip to content

Fix missing using statements in C# union type wrappers by reordering imports#7320

Merged
gavinbarron merged 10 commits intomainfrom
copilot/fix-missing-usings-issue
Jan 26, 2026
Merged

Fix missing using statements in C# union type wrappers by reordering imports#7320
gavinbarron merged 10 commits intomainfrom
copilot/fix-missing-usings-issue

Conversation

Copy link
Contributor

Copilot AI commented Jan 23, 2026

Fix for missing using statements in C# union type wrappers

Changes:

  • Reordered method calls in CSharpRefiner to call AddDefaultImports after ConvertUnionTypesToWrapper
  • Removed AddDefaultImportsToComposedTypeWrappers method (net -28 lines)
  • Added tests to verify usings are added correctly
  • Fixed whitespace formatting issues with dotnet format
  • Added changelog entry for the fix

Status:

  • All 44 CSharpLanguageRefinerTests pass
  • Code formatting verified with dotnet format
  • Generated code compiles successfully with ImplicitUsings disabled
  • Changelog updated with issue #7603

The fix aligns C# with all other language generators and resolves compilation errors when ImplicitUsings is disabled.

Original prompt

This section details on the original issue you should resolve

<issue_title>Missing usings if "ImplicitUsings" is disabled</issue_title>
<issue_description>### What are you generating using Kiota, clients or plugins?

API Client/SDK

In what context or format are you using Kiota?

Nuget tool

Client library/SDK language

Csharp

Describe the bug

See attached sample: the client is created from https://api.twitter.com/2/openapi.json
XKiota.zip

The project does not enable ImplicitUsings (as might happen for older projects).

There are two compilation errors due to missing using statements in class MediaSegments

The type or namespace name 'IDictionary<,>' could not be found (are you missing a using directive or an assembly reference?)
The type or namespace name 'Action<>' could not be found (are you missing a using directive or an assembly reference?)

Expected behavior

Two usings should be added:

using System;
using System.Collections.Generic;

How to reproduce

I used this command to create the client:

dotnet kiota generate -l CSharp -c TwitterClient -n XKiota.Client -d https://api.twitter.com/2/openapi.json -o ./Client --exclude-backward-compatible

Open API description file

https://api.twitter.com/2/openapi.json

Kiota Version

1.29.0

Latest Kiota version known to work for scenario above?(Not required)

No response

Known Workarounds

No response

Configuration

No response

Debug output

Click to expand log ```
</details>


### Other information

_No response_</issue_description>

<agent_instructions>Consider the following OpenAPI

{
"openapi": "3.0.0",
"info": {
"description": "X API v2 available endpoints",
"version": "2.157",
"title": "X API v2",
"termsOfService": "https://developer.x.com/en/developer-terms/agreement-and-policy.html",
"contact": {
"name": "X Developers",
"url": "https://developer.x.com/"
},
"license": {
"name": "X Developer Agreement and Policy",
"url": "https://developer.x.com/en/developer-terms/agreement-and-policy.html"
}
},
"paths": {
"/2/media/upload/{id}/append": {
"post": {
"security": [
{
"OAuth2UserToken": [
"media.write"
]
},
{
"UserToken": []
}
],
"tags": [
"Media"
],
"summary": "Append Media upload",
"description": "Appends data to a Media upload request.",
"externalDocs": {
"url": "https://docs.x.com/x-api/media/media-upload"
},
"operationId": "appendMediaUpload",
"parameters": [
{
"name": "id",
"in": "path",
"description": "The media identifier for the media to perform the append operation.",
"required": true,
"schema": {
"$ref": "#/components/schemas/MediaId"
},
"style": "simple"
}
],
"requestBody": {
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/MediaUploadAppendRequest"
}
},
"multipart/form-data": {
"schema": {
"$ref": "#/components/schemas/MediaUploadAppendRequest"
}
}
}
},
"responses": {
"200": {
"description": "The request has succeeded.",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/MediaUploadAppendResponse"
}
}
}
},
"default": {
"description": "The request has failed.",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Error"
}
},
"application/problem+json": {
"schema": {
"$ref": "#/components/schemas/Problem"
}
}
}
}
}
}
},
},
"servers": [
{
"description": "X API",
"url": "https://api.x.com"
}
],
"components": {
"Error": {
"type": "object",
"required": [
"code",
"message"
],
"properties": {
"code": {
"type": "integer",
"format": "int32"
},
"message": {
"type": "string"
}
}
},
"MediaId": {
"type": "string",
"description": "The unique identifier of this Media.",
"pattern": "^[0-9]{1,19}$",
"example": "1146654567674912769"
},
"MediaUploadAppendRequest": {
"anyOf"...


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot AI and others added 4 commits January 23, 2026 02:24
Co-authored-by: gavinbarron <7122716+gavinbarron@users.noreply.github.com>
Co-authored-by: gavinbarron <7122716+gavinbarron@users.noreply.github.com>
…raversal

Co-authored-by: gavinbarron <7122716+gavinbarron@users.noreply.github.com>
Co-authored-by: gavinbarron <7122716+gavinbarron@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix missing usings when ImplicitUsings is disabled Fix missing using statements in C# union type wrappers Jan 23, 2026
Copilot AI requested a review from gavinbarron January 23, 2026 02:40
@gavinbarron
Copy link
Contributor

@copilot can you please evaluate if the failure case reported with missing usings applies to any of the other languages that kiota can generate?

Copy link
Contributor Author

Copilot AI commented Jan 23, 2026

@copilot can you please evaluate if the failure case reported with missing usings applies to any of the other languages that kiota can generate?

I've evaluated all other language generators (Java, TypeScript, Python, Go, PHP, Ruby, and Dart). They all call AddDefaultImports after ConvertUnionTypesToWrapper, so they don't have this issue.

Only C# was calling AddDefaultImports before the union type conversion, which is why only C# was missing the necessary imports for composed type wrappers. The fix is specific to C# and not needed for other languages.

Co-authored-by: gavinbarron <7122716+gavinbarron@users.noreply.github.com>
Copilot AI changed the title Fix missing using statements in C# union type wrappers Fix missing using statements in C# union type wrappers by reordering imports Jan 23, 2026
@gavinbarron gavinbarron marked this pull request as ready for review January 23, 2026 16:20
@gavinbarron gavinbarron requested a review from a team as a code owner January 23, 2026 16:20
@gavinbarron
Copy link
Contributor

@copilot please run dotnet format to resolve the formatting issues that are causing build failures

Co-authored-by: gavinbarron <7122716+gavinbarron@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Jan 23, 2026

@copilot please run dotnet format to resolve the formatting issues that are causing build failures

Fixed formatting issues with dotnet format. The whitespace errors in CSharpLanguageRefinerTests.cs have been resolved in commit a924021.

@gavinbarron
Copy link
Contributor

@copilot please add an entry to the changelog

Co-authored-by: gavinbarron <7122716+gavinbarron@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Jan 23, 2026

@copilot please add an entry to the changelog

Added changelog entry in commit bc364b3. The entry documents the fix for missing using System; and using System.Collections.Generic; statements in C# union type wrappers when ImplicitUsings is disabled.

@gavinbarron gavinbarron enabled auto-merge (squash) January 26, 2026 19:44
@gavinbarron gavinbarron merged commit 7a56c7b into main Jan 26, 2026
290 checks passed
@gavinbarron gavinbarron deleted the copilot/fix-missing-usings-issue branch January 26, 2026 19:53
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.

Missing usings if "ImplicitUsings" is disabled

4 participants