Skip to content

Commit

Permalink
Split Parsable interface.
Browse files Browse the repository at this point in the history
  • Loading branch information
SilasKenneth committed Mar 14, 2022
1 parent a66c42c commit 72f43f4
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 20 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ 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
- 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;
}
17 changes: 12 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,19 +114,25 @@ 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();
}

foreach ($this->jsonNode as $key => $value){
$deserializer = $fieldDeserializers[$key] ?? null;

if ($deserializer !== null){
$deserializer($result, new JsonParseNode($value));
} else {
$result->getAdditionalData()[$key] = $value;
} else if (is_a($result, AdditionalDataHolder::class)) {
$data = $result->getAdditionalData();
$key = (string)$key;
$data[$key] = $value;
$result->setAdditionalData($data);
}
}
}
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

0 comments on commit 72f43f4

Please sign in to comment.