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

Release 1.2.0 #69

Merged
merged 15 commits into from
May 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
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.6.0
uses: dependabot/fetch-metadata@v2.1.0
with:
github-token: "${{ secrets.GITHUB_TOKEN }}"

Expand Down
27 changes: 22 additions & 5 deletions .github/workflows/pr-validation.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,42 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
php-versions: ['7.4', '8.0', '8.1', '8.2']
php-versions: ['7.4', '8.1', '8.2', '8.3']
steps:
- name: Checkout
uses: actions/[email protected].2
- name: Setup PHP and Xdebug for Code Coverage report
uses: actions/[email protected].1
- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php-versions }}
coverage: xdebug
coverage: none
- name: Install dependencies
run: composer install
- name: Run static analysis
run: ./vendor/bin/phpstan
- name: Run tests
run: ./vendor/bin/phpunit

code-coverage:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Setup PHP and Xdebug for Code Coverage report
uses: shivammathur/setup-php@v2
with:
php-version: '8.0'
coverage: xdebug
- name: Install dependencies
run: composer install
- name: Run static analysis
run: ./vendor/bin/phpstan
- name: Run tests with coverage
run: ./vendor/bin/phpunit --coverage-clover=coverage.xml
- 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' && !github.event.pull_request.head.repo.fork }}
if: ${{ !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 @@ -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