From 5658561f1043c8c628b74f28c8414dc3e3a4f65b Mon Sep 17 00:00:00 2001 From: Philip Gichuhi Date: Wed, 13 Jul 2022 19:49:22 +0300 Subject: [PATCH] Update constructor setting of default values --- .../Writers/Php/CodeMethodWriter.cs | 19 ++++++++++++++----- .../Writers/Php/CodeMethodWriterTests.cs | 2 +- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/src/Kiota.Builder/Writers/Php/CodeMethodWriter.cs b/src/Kiota.Builder/Writers/Php/CodeMethodWriter.cs index 5ab3859fe6..4500878df9 100644 --- a/src/Kiota.Builder/Writers/Php/CodeMethodWriter.cs +++ b/src/Kiota.Builder/Writers/Php/CodeMethodWriter.cs @@ -70,13 +70,22 @@ public override void WriteCodeElement(CodeMethod codeElement, LanguageWriter wr private static void WriteConstructorBody(CodeClass parentClass, CodeMethod currentMethod, LanguageWriter writer, bool inherits) { if(inherits) writer.WriteLine("parent::__construct();"); - foreach(var propWithDefault in parentClass.Properties - .Where(x => !string.IsNullOrEmpty(x.DefaultValue)) - .OrderByDescending(x => x.Kind) - .ThenBy(x => x.Name)) + foreach(var propWithDefault in parentClass.GetPropertiesOfKind( + CodePropertyKind.BackingStore, + CodePropertyKind.RequestBuilder, + CodePropertyKind.UrlTemplate, + CodePropertyKind.PathParameters) + .Where(x => !string.IsNullOrEmpty(x.DefaultValue)) + .OrderByDescending(x => x.Kind) + .ThenBy(x => x.Name)) { var isPathSegment = propWithDefault.IsOfKind(CodePropertyKind.PathParameters); - writer.WriteLine($"$this->{propWithDefault.Name.ToFirstCharacterLowerCase()} = {(isPathSegment ? "[]" : propWithDefault.DefaultValue.ReplaceDoubleQuoteWithSingleQuote())};"); + writer.WriteLine($"$this->{propWithDefault.Name.ToFirstCharacterLowerCase()} = {(isPathSegment ? "[]" :propWithDefault.DefaultValue.ReplaceDoubleQuoteWithSingleQuote())};"); + } + foreach(var propWithDefault in parentClass.GetPropertiesOfKind(CodePropertyKind.AdditionalData, CodePropertyKind.Custom) //additional data and custom properties rely on accessors + .Where(x => !string.IsNullOrEmpty(x.DefaultValue)) + .OrderBy(x => x.Name)) { + writer.WriteLine($"$this->set{propWithDefault.SymbolName.ToFirstCharacterUpperCase()}({propWithDefault.DefaultValue.ReplaceDoubleQuoteWithSingleQuote()});"); } if(currentMethod.IsOfKind(CodeMethodKind.Constructor, CodeMethodKind.ClientConstructor)) { AssignPropertyFromParameter(parentClass, currentMethod, CodeParameterKind.RequestAdapter, CodePropertyKind.RequestAdapter, writer); diff --git a/tests/Kiota.Builder.Tests/Writers/Php/CodeMethodWriterTests.cs b/tests/Kiota.Builder.Tests/Writers/Php/CodeMethodWriterTests.cs index 27bbd115d4..641e87b994 100644 --- a/tests/Kiota.Builder.Tests/Writers/Php/CodeMethodWriterTests.cs +++ b/tests/Kiota.Builder.Tests/Writers/Php/CodeMethodWriterTests.cs @@ -593,7 +593,7 @@ public void WriteConstructorBody() var result = stringWriter.ToString(); Assert.Contains("public function __construct", result); - Assert.Contains("$this->type = '#microsoft.graph.entity'", result); + Assert.Contains("$this->setType('#microsoft.graph.entity')", result); } [Fact]