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

Remove stringify from primitive types #5616

Merged
merged 36 commits into from
Oct 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
ed8cf85
Remove stringify from primitive types
shemogumbe Oct 16, 2024
7b65a27
clean up code
shemogumbe Oct 16, 2024
f43e99a
Merge branch 'main' into shem/remove_stringify_primitive_types
shemogumbe Oct 16, 2024
9b378b0
revert changes in launch.json file
shemogumbe Oct 16, 2024
2d3f071
Merge branch 'shem/remove_stringify_primitive_types' of https://githu…
shemogumbe Oct 16, 2024
64aa774
Updated changelog"
shemogumbe Oct 16, 2024
35b68a0
Merge branch 'main' into shem/remove_stringify_primitive_types
shemogumbe Oct 16, 2024
06da46e
move changelog to unrealeased
shemogumbe Oct 21, 2024
9c7eebf
Merge branch 'shem/remove_stringify_primitive_types' of https://githu…
shemogumbe Oct 21, 2024
3ee244b
update method writer tests
shemogumbe Oct 21, 2024
d22f203
Merge branch 'main' into shem/remove_stringify_primitive_types
shemogumbe Oct 21, 2024
26362a7
update code format
shemogumbe Oct 21, 2024
f745664
Merge branch 'shem/remove_stringify_primitive_types' of https://githu…
shemogumbe Oct 21, 2024
f419ecf
update code format
shemogumbe Oct 21, 2024
b0ac2ba
remove unused parameter
shemogumbe Oct 21, 2024
29cf0cf
update unit tests
shemogumbe Oct 21, 2024
8771a49
updete tests
shemogumbe Oct 21, 2024
d9adf4e
Merge branch 'main' into shem/remove_stringify_primitive_types
shemogumbe Oct 22, 2024
16f934c
fix unit test
shemogumbe Oct 22, 2024
70bc899
Merge branch 'shem/remove_stringify_primitive_types' of https://githu…
shemogumbe Oct 22, 2024
a347783
update unit tests
shemogumbe Oct 22, 2024
657fbef
fix integration tests
shemogumbe Oct 22, 2024
449b7d9
update get factory for enums
shemogumbe Oct 22, 2024
4a52f9d
update uni test for enums
shemogumbe Oct 22, 2024
6acb5c2
update uni test for enums
shemogumbe Oct 22, 2024
b04f415
Merge branch 'main' into shem/remove_stringify_primitive_types
shemogumbe Oct 22, 2024
5fb4c9a
Update CHANGELOG.md
shemogumbe Oct 22, 2024
61c4387
update method to internal
shemogumbe Oct 22, 2024
49cdeac
Merge branch 'shem/remove_stringify_primitive_types' of https://githu…
shemogumbe Oct 22, 2024
5e584e6
Merge branch 'main' into shem/remove_stringify_primitive_types
shemogumbe Oct 23, 2024
efd1df1
Merge branch 'main' into shem/remove_stringify_primitive_types
shemogumbe Oct 24, 2024
c6bc2d7
Update tests/Kiota.Builder.Tests/Writers/Python/CodeMethodWriterTests.cs
shemogumbe Oct 24, 2024
bd2b196
Update CHANGELOG.md
shemogumbe Oct 24, 2024
aa7fbb4
Merge branch 'main' into shem/remove_stringify_primitive_types
shemogumbe Oct 24, 2024
9614f7b
remove uused vars
shemogumbe Oct 24, 2024
ff89d6f
docs: removes duplicated changelog entry
baywet Oct 24, 2024
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
40 changes: 32 additions & 8 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,10 @@
"request": "launch",
"preLaunchTask": "build",
"program": "${workspaceFolder}/src/kiota/bin/Debug/net8.0/kiota.dll",
"args": ["search", "microsoft"],
"args": [
"search",
"microsoft"
],
"cwd": "${workspaceFolder}/src/kiota",
"console": "internalConsole",
"stopAtEntry": false
Expand All @@ -207,7 +210,10 @@
"request": "launch",
"preLaunchTask": "build",
"program": "${workspaceFolder}/src/kiota/bin/Debug/net8.0/kiota.dll",
"args": ["search", "test"],
"args": [
"search",
"test"
],
"cwd": "${workspaceFolder}/src/kiota",
"console": "internalConsole",
"stopAtEntry": false
Expand Down Expand Up @@ -249,7 +255,11 @@
"request": "launch",
"preLaunchTask": "build",
"program": "${workspaceFolder}/src/kiota/bin/Debug/net8.0/kiota.dll",
"args": ["info", "-l", "CSharp"],
"args": [
"info",
"-l",
"CSharp"
],
"cwd": "${workspaceFolder}/src/kiota",
"console": "internalConsole",
"stopAtEntry": false
Expand All @@ -260,7 +270,11 @@
"request": "launch",
"preLaunchTask": "build",
"program": "${workspaceFolder}/src/kiota/bin/Debug/net8.0/kiota.dll",
"args": ["update", "-o", "${workspaceFolder}/samples"],
"args": [
"update",
"-o",
"${workspaceFolder}/samples"
],
"cwd": "${workspaceFolder}/src/kiota",
"console": "internalConsole",
"stopAtEntry": false
Expand All @@ -271,7 +285,10 @@
"request": "launch",
"preLaunchTask": "build",
"program": "${workspaceFolder}/src/kiota/bin/Debug/net8.0/kiota.dll",
"args": ["workspace", "migrate"],
"args": [
"workspace",
"migrate"
],
"cwd": "${workspaceFolder}/samples/msgraph-mail/dotnet",
"console": "internalConsole",
"stopAtEntry": false,
Expand All @@ -285,7 +302,10 @@
"request": "launch",
"preLaunchTask": "build",
"program": "${workspaceFolder}/src/kiota/bin/Debug/net8.0/kiota.dll",
"args": ["client", "generate"],
"args": [
"client",
"generate"
],
"cwd": "${workspaceFolder}/samples/msgraph-mail/dotnet",
"console": "internalConsole",
"stopAtEntry": false,
Expand Down Expand Up @@ -349,7 +369,11 @@
"request": "launch",
"preLaunchTask": "build",
"program": "${workspaceFolder}/src/kiota/bin/Debug/net8.0/kiota.dll",
"args": ["login", "github", "device"],
"args": [
"login",
"github",
"device"
],
"cwd": "${workspaceFolder}/src/kiota",
"console": "internalConsole",
"stopAtEntry": false
Expand All @@ -361,4 +385,4 @@
"processId": "${command:pickProcess}"
}
]
}
}
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added

