Skip to content

Commit 2755282

Browse files
committed
defaults disabled for schema
1 parent bd13969 commit 2755282

File tree

8 files changed

+63
-64
lines changed

8 files changed

+63
-64
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

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);

src/Schema.php

Lines changed: 20 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,12 @@ class Schema extends JsonSchema implements MetaHolder
4040
const VERSION_DRAFT_06 = 6;
4141
const VERSION_DRAFT_07 = 7;
4242

43-
const REF = '$ref';
44-
const ID = '$id';
45-
const ID_D4 = 'id';
43+
const PROP_REF = '$ref';
44+
const PROP_ID = '$id';
45+
const PROP_ID_D4 = 'id';
4646

4747
// Object
48-
/** @var Properties|Schema[]|Schema */
48+
/** @var null|Properties|Schema[]|Schema */
4949
public $properties;
5050
/** @var Schema|bool */
5151
public $additionalProperties;
@@ -106,7 +106,7 @@ public static function import($data, Context $options = null)
106106
$options = new Context();
107107
}
108108

109-
//$options->applyDefaults = false; // todo check infinite recursion on items, additionalProperties, etc...
109+
$options->applyDefaults = false;
110110

111111
if (isset($options->schemasCache) && is_object($data)) {
112112
if ($options->schemasCache->contains($data)) {
@@ -120,7 +120,7 @@ public static function import($data, Context $options = null)
120120

121121
// string $data is expected to be $ref uri
122122
if (is_string($data)) {
123-
$data = (object)array(self::REF => $data);
123+
$data = (object)array(self::PROP_REF => $data);
124124
}
125125

126126
$data = self::unboolSchema($data);
@@ -302,8 +302,7 @@ private function processString($data, $path)
302302
if ($this->format !== null) {
303303
$validationError = Format::validationError($this->format, $data);
304304
if ($validationError !== null) {
305-
if ($this->format === "uri" && substr($path, -3) === ':id') {
306-
} else {
305+
if (!($this->format === "uri" && substr($path, -3) === ':id')) {
307306
$this->fail(new StringException($validationError), $path);
308307
}
309308
}
@@ -564,7 +563,6 @@ private function processObject($data, Context $options, $path, $result = null)
564563
if ($this->useObjectAsArray) {
565564
$result = array();
566565
} elseif (!$result instanceof ObjectItemContract) {
567-
//$result = $this->makeObjectItem($options);
568566
//* todo check performance impact
569567
if (null === $this->objectItemClass) {
570568
$result = new ObjectItem();
@@ -604,10 +602,10 @@ private function processObject($data, Context $options, $path, $result = null)
604602
// @todo better check for schema id
605603

606604
if ($import
607-
&& isset($data->{Schema::ID_D4})
605+
&& isset($data->{Schema::PROP_ID_D4})
608606
&& ($options->version === Schema::VERSION_DRAFT_04 || $options->version === Schema::VERSION_AUTO)
609-
&& is_string($data->{Schema::ID_D4}) /*&& (!isset($this->properties['id']))/* && $this->isMetaSchema($data)*/) {
610-
$id = $data->{Schema::ID_D4};
607+
&& is_string($data->{Schema::PROP_ID_D4})) {
608+
$id = $data->{Schema::PROP_ID_D4};
611609
$refResolver = $options->refResolver;
612610
$parentScope = $refResolver->updateResolutionScope($id);
613611
/** @noinspection PhpUnusedLocalVariableInspection */
@@ -617,10 +615,10 @@ private function processObject($data, Context $options, $path, $result = null)
617615
}
618616

619617
if ($import
620-
&& isset($data->{self::ID})
618+
&& isset($data->{self::PROP_ID})
621619
&& ($options->version >= Schema::VERSION_DRAFT_06 || $options->version === Schema::VERSION_AUTO)
622-
&& is_string($data->{self::ID}) /*&& (!isset($this->properties['id']))/* && $this->isMetaSchema($data)*/) {
623-
$id = $data->{self::ID};
620+
&& is_string($data->{self::PROP_ID})) {
621+
$id = $data->{self::PROP_ID};
624622
$refResolver = $options->refResolver;
625623
$parentScope = $refResolver->updateResolutionScope($id);
626624
/** @noinspection PhpUnusedLocalVariableInspection */
@@ -632,11 +630,11 @@ private function processObject($data, Context $options, $path, $result = null)
632630
if ($import) {
633631
try {
634632
while (
635-
isset($data->{self::REF})
636-
&& is_string($data->{self::REF})
637-
&& !isset($this->properties[self::REF])
633+
isset($data->{self::PROP_REF})
634+
&& is_string($data->{self::PROP_REF})
635+
&& !isset($this->properties[self::PROP_REF])
638636
) {
639-
$refString = $data->{self::REF};
637+
$refString = $data->{self::PROP_REF};
640638

641639
// todo check performance impact
642640
if ($refString === 'http://json-schema.org/draft-04/schema#'
@@ -655,7 +653,6 @@ private function processObject($data, Context $options, $path, $result = null)
655653

656654
$ref = $refResolver->resolveReference($refString);
657655
$data = self::unboolSchemaData($ref->getData());
658-
//$data = $ref->getData();
659656
if (!$options->validateOnly) {
660657
if ($ref->isImported()) {
661658
$refResult = $ref->getImported();
@@ -728,7 +725,6 @@ private function processObject($data, Context $options, $path, $result = null)
728725
&& !$options->validateOnly
729726
&& $options->applyDefaults
730727
&& $properties !== null
731-
&& $this->objectItemClass !== 'Swaggest\JsonSchema\Schema' // todo replace literal
732728
) {
733729
foreach ($properties as $key => $property) {
734730
if (isset($property->default)) {
@@ -777,7 +773,7 @@ private function processObject($data, Context $options, $path, $result = null)
777773
if ($prop instanceof Schema) {
778774
$value = $prop->process(
779775
$value,
780-
isset($defaultApplied[$key]) ? $options->withSkipValidation() : $options,
776+
isset($defaultApplied[$key]) ? $options->withDefault() : $options,
781777
$path . '->properties:' . $key
782778
);
783779
}
@@ -959,7 +955,7 @@ private function processContent($data, Context $options, $path)
959955
* @param mixed $data
960956
* @param Context $options
961957
* @param string $path
962-
* @param null $result
958+
* @param mixed|null $result
963959
* @return array|mixed|null|object|\stdClass
964960
* @throws InvalidValue
965961
* @throws \Exception
@@ -969,20 +965,17 @@ public function process($data, Context $options, $path = '#', $result = null)
969965
{
970966

971967
$import = $options->import;
972-
//$pathTrace = explode('->', $path);
973968

974969
if (!$import && $data instanceof ObjectItemContract) {
975970
$result = new \stdClass();
976971
if ($options->circularReferences->contains($data)) {
977972
/** @noinspection PhpIllegalArrayKeyTypeInspection */
978973
$path = $options->circularReferences[$data];
979974
// @todo $path is not a valid json pointer $ref
980-
$result->{self::REF} = $path;
975+
$result->{self::PROP_REF} = $path;
981976
return $result;
982-
// return $options->circularReferences[$data];
983977
}
984978
$options->circularReferences->attach($data, $path);
985-
//$options->circularReferences->attach($data, $result);
986979

987980
$data = $data->jsonSerialize();
988981
}

src/Structure/ClassStructureTrait.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public static function properties()
4141
/**
4242
* @param mixed $data
4343
* @param Context $options
44-
* @return static
44+
* @return static|mixed
4545
* @throws \Exception
4646
* @throws \Swaggest\JsonSchema\Exception
4747
* @throws \Swaggest\JsonSchema\InvalidValue

tests/src/PHPUnit/ReimportTest.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,8 @@ public function testDoubleImport()
2424
{
2525
$data = file_get_contents(__DIR__ . '/../../../spec/json-schema.json');
2626
$data = json_decode($data);
27-
//print_r($data);
2827

2928
$options = new Context();
30-
$options->applyDefaults = false; // todo check why default values break test
3129

3230
$schema = Schema::import($data, $options);
3331
//print_r(Properties::getFromSchema($schema)->enum);

0 commit comments

Comments
 (0)