From 00976bbd552c4168388565460ba164a400beecc0 Mon Sep 17 00:00:00 2001 From: Dan Wallis Date: Fri, 10 Mar 2023 18:11:35 +0000 Subject: [PATCH 1/8] Update testcase to reflect current behaviour --- .../Repositories/HasEntries/Yes/IsArray/normalized.json | 5 ++++- .../Repositories/HasEntries/Yes/IsArray/original.json | 5 +++-- .../Repositories/HasEntries/Yes/IsObject/normalized.json | 3 +++ .../Repositories/HasEntries/Yes/IsObject/original.json | 5 +++-- 4 files changed, 13 insertions(+), 5 deletions(-) diff --git a/test/Fixture/Vendor/Composer/ComposerJsonNormalizer/NormalizeNormalizesJson/Json/IsObject/HasEntries/Yes/HasProperty/Repositories/HasEntries/Yes/IsArray/normalized.json b/test/Fixture/Vendor/Composer/ComposerJsonNormalizer/NormalizeNormalizesJson/Json/IsObject/HasEntries/Yes/HasProperty/Repositories/HasEntries/Yes/IsArray/normalized.json index 07dc4887..ed5d95e8 100644 --- a/test/Fixture/Vendor/Composer/ComposerJsonNormalizer/NormalizeNormalizesJson/Json/IsObject/HasEntries/Yes/HasProperty/Repositories/HasEntries/Yes/IsArray/normalized.json +++ b/test/Fixture/Vendor/Composer/ComposerJsonNormalizer/NormalizeNormalizesJson/Json/IsObject/HasEntries/Yes/HasProperty/Repositories/HasEntries/Yes/IsArray/normalized.json @@ -17,7 +17,10 @@ "repositories": [ { "type": "composer", - "url": "http://packages.example.com" + "url": "http://packages.example.com", + "only": [ + "example/*" + ] }, { "type": "composer", diff --git a/test/Fixture/Vendor/Composer/ComposerJsonNormalizer/NormalizeNormalizesJson/Json/IsObject/HasEntries/Yes/HasProperty/Repositories/HasEntries/Yes/IsArray/original.json b/test/Fixture/Vendor/Composer/ComposerJsonNormalizer/NormalizeNormalizesJson/Json/IsObject/HasEntries/Yes/HasProperty/Repositories/HasEntries/Yes/IsArray/original.json index bc5ea9f3..a476f8b0 100644 --- a/test/Fixture/Vendor/Composer/ComposerJsonNormalizer/NormalizeNormalizesJson/Json/IsObject/HasEntries/Yes/HasProperty/Repositories/HasEntries/Yes/IsArray/original.json +++ b/test/Fixture/Vendor/Composer/ComposerJsonNormalizer/NormalizeNormalizesJson/Json/IsObject/HasEntries/Yes/HasProperty/Repositories/HasEntries/Yes/IsArray/original.json @@ -16,8 +16,9 @@ "homepage": "https://getcomposer.org/doc/04-schema.md#repositories", "repositories": [ { - "type": "composer", - "url": "http://packages.example.com" + "url": "http://packages.example.com", + "only": [ "example/*" ], + "type": "composer" }, { "type": "composer", diff --git a/test/Fixture/Vendor/Composer/ComposerJsonNormalizer/NormalizeNormalizesJson/Json/IsObject/HasEntries/Yes/HasProperty/Repositories/HasEntries/Yes/IsObject/normalized.json b/test/Fixture/Vendor/Composer/ComposerJsonNormalizer/NormalizeNormalizesJson/Json/IsObject/HasEntries/Yes/HasProperty/Repositories/HasEntries/Yes/IsObject/normalized.json index 2488df56..60fdb243 100644 --- a/test/Fixture/Vendor/Composer/ComposerJsonNormalizer/NormalizeNormalizesJson/Json/IsObject/HasEntries/Yes/HasProperty/Repositories/HasEntries/Yes/IsObject/normalized.json +++ b/test/Fixture/Vendor/Composer/ComposerJsonNormalizer/NormalizeNormalizesJson/Json/IsObject/HasEntries/Yes/HasProperty/Repositories/HasEntries/Yes/IsObject/normalized.json @@ -16,6 +16,9 @@ "homepage": "https://getcomposer.org/doc/04-schema.md#repositories", "repositories": { "foo": { + "only": [ + "foo/*" + ], "type": "composer", "url": "http://packages.foo.com" }, diff --git a/test/Fixture/Vendor/Composer/ComposerJsonNormalizer/NormalizeNormalizesJson/Json/IsObject/HasEntries/Yes/HasProperty/Repositories/HasEntries/Yes/IsObject/original.json b/test/Fixture/Vendor/Composer/ComposerJsonNormalizer/NormalizeNormalizesJson/Json/IsObject/HasEntries/Yes/HasProperty/Repositories/HasEntries/Yes/IsObject/original.json index 6f28e7fa..441409df 100644 --- a/test/Fixture/Vendor/Composer/ComposerJsonNormalizer/NormalizeNormalizesJson/Json/IsObject/HasEntries/Yes/HasProperty/Repositories/HasEntries/Yes/IsObject/original.json +++ b/test/Fixture/Vendor/Composer/ComposerJsonNormalizer/NormalizeNormalizesJson/Json/IsObject/HasEntries/Yes/HasProperty/Repositories/HasEntries/Yes/IsObject/original.json @@ -16,8 +16,9 @@ "homepage": "https://getcomposer.org/doc/04-schema.md#repositories", "repositories": { "foo": { - "type": "composer", - "url": "http://packages.foo.com" + "url": "http://packages.foo.com", + "only": [ "foo/*" ], + "type": "composer" }, "bar": { "type": "composer", From 2839e976b9b53f3e8a83316abbaf9bd08ec834e4 Mon Sep 17 00:00:00 2001 From: Dan Wallis Date: Fri, 10 Mar 2023 21:45:47 +0000 Subject: [PATCH 2/8] Sort Composer repository objects per schema --- psalm-baseline.xml | 4 ---- src/SchemaNormalizer.php | 15 ++++++--------- src/Vendor/Composer/ComposerJsonNormalizer.php | 10 +++++++--- .../HasEntries/Yes/IsObject/normalized.json | 6 +++--- 4 files changed, 16 insertions(+), 19 deletions(-) diff --git a/psalm-baseline.xml b/psalm-baseline.xml index 13c6e8cb..59f23a15 100644 --- a/psalm-baseline.xml +++ b/psalm-baseline.xml @@ -19,7 +19,6 @@ $itemSchema $oneOfSchema $value - $value decoded()]]> @@ -31,9 +30,6 @@ $value $value - - (string) $name - diff --git a/src/SchemaNormalizer.php b/src/SchemaNormalizer.php index 575b3baf..5f99668d 100644 --- a/src/SchemaNormalizer.php +++ b/src/SchemaNormalizer.php @@ -190,8 +190,7 @@ private function normalizeObject( * @see https://json-schema.org/understanding-json-schema/reference/object.html#properties */ if ( - $dataShouldBeSorted - && \property_exists($schema, 'properties') + \property_exists($schema, 'properties') && \is_object($schema->properties) ) { /** @var array $objectPropertiesThatAreDefinedBySchema */ @@ -228,14 +227,12 @@ private function normalizeObject( \ksort($additionalProperties); } - $valueSchema = new \stdClass(); - foreach ($additionalProperties as $name => $value) { - $normalized->{$name} = $this->normalizeData( - $value, - $valueSchema, - $pointerToData->append(Pointer\ReferenceToken::fromString((string) $name)), - ); + $normalized->{$name} = $this->normalizeArray( + [$value], + $schema, + $pointerToData->append(Pointer\ReferenceToken::fromString($name)), + )[0]; } return $normalized; diff --git a/src/Vendor/Composer/ComposerJsonNormalizer.php b/src/Vendor/Composer/ComposerJsonNormalizer.php index 930409d6..3c0ac178 100644 --- a/src/Vendor/Composer/ComposerJsonNormalizer.php +++ b/src/Vendor/Composer/ComposerJsonNormalizer.php @@ -65,7 +65,9 @@ public function __construct(string $schemaUri) * @see https://github.com/composer/installers/blob/v2.2.0/src/Composer/Installers/BaseInstaller.php#L52-L58 * @see https://github.com/composer/installers/blob/v2.2.0/src/Composer/Installers/BaseInstaller.php#L116-L126 */ - Pointer\Specification::equals(Pointer\JsonPointer::fromJsonString('/extra/installer-paths')), + Pointer\Specification::closure(static function (Pointer\JsonPointer $jsonPointer): bool { + return 1 === \preg_match('{^\/extra\/installer-paths\/[^/]+$}', $jsonPointer->toJsonString()); + }), /** * Patches need to be installed in a specific order. * @@ -73,7 +75,7 @@ public function __construct(string $schemaUri) * @see https://github.com/cweagans/composer-patches/blob/1.7.2/src/Patches.php#L315-L329 */ Pointer\Specification::closure(static function (Pointer\JsonPointer $jsonPointer): bool { - return 1 === \preg_match('{^/extra/patches/([^/])+$}', $jsonPointer->toJsonString()); + return 1 === \preg_match('{^\/extra\/patches\/[^/]+\/[^/]+\/[^/]+$}', $jsonPointer->toJsonString()); }), /** * Repositories need to be iterated in a specific order, but can be an array or an object. @@ -87,7 +89,9 @@ public function __construct(string $schemaUri) * * @see https://github.com/symfony/flex/blob/v2.2.3/src/Flex.php#L517-L519 */ - Pointer\Specification::equals(Pointer\JsonPointer::fromJsonString('/scripts/auto-scripts')), + Pointer\Specification::closure(static function (Pointer\JsonPointer $jsonPointer): bool { + return 1 === \preg_match('{^\/scripts\/auto-scripts\/[^/]+$}', $jsonPointer->toJsonString()); + }), ), ), new BinNormalizer(), diff --git a/test/Fixture/Vendor/Composer/ComposerJsonNormalizer/NormalizeNormalizesJson/Json/IsObject/HasEntries/Yes/HasProperty/Repositories/HasEntries/Yes/IsObject/normalized.json b/test/Fixture/Vendor/Composer/ComposerJsonNormalizer/NormalizeNormalizesJson/Json/IsObject/HasEntries/Yes/HasProperty/Repositories/HasEntries/Yes/IsObject/normalized.json index 60fdb243..fd2553c4 100644 --- a/test/Fixture/Vendor/Composer/ComposerJsonNormalizer/NormalizeNormalizesJson/Json/IsObject/HasEntries/Yes/HasProperty/Repositories/HasEntries/Yes/IsObject/normalized.json +++ b/test/Fixture/Vendor/Composer/ComposerJsonNormalizer/NormalizeNormalizesJson/Json/IsObject/HasEntries/Yes/HasProperty/Repositories/HasEntries/Yes/IsObject/normalized.json @@ -16,11 +16,11 @@ "homepage": "https://getcomposer.org/doc/04-schema.md#repositories", "repositories": { "foo": { + "type": "composer", + "url": "http://packages.foo.com", "only": [ "foo/*" - ], - "type": "composer", - "url": "http://packages.foo.com" + ] }, "bar": { "type": "composer", From d4ad6e0833a045b838c4985d80a809dcee95cb20 Mon Sep 17 00:00:00 2001 From: Dan Wallis Date: Fri, 10 Mar 2023 21:46:36 +0000 Subject: [PATCH 3/8] Update tests to match new behaviour --- .../HasType/IsScalar/WithPropertyDefinitions/normalized.json | 4 ++-- .../normalized.json | 4 ++-- .../IsScalar/WithoutPropertyDefinitions/normalized.json | 4 ++-- .../Json/IsObject/Schema/IsEmpty/normalized.json | 4 ++-- 4 files changed, 8 insertions(+), 8 deletions(-) diff --git a/test/Fixture/SchemaNormalizer/NormalizeNormalizesJson/WithCustomJsonPointerSpecification/Json/IsObject/Schema/HasType/IsScalar/WithPropertyDefinitions/normalized.json b/test/Fixture/SchemaNormalizer/NormalizeNormalizesJson/WithCustomJsonPointerSpecification/Json/IsObject/Schema/HasType/IsScalar/WithPropertyDefinitions/normalized.json index cde92d35..6254507b 100644 --- a/test/Fixture/SchemaNormalizer/NormalizeNormalizesJson/WithCustomJsonPointerSpecification/Json/IsObject/Schema/HasType/IsScalar/WithPropertyDefinitions/normalized.json +++ b/test/Fixture/SchemaNormalizer/NormalizeNormalizesJson/WithCustomJsonPointerSpecification/Json/IsObject/Schema/HasType/IsScalar/WithPropertyDefinitions/normalized.json @@ -2,8 +2,8 @@ "foo": {}, "bar": { "baz": { - "qux": "quux", - "quuz": "corge" + "quuz": "corge", + "qux": "quux" }, "grault": { "fred": "plugh", diff --git a/test/Fixture/SchemaNormalizer/NormalizeNormalizesJson/WithCustomJsonPointerSpecification/Json/IsObject/Schema/HasType/IsScalar/WithPropertyDefinitionsAndAdditionalProperties/normalized.json b/test/Fixture/SchemaNormalizer/NormalizeNormalizesJson/WithCustomJsonPointerSpecification/Json/IsObject/Schema/HasType/IsScalar/WithPropertyDefinitionsAndAdditionalProperties/normalized.json index 82b9a450..09b22c65 100644 --- a/test/Fixture/SchemaNormalizer/NormalizeNormalizesJson/WithCustomJsonPointerSpecification/Json/IsObject/Schema/HasType/IsScalar/WithPropertyDefinitionsAndAdditionalProperties/normalized.json +++ b/test/Fixture/SchemaNormalizer/NormalizeNormalizesJson/WithCustomJsonPointerSpecification/Json/IsObject/Schema/HasType/IsScalar/WithPropertyDefinitionsAndAdditionalProperties/normalized.json @@ -2,8 +2,8 @@ "foo": {}, "bar": { "baz": { - "qux": "quux", - "quuz": "corge" + "quuz": "corge", + "qux": "quux" }, "grault": { "fred": "plugh", diff --git a/test/Fixture/SchemaNormalizer/NormalizeNormalizesJson/WithCustomJsonPointerSpecification/Json/IsObject/Schema/HasType/IsScalar/WithoutPropertyDefinitions/normalized.json b/test/Fixture/SchemaNormalizer/NormalizeNormalizesJson/WithCustomJsonPointerSpecification/Json/IsObject/Schema/HasType/IsScalar/WithoutPropertyDefinitions/normalized.json index 4e66769d..a98bed56 100644 --- a/test/Fixture/SchemaNormalizer/NormalizeNormalizesJson/WithCustomJsonPointerSpecification/Json/IsObject/Schema/HasType/IsScalar/WithoutPropertyDefinitions/normalized.json +++ b/test/Fixture/SchemaNormalizer/NormalizeNormalizesJson/WithCustomJsonPointerSpecification/Json/IsObject/Schema/HasType/IsScalar/WithoutPropertyDefinitions/normalized.json @@ -1,8 +1,8 @@ { "bar": { "baz": { - "qux": "quux", - "quuz": "corge" + "quuz": "corge", + "qux": "quux" }, "grault": { "fred": "plugh", diff --git a/test/Fixture/SchemaNormalizer/NormalizeNormalizesJson/WithCustomJsonPointerSpecification/Json/IsObject/Schema/IsEmpty/normalized.json b/test/Fixture/SchemaNormalizer/NormalizeNormalizesJson/WithCustomJsonPointerSpecification/Json/IsObject/Schema/IsEmpty/normalized.json index 8b7cd8a4..11ac911d 100644 --- a/test/Fixture/SchemaNormalizer/NormalizeNormalizesJson/WithCustomJsonPointerSpecification/Json/IsObject/Schema/IsEmpty/normalized.json +++ b/test/Fixture/SchemaNormalizer/NormalizeNormalizesJson/WithCustomJsonPointerSpecification/Json/IsObject/Schema/IsEmpty/normalized.json @@ -1,12 +1,12 @@ { "bar": [ { - "foo": 9000, "bar": [ "foo", "bar", "baz" - ] + ], + "foo": 9000 }, { "foo": 3.1415 From d986041294786ebb9cc86f3bce7a7ca26096d23b Mon Sep 17 00:00:00 2001 From: Dan Wallis Date: Tue, 14 Mar 2023 12:42:50 +0000 Subject: [PATCH 4/8] Remove unnecessary escape characters --- src/Vendor/Composer/ComposerJsonNormalizer.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Vendor/Composer/ComposerJsonNormalizer.php b/src/Vendor/Composer/ComposerJsonNormalizer.php index 3c0ac178..263d1a8e 100644 --- a/src/Vendor/Composer/ComposerJsonNormalizer.php +++ b/src/Vendor/Composer/ComposerJsonNormalizer.php @@ -66,7 +66,7 @@ public function __construct(string $schemaUri) * @see https://github.com/composer/installers/blob/v2.2.0/src/Composer/Installers/BaseInstaller.php#L116-L126 */ Pointer\Specification::closure(static function (Pointer\JsonPointer $jsonPointer): bool { - return 1 === \preg_match('{^\/extra\/installer-paths\/[^/]+$}', $jsonPointer->toJsonString()); + return 1 === \preg_match('{^/extra/installer-paths/[^/]+$}', $jsonPointer->toJsonString()); }), /** * Patches need to be installed in a specific order. @@ -75,7 +75,7 @@ public function __construct(string $schemaUri) * @see https://github.com/cweagans/composer-patches/blob/1.7.2/src/Patches.php#L315-L329 */ Pointer\Specification::closure(static function (Pointer\JsonPointer $jsonPointer): bool { - return 1 === \preg_match('{^\/extra\/patches\/[^/]+\/[^/]+\/[^/]+$}', $jsonPointer->toJsonString()); + return 1 === \preg_match('{^/extra/patches/[^/]+/[^/]+/[^/]+$}', $jsonPointer->toJsonString()); }), /** * Repositories need to be iterated in a specific order, but can be an array or an object. @@ -90,7 +90,7 @@ public function __construct(string $schemaUri) * @see https://github.com/symfony/flex/blob/v2.2.3/src/Flex.php#L517-L519 */ Pointer\Specification::closure(static function (Pointer\JsonPointer $jsonPointer): bool { - return 1 === \preg_match('{^\/scripts\/auto-scripts\/[^/]+$}', $jsonPointer->toJsonString()); + return 1 === \preg_match('{^/scripts/auto-scripts/[^/]+$}', $jsonPointer->toJsonString()); }), ), ), From ec8e9d73a9067f3a4838faf498249794f80f56f4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andreas=20M=C3=B6ller?= Date: Tue, 2 May 2023 10:58:10 +0200 Subject: [PATCH 5/8] Fix: Cast to string --- psalm-baseline.xml | 3 +++ src/SchemaNormalizer.php | 2 +- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/psalm-baseline.xml b/psalm-baseline.xml index 59f23a15..ae32e935 100644 --- a/psalm-baseline.xml +++ b/psalm-baseline.xml @@ -30,6 +30,9 @@ $value $value + + (string) $name + diff --git a/src/SchemaNormalizer.php b/src/SchemaNormalizer.php index 5f99668d..526da7e7 100644 --- a/src/SchemaNormalizer.php +++ b/src/SchemaNormalizer.php @@ -231,7 +231,7 @@ private function normalizeObject( $normalized->{$name} = $this->normalizeArray( [$value], $schema, - $pointerToData->append(Pointer\ReferenceToken::fromString($name)), + $pointerToData->append(Pointer\ReferenceToken::fromString((string) $name)), )[0]; } From 3f249d0359b0b7c832873d24afd8ae59d02fef6a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andreas=20M=C3=B6ller?= Date: Tue, 2 May 2023 13:00:17 +0200 Subject: [PATCH 6/8] Fix: Revert unrelated changes to tests --- .../HasType/IsScalar/WithPropertyDefinitions/normalized.json | 4 ++-- .../normalized.json | 4 ++-- .../IsScalar/WithoutPropertyDefinitions/normalized.json | 4 ++-- .../Json/IsObject/Schema/IsEmpty/normalized.json | 4 ++-- 4 files changed, 8 insertions(+), 8 deletions(-) diff --git a/test/Fixture/SchemaNormalizer/NormalizeNormalizesJson/WithCustomJsonPointerSpecification/Json/IsObject/Schema/HasType/IsScalar/WithPropertyDefinitions/normalized.json b/test/Fixture/SchemaNormalizer/NormalizeNormalizesJson/WithCustomJsonPointerSpecification/Json/IsObject/Schema/HasType/IsScalar/WithPropertyDefinitions/normalized.json index 6254507b..cde92d35 100644 --- a/test/Fixture/SchemaNormalizer/NormalizeNormalizesJson/WithCustomJsonPointerSpecification/Json/IsObject/Schema/HasType/IsScalar/WithPropertyDefinitions/normalized.json +++ b/test/Fixture/SchemaNormalizer/NormalizeNormalizesJson/WithCustomJsonPointerSpecification/Json/IsObject/Schema/HasType/IsScalar/WithPropertyDefinitions/normalized.json @@ -2,8 +2,8 @@ "foo": {}, "bar": { "baz": { - "quuz": "corge", - "qux": "quux" + "qux": "quux", + "quuz": "corge" }, "grault": { "fred": "plugh", diff --git a/test/Fixture/SchemaNormalizer/NormalizeNormalizesJson/WithCustomJsonPointerSpecification/Json/IsObject/Schema/HasType/IsScalar/WithPropertyDefinitionsAndAdditionalProperties/normalized.json b/test/Fixture/SchemaNormalizer/NormalizeNormalizesJson/WithCustomJsonPointerSpecification/Json/IsObject/Schema/HasType/IsScalar/WithPropertyDefinitionsAndAdditionalProperties/normalized.json index 09b22c65..82b9a450 100644 --- a/test/Fixture/SchemaNormalizer/NormalizeNormalizesJson/WithCustomJsonPointerSpecification/Json/IsObject/Schema/HasType/IsScalar/WithPropertyDefinitionsAndAdditionalProperties/normalized.json +++ b/test/Fixture/SchemaNormalizer/NormalizeNormalizesJson/WithCustomJsonPointerSpecification/Json/IsObject/Schema/HasType/IsScalar/WithPropertyDefinitionsAndAdditionalProperties/normalized.json @@ -2,8 +2,8 @@ "foo": {}, "bar": { "baz": { - "quuz": "corge", - "qux": "quux" + "qux": "quux", + "quuz": "corge" }, "grault": { "fred": "plugh", diff --git a/test/Fixture/SchemaNormalizer/NormalizeNormalizesJson/WithCustomJsonPointerSpecification/Json/IsObject/Schema/HasType/IsScalar/WithoutPropertyDefinitions/normalized.json b/test/Fixture/SchemaNormalizer/NormalizeNormalizesJson/WithCustomJsonPointerSpecification/Json/IsObject/Schema/HasType/IsScalar/WithoutPropertyDefinitions/normalized.json index a98bed56..4e66769d 100644 --- a/test/Fixture/SchemaNormalizer/NormalizeNormalizesJson/WithCustomJsonPointerSpecification/Json/IsObject/Schema/HasType/IsScalar/WithoutPropertyDefinitions/normalized.json +++ b/test/Fixture/SchemaNormalizer/NormalizeNormalizesJson/WithCustomJsonPointerSpecification/Json/IsObject/Schema/HasType/IsScalar/WithoutPropertyDefinitions/normalized.json @@ -1,8 +1,8 @@ { "bar": { "baz": { - "quuz": "corge", - "qux": "quux" + "qux": "quux", + "quuz": "corge" }, "grault": { "fred": "plugh", diff --git a/test/Fixture/SchemaNormalizer/NormalizeNormalizesJson/WithCustomJsonPointerSpecification/Json/IsObject/Schema/IsEmpty/normalized.json b/test/Fixture/SchemaNormalizer/NormalizeNormalizesJson/WithCustomJsonPointerSpecification/Json/IsObject/Schema/IsEmpty/normalized.json index 11ac911d..8b7cd8a4 100644 --- a/test/Fixture/SchemaNormalizer/NormalizeNormalizesJson/WithCustomJsonPointerSpecification/Json/IsObject/Schema/IsEmpty/normalized.json +++ b/test/Fixture/SchemaNormalizer/NormalizeNormalizesJson/WithCustomJsonPointerSpecification/Json/IsObject/Schema/IsEmpty/normalized.json @@ -1,12 +1,12 @@ { "bar": [ { + "foo": 9000, "bar": [ "foo", "bar", "baz" - ], - "foo": 9000 + ] }, { "foo": 3.1415 From edd25f68bcf12a090a268393eafcfa8ef573f326 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andreas=20M=C3=B6ller?= Date: Tue, 2 May 2023 13:01:01 +0200 Subject: [PATCH 7/8] Fix: Use schema for additional properties when present --- psalm-baseline.xml | 1 + src/SchemaNormalizer.php | 23 +++++++++++++++---- .../Composer/ComposerJsonNormalizer.php | 10 +++----- 3 files changed, 22 insertions(+), 12 deletions(-) diff --git a/psalm-baseline.xml b/psalm-baseline.xml index ae32e935..13c6e8cb 100644 --- a/psalm-baseline.xml +++ b/psalm-baseline.xml @@ -19,6 +19,7 @@ $itemSchema $oneOfSchema $value + $value decoded()]]> diff --git a/src/SchemaNormalizer.php b/src/SchemaNormalizer.php index 526da7e7..4dabc2ce 100644 --- a/src/SchemaNormalizer.php +++ b/src/SchemaNormalizer.php @@ -190,7 +190,8 @@ private function normalizeObject( * @see https://json-schema.org/understanding-json-schema/reference/object.html#properties */ if ( - \property_exists($schema, 'properties') + $dataShouldBeSorted + && \property_exists($schema, 'properties') && \is_object($schema->properties) ) { /** @var array $objectPropertiesThatAreDefinedBySchema */ @@ -227,12 +228,24 @@ private function normalizeObject( \ksort($additionalProperties); } + $valueSchema = new \stdClass(); + + /** + * @see https://json-schema.org/understanding-json-schema/reference/object.html#additional-properties + */ + if ( + \property_exists($schema, 'additionalProperties') + && \is_object($schema->additionalProperties) + ) { + $valueSchema = $schema->additionalProperties; + } + foreach ($additionalProperties as $name => $value) { - $normalized->{$name} = $this->normalizeArray( - [$value], - $schema, + $normalized->{$name} = $this->normalizeData( + $value, + $valueSchema, $pointerToData->append(Pointer\ReferenceToken::fromString((string) $name)), - )[0]; + ); } return $normalized; diff --git a/src/Vendor/Composer/ComposerJsonNormalizer.php b/src/Vendor/Composer/ComposerJsonNormalizer.php index 263d1a8e..930409d6 100644 --- a/src/Vendor/Composer/ComposerJsonNormalizer.php +++ b/src/Vendor/Composer/ComposerJsonNormalizer.php @@ -65,9 +65,7 @@ public function __construct(string $schemaUri) * @see https://github.com/composer/installers/blob/v2.2.0/src/Composer/Installers/BaseInstaller.php#L52-L58 * @see https://github.com/composer/installers/blob/v2.2.0/src/Composer/Installers/BaseInstaller.php#L116-L126 */ - Pointer\Specification::closure(static function (Pointer\JsonPointer $jsonPointer): bool { - return 1 === \preg_match('{^/extra/installer-paths/[^/]+$}', $jsonPointer->toJsonString()); - }), + Pointer\Specification::equals(Pointer\JsonPointer::fromJsonString('/extra/installer-paths')), /** * Patches need to be installed in a specific order. * @@ -75,7 +73,7 @@ public function __construct(string $schemaUri) * @see https://github.com/cweagans/composer-patches/blob/1.7.2/src/Patches.php#L315-L329 */ Pointer\Specification::closure(static function (Pointer\JsonPointer $jsonPointer): bool { - return 1 === \preg_match('{^/extra/patches/[^/]+/[^/]+/[^/]+$}', $jsonPointer->toJsonString()); + return 1 === \preg_match('{^/extra/patches/([^/])+$}', $jsonPointer->toJsonString()); }), /** * Repositories need to be iterated in a specific order, but can be an array or an object. @@ -89,9 +87,7 @@ public function __construct(string $schemaUri) * * @see https://github.com/symfony/flex/blob/v2.2.3/src/Flex.php#L517-L519 */ - Pointer\Specification::closure(static function (Pointer\JsonPointer $jsonPointer): bool { - return 1 === \preg_match('{^/scripts/auto-scripts/[^/]+$}', $jsonPointer->toJsonString()); - }), + Pointer\Specification::equals(Pointer\JsonPointer::fromJsonString('/scripts/auto-scripts')), ), ), new BinNormalizer(), From c4b3db34f018da8eba98ef58ec98c8342628e0da Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andreas=20M=C3=B6ller?= Date: Tue, 2 May 2023 13:03:46 +0200 Subject: [PATCH 8/8] Fix: Update CHANGELOG.md --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index b9a273d7..b5ebd2f3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,6 +16,7 @@ For a full diff see [`4.0.2...main`][4.0.2...main]. ### Fixed - Adjusted `SchemaNormalizer` to account for objects with string and integer-like properties ([#868]), by [@alexis-saransig-lullabot] and [@fredden] +- Adjusted `SchemaNormalizer` to account for objects where schema describes additional properties ([#873]), by [@fredden] and [@localheinz] ## [`4.0.2`][4.0.2] @@ -608,6 +609,7 @@ For a full diff see [`5d8b3e2...0.1.0`][5d8b3e2...0.1.0]. [#861]: https://github.com/ergebnis/json-normalizer/pull/861 [#863]: https://github.com/ergebnis/json-normalizer/pull/863 [#868]: https://github.com/ergebnis/json-normalizer/pull/868 +[#873]: https://github.com/ergebnis/json-normalizer/pull/873 [#889]: https://github.com/ergebnis/json-normalizer/pull/889 [@alexis-saransig-lullabot]: https://github.com/alexis-saransig-lullabot