Skip to content

Commit

Permalink
Fix: Use schema for additional properties when present
Browse files Browse the repository at this point in the history
  • Loading branch information
localheinz committed May 2, 2023
1 parent 3f249d0 commit edd25f6
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 12 deletions.
1 change: 1 addition & 0 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
<code>$itemSchema</code>
<code>$oneOfSchema</code>
<code>$value</code>
<code>$value</code>
</MixedArgument>
<MixedArgumentTypeCoercion>
<code><![CDATA[$json->decoded()]]></code>
Expand Down
23 changes: 18 additions & 5 deletions src/SchemaNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, object> $objectPropertiesThatAreDefinedBySchema */
Expand Down Expand Up @@ -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;
Expand Down
10 changes: 3 additions & 7 deletions src/Vendor/Composer/ComposerJsonNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,17 +65,15 @@ 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.
*
* @see https://github.com/cweagans/composer-patches/blob/1.7.2/src/Patches.php#L229-L234
* @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.
Expand All @@ -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(),
Expand Down

0 comments on commit edd25f6

Please sign in to comment.