Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Adjust ComposerJsonNormalizer to reject JSON when it is not an object #804

Merged
merged 1 commit into from
Dec 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ For a full diff see [`3.0.0...main`][3.0.0...main].
- Adjusted `Vendor\Composer\BinNormalizer`, `Vendor\Composer\PackageHashNormalizer`, `Vendor\Composer\VersionConstraintNormalizer`, and `SchemaNormalizer` to encode JSON with `JSON_PRETTY_PRINT` flag ([#795]), by [@localheinz]
- Adjusted `Vendor\Composer\BinNormalizer`, `Vendor\Composer\PackageHashNormalizer`, `Vendor\Composer\VersionConstraintNormalizer`, and `SchemaNormalizer` to encode JSON with `JSON_UNESCAPED_SLASHES` flag ([#801]), by [@localheinz]
- Adjusted `Vendor\Composer\BinNormalizer`, `Vendor\Composer\PackageHashNormalizer`, `Vendor\Composer\VersionConstraintNormalizer`, and `SchemaNormalizer` to encode JSON with `JSON_UNESCAPED_UNICODE` flag ([#802]), by [@localheinz]
- Adjusted `Vendor\Composer\ComposerJsonNormalizer` to reject JSON when it is not an object ([#804]), by [@localheinz]

### Fixed

Expand Down Expand Up @@ -552,6 +553,7 @@ For a full diff see [`5d8b3e2...0.1.0`][5d8b3e2...0.1.0].
[#795]: https://github.com/ergebnis/json-normalizer/pull/795
[#801]: https://github.com/ergebnis/json-normalizer/pull/801
[#802]: https://github.com/ergebnis/json-normalizer/pull/802
[#804]: https://github.com/ergebnis/json-normalizer/pull/804

[@BackEndTea]: https://github.com/BackEndTea
[@dependabot]: https://github.com/dependabot
Expand Down
3 changes: 2 additions & 1 deletion psalm-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@
</MixedAssignment>
</file>
<file src="test/Unit/Vendor/Composer/ComposerJsonNormalizerTest.php">
<MixedAssignment occurrences="1">
<MixedAssignment occurrences="2">
<code>$fileInfo</code>
<code>$fileInfo</code>
</MixedAssignment>
</file>
Expand Down
4 changes: 0 additions & 4 deletions src/Vendor/Composer/ComposerJsonNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,6 @@ public function __construct(string $schemaUri)

public function normalize(Json $json): Json
{
if (!\is_object($json->decoded())) {
return $json;
}

return $this->normalizer->normalize($json);
}
}
78 changes: 75 additions & 3 deletions test/Unit/Vendor/Composer/ComposerJsonNormalizerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
namespace Ergebnis\Json\Normalizer\Test\Unit\Vendor\Composer;

use Ergebnis\Json\Json;
use Ergebnis\Json\Normalizer\Exception;
use Ergebnis\Json\Normalizer\Test;
use Ergebnis\Json\Normalizer\Vendor;
use PHPUnit\Framework;
Expand All @@ -25,6 +26,7 @@
* @covers \Ergebnis\Json\Normalizer\Vendor\Composer\ComposerJsonNormalizer
*
* @uses \Ergebnis\Json\Normalizer\ChainNormalizer
* @uses \Ergebnis\Json\Normalizer\Exception\OriginalInvalidAccordingToSchema
* @uses \Ergebnis\Json\Normalizer\Format\JsonEncodeOptions
* @uses \Ergebnis\Json\Normalizer\SchemaNormalizer
* @uses \Ergebnis\Json\Normalizer\Vendor\Composer\PackageHashNormalizer
Expand All @@ -35,9 +37,79 @@ final class ComposerJsonNormalizerTest extends Framework\TestCase
use Test\Util\Helper;

/**
* @dataProvider provideScenario
* @dataProvider provideScenarioWhereJsonIsInvalidAccordingToSchema
*/
public function testNormalizeNormalizes(Test\Fixture\Vendor\Composer\Scenario $scenario): void
public function testNormalizeRejectsJsonWhenItIsInvalidAccordingToSchema(Test\Fixture\Vendor\Composer\Scenario $scenario): void
{
$json = $scenario->original();

$normalizer = new Vendor\Composer\ComposerJsonNormalizer(\sprintf(
'file://%s',
\realpath(__DIR__ . '/../../../Fixture/Vendor/Composer/schema.json'),
));

$this->expectException(Exception\OriginalInvalidAccordingToSchema::class);

$normalizer->normalize($json);
}

/**
* @return \Generator<string, array{0: Test\Fixture\Vendor\Composer\Scenario}>
*/
public static function provideScenarioWhereJsonIsInvalidAccordingToSchema(): \Generator
{
$basePath = __DIR__ . '/../../../';

$iterator = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator(__DIR__ . '/../../../Fixture/Vendor/Composer/ComposerJsonNormalizer/NormalizeRejectsJson'));

foreach ($iterator as $fileInfo) {
/** @var \SplFileInfo $fileInfo */
if (!$fileInfo->isFile()) {
continue;
}

if ('original.json' !== $fileInfo->getBasename()) {
continue;
}

$originalFile = $fileInfo->getRealPath();

$normalizedFile = \preg_replace(
'/original\.json$/',
'normalized.json',
$originalFile,
);

if (!\is_string($normalizedFile)) {
throw new \RuntimeException(\sprintf(
'Unable to deduce normalized JSON file name from original JSON file name "%s".',
$originalFile,
));
}

if (!\file_exists($normalizedFile)) {
$normalizedFile = $originalFile;
}

$key = \substr(
$fileInfo->getPath(),
\strlen($basePath),
);

yield $key => [
Test\Fixture\Vendor\Composer\Scenario::create(
$key,
Json::fromFile($originalFile),
Json::fromFile($normalizedFile),
),
];
}
}

/**
* @dataProvider provideScenarioWhereJsonIsValidAccordingToSchema
*/
public function testNormalizeNormalizesJsonWhenItIsValidAccordingToSchema(Test\Fixture\Vendor\Composer\Scenario $scenario): void
{
$json = $scenario->original();

Expand All @@ -54,7 +126,7 @@ public function testNormalizeNormalizes(Test\Fixture\Vendor\Composer\Scenario $s
/**
* @return \Generator<string, array{0: Test\Fixture\Vendor\Composer\Scenario}>
*/
public static function provideScenario(): \Generator
public static function provideScenarioWhereJsonIsValidAccordingToSchema(): \Generator
{
$basePath = __DIR__ . '/../../../';

Expand Down