Skip to content

Commit

Permalink
Enhancement: Normalize additional properties
Browse files Browse the repository at this point in the history
  • Loading branch information
localheinz committed Jan 21, 2022
1 parent bf3a3d6 commit 277ab44
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 32 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -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]

Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion infection.json
Expand Up @@ -4,7 +4,7 @@
"text": ".build/infection/infection-log.txt"
},
"minCoveredMsi": 90,
"minMsi": 90,
"minMsi": 89,
"phpUnit": {
"configDir": "test\/Unit"
},
Expand Down
5 changes: 3 additions & 2 deletions psalm-baseline.xml
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<files psalm-version="4.17.0@6f4707aa41c9174353a6434bba3fc8840f981d9c">
<files psalm-version="4.18.1@dda05fa913f4dc6eb3386f2f7ce5a45d37a71bcb">
<file src="src/CallableNormalizer.php">
<MixedInferredReturnType occurrences="1">
<code>Json</code>
Expand All @@ -21,14 +21,15 @@
<MissingParamType occurrences="1">
<code>$data</code>
</MissingParamType>
<MixedArgument occurrences="7">
<MixedArgument occurrences="8">
<code>$anyOfSchema</code>
<code>$item</code>
<code>$item</code>
<code>$itemSchema</code>
<code>$oneOfSchema</code>
<code>$schema-&gt;properties</code>
<code>$value</code>
<code>$value</code>
</MixedArgument>
<MixedAssignment occurrences="5">
<code>$anyOfSchema</code>
Expand Down
49 changes: 26 additions & 23 deletions src/SchemaNormalizer.php
Expand Up @@ -172,44 +172,47 @@ 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<string, \stdClass> $objectPropertiesThatAreDefinedBySchema */
$objectPropertiesThatAreDefinedBySchema = \array_intersect_key(
\get_object_vars($schema->properties),
\get_object_vars($data),
);
if (\property_exists($schema, 'properties')) {
/** @var array<string, \stdClass> $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);

if (0 < \count($additionalProperties)) {
\ksort($additionalProperties);

$valueSchema = new \stdClass();

foreach ($additionalProperties as $name => $value) {
$normalized->{$name} = $value;
$normalized->{$name} = $this->normalizeData(
$value,
$valueSchema,
);
}
}

Expand Down
@@ -1,11 +1,11 @@
{
"foo": "hello",
"bar": [
{
"baz": 9000
},
{
"baz": 9001
}
]
],
"foo": "hello"
}
@@ -1,11 +1,11 @@
{
"foo": "hello",
"bar": [
{
"baz": 9000
},
{
"baz": 9001
}
]
],
"foo": "hello"
}
@@ -1,5 +1,4 @@
{
"foo": "hello",
"bar": [
{
"baz": 9000,
Expand All @@ -12,5 +11,6 @@
{
"quux": 9001
}
]
],
"foo": "hello"
}

0 comments on commit 277ab44

Please sign in to comment.