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 14, 2022
1 parent 9adfbc1 commit 49a33ed
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 8 deletions.
20 changes: 15 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,23 @@ 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)) {
var setterName = propWithDefault.SetterFromCurrentOrBaseType?.Name.ToFirstCharacterLowerCase() ?? $"set{propWithDefault.SymbolName.ToFirstCharacterUpperCase()}";
writer.WriteLine($"$this->{setterName}({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 @@ -203,7 +203,7 @@ public void WriteRequestExecutor()
new object[] { new CodeProperty { Name = "dateValue", Type = new CodeType { Name = "DateTime" }, Access = AccessModifier.Private}, "$writer->writeDateTimeValue('dateValue', $this->dateValue);" },
new object[] { new CodeProperty { Name = "duration", Type = new CodeType { Name = "duration" }, Access = AccessModifier.Private}, "$writer->writeDateIntervalValue('duration', $this->duration);" },
new object[] { new CodeProperty { Name = "stream", Type = new CodeType { Name = "binary" }, Access = AccessModifier.Private}, "$writer->writeBinaryContent('stream', $this->stream);" },
new object[] { new CodeProperty { Name = "definedInParent", Type = new CodeType { Name = "string"}, ExistsInBaseType = true}, "$write->writeStringValue('definedInParent', $this->definedInParent);"}
new object[] { new CodeProperty { Name = "definedInParent", Type = new CodeType { Name = "string"}, OriginalPropertyFromBaseType = new CodeProperty{}}, "$write->writeStringValue('definedInParent', $this->definedInParent);"}
};

[Theory]
Expand Down Expand Up @@ -483,7 +483,7 @@ public void WriteIndexerBody()
new object[] { new CodeProperty { Name = "years", Type = new CodeType { Name = "int", CollectionKind = CodeTypeBase.CodeTypeCollectionKind.Array }, Access = AccessModifier.Private},
"'years' => function (ParseNode $n) use ($o) { $o->setYears($n->getCollectionOfPrimitiveValues())"
},
new object[] { new CodeProperty{ Name = "definedInParent", Type = new CodeType { Name = "string"}, ExistsInBaseType = true }, "'definedInParent' => function (ParseNode $n) use ($o) { $o->setDefinedInParent($n->getStringValue())"}
new object[] { new CodeProperty{ Name = "definedInParent", Type = new CodeType { Name = "string"}, OriginalPropertyFromBaseType = new CodeProperty() }, "'definedInParent' => function (ParseNode $n) use ($o) { $o->setDefinedInParent($n->getStringValue())"}
};
private static CodeClass GetParentClassInStaticContext()
{
Expand Down 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 49a33ed

Please sign in to comment.