### Changed
- Fixed python generation client serailization failure str being quoted as "str"
- Fixed Issue with primitive values being stringified in python python. [#5417](https://github.com/microsoft/kiota/issues/5417)

- Fixed an issue where multipart request content would be ignored if other unstructured content was present in the description. [#5638](https://github.com/microsoft/kiota/issues/5638)
- Fixed an issue where when generating Go code the deserializer for unions was using `CodeClass` as a filter and not `CodeInterface`. [#4844](https://github.com/microsoft/kiota/issues/4844)
Expand All @@ -32,6 +34,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Fixed cyclic dependencies in generated Go code. [#2834](https://github.com/microsoft/kiota/issues/2834)
- Fixed a bug where default output folder is created on plugin edit and generate commands. [#5510](https://github.com/microsoft/kiota/issues/5429)
- Changed GeneratedCode attribute applied when generating CSharp to only include the major version of Kiota. [#5489](https://github.com/microsoft/kiota/issues/5489)

- Fixed generating CSharp client displays clean hint regardless of whether --clean-output is already passed [#5576](https://github.com/microsoft/kiota/issues/5576)

## [1.19.0] - 2024-10-03
Expand Down
7 changes: 4 additions & 3 deletions src/Kiota.Builder/Writers/Python/CodeMethodWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
var requestParams = new RequestParams(requestBodyParam, requestConfigParam, requestContentType);
if (!codeElement.IsOfKind(CodeMethodKind.Setter) &&
!(codeElement.IsOfKind(CodeMethodKind.Constructor) && parentClass.IsOfKind(CodeClassKind.RequestBuilder)))
foreach (var parameter in codeElement.Parameters.Where(static x => !x.Optional).OrderBy(static x => x.Name))

Check warning on line 44 in src/Kiota.Builder/Writers/Python/CodeMethodWriter.cs

View workflow job for this annotation

GitHub Actions / Build

Loop should be simplified by calling Select(parameter => parameter.Name)) (https://rules.sonarsource.com/csharp/RSPEC-3267)
{
var parameterName = parameter.Name;
writer.StartBlock($"if {parameterName} is None:");
Expand Down Expand Up @@ -113,7 +113,7 @@
break;
}
}
private void WriteRawUrlBuilderBody(CodeClass parentClass, CodeMethod codeElement, LanguageWriter writer)

Check warning on line 116 in src/Kiota.Builder/Writers/Python/CodeMethodWriter.cs

View workflow job for this annotation

GitHub Actions / Build

Make 'WriteRawUrlBuilderBody' a static method. (https://rules.sonarsource.com/csharp/RSPEC-2325)
{
var rawUrlParameter = codeElement.Parameters.OfKind(CodeParameterKind.RawUrl) ?? throw new InvalidOperationException("RawUrlBuilder method should have a RawUrl parameter");
var requestAdapterProperty = parentClass.GetPropertyOfKind(CodePropertyKind.RequestAdapter) ?? throw new InvalidOperationException("RawUrlBuilder method should have a RequestAdapter property");
Expand All @@ -136,7 +136,7 @@
writer.WriteLine($"return {parentClass.Name}()");
}
private const string ResultVarName = "result";
private void WriteFactoryMethodBodyForUnionModel(CodeMethod codeElement, CodeClass parentClass, CodeParameter parseNodeParameter, LanguageWriter writer)

Check warning on line 139 in src/Kiota.Builder/Writers/Python/CodeMethodWriter.cs

View workflow job for this annotation

GitHub Actions / Build

Refactor this method to reduce its Cognitive Complexity from 20 to the 15 allowed. (https://rules.sonarsource.com/csharp/RSPEC-3776)
{
writer.WriteLine($"{ResultVarName} = {parentClass.Name}()");
var includeElse = false;
Expand Down Expand Up @@ -165,7 +165,7 @@
}
writer.WriteLine($"return {ResultVarName}");
}
private void WriteFactoryMethodBodyForIntersectionModel(CodeMethod codeElement, CodeClass parentClass, CodeParameter parseNodeParameter, LanguageWriter writer)

Check warning on line 168 in src/Kiota.Builder/Writers/Python/CodeMethodWriter.cs

View workflow job for this annotation

GitHub Actions / Build

Refactor this method to reduce its Cognitive Complexity from 17 to the 15 allowed. (https://rules.sonarsource.com/csharp/RSPEC-3776)
{
writer.WriteLine($"{ResultVarName} = {parentClass.Name}()");
var includeElse = false;
Expand Down Expand Up @@ -326,7 +326,7 @@
return _SetterAccessProperties;
}
}
private void WriteConstructorBody(CodeClass parentClass, CodeMethod currentMethod, LanguageWriter writer, bool inherits)

Check warning on line 329 in src/Kiota.Builder/Writers/Python/CodeMethodWriter.cs

View workflow job for this annotation

GitHub Actions / Build

Refactor this method to reduce its Cognitive Complexity from 28 to the 15 allowed. (https://rules.sonarsource.com/csharp/RSPEC-3776)
{
if (inherits && !parentClass.IsOfKind(CodeClassKind.Model))
{
Expand Down Expand Up @@ -375,7 +375,7 @@
writer.IncreaseIndent();
}
}
private void WriteDirectAccessProperties(CodeClass parentClass, LanguageWriter writer)

Check warning on line 378 in src/Kiota.Builder/Writers/Python/CodeMethodWriter.cs

View workflow job for this annotation

GitHub Actions / Build

Refactor this method to reduce its Cognitive Complexity from 20 to the 15 allowed. (https://rules.sonarsource.com/csharp/RSPEC-3776)
{
foreach (var propWithDefault in parentClass.GetPropertiesOfKind(DirectAccessProperties)
.Where(static x => !string.IsNullOrEmpty(x.DefaultValue) && !NoneKeyword.Equals(x.DefaultValue, StringComparison.Ordinal))
Expand All @@ -392,7 +392,7 @@
conventions.WriteInLineDescription(propWithDefault, writer);
if (parentClass.IsOfKind(CodeClassKind.Model))
{
writer.WriteLine($"{propWithDefault.Name}: {(propWithDefault.Type.IsNullable ? "Optional[" : string.Empty)}{returnType}{(propWithDefault.Type.IsNullable ? "]" : string.Empty)} = {defaultValue}");

Check warning on line 395 in src/Kiota.Builder/Writers/Python/CodeMethodWriter.cs

View workflow job for this annotation

GitHub Actions / Build

Define a constant instead of using this literal 'Optional[' 7 times. (https://rules.sonarsource.com/csharp/RSPEC-1192)
writer.WriteLine();
}
else
Expand Down Expand Up @@ -429,7 +429,7 @@
}
}
private const string NoneKeyword = "None";
private void WriteSetterAccessPropertiesWithoutDefaults(CodeClass parentClass, LanguageWriter writer)

Check warning on line 432 in src/Kiota.Builder/Writers/Python/CodeMethodWriter.cs

View workflow job for this annotation

GitHub Actions / Build

Refactor this method to reduce its Cognitive Complexity from 17 to the 15 allowed. (https://rules.sonarsource.com/csharp/RSPEC-3776)
{
foreach (var propWithoutDefault in parentClass.GetPropertiesOfKind(SetterAccessProperties)
.Where(static x => string.IsNullOrEmpty(x.DefaultValue) || NoneKeyword.Equals(x.DefaultValue, StringComparison.Ordinal))
Expand Down Expand Up @@ -501,7 +501,7 @@
else
WriteDeserializerBodyForInheritedModel(inherits, codeElement, parentClass, writer);
}
private void WriteDeserializerBodyForUnionModel(CodeMethod method, CodeClass parentClass, LanguageWriter writer)

Check warning on line 504 in src/Kiota.Builder/Writers/Python/CodeMethodWriter.cs

View workflow job for this annotation

GitHub Actions / Build

Make 'WriteDeserializerBodyForUnionModel' a static method. (https://rules.sonarsource.com/csharp/RSPEC-2325)
{
foreach (var otherPropName in parentClass
.GetPropertiesOfKind(CodePropertyKind.Custom)
Expand All @@ -517,7 +517,7 @@
}
writer.WriteLine($"return {DefaultDeserializerValue}");
}
private void WriteDeserializerBodyForIntersectionModel(CodeClass parentClass, LanguageWriter writer)

Check warning on line 520 in src/Kiota.Builder/Writers/Python/CodeMethodWriter.cs

View workflow job for this annotation

GitHub Actions / Build

Make 'WriteDeserializerBodyForIntersectionModel' a static method. (https://rules.sonarsource.com/csharp/RSPEC-2325)
{
var complexProperties = parentClass.GetPropertiesOfKind(CodePropertyKind.Custom)
.Where(static x => x.Type is CodeType propType && propType.TypeDefinition is CodeClass && !x.Type.IsCollection)
Expand Down Expand Up @@ -583,7 +583,7 @@
var isStream = conventions.StreamTypeName.Equals(returnType, StringComparison.OrdinalIgnoreCase);
var returnTypeWithoutCollectionSymbol = GetReturnTypeWithoutCollectionSymbol(codeElement, returnType);
var genericTypeForSendMethod = GetSendRequestMethodName(isVoid, isStream, codeElement.ReturnType.IsCollection, returnTypeWithoutCollectionSymbol, isEnum);
var newFactoryParameter = GetTypeFactory(isVoid, isStream, returnTypeWithoutCollectionSymbol, isEnum);
var newFactoryParameter = GetTypeFactory(isVoid, isStream, isEnum, returnTypeWithoutCollectionSymbol);
var errorMappingVarName = NoneKeyword;
if (codeElement.ErrorMappings.Any())
{
Expand Down Expand Up @@ -809,10 +809,11 @@
_ => "write_object_value",
};
}
private string GetTypeFactory(bool isVoid, bool isStream, string returnType, bool isEnum)
internal string GetTypeFactory(bool isVoid, bool isStream, bool isEnum, string returnType)
{
if (isVoid) return string.Empty;
if (isStream || conventions.IsPrimitiveType(returnType) || isEnum) return $" \"{returnType}\",";
if (isStream || isEnum) return $" \"{returnType}\",";
if (conventions.IsPrimitiveType(returnType)) return $" {returnType},";
return $" {returnType},";
}
private string GetSendRequestMethodName(bool isVoid, bool isStream, bool isCollection, string returnType,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
using System;
using System.IO;
using System.Linq;

using Kiota.Builder.CodeDOM;
using Kiota.Builder.Extensions;
using Kiota.Builder.Writers;
using Kiota.Builder.Writers.Python;
using Moq;
using Xunit;

namespace Kiota.Builder.Tests.Writers.Python;
Expand All @@ -25,12 +25,15 @@ public sealed class CodeMethodWriterTests : IDisposable
private const string MethodDescription = "some description";
private const string ParamDescription = "some parameter description";
private const string ParamName = "param_name";


public CodeMethodWriterTests()
{
writer = LanguageWriter.GetLanguageWriter(GenerationLanguage.Python, DefaultPath, DefaultName);
tw = new StringWriter();
writer.SetTextWriter(tw);
root = CodeNamespace.InitRootNamespace();

}
private void setup(bool withInheritance = false)
{
Expand Down Expand Up @@ -791,6 +794,7 @@ public void WritesInheritedDeSerializerBody()
Assert.Contains("return fields", result);
Assert.DoesNotContain("defined_in_parent", result, StringComparison.OrdinalIgnoreCase);
}

[Fact]
public void WritesUnionDeSerializerBody()
{
Expand All @@ -815,6 +819,27 @@ public void WritesUnionDeSerializerBody()
Assert.Contains("return self.complex_type1_value.get_field_deserializers()", result);
Assert.Contains("return {}", result);
}
[Theory]
[InlineData(true, false, false, "string", "")]
[InlineData(false, true, false, "Stream", " \"Stream\",")]
[InlineData(false, false, true, "SomeEnum", " \"SomeEnum\",")]
[InlineData(false, false, false, "int", " int,")]
[InlineData(false, false, false, "CustomType", " CustomType,")]
public void GetTypeFactory_ReturnsCorrectString(bool isVoid, bool isStream, bool isEnum, string returnType, string expected)
{
var mockConventionService = new Mock<PythonConventionService>();

var codeMethodWriter = new CodeMethodWriter(
mockConventionService.Object,
"TestNamespace",
false // usesBackingStore
);

var result = codeMethodWriter.GetTypeFactory(isVoid, isStream, isEnum, returnType);


Assert.Equal(expected, result);
}
[Fact]
public void WritesIntersectionDeSerializerBody()
{
Expand Down
Loading