Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: missing untyped node imports #5327

Merged
merged 2 commits into from
Sep 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Changed

- Fixed shorthand for refresh option in workspace experience. [#5240](https://github.com/microsoft/kiota/issues/5240)
- Fixed a bug where untyped node imports would be missing CSharp/Go/TypeScript/Java. [#5326](https://github.com/microsoft/kiota/issues/5326)
- Fixed missing type options in help for plugin commands. [#5230](https://github.com/microsoft/kiota/issues/5230)
- Removed OpenAI plugins generation since the service does not support them anymore.
- Fixed a bug where the error message would not be deserialized if the property name matched a reserved property. [#5311](https://github.com/microsoft/kiota/issues/5311)
Expand Down
3 changes: 3 additions & 0 deletions src/Kiota.Builder/Refiners/CSharpRefiner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
Name = "DefaultQueryParameters",
IsExternal = true,
},
!_configuration.ExcludeBackwardCompatible,//TODO remove the condition for v2

Check warning on line 51 in src/Kiota.Builder/Refiners/CSharpRefiner.cs

View workflow job for this annotation

GitHub Actions / Build

Complete the task associated to this 'TODO' comment. (https://rules.sonarsource.com/csharp/RSPEC-1135)
!_configuration.ExcludeBackwardCompatible);
AddDefaultImports(generatedCode, defaultUsingEvaluators);
MoveClassesWithNamespaceNamesUnderNamespace(generatedCode);
Expand Down Expand Up @@ -143,7 +143,7 @@
private const string StoreNamespaceName = $"{AbstractionsNamespaceName}.Store";
private const string ExtensionsNamespaceName = $"{AbstractionsNamespaceName}.Extensions";

protected static readonly AdditionalUsingEvaluator[] defaultUsingEvaluators = {

Check warning on line 146 in src/Kiota.Builder/Refiners/CSharpRefiner.cs

View workflow job for this annotation

GitHub Actions / Build

Refactor this field to reduce its Cognitive Complexity from 26 to the 15 allowed. (https://rules.sonarsource.com/csharp/RSPEC-3776)
new (static x => x is CodeProperty prop && prop.IsOfKind(CodePropertyKind.RequestAdapter),
AbstractionsNamespaceName, "IRequestAdapter"),
new (static x => x is CodeMethod method && method.IsOfKind(CodeMethodKind.RequestGenerator),
Expand Down Expand Up @@ -185,6 +185,9 @@
AbstractionsNamespaceName, "RequestHeaders"),
new (static x => x is CodeProperty prop && prop.IsOfKind(CodePropertyKind.Custom) && prop.Type.Name.Equals(KiotaBuilder.UntypedNodeName, StringComparison.OrdinalIgnoreCase),
SerializationNamespaceName, KiotaBuilder.UntypedNodeName),
new (static x => x is CodeMethod @method && @method.IsOfKind(CodeMethodKind.RequestExecutor) && (method.ReturnType.Name.Equals(KiotaBuilder.UntypedNodeName, StringComparison.OrdinalIgnoreCase) ||
method.Parameters.Any(x => x.Kind is CodeParameterKind.RequestBody && x.Type.Name.Equals(KiotaBuilder.UntypedNodeName, StringComparison.OrdinalIgnoreCase))),
SerializationNamespaceName, KiotaBuilder.UntypedNodeName),
new (static x => x is CodeEnum prop && prop.Options.Any(x => x.IsNameEscaped),
"System.Runtime.Serialization", "EnumMemberAttribute"),
new (static x => x is IDeprecableElement element && element.Deprecation is not null && element.Deprecation.IsDeprecated,
Expand Down
3 changes: 3 additions & 0 deletions src/Kiota.Builder/Refiners/GoRefiner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
string.Empty,
false,
MergeOverLappedStrings);
if (_configuration.ExcludeBackwardCompatible) //TODO remove condition for v2

Check warning on line 49 in src/Kiota.Builder/Refiners/GoRefiner.cs

View workflow job for this annotation

GitHub Actions / Build

Complete the task associated to this 'TODO' comment. (https://rules.sonarsource.com/csharp/RSPEC-1135)
RemoveRequestConfigurationClasses(generatedCode,
new CodeUsing
{
Expand Down Expand Up @@ -190,7 +190,7 @@
);
AddParsableImplementsForModelClasses(
generatedCode,
"Parsable"

Check warning on line 193 in src/Kiota.Builder/Refiners/GoRefiner.cs

View workflow job for this annotation

GitHub Actions / Build

Define a constant instead of using this literal 'Parsable' 5 times. (https://rules.sonarsource.com/csharp/RSPEC-1192)
);
RenameInnerModelsToAppended(
generatedCode
Expand Down Expand Up @@ -234,7 +234,7 @@
CrawlTree(currentElement, GenerateCodeFiles);
}

private string MergeOverLappedStrings(string start, string end)

Check warning on line 237 in src/Kiota.Builder/Refiners/GoRefiner.cs

View workflow job for this annotation

GitHub Actions / Build

Make 'MergeOverLappedStrings' a static method. (https://rules.sonarsource.com/csharp/RSPEC-2325)
{
var search = "RequestBuilder";
start = start.ToFirstCharacterUpperCase();
Expand Down Expand Up @@ -552,7 +552,7 @@
"UUID",
"Guid"
};
private static readonly AdditionalUsingEvaluator[] defaultUsingEvaluators = {

Check warning on line 555 in src/Kiota.Builder/Refiners/GoRefiner.cs

View workflow job for this annotation

GitHub Actions / Build

Refactor this field to reduce its Cognitive Complexity from 27 to the 15 allowed. (https://rules.sonarsource.com/csharp/RSPEC-3776)
new (static x => x is CodeProperty prop && prop.IsOfKind(CodePropertyKind.RequestAdapter),
AbstractionsNamespaceName, "RequestAdapter"),
new (static x => x is CodeMethod method && method.IsOfKind(CodeMethodKind.RequestGenerator),
Expand Down Expand Up @@ -598,6 +598,9 @@
AbstractionsNamespaceName, MultipartBodyClassName),
new (static x => x is CodeProperty prop && prop.IsOfKind(CodePropertyKind.Custom) && prop.Type.Name.Equals(KiotaBuilder.UntypedNodeName, StringComparison.OrdinalIgnoreCase),
SerializationNamespaceName, KiotaBuilder.UntypedNodeName),
new (static x => x is CodeMethod @method && @method.IsOfKind(CodeMethodKind.RequestExecutor) && (method.ReturnType.Name.Equals(KiotaBuilder.UntypedNodeName, StringComparison.OrdinalIgnoreCase) ||
method.Parameters.Any(x => x.Kind is CodeParameterKind.RequestBody && x.Type.Name.Equals(KiotaBuilder.UntypedNodeName, StringComparison.OrdinalIgnoreCase))),
SerializationNamespaceName, KiotaBuilder.UntypedNodeName),
new (static x => x is CodeEnum @enum && @enum.Flags,"", "math"),
};
private const string MultipartBodyClassName = "MultipartBody";
Expand Down
5 changes: 4 additions & 1 deletion src/Kiota.Builder/Refiners/JavaRefiner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@
private static void SetSetterParametersToNullable(CodeElement currentElement, params Tuple<CodeMethodKind, CodePropertyKind>[] accessorPairs)
{
if (currentElement is CodeMethod method &&
accessorPairs.Any(x => method.IsOfKind(x.Item1) && (method.AccessedProperty?.IsOfKind(x.Item2) ?? false)))

Check warning on line 204 in src/Kiota.Builder/Refiners/JavaRefiner.cs

View workflow job for this annotation

GitHub Actions / Build

Collection-specific "Exists" method should be used instead of the "Any" extension. (https://rules.sonarsource.com/csharp/RSPEC-6605)
foreach (var param in method.Parameters)
param.Type.IsNullable = true;
CrawlTree(currentElement, element => SetSetterParametersToNullable(element, accessorPairs));
Expand All @@ -214,7 +214,7 @@
var nUsing = new CodeUsing
{
Name = "EnumSet",
Declaration = new CodeType { Name = "java.util", IsExternal = true },

Check warning on line 217 in src/Kiota.Builder/Refiners/JavaRefiner.cs

View workflow job for this annotation

GitHub Actions / Build

Define a constant instead of using this literal 'java.util' 6 times. (https://rules.sonarsource.com/csharp/RSPEC-1192)
};
currentClass.AddUsing(nUsing);
}
Expand All @@ -224,7 +224,7 @@
private static readonly JavaConventionService conventionService = new();
private const string AbstractionsNamespaceName = "com.microsoft.kiota";
private const string SerializationNamespaceName = "com.microsoft.kiota.serialization";
private static readonly AdditionalUsingEvaluator[] defaultUsingEvaluators = {

Check warning on line 227 in src/Kiota.Builder/Refiners/JavaRefiner.cs

View workflow job for this annotation

GitHub Actions / Build

Refactor this field to reduce its Cognitive Complexity from 30 to the 15 allowed. (https://rules.sonarsource.com/csharp/RSPEC-3776)
new (static x => x is CodeProperty prop && prop.IsOfKind(CodePropertyKind.RequestAdapter),
AbstractionsNamespaceName, "RequestAdapter"),
new (static x => x is CodeProperty prop && prop.IsOfKind(CodePropertyKind.PathParameters) || x is CodeMethod method && method.IsOfKind(CodeMethodKind.QueryParametersMapper),
Expand Down Expand Up @@ -264,7 +264,10 @@
AbstractionsNamespaceName, "QueryParameters"),
new (static x => x is CodeClass @class && @class.OriginalComposedType is CodeIntersectionType intersectionType && intersectionType.Types.Any(static y => !y.IsExternal),
SerializationNamespaceName, "ParseNodeHelper"),
new (static x => (x is CodeMethod @method && @method.IsOfKind(CodeMethodKind.Getter, CodeMethodKind.Setter) && @method.AccessedProperty != null && (@method.AccessedProperty.IsOfKind(CodePropertyKind.Custom) && @method.AccessedProperty.Type.Name.Equals(KiotaBuilder.UntypedNodeName, StringComparison.OrdinalIgnoreCase) )) ,
new (static x => x is CodeMethod @method && @method.IsOfKind(CodeMethodKind.Getter, CodeMethodKind.Setter) && @method.AccessedProperty != null && @method.AccessedProperty.IsOfKind(CodePropertyKind.Custom) && @method.AccessedProperty.Type.Name.Equals(KiotaBuilder.UntypedNodeName, StringComparison.OrdinalIgnoreCase),
SerializationNamespaceName, KiotaBuilder.UntypedNodeName),
new (static x => x is CodeMethod @method && @method.IsOfKind(CodeMethodKind.RequestExecutor) && (method.ReturnType.Name.Equals(KiotaBuilder.UntypedNodeName, StringComparison.OrdinalIgnoreCase) ||
method.Parameters.Any(x => x.Kind is CodeParameterKind.RequestBody && x.Type.Name.Equals(KiotaBuilder.UntypedNodeName, StringComparison.OrdinalIgnoreCase))),
SerializationNamespaceName, KiotaBuilder.UntypedNodeName),
new (static x => x is CodeMethod method && method.IsOfKind(CodeMethodKind.RequestExecutor, CodeMethodKind.RequestGenerator) && method.Parameters.Any(static y => y.IsOfKind(CodeParameterKind.RequestBody) && y.Type.Name.Equals(MultipartBodyClassName, StringComparison.OrdinalIgnoreCase)),
AbstractionsNamespaceName, MultipartBodyClassName)
Expand Down
2 changes: 1 addition & 1 deletion src/Kiota.Builder/Refiners/TypeScriptRefiner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -704,7 +704,7 @@
m.IsOfKind(CodeMethodKind.RequestExecutor, CodeMethodKind.RequestGenerator) &&
m.Parameters.Any(IsMultipartBody);
// for Kiota abstraction library if the code is not required for runtime purposes e.g. interfaces then the IsErasable flag is set to true
private static readonly AdditionalUsingEvaluator[] defaultUsingEvaluators = {

Check warning on line 707 in src/Kiota.Builder/Refiners/TypeScriptRefiner.cs

View workflow job for this annotation

GitHub Actions / Build

Refactor this field to reduce its Cognitive Complexity from 16 to the 15 allowed. (https://rules.sonarsource.com/csharp/RSPEC-3776)
new (static x => x is CodeMethod method && method.Kind is CodeMethodKind.ClientConstructor,
AbstractionsPackageName, true, "RequestAdapter"),
new (static x => x is CodeMethod method && method.Kind is CodeMethodKind.RequestGenerator,
Expand All @@ -727,7 +727,7 @@
new (static x => x is CodeMethod m && HasMultipartBody(m),
AbstractionsPackageName, MultipartBodyClassName, $"serialize{MultipartBodyClassName}"),
new (static x => (x is CodeProperty prop && prop.IsOfKind(CodePropertyKind.Custom) && prop.Type.Name.Equals(KiotaBuilder.UntypedNodeName, StringComparison.OrdinalIgnoreCase))
|| (x is CodeMethod method && (method.Parameters.Any(param => param.Type.Name.Equals(KiotaBuilder.UntypedNodeName, StringComparison.OrdinalIgnoreCase)) || method.ReturnType.Name.Equals(KiotaBuilder.UntypedNodeName, StringComparison.OrdinalIgnoreCase))),
|| (x is CodeMethod method && (method.Parameters.Any(param => param.Kind is CodeParameterKind.RequestBody && param.Type.Name.Equals(KiotaBuilder.UntypedNodeName, StringComparison.OrdinalIgnoreCase)) || method.ReturnType.Name.Equals(KiotaBuilder.UntypedNodeName, StringComparison.OrdinalIgnoreCase))),
AbstractionsPackageName, KiotaBuilder.UntypedNodeName, "createUntypedNodeFromDiscriminatorValue"),
};
private const string MultipartBodyClassName = "MultipartBody";
Expand Down
33 changes: 32 additions & 1 deletion tests/Kiota.Builder.Tests/Refiners/GoLanguageRefinerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1195,6 +1195,37 @@ public async Task NormalizeNamespaceNameAsync()
Assert.Equal("github.com/OrgName/RepoName.apisdk.models", models.Name);
Assert.Equal("github.com/OrgName/RepoName.apisdk.models.submodels.camelcase", camelCaseModel.Name);
}

[Fact]
public async Task AddsUsingForUntypedNodeInMethodParameterAsync()
{
var requestBuilderClass = root.AddClass(new CodeClass() { Name = "NodeRequestBuilder" }).First();
var method = new CodeMethod
{
Name = "getAsync",
ReturnType = new CodeType
{
Name = "string",
IsExternal = true
},
Kind = CodeMethodKind.RequestExecutor
};
method.AddParameter(new CodeParameter()
{
Name = "jsonData",
Type = new CodeType()
{
Name = KiotaBuilder.UntypedNodeName,
IsExternal = true
},
Kind = CodeParameterKind.RequestBody
});
requestBuilderClass.AddMethod(method);
await ILanguageRefiner.RefineAsync(new GenerationConfiguration { Language = GenerationLanguage.Go }, root);
Assert.Equal(GoRefiner.UntypedNodeName, method.Parameters.First().Type.Name);// type is renamed
Assert.NotEmpty(requestBuilderClass.StartBlock.Usings);
var nodeUsing = requestBuilderClass.StartBlock.Usings.Where(static declaredUsing => declaredUsing.Name.Equals(KiotaBuilder.UntypedNodeName, StringComparison.OrdinalIgnoreCase)).ToArray();
Assert.Single(nodeUsing);
Assert.Equal("github.com/microsoft/kiota-abstractions-go/serialization", nodeUsing[0].Declaration.Name);
}
#endregion
}
32 changes: 32 additions & 0 deletions tests/Kiota.Builder.Tests/Refiners/JavaLanguageRefinerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -729,5 +729,37 @@ public async Task AddsUsingForUntypedNodeAsync(bool usesBackingStore)
Assert.Equal(2, nodeUsing.Length); // one for the getter and another for setter. Writer will unionise
Assert.Equal("com.microsoft.kiota.serialization", nodeUsing[0].Declaration.Name);
}
[Fact]
public async Task AddsUsingForUntypedNodeInMethodParameterAsync()
{
var requestBuilderClass = root.AddClass(new CodeClass() { Name = "NodeRequestBuilder" }).First();
var method = new CodeMethod
{
Name = "getAsync",
ReturnType = new CodeType
{
Name = "string",
IsExternal = true
},
Kind = CodeMethodKind.RequestExecutor
};
method.AddParameter(new CodeParameter()
{
Name = "jsonData",
Type = new CodeType()
{
Name = KiotaBuilder.UntypedNodeName,
IsExternal = true
},
Kind = CodeParameterKind.RequestBody
});
requestBuilderClass.AddMethod(method);
await ILanguageRefiner.RefineAsync(new GenerationConfiguration { Language = GenerationLanguage.Java }, root);
Assert.Equal(KiotaBuilder.UntypedNodeName, method.Parameters.First().Type.Name);// type is renamed
Assert.NotEmpty(requestBuilderClass.StartBlock.Usings);
var nodeUsing = requestBuilderClass.StartBlock.Usings.Where(static declaredUsing => declaredUsing.Name.Equals(KiotaBuilder.UntypedNodeName, StringComparison.OrdinalIgnoreCase)).ToArray();
Assert.Single(nodeUsing);
Assert.Equal("com.microsoft.kiota.serialization", nodeUsing[0].Declaration.Name);
}
#endregion
}
Original file line number Diff line number Diff line change
Expand Up @@ -1014,7 +1014,8 @@ public async Task AddsUsingForUntypedNodeInMethodParameterAsync()
{
Name = KiotaBuilder.UntypedNodeName,
IsExternal = true
}
},
Kind = CodeParameterKind.RequestBody
});
requestBuilderClass.AddMethod(method);
await ILanguageRefiner.RefineAsync(new GenerationConfiguration { Language = GenerationLanguage.TypeScript }, root);
Expand Down
Loading