diff --git a/.github/workflows/auto-merge-dependabot.yml b/.github/workflows/auto-merge-dependabot.yml index 86df420..6e5953f 100644 --- a/.github/workflows/auto-merge-dependabot.yml +++ b/.github/workflows/auto-merge-dependabot.yml @@ -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 }}" diff --git a/.github/workflows/pr-validation.yml b/.github/workflows/pr-validation.yml index 7ad1781..544d8b5 100644 --- a/.github/workflows/pr-validation.yml +++ b/.github/workflows/pr-validation.yml @@ -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 }} diff --git a/composer.json b/composer.json index aa66465..e8970b2 100644 --- a/composer.json +++ b/composer.json @@ -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": "*" diff --git a/src/JsonSerializationWriter.php b/src/JsonSerializationWriter.php index 1231a88..dd19581 100644 --- a/src/JsonSerializationWriter.php +++ b/src/JsonSerializationWriter.php @@ -151,43 +151,57 @@ public function writeCollectionOfObjectValues(?string $key, ?array $values): voi } /** - * @inheritDoc + * Serializes additional object values + * + * @param array $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; } diff --git a/tests/JsonSerializationWriterTest.php b/tests/JsonSerializationWriterTest.php index 8a6bca5..c4832d5 100644 --- a/tests/JsonSerializationWriterTest.php +++ b/tests/JsonSerializationWriterTest.php @@ -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; @@ -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')]);