Skip to content

Commit

Permalink
Update constructor setting of default values
Browse files Browse the repository at this point in the history
  • Loading branch information
Ndiritu committed Jul 13, 2022
1 parent 11853f5 commit 5658561
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
19 changes: 14 additions & 5 deletions src/Kiota.Builder/Writers/Php/CodeMethodWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down

0 comments on commit 5658561

Please sign in to comment.