diff --git a/.github/policies/resourceManagement.yml b/.github/policies/resourceManagement.yml index 0f7b81e..dedbd45 100644 --- a/.github/policies/resourceManagement.yml +++ b/.github/policies/resourceManagement.yml @@ -16,7 +16,7 @@ configuration: - isIssue - isOpen - hasLabel: - label: 'Needs: Author Feedback' + label: 'status:waiting-for-author-feedback' - hasLabel: label: 'Status: No Recent Activity' - noActivitySince: @@ -31,7 +31,7 @@ configuration: - isIssue - isOpen - hasLabel: - label: 'Needs: Author Feedback' + label: 'status:waiting-for-author-feedback' - noActivitySince: days: 4 - isNotLabeledWith: @@ -64,13 +64,13 @@ configuration: - isActivitySender: issueAuthor: True - hasLabel: - label: 'Needs: Author Feedback' + label: 'status:waiting-for-author-feedback' - isOpen then: - addLabel: label: 'Needs: Attention :wave:' - removeLabel: - label: 'Needs: Author Feedback' + label: 'status:waiting-for-author-feedback' description: - if: - payloadType: Issues diff --git a/CHANGELOG.md b/CHANGELOG.md index 2c3311d..2757b91 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Changed +## [1.3.0] + +### Added + +### Changed +- fix(logic): Don't cast types since this might introduce some logical bugs. Make sure values match possible values for that type. +- fix(serialization): Fix how composed types are handled. + ## [1.0.1] ### Changed diff --git a/CODE_OF_CONDUCT.md b/CODE_OF_CONDUCT.md index f9ba8cf..686e5e7 100644 --- a/CODE_OF_CONDUCT.md +++ b/CODE_OF_CONDUCT.md @@ -7,3 +7,4 @@ Resources: - [Microsoft Open Source Code of Conduct](https://opensource.microsoft.com/codeofconduct/) - [Microsoft Code of Conduct FAQ](https://opensource.microsoft.com/codeofconduct/faq/) - Contact [opencode@microsoft.com](mailto:opencode@microsoft.com) with questions or concerns +- Employees can reach out at [aka.ms/opensource/moderation-support](https://aka.ms/opensource/moderation-support) diff --git a/README.md b/README.md index c3cb87a..de659c4 100644 --- a/README.md +++ b/README.md @@ -17,7 +17,7 @@ run `composer require microsoft/kiota-serialization-json` or add the following t ```Shell { "require": { - "microsoft/kiota-serialization-json": "^1.0.1" + "microsoft/kiota-serialization-json": "^1.3.0" } } ``` diff --git a/composer.json b/composer.json index c7646df..5764ee5 100644 --- a/composer.json +++ b/composer.json @@ -20,7 +20,7 @@ } }, "require": { - "microsoft/kiota-abstractions": "^1.3.1", + "microsoft/kiota-abstractions": "^1.4.0", "guzzlehttp/psr7": "^1.6 || ^2", "php": "^7.4 || ^8", "ext-json": "*" diff --git a/src/JsonParseNode.php b/src/JsonParseNode.php index b045744..bd1cda0 100644 --- a/src/JsonParseNode.php +++ b/src/JsonParseNode.php @@ -53,28 +53,28 @@ public function getChildNode(string $identifier): ?ParseNode { * @inheritDoc */ public function getStringValue(): ?string { - return $this->jsonNode !== null ? addcslashes(strval($this->jsonNode), "\\\r\n") : null; + return is_string($this->jsonNode) ? addcslashes($this->jsonNode, "\\\r\n") : null; } /** * @inheritDoc */ public function getBooleanValue(): ?bool { - return $this->jsonNode !== null ? (bool)$this->jsonNode : null; + return is_bool($this->jsonNode) ? $this->jsonNode : null; } /** * @inheritDoc */ public function getIntegerValue(): ?int { - return $this->jsonNode !== null ? intval($this->jsonNode) : null; + return is_int($this->jsonNode) ? $this->jsonNode : null; } /** * @inheritDoc */ public function getFloatValue(): ?float { - return $this->jsonNode !== null ? floatval($this->jsonNode) : null; + return is_float($this->jsonNode) ? $this->jsonNode : null; } /** diff --git a/src/JsonSerializationWriter.php b/src/JsonSerializationWriter.php index c798eb0..25197df 100644 --- a/src/JsonSerializationWriter.php +++ b/src/JsonSerializationWriter.php @@ -14,6 +14,7 @@ use Microsoft\Kiota\Abstractions\Types\Time; use Psr\Http\Message\StreamInterface; use stdClass; +use Microsoft\Kiota\Abstractions\Serialization\ComposedTypeWrapper; /** * @method onBeforeObjectSerialization(?Parsable $value); @@ -186,7 +187,11 @@ public function writeObjectValue(?string $key, $value, ?Parsable ...$additionalV if ($this->getOnBeforeObjectSerialization() !== null) { $this->getOnBeforeObjectSerialization()($value); } - $this->writer [] = '{'; + $isComposedType = $value instanceof ComposedTypeWrapper; + + if (!$isComposedType) { + $this->writer [] = '{'; + } if ($this->getOnStartObjectSerialization() !== null) { $this->getOnStartObjectSerialization()($value, $this); } @@ -200,7 +205,9 @@ public function writeObjectValue(?string $key, $value, ?Parsable ...$additionalV if ($this->getOnAfterObjectSerialization() !== null) { $this->getOnAfterObjectSerialization()($value); } - $this->writer [] = '}'; + if (!$isComposedType) { + $this->writer [] = '}'; + } if ($key !== null && $value !== null) { $this->writer [] = self::PROPERTY_SEPARATOR; } diff --git a/tests/JsonParseNodeTest.php b/tests/JsonParseNodeTest.php index ce63400..8c69ccf 100644 --- a/tests/JsonParseNodeTest.php +++ b/tests/JsonParseNodeTest.php @@ -29,9 +29,11 @@ protected function setUp(): void { } public function testGetIntegerValue(): void { - $this->parseNode = new JsonParseNode('1243.78'); + $this->parseNode = new JsonParseNode(1243.78); $expected = $this->parseNode->getIntegerValue(); - $this->assertEquals(1243, $expected); + $this->assertEquals(null, $expected); + $this->parseNode = new JsonParseNode(1243); + $this->assertEquals(1243, $this->parseNode->getIntegerValue()); } public function testGetCollectionOfObjectValues(): void { @@ -61,7 +63,7 @@ public function testGetObjectValue(): void { } public function testGetFloatValue(): void { - $this->parseNode = new JsonParseNode('1243.12'); + $this->parseNode = new JsonParseNode(1243.12); $expected = $this->parseNode->getFloatValue(); $this->assertEquals(1243.12, $expected); } diff --git a/tests/JsonSerializationWriterTest.php b/tests/JsonSerializationWriterTest.php index 57ffaff..c84006e 100644 --- a/tests/JsonSerializationWriterTest.php +++ b/tests/JsonSerializationWriterTest.php @@ -4,6 +4,7 @@ use DateInterval; use GuzzleHttp\Psr7\Utils; +use Microsoft\Kiota\Abstractions\Serialization\ComposedTypeWrapper; use Microsoft\Kiota\Abstractions\Serialization\Parsable; use Microsoft\Kiota\Abstractions\Serialization\ParseNode; use Microsoft\Kiota\Abstractions\Serialization\SerializationWriter; @@ -262,4 +263,53 @@ public function testWriteBinaryContentValue(): void $content = $this->jsonSerializationWriter->getSerializedContent(); $this->assertEquals("\"body\":\"Hello world!!!\\r\\t\\t\\t\\n\",\"body3\":\"Hello world!!!\\r\\t\\t\\t\\n\"", $content->getContents()); } + + public function testWriteObjectValueForComposedTypes(): void + { + $this->jsonSerializationWriter = new JsonSerializationWriter(); + + $obj = new class implements Parsable, ComposedTypeWrapper { + public function getString(): ?string {return null;} + public function getBoolean(): ?bool { return null;} + public function getInteger(): ?int { return null;} + public function setBoolean(?bool $value): void {} + public function setInteger(?int $value): void {} + public function setString(?string $value): void {} + public function setDouble(?float $value): void {} + public function getDouble(): ?float {return 26.6;} + public function getFieldDeserializers(): array { return [];} + public function serialize(SerializationWriter $writer): void + { + if ($this->getInteger() !== null) { + $writer->writeIntegerValue(null, $this->getInteger()); + } elseif ($this->getBoolean() !== null) { + $writer->writeBooleanValue(null, $this->getBoolean()); + } elseif ($this->getString() !== null) { + $writer->writeStringValue(null, $this->getString()); + } else if ($this->getDouble() !== null) { + $writer->writeFloatValue(null, $this->getDouble()); + } + } + + public function createFromDiscriminator(ParseNode $parseNode): self { + $result = new $this(); + if ($parseNode->getBooleanValue() !== null) { + $result->setBoolean($parseNode->getBooleanValue()); + } else if ($parseNode->getFloatValue() !== null) { + $result->setDouble($parseNode->getFloatValue()); + } else if ($parseNode->getIntegerValue() !== null) { + $result->setInteger($parseNode->getIntegerValue()); + } else if ($parseNode->getStringValue() !== null) { + $result->setString($parseNode->getStringValue()); + } + return $result; + } + + }; + + $this->jsonSerializationWriter->writeObjectValue(null, $obj); + + $dd = $this->jsonSerializationWriter->getSerializedContent()->getContents(); + $this->assertEquals('26.6', $dd); + } }