Skip to content

Commit

Permalink
Merge pull request #1790 from microsoft/chore/fix_build_error_in_gene…
Browse files Browse the repository at this point in the history
…rated_code

Fix build errors in CLI generated code
  • Loading branch information
baywet authored Aug 18, 2022
2 parents 098f3da + 20fb3e1 commit 7436411
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 13 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Changed

- Fix issue with duplicate variable declaration in command handlers (Shell)
- Update namespace qualification algorithm (helps in resolving when a type name appears in multiple namespaces) to use case insensitive string comparison (CSharp).

## [0.4.0] - 2022-08-18

### Added
Expand Down
15 changes: 13 additions & 2 deletions src/Kiota.Builder/Refiners/ShellRefiner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,30 @@ public override void Refine(CodeNamespace generatedCode)
CapitalizeNamespacesFirstLetters(generatedCode);
ReplaceBinaryByNativeType(generatedCode, "Stream", "System.IO");
MakeEnumPropertiesNullable(generatedCode);
CreateCommandBuilders(generatedCode);
/* Exclude the following as their names will be capitalized making the change unnecessary in this case sensitive language
* code classes, class declarations, property names, using declarations, namespace names
* Exclude CodeMethod as the return type will also be capitalized (excluding the CodeType is not enough since this is evaluated at the code method level)
*/
ReplaceReservedNames(
generatedCode,
new CSharpReservedNamesProvider(), x => $"@{x.ToFirstCharacterUpperCase()}",
new HashSet<Type> { typeof(CodeClass), typeof(ClassDeclaration), typeof(CodeProperty), typeof(CodeUsing), typeof(CodeNamespace), typeof(CodeMethod) }
new HashSet<Type> { typeof(CodeClass), typeof(ClassDeclaration), typeof(CodeProperty), typeof(CodeUsing), typeof(CodeNamespace), typeof(CodeMethod), typeof(CodeEnum) }
);
// Replace the reserved types
ReplaceReservedModelTypes(generatedCode, new CSharpReservedTypesProvider(), x => $"{x}Object");
AddParentClassToErrorClasses(
generatedCode,
"ApiException",
"Microsoft.Kiota.Abstractions"
);
AddDiscriminatorMappingsUsingsToParentClasses(
generatedCode,
"IParseNode"
);
DisambiguatePropertiesWithClassNames(generatedCode);
AddConstructorsForDefaultValues(generatedCode, false);
AddSerializationModulesImport(generatedCode);
CreateCommandBuilders(generatedCode);
}

private static void CreateCommandBuilders(CodeElement currentElement)
Expand Down
4 changes: 2 additions & 2 deletions src/Kiota.Builder/Writers/CSharp/CSharpConventionService.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using System.Linq;
using Kiota.Builder.Extensions;
Expand Down Expand Up @@ -143,7 +143,7 @@ private static bool DoesTypeExistsInTargetAncestorNamespace(CodeType currentType
var rootNs = ns?.GetRootNamespace();
while (ns is not null && ns != rootNs && !hasChildWithName)
{
hasChildWithName = ns.GetChildElements(true).OfType<CodeClass>().Any(c => c.Name?.Equals(typeName) == true);
hasChildWithName = ns.GetChildElements(true).OfType<CodeClass>().Any(c => c.Name?.Equals(typeName, StringComparison.OrdinalIgnoreCase) == true);
ns = ns.Parent is CodeNamespace n ? n : (ns.GetImmediateParentOfType<CodeNamespace>());
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Kiota.Builder/Writers/Shell/ShellCodeMethodWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ private void WriteExecutableCommand(CodeMethod codeElement, RequestParams reques
}
}
return (type, NormalizeToIdentifier(p.Name), p);
}).ToList();
}).DistinctBy(static p => p.Item2).ToList();
var availableOptions = WriteExecutableCommandOptions(writer, parameters);

