Skip to content

Commit 6621a10

Browse files
committed
Update serialization documentation
Added more namespaced tests
1 parent a2dfc79 commit 6621a10

File tree

5 files changed

+49
-15
lines changed

5 files changed

+49
-15
lines changed

.github/ISSUE_TEMPLATE/bug_report.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,10 @@ A clear and concise description of what you expected to happen.
1616
**Schema**
1717
Please provide the schema which causes the issue. If multiple schemas are involved (eg. referenced schemas) please append a zip file with the complete structure.
1818

19+
Additionally include custom GeneratorConfiguration settings.
20+
1921
**Version:**
2022
Which version of the library do you use?
2123

2224
**Additional context**
23-
Add any other context about the problem here.
25+
Add any other context about the problem here which might help to solve the issue.

docs/source/gettingStarted.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -237,11 +237,11 @@ Generated interface:
237237

238238
.. code-block:: php
239239
240-
public function toArray([int $depth = 512]): array;
241-
public function toJSON([int $options = 0 [, int $depth = 512]]): string;
240+
public function toArray([array $except = [] [, int $depth = 512]]): array;
241+
public function toJSON([array $except = [] [, int $options = 0 [, int $depth = 512]]]): string;
242242
public function jsonSerialize(): array;
243243
244-
The generated class will implement the interface **PHPModelGenerator\\Interfaces\\SerializationInterface** implemented in the php-json-schema-model-generator-production repository. This interface can be used to write additional generic modules to handle the generated models. The $depth parameter defines the maximum amount of nested objects which are serialized. The $options parameter for the toJSON method provides access to the underlying option bitmask of `json_encode <https://www.php.net/manual/de/function.json-encode.php>`_.
244+
The generated class will implement the interface **PHPModelGenerator\\Interfaces\\SerializationInterface** implemented in the php-json-schema-model-generator-production repository. This interface can be used to write additional generic modules to handle the generated models. Via the $except parameter you can pass an array of properties which will not be serialized (eg. password data for a user object). The $depth parameter defines the maximum amount of nested objects which are serialized. The $options parameter for the toJSON method provides access to the underlying option bitmask of `json_encode <https://www.php.net/manual/de/function.json-encode.php>`_.
245245

246246
Additionally the class will implement the PHP builtin interface **\JsonSerializable** which allows the direct usage of the generated classes in a custom json_encode.
247247

src/Model/GeneratorConfiguration.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ public function getNamespacePrefix(): string
141141
*/
142142
public function setNamespacePrefix(string $namespacePrefix): self
143143
{
144-
$this->namespacePrefix = $namespacePrefix;
144+
$this->namespacePrefix = trim($namespacePrefix, '\\');
145145

146146
return $this;
147147
}

tests/AbstractPHPModelGeneratorTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,14 @@ public function implicitNullDataProvider(): array
392392
];
393393
}
394394

395+
public function namespaceDataProvider(): array
396+
{
397+
return [
398+
'No namespace' => [''],
399+
'Custom namespace' => ['\MyApp\Model\\'],
400+
];
401+
}
402+
395403
/**
396404
* Get the annotated type for an object property
397405
*

tests/Basic/FilterTest.php

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -459,14 +459,20 @@ public function testFilterExceptionsAreCaught(): void
459459
new $className(['created' => 'Hello', 'name' => 12]);
460460
}
461461

462-
public function testAdditionalFilterOptions(): void
462+
/**
463+
* @dataProvider namespaceDataProvider
464+
*
465+
* @param string $namespace
466+
*/
467+
public function testAdditionalFilterOptions(string $namespace): void
463468
{
464469
$className = $this->generateClassFromFile(
465470
'FilterOptions.json',
466-
(new GeneratorConfiguration())->setSerialization(true)
471+
(new GeneratorConfiguration())->setSerialization(true)->setNamespacePrefix($namespace)
467472
);
468473

469-
$object = new $className(['created' => '10122020']);
474+
$fqcn = $namespace . $className;
475+
$object = new $fqcn(['created' => '10122020']);
470476

471477
$this->assertSame((new DateTime('2020-12-10'))->format(DATE_ATOM), $object->getCreated()->format(DATE_ATOM));
472478

@@ -566,12 +572,17 @@ public static function exceptionFilter(string $value): void
566572
throw new Exception("Exception filter called with $value");
567573
}
568574

569-
public function testTransformingToScalarType()
575+
/**
576+
* @dataProvider namespaceDataProvider
577+
*
578+
* @param string $namespace
579+
*/
580+
public function testTransformingToScalarType(string $namespace)
570581
{
571582
$className = $this->generateClassFromFile(
572583
'TransformingScalarFilter.json',
573584
(new GeneratorConfiguration())
574-
->setNamespacePrefix('\MyApp\Model')
585+
->setNamespacePrefix($namespace)
575586
->setSerialization(true)
576587
->setImmutable(false)
577588
->addFilter(
@@ -584,7 +595,7 @@ public function testTransformingToScalarType()
584595
)
585596
);
586597

587-
$fqcn = "\\MyApp\\Model\\$className";
598+
$fqcn = $namespace . $className;
588599
$object = new $fqcn(['value' => 9]);
589600

590601
$this->assertSame('1001', $object->getValue());
@@ -645,16 +656,20 @@ public function testFilterChainWithTransformingFilter(): void
645656
}
646657

647658
/**
648-
* @dataProvider implicitNullDataProvider
659+
* @dataProvider implicitNullNamespaceDataProvider
649660
*
650661
* @param bool $implicitNull
662+
* @param string $namespace
651663
*/
652-
public function testFilterChainWithTransformingFilterOnMultiTypeProperty(bool $implicitNull): void
653-
{
664+
public function testFilterChainWithTransformingFilterOnMultiTypeProperty(
665+
bool $implicitNull,
666+
string $namespace
667+
): void {
654668
$className = $this->generateClassFromFile(
655669
'FilterChainMultiType.json',
656670
(new GeneratorConfiguration())
657671
->setImplicitNull($implicitNull)
672+
->setNamespacePrefix($namespace)
658673
->setSerialization(true)
659674
->setImmutable(false)
660675
->addFilter(
@@ -667,7 +682,8 @@ public function testFilterChainWithTransformingFilterOnMultiTypeProperty(bool $i
667682
false
668683
);
669684

670-
$object = new $className(['filteredProperty' => '2020-12-12 12:12:12']);
685+
$fqcn = $namespace . $className;
686+
$object = new $fqcn(['filteredProperty' => '2020-12-12 12:12:12']);
671687

672688
$this->assertInstanceOf(DateTime::class, $object->getFilteredProperty());
673689
$this->assertSame('2020-12-12T00:00:00+00:00', $object->getFilteredProperty()->format(DateTime::ATOM));
@@ -681,6 +697,14 @@ public function testFilterChainWithTransformingFilterOnMultiTypeProperty(bool $i
681697
$this->assertSame(['filteredProperty' => '2020-12-12T00:00:00+0000'], $object->toArray());
682698
}
683699

700+
public function implicitNullNamespaceDataProvider(): array
701+
{
702+
return $this->combineDataProvider(
703+
$this->implicitNullDataProvider(),
704+
$this->namespaceDataProvider()
705+
);
706+
}
707+
684708
public function testFilterAfterTransformingFilterIsSkippedIfTransformingFilterFails(): void
685709
{
686710
$this->expectException(ErrorRegistryException::class);

0 commit comments

Comments
 (0)