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

Split Parsable interface PHP #1375

Merged
merged 2 commits into from
Mar 14, 2022
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: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Added default implementations for table and JSON output in CLI commons (Shell) #1326

### Changed
- Add missing method getBinaryContent to the ParseNode interface(PHP).
- Split the Parsable interface into AdditionalData interface and Parsable interface(PHP) #1324.
- Shell commands will now default to writing indented JSON. This option can be disabled through the CLI option `--json-no-indent` (Shell) #1326
- Update System.CommandLine version (Shell) #1338
- Add async writers in output formatters (Shell) #1326
Expand Down
17 changes: 17 additions & 0 deletions abstractions/php/src/Serialization/AdditionalDataHolder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace Microsoft\Kiota\Abstractions\Serialization;

interface AdditionalDataHolder {
/**
* Gets the additional data for this object that did not belong to the properties.
* @return array<string,mixed> The additional data for this object.
*/
public function getAdditionalData(): array;

/**
* Sets the additional data for this object that did not belong to the properties.
* @param array<string, mixed> $value The additional data for this object.
*/
public function setAdditionalData(array $value): void;
}
12 changes: 0 additions & 12 deletions abstractions/php/src/Serialization/Parsable.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,4 @@ public function getFieldDeserializers(): array;
* @param SerializationWriter $writer The writer to write to.
*/
public function serialize(SerializationWriter $writer): void;

/**
* Gets the additional data for this object that did not belong to the properties.
* @return array<string,mixed>|null The additional data for this object.
*/
public function getAdditionalData(): ?array;

/**
* Sets the additional data for this object that did not belong to the properties.
* @param array<string, mixed> $value The additional data for this object.
*/
public function setAdditionalData(array $value): void;
}
6 changes: 6 additions & 0 deletions abstractions/php/src/Serialization/ParseNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Microsoft\Kiota\Abstractions\Types\Byte;
use Microsoft\Kiota\Abstractions\Types\Date;
use Microsoft\Kiota\Abstractions\Types\Time;
use Psr\Http\Message\StreamInterface;

