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

PHP: Multi-definition trimming and property defaults #1737

Merged
merged 8 commits into from
Jul 15, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- Fixed a bug where collections types would generate invalid return types in CSharp.
- Fixed a bug where a nullable entry in anyOf schemas would create unnecessary composed types.
- Removed duplicate properties defined in base types from model serialization and deserialization methods and initialise property defaults in constructor. [#1737](https://github.com/microsoft/kiota/pull/1737)

## [0.3.0] - 2022-07-08

Expand Down
24 changes: 13 additions & 11 deletions src/Kiota.Builder/Writers/Php/CodeMethodWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,21 +71,22 @@ private static void WriteConstructorBody(CodeClass parentClass, CodeMethod curre
if(inherits)
writer.WriteLine("parent::__construct();");
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))
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.NamePrefix}{propWithDefault.Name.ToFirstCharacterLowerCase()} = {(isPathSegment ? "[]" :propWithDefault.DefaultValue.ReplaceDoubleQuoteWithSingleQuote())};");
baywet marked this conversation as resolved.
Show resolved Hide resolved
writer.WriteLine($"$this->{propWithDefault.Name.ToFirstCharacterLowerCase()} = {(isPathSegment ? "[]" :propWithDefault.DefaultValue.ReplaceDoubleQuoteWithSingleQuote())};");
}
foreach(var propWithDefault in parentClass.GetPropertiesOfKind(CodePropertyKind.AdditionalData) //additional data and backing Store rely on accessors
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->{propWithDefault.Name.ToFirstCharacterLowerCase()} = {propWithDefault.DefaultValue};");
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 Expand Up @@ -238,7 +239,7 @@ private void WriteSerializerBody(CodeMethod codeMethod, CodeClass parentClass, L
if(inherits && implementsParsable)
writer.WriteLine($"parent::serialize({writerParameterName});");
var customProperties = parentClass.GetPropertiesOfKind(CodePropertyKind.Custom);
foreach(var otherProp in customProperties) {
foreach(var otherProp in customProperties.Where(x => !x.ExistsInBaseType)) {
writer.WriteLine($"{writerParameterName}->{GetSerializationMethodName(otherProp.Type)}('{otherProp.SerializationName ?? otherProp.Name.ToFirstCharacterLowerCase()}', $this->{otherProp.Name.ToFirstCharacterLowerCase()});");
}
if(additionalDataProperty != null)
Expand Down Expand Up @@ -401,6 +402,7 @@ private void WriteDeserializerBody(CodeClass parentClass, LanguageWriter writer,
if(fieldToSerialize.Any()) {
writer.IncreaseIndent();
fieldToSerialize
.Where(x => !x.ExistsInBaseType)
.OrderBy(x => x.Name)
.Select(x =>
$"'{x.SerializationName ?? x.Name.ToFirstCharacterLowerCase()}' => function (ParseNode $n) use ({currentObjectName}) {{ {currentObjectName}->{x.Setter.Name.ToFirstCharacterLowerCase()}({GetDeserializationMethodName(x.Type, method)}); }},")
Expand Down
24 changes: 20 additions & 4 deletions tests/Kiota.Builder.Tests/Writers/Php/CodeMethodWriterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +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"}, OriginalPropertyFromBaseType = new CodeProperty{}}, "$write->writeStringValue('definedInParent', $this->definedInParent);"}
};

[Theory]
Expand Down Expand Up @@ -233,7 +234,10 @@ public void WriteSerializer(CodeProperty property, string expected)
_codeMethodWriter.WriteCodeElement(codeMethod, languageWriter);
var result = stringWriter.ToString();
Assert.Contains("public function serialize(SerializationWriter $writer)", result);
Assert.Contains(expected, stringWriter.ToString());
if (property.ExistsInBaseType)
Assert.DoesNotContain(expected, result);
else
Assert.Contains(expected, stringWriter.ToString());
}

[Fact]
Expand Down Expand Up @@ -479,6 +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"}, OriginalPropertyFromBaseType = new CodeProperty() }, "'definedInParent' => function (ParseNode $n) use ($o) { $o->setDefinedInParent($n->getStringValue())"}
};
private static CodeClass GetParentClassInStaticContext()
{
Expand Down Expand Up @@ -509,7 +514,10 @@ public void WriteDeserializer(CodeProperty property, string expected)
parentClass.AddProperty(property);
_refiner.Refine(parentClass.Parent as CodeNamespace);
languageWriter.Write(deserializerMethod);
Assert.Contains(expected, stringWriter.ToString());
if (property.ExistsInBaseType)
Assert.DoesNotContain(expected, stringWriter.ToString());
else
Assert.Contains(expected, stringWriter.ToString());
}

[Fact]
Expand Down Expand Up @@ -571,13 +579,21 @@ public void WriteConstructorBody()
ReturnType = new CodeType() {Name = "void"},
Kind = CodeMethodKind.Constructor
};
var closingClass = parentClass;
parentClass.AddMethod(constructor);


var propWithDefaultValue = new CodeProperty()
{
Name = "type",
DefaultValue = "\"#microsoft.graph.entity\"",
Kind = CodePropertyKind.Custom
};
parentClass.AddProperty(propWithDefaultValue);

_codeMethodWriter.WriteCodeElement(constructor, languageWriter);
var result = stringWriter.ToString();

Assert.Contains("public function __construct", result);
Assert.Contains("$this->setType('#microsoft.graph.entity')", result);
}

[Fact]
Expand Down