Skip to content

Commit

Permalink
Merge pull request #41 from microsoft/dev
Browse files Browse the repository at this point in the history
v0.6.0
  • Loading branch information
Ndiritu authored Jun 29, 2023
2 parents fb999ab + 1c50e49 commit 0eeb4e7
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 31 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/auto-merge-dependabot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jobs:
steps:
- name: Dependabot metadata
id: metadata
uses: dependabot/fetch-metadata@v1.5.1
uses: dependabot/fetch-metadata@v1.6.0
with:
github-token: "${{ secrets.GITHUB_TOKEN }}"

Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/pr-validation.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ jobs:
- name: Fix code coverage paths
run: sed -i 's@'$GITHUB_WORKSPACE'@/github/workspace/@g' coverage.xml
- name: SonarCloud Scan
if: ${{ matrix.php-versions == '8.0' }}
if: ${{ matrix.php-versions == '8.0' && !github.event.pull_request.head.repo.fork }}
uses: SonarSource/sonarcloud-github-action@master
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
}
},
"require": {
"microsoft/kiota-abstractions": "^0.7.0",
"microsoft/kiota-abstractions": "^0.8.0",
"guzzlehttp/psr7": "^1.6 || ^2",
"php": "^7.4 || ^8",
"ext-json": "*"
Expand Down
70 changes: 42 additions & 28 deletions src/JsonSerializationWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,43 +151,57 @@ public function writeCollectionOfObjectValues(?string $key, ?array $values): voi
}

/**
* @inheritDoc
* Serializes additional object values
*
* @param array<Parsable|null> $additionalValuesToMerge
* @return void
*/
public function writeObjectValue(?string $key, $value, Parsable ...$additionalValuesToMerge): void {
if ($value !== null || count($additionalValuesToMerge) > 0) {
if(!empty($key)) {
$this->writePropertyName($key);
private function writeAdditionalObjectValues(array $additionalValuesToMerge): void {
foreach ($additionalValuesToMerge as $additionalValueToMerge) {
if (is_null($additionalValueToMerge)) {
continue;
}
if ($this->getOnBeforeObjectSerialization() !== null) {
$this->getOnBeforeObjectSerialization()($value);
call_user_func($this->getOnBeforeObjectSerialization(), $additionalValueToMerge, $this);
}
$this->writer [] = '{';
if ($this->getOnStartObjectSerialization() !== null) {
$this->getOnStartObjectSerialization()($value, $this);
}
if ($value !== null) {
$value->serialize($this);
}
foreach ($additionalValuesToMerge as $additionalValueToMerge) {
if ($this->getOnBeforeObjectSerialization() !== null) {
call_user_func($this->getOnBeforeObjectSerialization(), $additionalValueToMerge, $this);
}
if ($this->getOnStartObjectSerialization() !== null) {
call_user_func($this->getOnStartObjectSerialization(), $additionalValueToMerge, $this);
}
$additionalValueToMerge->serialize($this);
if ($this->getOnAfterObjectSerialization() !== null) {
call_user_func($this->getOnAfterObjectSerialization(), $additionalValueToMerge);
}
}
if ($this->writer[count($this->writer) - 1] === ',') {
array_pop($this->writer);
call_user_func($this->getOnStartObjectSerialization(), $additionalValueToMerge, $this);
}
$additionalValueToMerge->serialize($this);
if ($this->getOnAfterObjectSerialization() !== null) {
$this->getOnAfterObjectSerialization()($value);
call_user_func($this->getOnAfterObjectSerialization(), $additionalValueToMerge);
}
$this->writer [] = '}';
}
}

/**
* @inheritDoc
*/
public function writeObjectValue(?string $key, $value, ?Parsable ...$additionalValuesToMerge): void {
if ($value == null && count($additionalValuesToMerge) === 0) {
return;
}
if(!empty($key)) {
$this->writePropertyName($key);
}
if ($this->getOnBeforeObjectSerialization() !== null) {
$this->getOnBeforeObjectSerialization()($value);
}
$this->writer [] = '{';
if ($this->getOnStartObjectSerialization() !== null) {
$this->getOnStartObjectSerialization()($value, $this);
}
if ($value !== null) {
$value->serialize($this);
}
$this->writeAdditionalObjectValues($additionalValuesToMerge);
if ($this->writer[count($this->writer) - 1] === ',') {
array_pop($this->writer);
}
if ($this->getOnAfterObjectSerialization() !== null) {
$this->getOnAfterObjectSerialization()($value);
}
$this->writer [] = '}';
if ($key !== null && $value !== null) {
$this->writer [] = self::PROPERTY_SEPARATOR;
}
Expand Down
21 changes: 21 additions & 0 deletions tests/JsonSerializationWriterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

use DateInterval;
use GuzzleHttp\Psr7\Utils;
use Microsoft\Kiota\Abstractions\Serialization\Parsable;
use Microsoft\Kiota\Abstractions\Serialization\ParseNode;
use Microsoft\Kiota\Abstractions\Serialization\SerializationWriter;
use Microsoft\Kiota\Abstractions\Types\Date;
use Microsoft\Kiota\Abstractions\Types\Time;
Expand Down Expand Up @@ -126,6 +128,25 @@ public function testWriteObjectValue(): void{
$this->assertEquals($expected, $actual);
}

public function testWriteIntersectionWrapperObjectValue(): void
{
$person1 = new Person();
$person1->setName("John");
$person1->setMaritalStatus(new MaritalStatus('single'));
$address = new Address();
$address->setCity('Nairobi');
$jsonSerializationWriter = new JsonSerializationWriter();
$beforeSerialization = fn (Parsable $n) => true;
$afterSerialization = fn (Parsable $n) => true;
$startSerialization = fn (Parsable $p, SerializationWriter $n) => true;
$jsonSerializationWriter->setOnBeforeObjectSerialization($beforeSerialization);
$jsonSerializationWriter->setOnAfterObjectSerialization($afterSerialization);
$jsonSerializationWriter->setOnStartObjectSerialization($startSerialization);
$jsonSerializationWriter->writeObjectValue("intersection", $person1, $address, null);
$expected = '"intersection":{"name":"John","maritalStatus":"single","city":"Nairobi"}';
$this->assertEquals($expected, $jsonSerializationWriter->getSerializedContent()->getContents());
}

public function testWriteEnumValue(): void{
$this->jsonSerializationWriter = new JsonSerializationWriter();
$this->jsonSerializationWriter->writeAnyValue("status", [new MaritalStatus('married'), new MaritalStatus('single')]);
Expand Down

0 comments on commit 0eeb4e7

Please sign in to comment.