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 1 commit
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
Next Next commit
Filter out properties that exist in base type from serializer/deseria…
…lizer methods in models
  • Loading branch information
Ndiritu committed Jul 14, 2022
commit 170fb7fff26e1a567b65a027bfe20c979d62a150
3 changes: 2 additions & 1 deletion src/Kiota.Builder/Writers/Php/CodeMethodWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,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 +401,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
2 changes: 1 addition & 1 deletion src/kiota/Properties/launchSettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"profiles": {
"kiota": {
"commandName": "Project",
"commandLineArgs": "--openapi C:\\src\\msgraph-sdk-powershell\\openApiDocs\\v1.0\\mail.yml -o C:\\Users\\darrmi\\source\\github\\darrelmiller\\OpenApiClient\\Generated -c GraphClient --loglevel Information"
"commandLineArgs": "--openapi https://raw.githubusercontent.com/microsoftgraph/msgraph-metadata/master/openapi/v1.0/openapi.yaml -l PHP -o C:\\Users\\pgichuhi\\projects\\Generated -c BaseGraphClient --log-level Information -n Microsoft\\Graph"
}
}
}
12 changes: 10 additions & 2 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"}, ExistsInBaseType = true}, "$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"}, ExistsInBaseType = true }, "'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