Skip to content

Commit

Permalink
fixes for untyped node parameter and return types present in python g…
Browse files Browse the repository at this point in the history
…eneration
  • Loading branch information
andrueastman committed Nov 8, 2024
1 parent 81a22a9 commit d326eef
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 10 deletions.
25 changes: 19 additions & 6 deletions src/Kiota.Builder/Refiners/CommonLanguageRefiner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1475,15 +1475,28 @@ protected static void RemoveRequestConfigurationClassesCommonProperties(CodeElem

CrawlTree(currentElement, x => RemoveRequestConfigurationClassesCommonProperties(x, baseTypeUsing));
}
protected static void RemoveUntypedNodePropertyValues(CodeElement currentElement)
protected static void RemoveUntypedNodeTypeValues(CodeElement currentElement)
{
if (currentElement is CodeProperty currentProperty
&& currentElement.Parent is CodeClass parentClass
&& currentProperty.Type.Name.Equals(KiotaBuilder.UntypedNodeName, StringComparison.OrdinalIgnoreCase))
switch (currentElement)
{
parentClass.RemoveChildElement(currentProperty);
case CodeProperty currentProperty when currentElement.Parent is CodeClass parentClass && currentProperty.Type.Name.Equals(KiotaBuilder.UntypedNodeName, StringComparison.OrdinalIgnoreCase):
parentClass.RemoveChildElement(currentProperty);
break;
case CodeMethod currentMethod when currentMethod.IsOfKind(CodeMethodKind.RequestExecutor):
if (currentMethod.ReturnType.Name.Equals(KiotaBuilder.UntypedNodeName, StringComparison.OrdinalIgnoreCase))
{
currentMethod.ReturnType = new CodeType { Name = "binary", IsExternal = true };
}
if (currentMethod.Parameters.Where(x => x.Kind is CodeParameterKind.RequestBody && x.Type.Name.Equals(KiotaBuilder.UntypedNodeName, StringComparison.OrdinalIgnoreCase)).ToList() is { Count: > 0 } parameters)
{
foreach (var parameter in parameters)
{
parameter.Type = new CodeType { Name = "binary", IsExternal = true };
}
}
break;
}
CrawlTree(currentElement, RemoveUntypedNodePropertyValues);
CrawlTree(currentElement, RemoveUntypedNodeTypeValues);
}
protected static void RemoveRequestConfigurationClasses(CodeElement currentElement, CodeUsing? configurationParameterTypeUsing = null, CodeType? defaultValueForGenericTypeParam = null, bool keepRequestConfigurationClass = false, bool addDeprecation = false, CodeUsing? usingForDefaultGenericParameter = null)
{
Expand Down
2 changes: 1 addition & 1 deletion src/Kiota.Builder/Refiners/PhpRefiner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public override Task RefineAsync(CodeNamespace generatedCode, CancellationToken
AddParsableImplementsForModelClasses(generatedCode, "Parsable");
AddRequestConfigurationConstructors(generatedCode);
AddDefaultImports(generatedCode, defaultUsingEvaluators);
RemoveUntypedNodePropertyValues(generatedCode);
RemoveUntypedNodeTypeValues(generatedCode);
AddCollectionValidationUtilImportToModels(generatedCode);
cancellationToken.ThrowIfCancellationRequested();
AddGetterAndSetterMethods(generatedCode,
Expand Down
2 changes: 1 addition & 1 deletion src/Kiota.Builder/Refiners/PythonRefiner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public override Task RefineAsync(CodeNamespace generatedCode, CancellationToken
);
CorrectCommonNames(generatedCode);
RemoveMethodByKind(generatedCode, CodeMethodKind.RawUrlConstructor);
RemoveUntypedNodePropertyValues(generatedCode);
RemoveUntypedNodeTypeValues(generatedCode);
DisableActionOf(generatedCode,
CodeParameterKind.RequestConfiguration);
MoveRequestBuilderPropertiesToBaseType(generatedCode,
Expand Down
2 changes: 1 addition & 1 deletion src/Kiota.Builder/Refiners/RubyRefiner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public override Task RefineAsync(CodeNamespace generatedCode, CancellationToken
cancellationToken.ThrowIfCancellationRequested();
AddParsableImplementsForModelClasses(generatedCode, "MicrosoftKiotaAbstractions::Parsable");
AddDefaultImports(generatedCode, defaultUsingEvaluators);
RemoveUntypedNodePropertyValues(generatedCode);
RemoveUntypedNodeTypeValues(generatedCode);
CorrectCoreType(generatedCode, CorrectMethodType, CorrectPropertyType, CorrectImplements);
cancellationToken.ThrowIfCancellationRequested();
ReplacePropertyNames(generatedCode,
Expand Down
2 changes: 1 addition & 1 deletion src/Kiota.Builder/Refiners/SwiftRefiner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,10 @@ public override Task RefineAsync(CodeNamespace generatedCode, CancellationToken
true,
false,
true);
RemoveUntypedNodeTypeValues(generatedCode);
AddDefaultImports(
generatedCode,
defaultUsingEvaluators);
RemoveUntypedNodePropertyValues(generatedCode);
cancellationToken.ThrowIfCancellationRequested();
CorrectCoreType(
generatedCode,
Expand Down
29 changes: 29 additions & 0 deletions tests/Kiota.Builder.Tests/Refiners/PythonLanguageRefinerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -584,5 +584,34 @@ public async Task AddsPropertiesAndMethodTypesImportsPythonAsync()
Assert.Single(requestBuilder.Methods, x => x.IsOfKind(CodeMethodKind.RequestExecutor));
Assert.DoesNotContain("QueryParameters", declaration.Usings.Select(x => x.Name));
}
[Fact]
public async Task ReplacesUntypedNodeInMethodParameterAndReturnTypeAsync()
{
var requestBuilderClass = root.AddClass(new CodeClass() { Name = "NodeRequestBuilder" }).First();
var method = new CodeMethod
{
Name = "getAsync",
ReturnType = new CodeType()
{
Name = KiotaBuilder.UntypedNodeName,//Returns untyped node
IsExternal = true
},
Kind = CodeMethodKind.RequestExecutor
};
method.AddParameter(new CodeParameter()
{
Name = "jsonData",
Type = new CodeType()
{
Name = KiotaBuilder.UntypedNodeName, //Has untyped node parameter
IsExternal = true
},
Kind = CodeParameterKind.RequestBody
});
requestBuilderClass.AddMethod(method);
await ILanguageRefiner.RefineAsync(new GenerationConfiguration { Language = GenerationLanguage.Python }, root);
Assert.Equal("bytes", method.Parameters.First().Type.Name);// type is renamed to use the stream type
Assert.Equal("bytes", method.ReturnType.Name);// return type is renamed to use the stream type
}
#endregion
}

0 comments on commit d326eef

Please sign in to comment.