var isHandlerVoid = conventions.VoidTypeName.Equals(originalMethod.ReturnType.Name, StringComparison.OrdinalIgnoreCase);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -238,19 +238,25 @@ public void WritesContainerCommandWithConflictingTypes()
method.Kind = CodeMethodKind.CommandBuilder;
method.SimpleName = "User";
// Types: A.B.T1
// A.B.C.T2
// A.B.C.D.T1
var ns1 = root.AddNamespace("Test.Name");
var ns2 = ns1.AddNamespace("Test.Name.Sub1");
var ns3 = ns2.AddNamespace("Test.Name.Sub1.Sub2");
var t1 = new CodeClass { Name = "TestClass1", Kind = CodeClassKind.RequestBuilder };
// A.B.C.D.T2
// A.B.C.D.E.F.T1
var ns1 = root.AddNamespace("A.B");
var ns2 = ns1.AddNamespace("A.B.C.D");
var ns3 = ns2.AddNamespace("A.B.C.D.E.F");
parentClass.Kind = CodeClassKind.RequestBuilder;
var t1 = new CodeClass { Name = "TestRequestBuilder", Kind = CodeClassKind.RequestBuilder };
var t1a = new CodeClass { Name = "TestRequestBuilder2", Kind = CodeClassKind.RequestBuilder };
ns1.AddClass(t1);
ns1.AddClass(t1a);
var t2 = parentClass;
ns2.AddClass(t2);
var t1Sub = new CodeClass { Name = "TestClass1", Kind = CodeClassKind.RequestBuilder };
var t1Sub = new CodeClass { Name = "TestRequestBuilder", Kind = CodeClassKind.RequestBuilder };
var t1Sub2 = new CodeClass { Name = "testRequestBuilder2", Kind = CodeClassKind.RequestBuilder }; // Should match ignoring case
ns3.AddClass(t1Sub);
ns3.AddClass(t1Sub2);
t1Sub.AddMethod(new CodeMethod { Kind = CodeMethodKind.CommandBuilder, Name = "BuildTestMethod1", ReturnType = new CodeType() });
t1Sub.AddMethod(new CodeMethod { Kind = CodeMethodKind.CommandBuilder, Name = "BuildTestMethod2", ReturnType = new CodeType { CollectionKind = CodeTypeBase.CodeTypeCollectionKind.Array } });

method.AccessedProperty = new CodeProperty
{
Type = new CodeType
Expand All @@ -259,14 +265,30 @@ public void WritesContainerCommandWithConflictingTypes()
TypeDefinition = t1Sub
}
};
var method2 = new CodeMethod { Kind = CodeMethodKind.CommandBuilder, Name = "methodName2", SimpleName = "Mail", ReturnType = new CodeType { Name = ReturnTypeName } };
parentClass.AddMethod(method2);

t1Sub2.AddMethod(new CodeMethod { Kind = CodeMethodKind.CommandBuilder, Name = "BuildTestMethod1", ReturnType = new CodeType() });

method2.AccessedProperty = new CodeProperty
{
Type = new CodeType
{
Name = "testRequestBuilder2",
TypeDefinition = t1Sub2
}
};

AddRequestProperties();

writer.Write(method);
writer.Write(method2);
var result = tw.ToString();

Assert.Contains("var command = new Command(\"user\");", result);
Assert.Contains("var builder = new Test.Name.Sub1.Sub2.TestRequestBuilder", result);
Assert.Contains("var builder = new Test.A.B.C.D.E.F.TestRequestBuilder", result);
// Test case insensitive match
Assert.Contains("var builder = new Test.A.B.C.D.E.F.TestRequestBuilder2", result);
Assert.Contains("command.AddCommand(builder.BuildTestMethod1());", result);
Assert.Contains("foreach (var cmd in builder.BuildTestMethod2()) {", result);
Assert.Contains("command.AddCommand(cmd);", result);
Expand Down

0 comments on commit 7436411

Please sign in to comment.