Skip to content

Commit

Permalink
fix object mapper to not modify key while hydrating
Browse files Browse the repository at this point in the history
  • Loading branch information
Zrnik committed Apr 13, 2024
1 parent d58bf9b commit 487aacc
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 8 deletions.
41 changes: 33 additions & 8 deletions src/Content/JsonContentFacade.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace Zrnik\Zweist\Content;

use EventSauce\ObjectHydrator\DefinitionProvider;
use EventSauce\ObjectHydrator\KeyFormatterWithoutConversion;
use EventSauce\ObjectHydrator\ObjectMapperUsingReflection;
use EventSauce\ObjectHydrator\UnableToHydrateObject;
use JsonException;
Expand All @@ -30,26 +32,51 @@ public function __construct(
/**
* @template T of object
* @param RequestInterface $request
* @param class-string<T> $requestSchema
* @param class-string<T> $className
* @return T
* @throws JsonContentException
* @noinspection PhpDocSignatureInspection
*/
public function parseRequest(
RequestInterface $request,
string $requestSchema,
): mixed
string $className,
): object
{
try {
/** @var T */
return (new ObjectMapperUsingReflection())->hydrateObject(
$requestSchema,
return $this->hydrateObject(
$className,
json_decode(
(string) $request->getBody(),
true,
512,
JSON_THROW_ON_ERROR
),
);
} catch (JsonException $jsonException) {
throw JsonRequestException::fromJsonException($jsonException);
}
}

/**
* @template T of object
* @param class-string<T> $className
* @param array<mixed> $data
* @return T
* @noinspection PhpPluralMixedCanBeReplacedWithArrayInspection
* @noinspection PhpDocSignatureInspection
*/
public function hydrateObject(string $className, array $data): object
{
try {
/** @var T */
return (new ObjectMapperUsingReflection(
new DefinitionProvider(
keyFormatter: new KeyFormatterWithoutConversion(),
),
))->hydrateObject(
$className,
$data,
);
} catch (UnableToHydrateObject $unableToHydrateObject) {

$previous = $unableToHydrateObject->getPrevious();
Expand All @@ -65,8 +92,6 @@ public function parseRequest(
}

throw JsonRequestException::unhandledObjectHydrate($unableToHydrateObject); // @codeCoverageIgnore
} catch (JsonException $jsonException) {
throw JsonRequestException::fromJsonException($jsonException);
} catch (Throwable $throwable) { // @codeCoverageIgnoreStart
throw JsonRequestException::unhandledThrowable($throwable);
} // @codeCoverageIgnoreEnd
Expand Down
46 changes: 46 additions & 0 deletions tests/ObjectHydrateTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

declare(strict_types=1);

namespace Zrnik\Zweist\Tests;

use Nyholm\Psr7\Factory\Psr17Factory;
use PHPUnit\Framework\TestCase;
use Zrnik\Zweist\Content\JsonContentFacade;
use Zrnik\Zweist\Tests\ExampleSchema\UnrelatedSchemaClass;

class ObjectHydrateTest extends TestCase
{
private JsonContentFacade $jsonContentFacade;

protected function setUp(): void
{
$this->jsonContentFacade = new JsonContentFacade(
new Psr17Factory(),
new Psr17Factory(),
);
}

/**
* This is tested, because object mapper by default
* converts from camel case to snake case.
*
* We don't want that.
*
* @return void
*/
public function testHydrate(): void
{
$json = [
'unrelatedValue' => 'Example Text',
];

/** @var UnrelatedSchemaClass $unrelatedSchemaClass */
$unrelatedSchemaClass = $this->jsonContentFacade->hydrateObject(
UnrelatedSchemaClass::class,
$json
);

$this->assertSame('Example Text', $unrelatedSchemaClass->unrelatedValue);
}
}

0 comments on commit 487aacc

Please sign in to comment.