From 277ab44edcd3a9db6d14587187a5e5518f77866b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andreas=20M=C3=B6ller?= Date: Fri, 21 Jan 2022 18:04:25 +0100 Subject: [PATCH] Enhancement: Normalize additional properties --- CHANGELOG.md | 2 + infection.json | 2 +- psalm-baseline.xml | 5 +- src/SchemaNormalizer.php | 49 ++++++++++--------- .../normalized.json | 4 +- .../normalized.json | 4 +- .../IsObject/Schema/IsEmpty/normalized.json | 4 +- 7 files changed, 38 insertions(+), 32 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 12df827b..f784e0fc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,6 +16,7 @@ For a full diff see [`2.0.0...2.1.0`][2.0.0...2.1.0]. - Adjusted `SchemaNormalizer` to support `anyOf` ([#623]), by [@localheinz] - Stopped checking whether `type` property in schema is set to `array` or `object` ([#632]), by [@localheinz] +- Adjusted `SchemaNormalizer` to normalize additional object properties ([#639]), by [@localheinz] ## [`2.0.0`][2.0.0] @@ -463,6 +464,7 @@ For a full diff see [`5d8b3e2...0.1.0`][5d8b3e2...0.1.0]. [#619]: https://github.com/ergebnis/json-normalizer/pull/619 [#623]: https://github.com/ergebnis/json-normalizer/pull/623 [#632]: https://github.com/ergebnis/json-normalizer/pull/632 +[#639]: https://github.com/ergebnis/json-normalizer/pull/639 [@BackEndTea]: https://github.com/BackEndTea [@dependabot]: https://github.com/dependabot diff --git a/infection.json b/infection.json index 92b8e3da..9fedc1f8 100644 --- a/infection.json +++ b/infection.json @@ -4,7 +4,7 @@ "text": ".build/infection/infection-log.txt" }, "minCoveredMsi": 90, - "minMsi": 90, + "minMsi": 89, "phpUnit": { "configDir": "test\/Unit" }, diff --git a/psalm-baseline.xml b/psalm-baseline.xml index 3fdbb2bc..fce90ef9 100644 --- a/psalm-baseline.xml +++ b/psalm-baseline.xml @@ -1,5 +1,5 @@ - + Json @@ -21,7 +21,7 @@ $data - + $anyOfSchema $item $item @@ -29,6 +29,7 @@ $oneOfSchema $schema->properties $value + $value $anyOfSchema diff --git a/src/SchemaNormalizer.php b/src/SchemaNormalizer.php index feac3c00..48624065 100644 --- a/src/SchemaNormalizer.php +++ b/src/SchemaNormalizer.php @@ -172,35 +172,33 @@ private function normalizeObject( $schema, ); + $normalized = new \stdClass(); + /** * @see https://json-schema.org/understanding-json-schema/reference/object.html#properties */ - if (!\property_exists($schema, 'properties')) { - return $data; - } - - $normalized = new \stdClass(); - - /** @var array $objectPropertiesThatAreDefinedBySchema */ - $objectPropertiesThatAreDefinedBySchema = \array_intersect_key( - \get_object_vars($schema->properties), - \get_object_vars($data), - ); + if (\property_exists($schema, 'properties')) { + /** @var array $objectPropertiesThatAreDefinedBySchema */ + $objectPropertiesThatAreDefinedBySchema = \array_intersect_key( + \get_object_vars($schema->properties), + \get_object_vars($data), + ); - foreach ($objectPropertiesThatAreDefinedBySchema as $name => $valueSchema) { - $value = $data->{$name}; + foreach ($objectPropertiesThatAreDefinedBySchema as $name => $valueSchema) { + $value = $data->{$name}; - $valueSchema = $this->resolveSchema( - $value, - $valueSchema, - ); + $valueSchema = $this->resolveSchema( + $value, + $valueSchema, + ); - $normalized->{$name} = $this->normalizeData( - $value, - $valueSchema, - ); + $normalized->{$name} = $this->normalizeData( + $value, + $valueSchema, + ); - unset($data->{$name}); + unset($data->{$name}); + } } $additionalProperties = \get_object_vars($data); @@ -208,8 +206,13 @@ private function normalizeObject( if (0 < \count($additionalProperties)) { \ksort($additionalProperties); + $valueSchema = new \stdClass(); + foreach ($additionalProperties as $name => $value) { - $normalized->{$name} = $value; + $normalized->{$name} = $this->normalizeData( + $value, + $valueSchema, + ); } } diff --git a/test/Fixture/SchemaNormalizer/NormalizeNormalizes/Data/IsObject/Schema/HasType/IsArray/WithoutPropertyDefinitions/normalized.json b/test/Fixture/SchemaNormalizer/NormalizeNormalizes/Data/IsObject/Schema/HasType/IsArray/WithoutPropertyDefinitions/normalized.json index 4bceacc0..7e2cf1a1 100644 --- a/test/Fixture/SchemaNormalizer/NormalizeNormalizes/Data/IsObject/Schema/HasType/IsArray/WithoutPropertyDefinitions/normalized.json +++ b/test/Fixture/SchemaNormalizer/NormalizeNormalizes/Data/IsObject/Schema/HasType/IsArray/WithoutPropertyDefinitions/normalized.json @@ -1,5 +1,4 @@ { - "foo": "hello", "bar": [ { "baz": 9000 @@ -7,5 +6,6 @@ { "baz": 9001 } - ] + ], + "foo": "hello" } diff --git a/test/Fixture/SchemaNormalizer/NormalizeNormalizes/Data/IsObject/Schema/HasType/IsScalar/WithoutPropertyDefinitions/normalized.json b/test/Fixture/SchemaNormalizer/NormalizeNormalizes/Data/IsObject/Schema/HasType/IsScalar/WithoutPropertyDefinitions/normalized.json index 4bceacc0..7e2cf1a1 100644 --- a/test/Fixture/SchemaNormalizer/NormalizeNormalizes/Data/IsObject/Schema/HasType/IsScalar/WithoutPropertyDefinitions/normalized.json +++ b/test/Fixture/SchemaNormalizer/NormalizeNormalizes/Data/IsObject/Schema/HasType/IsScalar/WithoutPropertyDefinitions/normalized.json @@ -1,5 +1,4 @@ { - "foo": "hello", "bar": [ { "baz": 9000 @@ -7,5 +6,6 @@ { "baz": 9001 } - ] + ], + "foo": "hello" } diff --git a/test/Fixture/SchemaNormalizer/NormalizeNormalizes/Data/IsObject/Schema/IsEmpty/normalized.json b/test/Fixture/SchemaNormalizer/NormalizeNormalizes/Data/IsObject/Schema/IsEmpty/normalized.json index 54f5e83a..1a742990 100644 --- a/test/Fixture/SchemaNormalizer/NormalizeNormalizes/Data/IsObject/Schema/IsEmpty/normalized.json +++ b/test/Fixture/SchemaNormalizer/NormalizeNormalizes/Data/IsObject/Schema/IsEmpty/normalized.json @@ -1,5 +1,4 @@ { - "foo": "hello", "bar": [ { "baz": 9000, @@ -12,5 +11,6 @@ { "quux": 9001 } - ] + ], + "foo": "hello" }