-
Notifications
You must be signed in to change notification settings - Fork 214
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4751 from peterwurzinger/fix/4475
Using full names of generated types to avoid ambiguities with existing namespaces
- Loading branch information
Showing
8 changed files
with
216 additions
and
91 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
src/Kiota.Builder/Writers/CSharp/TypeDefinitionExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
using System; | ||
using System.Text; | ||
using Kiota.Builder.CodeDOM; | ||
using Kiota.Builder.Extensions; | ||
|
||
namespace Kiota.Builder.Writers.CSharp; | ||
|
||
internal static class TypeDefinitionExtensions | ||
{ | ||
public static string GetFullName(this ITypeDefinition typeDefinition) | ||
{ | ||
ArgumentNullException.ThrowIfNull(typeDefinition); | ||
|
||
var fullNameBuilder = new StringBuilder(); | ||
return AppendTypeName(typeDefinition, fullNameBuilder).ToString(); | ||
} | ||
|
||
private static StringBuilder AppendTypeName(ITypeDefinition typeDefinition, StringBuilder fullNameBuilder) | ||
{ | ||
if (string.IsNullOrEmpty(typeDefinition.Name)) | ||
throw new ArgumentException("Cannot append a full name for a type without a name.", nameof(typeDefinition)); | ||
|
||
fullNameBuilder.Insert(0, typeDefinition.Name.ToFirstCharacterUpperCase()); | ||
switch (typeDefinition.Parent) | ||
{ | ||
case null: | ||
return fullNameBuilder; | ||
case ITypeDefinition parentTypeDefinition: | ||
{ | ||
fullNameBuilder.Insert(0, '.'); | ||
return AppendTypeName(parentTypeDefinition, fullNameBuilder); | ||
} | ||
case CodeNamespace codeNamespace: | ||
{ | ||
if (!string.IsNullOrEmpty(codeNamespace.Name)) | ||
fullNameBuilder.Insert(0, $"{codeNamespace.Name}."); | ||
|
||
return fullNameBuilder; | ||
} | ||
default: | ||
throw new InvalidOperationException($"Type {typeDefinition.Name} contains an invalid parent of type {typeDefinition.Parent.GetType().FullName}."); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.