Skip to content

Commit

Permalink
Merge pull request #68 from microsoft/fix/negative-intervals
Browse files Browse the repository at this point in the history
Make sure negative date periods are correctly handled.
  • Loading branch information
SilasKenneth authored May 8, 2024
2 parents bc9d0f2 + 96898b8 commit 46dbfcd
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 14 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
}
},
"require": {
"microsoft/kiota-abstractions": "^1.0.2",
"microsoft/kiota-abstractions": "^1.3.1",
"guzzlehttp/psr7": "^1.6 || ^2",
"php": "^7.4 || ^8",
"ext-json": "*"
Expand Down
9 changes: 7 additions & 2 deletions src/JsonParseNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,19 @@
use Microsoft\Kiota\Abstractions\Serialization\AdditionalDataHolder;
use Microsoft\Kiota\Abstractions\Serialization\Parsable;
use Microsoft\Kiota\Abstractions\Serialization\ParseNode;
use Microsoft\Kiota\Abstractions\Serialization\ParseNodeFromStringTrait;
use Microsoft\Kiota\Abstractions\Types\Date;
use Microsoft\Kiota\Abstractions\Types\Time;
use Psr\Http\Message\StreamInterface;
use RuntimeException;

/**
* @method onBeforeAssignFieldValues(Parsable $result)
* @method onAfterAssignFieldValues(Parsable $result)
*/
class JsonParseNode implements ParseNode
{
use ParseNodeFromStringTrait;

/** @var mixed|null $jsonNode*/
private $jsonNode;

Expand Down Expand Up @@ -282,7 +284,10 @@ public function getDateTimeValue(): ?DateTime {
* @throws Exception
*/
public function getDateIntervalValue(): ?DateInterval{
return ($this->jsonNode !== null) ? new DateInterval(strval($this->jsonNode)) : null;
if ($this->jsonNode === null){
return null;
}
return $this->parseDateIntervalFromString(strval($this->jsonNode));
}

public function getBinaryContent(): ?StreamInterface {
Expand Down
17 changes: 7 additions & 10 deletions src/JsonSerializationWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@

use DateInterval;
use DateTime;
use DateTimeInterface;
use GuzzleHttp\Psr7\Utils;
use InvalidArgumentException;
use Microsoft\Kiota\Abstractions\Enum;
use Microsoft\Kiota\Abstractions\Serialization\Parsable;
use Microsoft\Kiota\Abstractions\Serialization\SerializationWriter;
use Microsoft\Kiota\Abstractions\Serialization\SerializationWriterToStringTrait;
use Microsoft\Kiota\Abstractions\Types\Date;
use Microsoft\Kiota\Abstractions\Types\Time;
use Psr\Http\Message\StreamInterface;
Expand All @@ -22,6 +22,8 @@
*/
class JsonSerializationWriter implements SerializationWriter
{
use SerializationWriterToStringTrait;

/** @var array<mixed> $writer */
private array $writer = [];

Expand All @@ -45,13 +47,11 @@ private function writePropertyName(string $propertyName): void {
* @inheritDoc
*/
public function writeStringValue(?string $key, ?string $value): void {

$propertyValue = $value !== null ? '"'.addcslashes($value, "\\\r\n\"\t").'"' : '';
if ($value !== null) {
if (!empty($key)) {
$this->writePropertyName($key);
}
$this->writePropertyValue($key, $propertyValue);
$this->writePropertyValue($key, "\"{$this->getStringValueAsEscapedString($value)}\"");
}
}

Expand All @@ -63,8 +63,7 @@ public function writeBooleanValue(?string $key, ?bool $value): void {
if (!empty($key)) {
$this->writePropertyName($key);
}
$options = ['false', 'true'];
$this->writePropertyValue($key, $options[$value]);
$this->writePropertyValue($key, $this->getBooleanValueAsString($value));
}
}

Expand Down Expand Up @@ -100,7 +99,7 @@ public function writeDateTimeValue(?string $key, ?DateTime $value): void {
if (!empty($key)) {
$this->writePropertyName($key);
}
$this->writePropertyValue($key, "\"{$value->format(DateTimeInterface::RFC3339)}\"");
$this->writePropertyValue($key, "\"{$this->getDateTimeValueAsString($value)}\"");
}
}

Expand Down Expand Up @@ -452,9 +451,7 @@ public function writeDateIntervalValue(?string $key, ?DateInterval $value): void
if (!empty($key)) {
$this->writePropertyName($key);
}
$res = "P{$value->y}Y{$value->y}M{$value->d}DT{$value->h}H{$value->i}M{$value->s}S";
$val = "\"$res\"" ;
$this->writePropertyValue($key, $val);
$this->writePropertyValue($key, "\"{$this->getDateIntervalValueAsString($value)}\"");
}
}
}
12 changes: 12 additions & 0 deletions tests/JsonParseNodeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Microsoft\Kiota\Serialization\Tests;

use DateInterval;
use DateTime;
use DateTimeInterface;
use Exception;
Expand Down Expand Up @@ -184,4 +185,15 @@ public function testGetBinaryContentFromArray(): void {
$this->stream->rewind();
$this->assertEquals($this->stream->getContents(), $this->parseNode->getBinaryContent()->getContents());
}

/**
* @throws Exception
*/
public function testGetNegativeDateInterval(): void
{
$this->parseNode = new JsonParseNode('-P1D');
$expected = new DateInterval('P1D');
$expected->invert = 1;
$this->assertEquals($this->parseNode->getDateIntervalValue(), $expected);
}
}
12 changes: 11 additions & 1 deletion tests/JsonSerializationWriterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,17 @@ public function testWriteDateIntervalValue(): void
$this->jsonSerializationWriter->writeAnyValue('timeTaken', $interval);

$content = $this->jsonSerializationWriter->getSerializedContent();
$this->assertEquals('"timeTaken":"P0Y0M300DT0H0M100S"', $content->getContents());
$this->assertEquals('"timeTaken":"P300DT100S"', $content->getContents());
}

public function testWriteNegativeDateIntervalValue(): void
{
$this->jsonSerializationWriter = new JsonSerializationWriter();
$interval = new DateInterval('P1DT6H7M7S');
$interval->invert = 1;
$this->jsonSerializationWriter->writeAnyValue('timeTaken', $interval);
$content = $this->jsonSerializationWriter->getSerializedContent();
$this->assertEquals('"timeTaken":"-P1DT6H7M7S"', $content->getContents());
}

public function testWriteBinaryContentValue(): void
Expand Down

0 comments on commit 46dbfcd

Please sign in to comment.