Skip to content

Fix objects are non unique despite key order #819

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

Merged
merged 3 commits into from
Apr 11, 2025
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 @@ -6,6 +6,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]
### Fixed
- Fix objects are non-unique despite key order ([#819](https://github.com/jsonrainbow/json-schema/pull/819))

## [6.4.1] - 2025-04-04
### Fixed
Expand Down
17 changes: 9 additions & 8 deletions src/JsonSchema/Constraints/CollectionConstraint.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use JsonSchema\ConstraintError;
use JsonSchema\Entity\JsonPointer;
use JsonSchema\Tool\DeepComparer;

/**
* The CollectionConstraint Constraints, validates an array against a given schema
Expand All @@ -39,14 +40,14 @@ public function check(&$value, $schema = null, ?JsonPointer $path = null, $i = n

// Verify uniqueItems
if (isset($schema->uniqueItems) && $schema->uniqueItems) {
$unique = $value;
if (is_array($value) && count($value)) {
$unique = array_map(function ($e) {
return var_export($e, true);
}, $value);
}
if (count(array_unique($unique)) != count($value)) {
$this->addError(ConstraintError::UNIQUE_ITEMS(), $path);
$count = count($value);
for ($x = 0; $x < $count - 1; $x++) {
for ($y = $x + 1; $y < $count; $y++) {
if (DeepComparer::isEqual($value[$x], $value[$y])) {
$this->addError(ConstraintError::UNIQUE_ITEMS(), $path);
break 2;
}
}
}
}

Expand Down
122 changes: 63 additions & 59 deletions tests/Constraints/UniqueItemsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,142 +16,146 @@ class UniqueItemsTest extends BaseTestCase
public function getInvalidTests(): array
{
return [
[
'[1,2,2]',
'{
'Non unique integers' => [
'input' => '[1,2,2]',
'schema' => '{
"type":"array",
"uniqueItems": true
}'
],
[
'[{"a":"b"},{"a":"c"},{"a":"b"}]',
'{
'Non unique objects' => [
'input' => '[{"a":"b"},{"a":"c"},{"a":"b"}]',
'schema' => '{
"type":"array",
"uniqueItems": true
}'
],
[
'[{"foo": {"bar" : {"baz" : true}}}, {"foo": {"bar" : {"baz" : true}}}]',
'{
'Non unique objects - three levels deep' => [
'input' => '[{"foo": {"bar" : {"baz" : true}}}, {"foo": {"bar" : {"baz" : true}}}]',
'schema' => '{
"type": "array",
"uniqueItems": true
}'
],
[
'[1.0, 1.00, 1]',
'{
'Non unique mathematical values for the number one' => [
'input' => '[1.0, 1.00, 1]',
'schema' => '{
"type": "array",
"uniqueItems": true
}'
],
[
'[["foo"], ["foo"]]',
'{
'Non unique arrays' => [
'input' => '[["foo"], ["foo"]]',
'schema' => '{
"type": "array",
"uniqueItems": true
}'
],
[
'[{}, [1], true, null, {}, 1]',
'{
'Non unique mix of different types' => [
'input' => '[{}, [1], true, null, {}, 1]',
'schema' => '{
"type": "array",
"uniqueItems": true
}'
],
'objects are non-unique despite key order' => [
'input' => '[{"a": 1, "b": 2}, {"b": 2, "a": 1}]',
'schema' => '{"uniqueItems": true}',
]
];
}

public function getValidTests(): array
{
return [
[
'[1,2,3]',
'{
"type":"array",
"uniqueItems": true
'unique integers' => [
'input' => '[1,2,3]',
'schema' => '{
"type":"array",
"uniqueItems": true
}'
],
[
'[{"foo": 12}, {"bar": false}]',
'{
'unique objects' =>[
'input' => '[{"foo": 12}, {"bar": false}]',
'schema' => '{
"type": "array",
"uniqueItems": true
}'
],
[
'[1, true]',
'{
'Integer one and boolean true' => [
'input' => '[1, true]',
'schema' => '{
"type": "array",
"uniqueItems": true
}'
],
[
'[0, false]',
'{
'Integer zero and boolean false' => [
'input' => '[0, false]',
'schema' => '{
"type": "array",
"uniqueItems": true
}'
],
[
'[{"foo": {"bar" : {"baz" : true}}}, {"foo": {"bar" : {"baz" : false}}}]',
'{
'Objects with different value three levels deep' => [
'input' => '[{"foo": {"bar" : {"baz" : true}}}, {"foo": {"bar" : {"baz" : false}}}]',
'schema' => '{
"type": "array",
"uniqueItems": true
}'
],
[
'[["foo"], ["bar"]]',
'{
'Array of strings' => [
'input' => '[["foo"], ["bar"]]',
'schema' => '{
"type": "array",
"uniqueItems": true
}'
],
[
'[{}, [1], true, null, 1]',
'{
'Object, Array, boolean, null and integer' => [
'input' => '[{}, [1], true, null, 1]',
'schema' => '{
"type": "array",
"uniqueItems": true
}'
],
// below equals the invalid tests, but with uniqueItems set to false
[
'[1,2,2]',
'{
'Non unique integers' => [
'input' => '[1,2,2]',
'schema' => '{
"type":"array",
"uniqueItems": false
}'
],
[
'[{"a":"b"},{"a":"c"},{"a":"b"}]',
'{
'Non unique objects' => [
'input' => '[{"a":"b"},{"a":"c"},{"a":"b"}]',
'schema' => '{
"type":"array",
"uniqueItems": false
}'
],
[
'[{"foo": {"bar" : {"baz" : true}}}, {"foo": {"bar" : {"baz" : true}}}]',
'{
'Non unique objects - three levels deep' => [
'input' => '[{"foo": {"bar" : {"baz" : true}}}, {"foo": {"bar" : {"baz" : true}}}]',
'schema' => '{
"type": "array",
"uniqueItems": false
}'
],
[
'[1.0, 1.00, 1]',
'{
'Non unique mathematical values for the number one' => [
'input' => '[1.0, 1.00, 1]',
'schema' => '{
"type": "array",
"uniqueItems": false
}'
],
[
'[["foo"], ["foo"]]',
'{
'Non unique arrays' => [
'input' => '[["foo"], ["foo"]]',
'schema' => '{
"type": "array",
"uniqueItems": false
}'
],
[
'[{}, [1], true, null, {}, 1]',
'{
'Non unique mix of different types' => [
'input' => '[{}, [1], true, null, {}, 1]',
'schema' => '{
"type": "array",
"uniqueItems": false
}'
Expand Down