Skip to content

Commit 8457c91

Browse files
authored
Merge pull request #20 from swaggest/defaults
Defaults
2 parents bd13969 + 8f40bb7 commit 8457c91

19 files changed

+408
-140
lines changed

.gitattributes

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
* text eol=lf
22
*.serialized -text
33
*.dat -text
4-
/spec export-ignore
54
/tests export-ignore
65
/stubs export-ignore
76
/tools export-ignore

.scrutinizer.yml

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
checks:
2+
php:
3+
code_rating: true
4+
duplication: true
15
filter:
26
paths: [src/*]
37
excluded_paths: [vendor/*, tests/*]
@@ -15,4 +19,12 @@ tools:
1519
excluded_dirs: [vendor, tests]
1620
php_cpd:
1721
enabled: true
18-
excluded_dirs: [vendor, tests]
22+
excluded_dirs: [vendor, tests]
23+
24+
25+
build:
26+
nodes:
27+
analysis:
28+
tests:
29+
override:
30+
- php-scrutinizer-run

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,8 @@ class User extends ClassStructure
144144
$defaultOptions = new UserOptions();
145145
$defaultOptions->autoLogin = true;
146146
$defaultOptions->groupName = 'guest';
147-
$properties->options = (clone UserOptions::schema())->setDefault(UserOptions::export($defaultOptions));
147+
// UserOptions::schema() is safe to change as it is protected with lazy cloning
148+
$properties->options = UserOptions::schema()->setDefault(UserOptions::export($defaultOptions));
148149

149150
// Dynamic (phpdoc-defined) properties can be used as well
150151
$properties->quantity = Schema::integer();

src/Constraint/Properties.php

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,33 +4,31 @@
44

55
use Swaggest\JsonSchema\Exception;
66
use Swaggest\JsonSchema\Schema;
7+
use Swaggest\JsonSchema\SchemaContract;
78
use Swaggest\JsonSchema\Structure\Egg;
89
use Swaggest\JsonSchema\Structure\Nested;
910
use Swaggest\JsonSchema\Structure\ObjectItem;
1011

1112
/**
12-
* @method Schema __get($key)
13+
* @method SchemaContract __get($key)
1314
* @method Schema[] toArray()
1415
*/
1516
class Properties extends ObjectItem implements Constraint
1617
{
18+
private $__isReadOnly = false;
19+
1720
/** @var Schema[] */
1821
protected $__arrayOfData = array();
1922

2023
/** @var Schema */
2124
protected $__schema;
2225

23-
public function setSchema(Schema $schema)
26+
public function lock()
2427
{
25-
$this->__schema = $schema;
28+
$this->__isReadOnly = true;
2629
return $this;
2730
}
2831

29-
public function getSchema()
30-
{
31-
return $this->__schema;
32-
}
33-
3432
/**
3533
* @param string $name
3634
* @param mixed $column
@@ -39,6 +37,9 @@ public function getSchema()
3937
*/
4038
public function __set($name, $column)
4139
{
40+
if ($this->__isReadOnly) {
41+
throw new Exception('Trying to modify read-only Properties');
42+
}
4243
if ($column instanceof Nested) {
4344
$this->addNested($column->schema, $name);
4445
return $this;
@@ -55,30 +56,28 @@ public static function create()
5556
/** @var Schema|null */
5657
private $additionalProperties;
5758

58-
/**
59-
* @param Schema $additionalProperties
60-
* @return Properties
61-
*/
62-
public function setAdditionalProperties(Schema $additionalProperties = null)
63-
{
64-
$this->additionalProperties = $additionalProperties;
65-
return $this;
66-
}
67-
68-
6959
/** @var Egg[][] */
7060
public $nestedProperties = array();
7161

72-
/** @var Schema[] */
62+
/** @var string[] */
7363
public $nestedPropertyNames = array();
7464

75-
protected function addNested(Schema $nested, $name)
65+
/**
66+
* @param SchemaContract $nested
67+
* @param string $name
68+
* @return $this
69+
* @throws Exception
70+
*/
71+
protected function addNested(SchemaContract $nested, $name)
7672
{
77-
if (null === $nested->properties) {
73+
if ($this->__isReadOnly) {
74+
throw new Exception('Trying to modify read-only Properties');
75+
}
76+
if (null === $nested->getProperties()) {
7877
throw new Exception('Schema with properties required', Exception::PROPERTIES_REQUIRED);
7978
}
8079
$this->nestedPropertyNames[$name] = $name;
81-
foreach ($nested->properties->toArray() as $propertyName => $property) {
80+
foreach ($nested->getProperties()->toArray() as $propertyName => $property) {
8281
$this->nestedProperties[$propertyName][] = new Egg($nested, $name, $property);
8382
}
8483
return $this;

src/Context.php

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class Context extends MagicMap
3030
/** @var bool */
3131
public $skipValidation = false;
3232

33-
/** @var string[] map of from -> to class names */
33+
/** @var string[]|null map of from -> to class names */
3434
public $objectItemClassMapping;
3535

3636
/** @var bool allow soft cast from to/strings */
@@ -107,23 +107,20 @@ public function setRemoteRefProvider($remoteRefProvider)
107107
}
108108

109109
/** @var self */
110-
private $withSkipValidation;
110+
private $withDefault;
111111

112112
/**
113113
* @return Context
114114
*/
115-
public function withSkipValidation()
115+
public function withDefault()
116116
{
117-
if (null === $this->withSkipValidation) {
118-
if ($this->skipValidation) {
119-
$this->withSkipValidation = $this;
120-
} else {
121-
$this->withSkipValidation = clone $this;
122-
$this->withSkipValidation->skipValidation = true;
123-
}
117+
if (null === $this->withDefault) {
118+
$this->withDefault = clone $this;
119+
$this->withDefault->skipValidation = true;
120+
$this->withDefault->applyDefaults = false;
124121
}
125122

126-
return $this->withSkipValidation;
123+
return $this->withDefault;
127124
}
128125

129126

src/JsonSchema.php

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -47,28 +47,28 @@ class JsonSchema extends ClassStructure {
4747

4848
public $default;
4949

50-
/** @var float */
50+
/** @var float|null */
5151
public $multipleOf;
5252

53-
/** @var float */
53+
/** @var float|null */
5454
public $maximum;
5555

56-
/** @var bool|float */
56+
/** @var bool|float|null */
5757
public $exclusiveMaximum;
5858

59-
/** @var float */
59+
/** @var float|null */
6060
public $minimum;
6161

6262
/** @var bool|float */
6363
public $exclusiveMinimum;
6464

65-
/** @var int */
65+
/** @var int|null */
6666
public $maxLength;
6767

68-
/** @var int */
68+
/** @var int|null */
6969
public $minLength;
7070

71-
/** @var string */
71+
/** @var string|null */
7272
public $pattern;
7373

7474
/** @var bool|JsonSchema */
@@ -77,13 +77,13 @@ class JsonSchema extends ClassStructure {
7777
/** @var JsonSchema|JsonSchema[]|array */
7878
public $items;
7979

80-
/** @var int */
80+
/** @var int|null */
8181
public $maxItems;
8282

83-
/** @var int */
83+
/** @var int|null */
8484
public $minItems;
8585

86-
/** @var bool */
86+
/** @var bool|null */
8787
public $uniqueItems;
8888

8989
/** @var int */
@@ -116,7 +116,7 @@ class JsonSchema extends ClassStructure {
116116
/** @var array|string */
117117
public $type;
118118

119-
/** @var string */
119+
/** @var string|null */
120120
public $format;
121121

122122
/** @var string */

src/RefResolver.php

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -157,11 +157,11 @@ public function resolveReference($referencePath)
157157
/** @var JsonSchema $branch */
158158
$branch = &$refResolver->rootData;
159159
while (!empty($path)) {
160-
if (isset($branch->{Schema::ID_D4}) && is_string($branch->{Schema::ID_D4})) {
161-
$refResolver->updateResolutionScope($branch->{Schema::ID_D4});
160+
if (isset($branch->{Schema::PROP_ID_D4}) && is_string($branch->{Schema::PROP_ID_D4})) {
161+
$refResolver->updateResolutionScope($branch->{Schema::PROP_ID_D4});
162162
}
163-
if (isset($branch->{Schema::ID}) && is_string($branch->{Schema::ID})) {
164-
$refResolver->updateResolutionScope($branch->{Schema::ID});
163+
if (isset($branch->{Schema::PROP_ID}) && is_string($branch->{Schema::PROP_ID})) {
164+
$refResolver->updateResolutionScope($branch->{Schema::PROP_ID});
165165
}
166166

167167
$folder = array_shift($path);
@@ -223,22 +223,22 @@ public function preProcessReferences($data, Context $options, $nestingLevel = 0)
223223
} elseif ($data instanceof \stdClass) {
224224
/** @var JsonSchema $data */
225225
if (
226-
isset($data->{Schema::ID_D4})
227-
&& is_string($data->{Schema::ID_D4})
226+
isset($data->{Schema::PROP_ID_D4})
227+
&& is_string($data->{Schema::PROP_ID_D4})
228228
&& (($options->version === Schema::VERSION_AUTO) || $options->version === Schema::VERSION_DRAFT_04)
229229
) {
230-
$prev = $this->setupResolutionScope($data->{Schema::ID_D4}, $data);
230+
$prev = $this->setupResolutionScope($data->{Schema::PROP_ID_D4}, $data);
231231
/** @noinspection PhpUnusedLocalVariableInspection */
232232
$_ = new ScopeExit(function () use ($prev) {
233233
$this->setResolutionScope($prev);
234234
});
235235
}
236236

237-
if (isset($data->{Schema::ID})
238-
&& is_string($data->{Schema::ID})
237+
if (isset($data->{Schema::PROP_ID})
238+
&& is_string($data->{Schema::PROP_ID})
239239
&& (($options->version === Schema::VERSION_AUTO) || $options->version >= Schema::VERSION_DRAFT_06)
240240
) {
241-
$prev = $this->setupResolutionScope($data->{Schema::ID}, $data);
241+
$prev = $this->setupResolutionScope($data->{Schema::PROP_ID}, $data);
242242
/** @noinspection PhpUnusedLocalVariableInspection */
243243
$_ = new ScopeExit(function () use ($prev) {
244244
$this->setResolutionScope($prev);

0 commit comments

Comments
 (0)