Skip to content

Commit

Permalink
fix: bump bluesky-social and serialize certain props as objects
Browse files Browse the repository at this point in the history
  • Loading branch information
ramsey committed May 25, 2023
1 parent 0605233 commit 42dd878
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 4 deletions.
2 changes: 1 addition & 1 deletion resources/bluesky-social
Submodule bluesky-social updated 394 files
2 changes: 2 additions & 0 deletions src/Parser/Parser.php
Expand Up @@ -13,6 +13,8 @@ public function getParserFactory(): ParserFactory;
/**
* @param object | string $data A JSON object or string representation of
* the Lexicon entity to parse.
*
* @throws UnableToParse
*/
public function parse(object | string $data): LexEntity;

Expand Down
20 changes: 19 additions & 1 deletion src/Types/LexEntityJsonSerializer.php
Expand Up @@ -5,11 +5,29 @@
namespace SocialWeb\Atproto\Lexicon\Types;

use function array_filter;
use function array_walk;
use function in_array;

trait LexEntityJsonSerializer
{
/**
* These properties are key-value pair arrays that are converted to JSON
* objects when serialized as JSON. If they are empty, they must be
* converted to empty objects for proper serialization.
*/
private const OBJECT_PROPS = ['defs', 'refs', 'properties'];

public function jsonSerialize(): object
{
return (object) array_filter((array) $this, fn (mixed $v): bool => $v !== null);
$objectifyProps = function (mixed &$value, string $key): void {
if (in_array($key, self::OBJECT_PROPS) && $value === []) {
$value = (object) [];
}
};

$properties = (array) $this;
array_walk($properties, $objectifyProps);

return (object) array_filter($properties, fn (mixed $v): bool => $v !== null);
}
}
3 changes: 2 additions & 1 deletion tests/ParseCanonicalSchemasTest.php
Expand Up @@ -4,6 +4,7 @@

namespace SocialWeb\Test\Atproto\Lexicon;

use PHPUnit\Framework\AssertionFailedError;
use SocialWeb\Atproto\Lexicon\LexiconException;
use SocialWeb\Atproto\Lexicon\Parser\DefaultParserFactory;
use SocialWeb\Atproto\Lexicon\Parser\DefaultSchemaRepository;
Expand Down Expand Up @@ -65,7 +66,7 @@ public function testParsingCanonicalSchemas(): void

// Make sure the structure we created matches the original document.
$this->assertJsonStringEqualsJsonString($file->getContents(), (string) json_encode($document));
} catch (LexiconException $exception) {
} catch (LexiconException | AssertionFailedError $exception) {
$failedSchemas[] = [
'file' => $file->getRealPath(),
'error' => $exception->getMessage(),
Expand Down
20 changes: 19 additions & 1 deletion tests/Types/LexEntityJsonSerializerTest.php
Expand Up @@ -23,6 +23,15 @@ public function testJsonSerializeDoesNotFilterOutFalsyValues(): void

/** @var mixed[] | null */
public ?array $qux = null;

/** @var array<string, mixed> */
public array $defs = [];

/** @var array<string, mixed> */
public array $refs = [];

/** @var array<string, mixed> */
public array $properties = [];
};

$sut->foo = '';
Expand All @@ -31,7 +40,7 @@ public function testJsonSerializeDoesNotFilterOutFalsyValues(): void
$sut->qux = [];

$this->assertJsonStringEqualsJsonString(
'{"foo":"","bar":0,"baz":false,"qux":[]}',
'{"foo":"","bar":0,"baz":false,"qux":[],"defs":{},"refs":{},"properties":{}}',
(string) json_encode($sut),
);
}
Expand Down Expand Up @@ -69,6 +78,15 @@ public function testJsonSerializeReturnsEmptyObjectWhenAllValuesAreNull(): void

/** @var mixed[] | null */
public ?array $qux = null;

/** @var array<string, mixed> | null */
public ?array $defs = null;

/** @var array<string, mixed> | null */
public ?array $refs = null;

/** @var array<string, mixed> | null */
public ?array $properties = null;
};

$this->assertJsonStringEqualsJsonString(
Expand Down

0 comments on commit 42dd878

Please sign in to comment.