From e4d0c383694dcb2c1e81177298f5a502842b7dc6 Mon Sep 17 00:00:00 2001 From: Silas Kenneth Date: Wed, 29 May 2024 15:26:06 +0300 Subject: [PATCH 1/5] Fix for scalar member composed types. --- src/Kiota.Builder/Refiners/PhpRefiner.cs | 38 ++++++++++--------- .../Writers/Php/CodeMethodWriter.cs | 4 +- 2 files changed, 23 insertions(+), 19 deletions(-) diff --git a/src/Kiota.Builder/Refiners/PhpRefiner.cs b/src/Kiota.Builder/Refiners/PhpRefiner.cs index c861b2405e..19ccb59918 100644 --- a/src/Kiota.Builder/Refiners/PhpRefiner.cs +++ b/src/Kiota.Builder/Refiners/PhpRefiner.cs @@ -14,6 +14,7 @@ public class PhpRefiner : CommonLanguageRefiner private static readonly CodeUsingDeclarationNameComparer usingComparer = new(); public PhpRefiner(GenerationConfiguration configuration) : base(configuration) { } + private const string AbstractionsNamespaceName = @"Microsoft\Kiota\Abstractions"; public override Task Refine(CodeNamespace generatedCode, CancellationToken cancellationToken) { @@ -31,7 +32,7 @@ public override Task Refine(CodeNamespace generatedCode, CancellationToken cance Name = "BaseRequestBuilder", Declaration = new CodeType { - Name = "Microsoft\\Kiota\\Abstractions", + Name = $@"{AbstractionsNamespaceName}\Serialization", IsExternal = true } }, AccessModifier.Public); @@ -45,7 +46,10 @@ public override Task Refine(CodeNamespace generatedCode, CancellationToken cance ConvertUnionTypesToWrapper(generatedCode, _configuration.UsesBackingStore, static s => s, - false); + false, + AbstractionsNamespaceName, + "ComposedTypeWrapper" + ); ReplaceReservedNames(generatedCode, new PhpReservedNamesProvider(), reservedWord => $"Escaped{reservedWord.ToFirstCharacterUpperCase()}", new HashSet { typeof(CodeEnumOption) }); AddQueryParameterFactoryMethod(generatedCode); AddPrimaryErrorMessage(generatedCode, "getPrimaryErrorMessage", () => new CodeType { IsExternal = true, IsNullable = false, Name = "string" }); @@ -143,7 +147,7 @@ public override Task Refine(CodeNamespace generatedCode, CancellationToken cance Name = "Date", Declaration = new CodeType { - Name = "Microsoft\\Kiota\\Abstractions\\Types", + Name = $@"{AbstractionsNamespaceName}\Types", IsExternal = true, }, }) @@ -154,7 +158,7 @@ public override Task Refine(CodeNamespace generatedCode, CancellationToken cance Name = "Time", Declaration = new CodeType { - Name = "Microsoft\\Kiota\\Abstractions\\Types", + Name = $@"{AbstractionsNamespaceName}\Types", IsExternal = true, }, }) @@ -184,34 +188,34 @@ public override Task Refine(CodeNamespace generatedCode, CancellationToken cance }; private static readonly AdditionalUsingEvaluator[] defaultUsingEvaluators = { new (static x => x is CodeProperty prop && prop.IsOfKind(CodePropertyKind.RequestAdapter), - "Microsoft\\Kiota\\Abstractions", "RequestAdapter"), + AbstractionsNamespaceName, "RequestAdapter"), new (static x => x is CodeMethod method && method.IsOfKind(CodeMethodKind.RequestGenerator), - "Microsoft\\Kiota\\Abstractions", "HttpMethod", "RequestInformation"), + AbstractionsNamespaceName, "HttpMethod", "RequestInformation"), new (static x => x is CodeClass @class && @class.IsOfKind(CodeClassKind.Model) && @class.Properties.Any(static y => y.IsOfKind(CodePropertyKind.AdditionalData)), - "Microsoft\\Kiota\\Abstractions\\Serialization", "AdditionalDataHolder"), + $@"{AbstractionsNamespaceName}\Serialization", "AdditionalDataHolder"), new (static x => x is CodeMethod method && method.IsOfKind(CodeMethodKind.Serializer), - "Microsoft\\Kiota\\Abstractions\\Serialization", "SerializationWriter"), + $@"{AbstractionsNamespaceName}\Serialization", "SerializationWriter"), new (static x => x is CodeMethod method && method.IsOfKind(CodeMethodKind.Deserializer), - "Microsoft\\Kiota\\Abstractions\\Serialization", "ParseNode"), + $@"{AbstractionsNamespaceName}\Serialization", "ParseNode"), new (static x => x is CodeClass @class && @class.IsOfKind(CodeClassKind.Model), - "Microsoft\\Kiota\\Abstractions\\Serialization", "Parsable"), + $@"{AbstractionsNamespaceName}\Serialization", "Parsable"), new (static x => x is CodeMethod method && method.IsOfKind(CodeMethodKind.ClientConstructor) && method.Parameters.Any(y => y.IsOfKind(CodeParameterKind.BackingStore)), - "Microsoft\\Kiota\\Abstractions\\Store", "BackingStoreFactory", "BackingStoreFactorySingleton"), + $@"{AbstractionsNamespaceName}\Store", "BackingStoreFactory", "BackingStoreFactorySingleton"), new (static x => x is CodeProperty prop && prop.IsOfKind(CodePropertyKind.BackingStore), - "Microsoft\\Kiota\\Abstractions\\Store", "BackingStore", "BackedModel", "BackingStoreFactorySingleton" ), + $@"{AbstractionsNamespaceName}\Store", "BackingStore", "BackedModel", "BackingStoreFactorySingleton" ), new (static x => x is CodeMethod method && method.IsOfKind(CodeMethodKind.RequestExecutor), "Http\\Promise", "Promise"), new (static x => x is CodeMethod method && method.IsOfKind(CodeMethodKind.RequestExecutor), "", "Exception"), - new (static x => x is CodeEnum, "Microsoft\\Kiota\\Abstractions\\", "Enum"), + new (static x => x is CodeEnum, AbstractionsNamespaceName, "Enum"), new(static x => x is CodeProperty {Type.Name: {}} property && property.Type.Name.Equals("DateTime", StringComparison.OrdinalIgnoreCase), "", "\\DateTime"), new(static x => x is CodeProperty {Type.Name: {}} property && property.Type.Name.Equals("DateTimeOffset", StringComparison.OrdinalIgnoreCase), "", "\\DateTime"), - new(static x => x is CodeMethod method && method.IsOfKind(CodeMethodKind.ClientConstructor), "Microsoft\\Kiota\\Abstractions", "ApiClientBuilder"), + new(static x => x is CodeMethod method && method.IsOfKind(CodeMethodKind.ClientConstructor), AbstractionsNamespaceName, "ApiClientBuilder"), new(static x => x is CodeProperty property && property.IsOfKind(CodePropertyKind.QueryParameter) && !string.IsNullOrEmpty(property.SerializationName), "Microsoft\\Kiota\\Abstractions", "QueryParameter"), - new(static x => x is CodeClass codeClass && codeClass.IsOfKind(CodeClassKind.RequestConfiguration), "Microsoft\\Kiota\\Abstractions", "RequestOption"), + new(static x => x is CodeClass codeClass && codeClass.IsOfKind(CodeClassKind.RequestConfiguration), AbstractionsNamespaceName, "RequestOption"), new (static x => x is CodeClass { OriginalComposedType: CodeIntersectionType intersectionType } && intersectionType.Types.Any(static y => !y.IsExternal), - "Microsoft\\Kiota\\Abstractions\\Serialization", "ParseNodeHelper"), + $@"{AbstractionsNamespaceName}\Serialization", "ParseNodeHelper"), 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)), - @"Microsoft\Kiota\Abstractions", MultipartBodyClassName) + AbstractionsNamespaceName, MultipartBodyClassName) }; private const string MultipartBodyClassName = "MultiPartBody"; diff --git a/src/Kiota.Builder/Writers/Php/CodeMethodWriter.cs b/src/Kiota.Builder/Writers/Php/CodeMethodWriter.cs index 4a96bd3eec..7aea844e06 100644 --- a/src/Kiota.Builder/Writers/Php/CodeMethodWriter.cs +++ b/src/Kiota.Builder/Writers/Php/CodeMethodWriter.cs @@ -855,7 +855,7 @@ private void WriteModelFactoryMethodBody(CodeMethod codeElement, CodeClass paren { if (parentClass.DiscriminatorInformation.ShouldWriteDiscriminatorForUnionType || parentClass.DiscriminatorInformation.ShouldWriteDiscriminatorForIntersectionType) writer.WriteLine($"{ResultVarName} = new {parentClass.Name.ToFirstCharacterUpperCase()}();"); - var writeDiscriminatorValueRead = parentClass.DiscriminatorInformation.ShouldWriteParseNodeCheck && !parentClass.DiscriminatorInformation.ShouldWriteDiscriminatorForIntersectionType; + var writeDiscriminatorValueRead = parentClass.DiscriminatorInformation is { ShouldWriteParseNodeCheck: true, ShouldWriteDiscriminatorForIntersectionType: false, HasBasicDiscriminatorInformation: true }; if (writeDiscriminatorValueRead && codeElement.Parameters.OfKind(CodeParameterKind.ParseNode) is CodeParameter parseNodeParameter) @@ -868,7 +868,7 @@ private void WriteModelFactoryMethodBody(CodeMethod codeElement, CodeClass paren if (parentClass.DiscriminatorInformation.ShouldWriteDiscriminatorForInheritedType) WriteFactoryMethodBodyForInheritedModel(parentClass.DiscriminatorInformation.DiscriminatorMappings, writer, codeElement); - else if (parentClass.DiscriminatorInformation.ShouldWriteDiscriminatorForUnionType && parentClass.DiscriminatorInformation.HasBasicDiscriminatorInformation) + else if (parentClass.DiscriminatorInformation is { ShouldWriteDiscriminatorForUnionType: true, HasBasicDiscriminatorInformation: true}) WriteFactoryMethodBodyForUnionModelForDiscriminatedTypes(codeElement, parentClass, writer); else if (parentClass.DiscriminatorInformation.ShouldWriteDiscriminatorForIntersectionType) WriteFactoryMethodBodyForIntersectionModel(codeElement, parentClass, writer); From 490289cb984db3370ea1d3c7d8cca1b380ab979c Mon Sep 17 00:00:00 2001 From: Silas Kenneth Date: Wed, 29 May 2024 15:35:24 +0300 Subject: [PATCH 2/5] Refactor to use constant. --- src/Kiota.Builder/Refiners/PhpRefiner.cs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/Kiota.Builder/Refiners/PhpRefiner.cs b/src/Kiota.Builder/Refiners/PhpRefiner.cs index 19ccb59918..d3b3c7f242 100644 --- a/src/Kiota.Builder/Refiners/PhpRefiner.cs +++ b/src/Kiota.Builder/Refiners/PhpRefiner.cs @@ -75,7 +75,7 @@ public override Task Refine(CodeNamespace generatedCode, CancellationToken cance AddParentClassToErrorClasses( generatedCode, "ApiException", - "Microsoft\\Kiota\\Abstractions" + AbstractionsNamespaceName ); MoveClassesWithNamespaceNamesUnderNamespace(generatedCode); AddConstructorsForDefaultValues(generatedCode, true); @@ -110,7 +110,7 @@ public override Task Refine(CodeNamespace generatedCode, CancellationToken cance } ); cancellationToken.ThrowIfCancellationRequested(); - AddSerializationModulesImport(generatedCode, ["Microsoft\\Kiota\\Abstractions\\ApiClientBuilder"], null, '\\'); + AddSerializationModulesImport(generatedCode, [$@"{AbstractionsNamespaceName}\ApiClientBuilder"], null, '\\'); cancellationToken.ThrowIfCancellationRequested(); AddPropertiesAndMethodTypesImports(generatedCode, true, false, true); CorrectBackingStoreSetterParam(generatedCode); @@ -123,7 +123,7 @@ public override Task Refine(CodeNamespace generatedCode, CancellationToken cance Name = "BaseRequestConfiguration", Declaration = new CodeType { - Name = "Microsoft\\Kiota\\Abstractions", + Name = AbstractionsNamespaceName, IsExternal = true } }); @@ -210,7 +210,7 @@ public override Task Refine(CodeNamespace generatedCode, CancellationToken cance new(static x => x is CodeProperty {Type.Name: {}} property && property.Type.Name.Equals("DateTime", StringComparison.OrdinalIgnoreCase), "", "\\DateTime"), new(static x => x is CodeProperty {Type.Name: {}} property && property.Type.Name.Equals("DateTimeOffset", StringComparison.OrdinalIgnoreCase), "", "\\DateTime"), new(static x => x is CodeMethod method && method.IsOfKind(CodeMethodKind.ClientConstructor), AbstractionsNamespaceName, "ApiClientBuilder"), - new(static x => x is CodeProperty property && property.IsOfKind(CodePropertyKind.QueryParameter) && !string.IsNullOrEmpty(property.SerializationName), "Microsoft\\Kiota\\Abstractions", "QueryParameter"), + new(static x => x is CodeProperty property && property.IsOfKind(CodePropertyKind.QueryParameter) && !string.IsNullOrEmpty(property.SerializationName), AbstractionsNamespaceName, "QueryParameter"), new(static x => x is CodeClass codeClass && codeClass.IsOfKind(CodeClassKind.RequestConfiguration), AbstractionsNamespaceName, "RequestOption"), new (static x => x is CodeClass { OriginalComposedType: CodeIntersectionType intersectionType } && intersectionType.Types.Any(static y => !y.IsExternal), $@"{AbstractionsNamespaceName}\Serialization", "ParseNodeHelper"), @@ -343,7 +343,7 @@ private void AddCollectionValidationUtilImportToModels(CodeElement codeElement) { if (codeElement is CodeClass codeClass && codeClass.Kind == CodeClassKind.Model) { - var typeUtilsUsing = new CodeUsing { Name = "TypeUtils", Declaration = new CodeType { Name = "Microsoft\\Kiota\\Abstractions\\Types", IsExternal = true } }; + var typeUtilsUsing = new CodeUsing { Name = "TypeUtils", Declaration = new CodeType { Name = $@"{AbstractionsNamespaceName}\Types", IsExternal = true } }; if (codeClass.Properties.Any(x => x.Kind == CodePropertyKind.Custom && x.Type is CodeType codeType From 13787c04e30b2d8e973ad3892e902a73b2991182 Mon Sep 17 00:00:00 2001 From: Silas Kenneth Date: Wed, 29 May 2024 15:44:14 +0300 Subject: [PATCH 3/5] Fix formating issue and wrong import. --- src/Kiota.Builder/Refiners/PhpRefiner.cs | 4 ++-- src/Kiota.Builder/Writers/Php/CodeMethodWriter.cs | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Kiota.Builder/Refiners/PhpRefiner.cs b/src/Kiota.Builder/Refiners/PhpRefiner.cs index d3b3c7f242..95985de135 100644 --- a/src/Kiota.Builder/Refiners/PhpRefiner.cs +++ b/src/Kiota.Builder/Refiners/PhpRefiner.cs @@ -32,7 +32,7 @@ public override Task Refine(CodeNamespace generatedCode, CancellationToken cance Name = "BaseRequestBuilder", Declaration = new CodeType { - Name = $@"{AbstractionsNamespaceName}\Serialization", + Name = $@"{AbstractionsNamespaceName}", IsExternal = true } }, AccessModifier.Public); @@ -47,7 +47,7 @@ public override Task Refine(CodeNamespace generatedCode, CancellationToken cance _configuration.UsesBackingStore, static s => s, false, - AbstractionsNamespaceName, + $@"{AbstractionsNamespaceName}\Serialization", "ComposedTypeWrapper" ); ReplaceReservedNames(generatedCode, new PhpReservedNamesProvider(), reservedWord => $"Escaped{reservedWord.ToFirstCharacterUpperCase()}", new HashSet { typeof(CodeEnumOption) }); diff --git a/src/Kiota.Builder/Writers/Php/CodeMethodWriter.cs b/src/Kiota.Builder/Writers/Php/CodeMethodWriter.cs index 7aea844e06..8b8c870165 100644 --- a/src/Kiota.Builder/Writers/Php/CodeMethodWriter.cs +++ b/src/Kiota.Builder/Writers/Php/CodeMethodWriter.cs @@ -868,7 +868,7 @@ private void WriteModelFactoryMethodBody(CodeMethod codeElement, CodeClass paren if (parentClass.DiscriminatorInformation.ShouldWriteDiscriminatorForInheritedType) WriteFactoryMethodBodyForInheritedModel(parentClass.DiscriminatorInformation.DiscriminatorMappings, writer, codeElement); - else if (parentClass.DiscriminatorInformation is { ShouldWriteDiscriminatorForUnionType: true, HasBasicDiscriminatorInformation: true}) + else if (parentClass.DiscriminatorInformation is { ShouldWriteDiscriminatorForUnionType: true, HasBasicDiscriminatorInformation: true }) WriteFactoryMethodBodyForUnionModelForDiscriminatedTypes(codeElement, parentClass, writer); else if (parentClass.DiscriminatorInformation.ShouldWriteDiscriminatorForIntersectionType) WriteFactoryMethodBodyForIntersectionModel(codeElement, parentClass, writer); From c1956fc9de559abdf84f6b60ecda83bce1a24458 Mon Sep 17 00:00:00 2001 From: Silas Kenneth Date: Tue, 4 Jun 2024 10:56:16 +0300 Subject: [PATCH 4/5] Add CHANGELOG entry. --- CHANGELOG.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a469a745b7..ad45f52ce8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -44,9 +44,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Fixes a bug where CLI Generation does not handle path parameters of type "string" and format "date", "date-time", "time", etc. [#4615](https://github.com/microsoft/kiota/issues/4615) - Fixes a bug where request executors would be missing Untyped parameters in dotnet [#4692](https://github.com/microsoft/kiota/issues/4692) - Fixes a bug where indexers in include/exclude patters were not normalized if the indexer was the last segment without a slash at the end [#4715](https://github.com/microsoft/kiota/issues/4715) -- Fixes a bug where CLI generation doesnot handle parameters of type string array. [#4707](https://github.com/microsoft/kiota/issues/4707) +- Fixes a bug where CLI generation doesn't handle parameters of type string array. [#4707](https://github.com/microsoft/kiota/issues/4707) - Fixed a bug where models would not be created when a multipart content schema existed with no encoding [#4734](https://github.com/microsoft/kiota/issues/4734) - Fixes a bug where warnings about discriminator not being inherited were generated [#4761](https://github.com/microsoft/kiota/issues/4761) +- Fix scalar member composed type serialization in PHP [#2827](https://github.com/issues/2827) ## [1.14.0] - 2024-05-02 From 7a98490c58a4abfeb075610b689a065aa9e902d8 Mon Sep 17 00:00:00 2001 From: Silas Kenneth Date: Tue, 4 Jun 2024 11:06:47 +0300 Subject: [PATCH 5/5] Fix issue link. --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ad45f52ce8..41b56cd3fc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -47,7 +47,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Fixes a bug where CLI generation doesn't handle parameters of type string array. [#4707](https://github.com/microsoft/kiota/issues/4707) - Fixed a bug where models would not be created when a multipart content schema existed with no encoding [#4734](https://github.com/microsoft/kiota/issues/4734) - Fixes a bug where warnings about discriminator not being inherited were generated [#4761](https://github.com/microsoft/kiota/issues/4761) -- Fix scalar member composed type serialization in PHP [#2827](https://github.com/issues/2827) +- Fix scalar member composed type serialization in PHP [#2827](https://github.com/microsoft/kiota/issues/2827) ## [1.14.0] - 2024-05-02