interface ParseNode {
/**
Expand Down Expand Up @@ -98,6 +99,11 @@ public function getEnumValue(string $targetEnum): ?Enum;
*/
public function getByteValue(): ?Byte;

/**
* Get a Stream from node.
* @return StreamInterface|null
*/
public function getBinaryContent(): ?StreamInterface;
baywet marked this conversation as resolved.
Show resolved Hide resolved
/**
* Gets the callback called before the node is deserialized.
* @return callable the callback called before the node is deserialized.
Expand Down
24 changes: 19 additions & 5 deletions serialization/php/json/src/JsonParseNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use GuzzleHttp\Psr7\Utils;
use InvalidArgumentException;
use Microsoft\Kiota\Abstractions\Enum;
use Microsoft\Kiota\Abstractions\Serialization\AdditionalDataHolder;
use Microsoft\Kiota\Abstractions\Serialization\Parsable;
use Microsoft\Kiota\Abstractions\Serialization\ParseNode;
use Microsoft\Kiota\Abstractions\Types\Byte;
Expand Down Expand Up @@ -113,21 +114,34 @@ public function getObjectValue(string $type): ?Parsable {
}

/**
* @param Parsable $result
* @param Parsable|AdditionalDataHolder $result
* @return void
*/
private function assignFieldValues(Parsable $result): void {
$fieldDeserializers = $result->getFieldDeserializers();

private function assignFieldValues($result): void {
$fieldDeserializers = [];
if (is_a($result, Parsable::class)){
$fieldDeserializers = $result->getFieldDeserializers();
}
$isAdditionalDataHolder = false;
$additionalData = [];
if (is_a($result, AdditionalDataHolder::class)) {
$isAdditionalDataHolder = true;
$additionalData = $result->getAdditionalData();
}
foreach ($this->jsonNode as $key => $value){
$deserializer = $fieldDeserializers[$key] ?? null;

if ($deserializer !== null){
$deserializer($result, new JsonParseNode($value));
} else {
$result->getAdditionalData()[$key] = $value;
$key = (string)$key;
$additionalData[$key] = $value;
}
}

if ( $isAdditionalDataHolder ) {
$result->setAdditionalData($additionalData);
}
}

/**
Expand Down
4 changes: 2 additions & 2 deletions serialization/php/json/tests/JsonParseNodeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class JsonParseNodeTest extends TestCase
private StreamInterface $stream;

protected function setUp(): void {
$this->stream = Utils::streamFor('{"name": "Silas Kenneth", "age": 98, "height": 123.122, "maritalStatus": "complicated,single", "address": {"city": "Nairobi", "street": "Luthuli"}}');
$this->stream = Utils::streamFor('{"@odata.type":"Missing", "name": "Silas Kenneth", "age": 98, "height": 123.122, "maritalStatus": "complicated,single", "address": {"city": "Nairobi", "street": "Luthuli"}}');
}

public function testGetIntegerValue(): void {
Expand Down Expand Up @@ -172,7 +172,7 @@ public function testGetChildNode(): void {
/** @var Address $address */
$address = $child->getObjectValue(Address::class);
$this->assertInstanceOf(Address::class, $address);
$this->assertEquals((string)'Nairobi', $address->getCity());
$this->assertEquals('Nairobi', $address->getCity());

}
}
7 changes: 6 additions & 1 deletion src/Kiota.Builder/Refiners/PhpRefiner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public override void Refine(CodeNamespace generatedCode)
ReplaceDefaultDeserializationModules(generatedCode, "Microsoft\\Kiota\\Serialization\\Json\\JsonParseNodeFactory");
AliasUsingWithSameSymbol(generatedCode);
AddSerializationModulesImport(generatedCode, new []{"Microsoft\\Kiota\\Abstractions\\ApiClientBuilder"}, null, '\\');
CorrectCoreType(generatedCode, CorrectMethodType, CorrectPropertyType);
CorrectCoreType(generatedCode, CorrectMethodType, CorrectPropertyType, CorrectImplements);
AddGetterAndSetterMethods(generatedCode,
new() {
CodePropertyKind.Custom,
Expand Down Expand Up @@ -89,6 +89,8 @@ public override void Refine(CodeNamespace generatedCode)
"Microsoft\\Kiota\\Abstractions", "HttpMethod", "RequestInformation", "RequestOption"),
new (x => x is CodeMethod method && method.IsOfKind(CodeMethodKind.RequestExecutor),
"Microsoft\\Kiota\\Abstractions", "ResponseHandler"),
new (x => x is CodeClass @class && @class.IsOfKind(CodeClassKind.Model) && @class.Properties.Any(x => x.IsOfKind(CodePropertyKind.AdditionalData)),
"Microsoft\\Kiota\\Abstractions\\Serialization", "AdditionalDataHolder"),
new (x => x is CodeMethod method && method.IsOfKind(CodeMethodKind.Serializer),
"Microsoft\\Kiota\\Abstractions\\Serialization", "SerializationWriter"),
new (x => x is CodeMethod method && method.IsOfKind(CodeMethodKind.Deserializer),
Expand Down Expand Up @@ -164,6 +166,9 @@ private static void CorrectParameterType(CodeElement codeElement)
});
CrawlTree(codeElement, CorrectParameterType);
}
private static void CorrectImplements(ProprietableBlockDeclaration block) {
block.Implements.Where(x => "IAdditionalDataHolder".Equals(x.Name, StringComparison.OrdinalIgnoreCase)).ToList().ForEach(x => x.Name = x.Name[1..]); // skipping the I
}

private static void AliasUsingWithSameSymbol(CodeElement currentElement) {
if(currentElement is CodeClass currentClass && currentClass.StartBlock != null && currentClass.StartBlock.Usings.Any(x => !x.IsExternal)) {
Expand Down