Skip to content

Commit 74541f0

Browse files
author
Enno Woortmann
committed
Update docs, add test cases
1 parent f698d45 commit 74541f0

File tree

3 files changed

+58
-14
lines changed

3 files changed

+58
-14
lines changed

docs/source/complexTypes/object.rst

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,12 +93,15 @@ Naming
9393
Naming of classes
9494
^^^^^^^^^^^^^^^^^
9595

96-
If the given main object in a JSON-Schema file contains a `$id` the id will be used as class name. Otherwise the name of the file will be used.
96+
If the given main object in a JSON-Schema file contains a `title`, the title will be used as class name.
97+
Otherwise, if an `$id` is present, the basename of the $id and as a last fallback the name of the file will be used.
9798

9899
Naming of nested classes
99100
^^^^^^^^^^^^^^^^^^^^^^^^
100101

101-
For the class name of a nested class the `$id` property of the nested object is used. If the id property isn't present the property key will be prefixed with the parent class. If an object `Person` has a nested object `car` without a `$id` the class for car will be named **Person_Car**.
102+
For the class name of a nested class the `title` property (fallback to `$id`) of the nested object is used.
103+
If neither the title nor the $id property is present the property key will be prefixed with the parent class.
104+
If an object `Person` has a nested object `car` without a `title` and an `$id` the class for car will be named **Person_Car**.
102105

103106
Property Name Normalization
104107
^^^^^^^^^^^^^^^^^^^^^^^^^^^

docs/source/generic/references.rst

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,20 @@ Supported reference types
1111
* relative reference based on the location on the file system to a complete file (example: `"$ref": "./../modules/myObject.json"`)
1212
* relative reference based on the location on the file system to an object by id (example: `"$ref": "./../modules/myObject.json#IdOfMyObject"`)
1313
* relative reference based on the location on the file system to an object by path (example: `"$ref": "./../modules/myObject.json#/definitions/myObject"`)
14+
* absolute reference based on the location on the file system to a complete file (example: `"$ref": "/modules/myObject.json"`)
15+
* absolute reference based on the location on the file system to an object by id (example: `"$ref": "/modules/myObject.json#IdOfMyObject"`)
16+
* absolute reference based on the location on the file system to an object by path (example: `"$ref": "/modules/myObject.json#/definitions/myObject"`)
1417
* network reference to a complete file (example: `"$ref": "https://my.domain.com/schema/modules/myObject.json"`)
1518
* network reference to an object by id (example: `"$ref": "https://my.domain.com/schema/modules/myObject.json#IdOfMyObject"`)
1619
* network reference to an object by path (example: `"$ref": "https://my.domain.com/schema/modules/myObject.json#/definitions/myObject"`)
1720

21+
If an `$id` is present in the schema, the `$ref` will be resolved relative to the `$id` (except the `$ref` already is an absolute reference, e.g. a full URL).
22+
The behaviour of `$ref` resolving can be overwritten by implementing a custom **SchemaProviderInterface**, for example when you want to use network references behind an authorization.
23+
24+
.. note::
25+
26+
For absolute local references, the default implementation traverses up the directory tree until it finds a matching file to find the project root
27+
1828
Object reference
1929
----------------
2030

tests/Objects/ReferencePropertyTest.php

Lines changed: 43 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use PHPModelGenerator\Exception\RenderException;
1111
use PHPModelGenerator\Exception\SchemaException;
1212
use PHPModelGenerator\Model\GeneratorConfiguration;
13+
use PHPModelGenerator\Model\Schema;
1314
use PHPModelGenerator\Tests\AbstractPHPModelGeneratorTestCase;
1415
use stdClass;
1516

@@ -24,13 +25,12 @@ class ReferencePropertyTest extends AbstractPHPModelGeneratorTestCase
2425

2526
/**
2627
* @dataProvider internalReferenceProvider
27-
* @dataProvider notResolvedExternalReferenceProvider
2828
*
2929
* @throws FileSystemException
3030
* @throws RenderException
3131
* @throws SchemaException
3232
*/
33-
public function testNotResolvedReferenceThrowsAnException(string $reference): void
33+
public function testNotResolvedInternalReferenceThrowsAnException(string $reference): void
3434
{
3535
$this->expectException(SchemaException::class);
3636
$this->expectExceptionMessageMatches(
@@ -57,16 +57,6 @@ public function externalReferenceProvider(): array
5757
];
5858
}
5959

60-
public function notResolvedExternalReferenceProvider(): array
61-
{
62-
return [
63-
'External non existing file' => ['../ReferencePropertyTest_external/notExisting'],
64-
'External path reference in non existing file' => ['../ReferencePropertyTest_external/notExisting#person'],
65-
'External non existing path reference' => ['../ReferencePropertyTest_external/library.json#/definitions/animal'],
66-
'External non existing direct reference' => ['../ReferencePropertyTest_external/library.json#animal'],
67-
];
68-
}
69-
7060
/**
7161
* @dataProvider internalReferenceProvider
7262
* @dataProvider externalReferenceProvider
@@ -452,6 +442,47 @@ public function nestedReferenceProvider(): array
452442
];
453443
}
454444

445+
/**
446+
* @dataProvider nonResolvableExternalReferenceProvider
447+
*/
448+
public function testNonResolvableExternalReference(string $id, string $reference): void
449+
{
450+
$this->expectException(SchemaException::class);
451+
$this->expectExceptionMessageMatches(
452+
sprintf('/Unresolved Reference %s#\/definitions\/family in file .*\.json/', str_replace('/', '\/', $reference)),
453+
);
454+
455+
$this->generateClassFromFileTemplate('NestedExternalReference.json', [$id, $reference]);
456+
}
457+
458+
public function nonResolvableExternalReferenceProvider(): array
459+
{
460+
$baseURL = 'https://raw.githubusercontent.com/wol-soft/php-json-schema-model-generator/master/tests/Schema/';
461+
462+
return [
463+
'local reference - relative' => [
464+
'NestedExternalReference.json',
465+
'../ReferencePropertyTest_external/nonexistent.json',
466+
],
467+
'local reference - context absolute' => [
468+
'NestedExternalReference.json',
469+
'/ReferencePropertyTest_external/nonexistent.json',
470+
],
471+
'network reference - full URL' => [
472+
'NestedExternalReference.json',
473+
$baseURL . 'ReferencePropertyTest_external/nonexistent.json',
474+
],
475+
'network reference - relative path to full URL $id' => [
476+
$baseURL . 'ReferencePropertyTest/NestedExternalReference.json',
477+
'../ReferencePropertyTest_external/nonexistent.json',
478+
],
479+
'network reference - absolute path to full URL $id' => [
480+
$baseURL . 'ReferencePropertyTest/NestedExternalReference.json',
481+
'/wol-soft/php-json-schema-model-generator/master/tests/Schema/ReferencePropertyTest_external/nonexistent.json',
482+
],
483+
];
484+
}
485+
455486
public function testInvalidBaseReferenceThrowsAnException(): void
456487
{
457488
$this->expectException(SchemaException::class);

0 commit comments

Comments
 (0)