Skip to content

Commit

Permalink
Merge pull request #623 from ergebnis/feature/any-of
Browse files Browse the repository at this point in the history
Enhancement: Adjust `SchemaNormalizer` to support `anyOf`
  • Loading branch information
localheinz authored Jan 4, 2022
2 parents a068d2a + e85e599 commit 2039eb1
Show file tree
Hide file tree
Showing 12 changed files with 187 additions and 5 deletions.
15 changes: 13 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,15 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),

## Unreleased

For a full diff see [`2.0.0...main`][2.0.0...main].
For a full diff see [`2.1.0...main`][2.1.0...main].

## [`2.1.0`][2.1.0]

For a full diff see [`2.0.0...2.1.0`][2.0.0...2.1.0].

### Changed

- Adjusted `SchemaNormalizer` to support `anyOf` ([#623]), by [@localheinz]

## [`2.0.0`][2.0.0]

Expand Down Expand Up @@ -346,6 +354,7 @@ For a full diff see [`5d8b3e2...0.1.0`][5d8b3e2...0.1.0].
[1.0.2]: https://github.com/ergebnis/json-normalizer/releases/tag/1.0.2
[1.0.3]: https://github.com/ergebnis/json-normalizer/releases/tag/1.0.3
[2.0.0]: https://github.com/ergebnis/json-normalizer/releases/tag/2.0.0
[2.1.0]: https://github.com/ergebnis/json-normalizer/releases/tag/2.1.0

[5d8b3e2...0.1.0]: https://github.com/ergebnis/json-normalizer/compare/5d8b3e2...0.1.0
[0.1.0...0.2.0]: https://github.com/ergebnis/json-normalizer/compare/0.1.0...0.2.0
Expand All @@ -371,7 +380,8 @@ For a full diff see [`5d8b3e2...0.1.0`][5d8b3e2...0.1.0].
[1.0.1...1.0.2]: https://github.com/ergebnis/json-normalizer/compare/1.0.1...1.0.2
[1.0.2...1.0.3]: https://github.com/ergebnis/json-normalizer/compare/1.0.2...1.0.3
[1.0.3...2.0.0]: https://github.com/ergebnis/json-normalizer/compare/1.0.3...2.0.0
[2.0.0...main]: https://github.com/ergebnis/json-normalizer/compare/2.0.0...main
[2.0.0...2.1.0]: https://github.com/ergebnis/json-normalizer/compare/2.0.0...2.1.0
[2.1.0...main]: https://github.com/ergebnis/json-normalizer/compare/2.1.0...main

[#1]: https://github.com/ergebnis/json-normalizer/pull/1
[#2]: https://github.com/ergebnis/json-normalizer/pull/2
Expand Down Expand Up @@ -450,6 +460,7 @@ For a full diff see [`5d8b3e2...0.1.0`][5d8b3e2...0.1.0].
[#616]: https://github.com/ergebnis/json-normalizer/pull/616
[#618]: https://github.com/ergebnis/json-normalizer/pull/618
[#619]: https://github.com/ergebnis/json-normalizer/pull/619
[#623]: https://github.com/ergebnis/json-normalizer/pull/623

[@BackEndTea]: https://github.com/BackEndTea
[@dependabot]: https://github.com/dependabot
Expand Down
8 changes: 5 additions & 3 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<files psalm-version="4.16.1@aa7e400908833b10c0333861f86cd48c510b60eb">
<files psalm-version="4.17.0@6f4707aa41c9174353a6434bba3fc8840f981d9c">
<file src="src/CallableNormalizer.php">
<MixedInferredReturnType occurrences="1">
<code>Json</code>
Expand All @@ -21,15 +21,17 @@
<MissingParamType occurrences="1">
<code>$data</code>
</MissingParamType>
<MixedArgument occurrences="6">
<MixedArgument occurrences="7">
<code>$anyOfSchema</code>
<code>$item</code>
<code>$item</code>
<code>$itemSchema</code>
<code>$oneOfSchema</code>
<code>$schema-&gt;properties</code>
<code>$value</code>
</MixedArgument>
<MixedAssignment occurrences="4">
<MixedAssignment occurrences="5">
<code>$anyOfSchema</code>
<code>$itemSchema</code>
<code>$oneOfSchema</code>
<code>$value</code>
Expand Down
23 changes: 23 additions & 0 deletions src/SchemaNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,29 @@ private function resolveSchema(
$data,
\stdClass $schema
): \stdClass {
/**
* @see https://spacetelescope.github.io/understanding-json-schema/reference/combining.html#anyof
*/
if (
\property_exists($schema, 'anyOf')
&& \is_array($schema->anyOf)
) {
foreach ($schema->anyOf as $anyOfSchema) {
$result = $this->schemaValidator->validate(
SchemaValidator\Json::fromString(\json_encode($data)),
SchemaValidator\Json::fromString(\json_encode($anyOfSchema)),
SchemaValidator\JsonPointer::empty(),
);

if ($result->isValid()) {
return $this->resolveSchema(
$data,
$anyOfSchema,
);
}
}
}

/**
* @see https://spacetelescope.github.io/understanding-json-schema/reference/combining.html#oneof
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[
{
"type": "Website",
"url": "https://localheinz.com"
},
"https://github.com/localheinz",
{
"type": "Twitter",
"url": "https://twitter.com/localheinz"
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[
{
"url": "https://localheinz.com",
"type": "Website"
},
"https://github.com/localheinz",
{
"url": "https://twitter.com/localheinz",
"type": "Twitter"
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"type": "array",
"items": {
"anyOf": [
{
"type": "object",
"additionalProperties": false,
"properties": {
"type": {
"type": "string"
},
"url": {
"type": "string"
}
}
},
{
"type": "string"
}
]
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[
{
"type": "Website",
"url": "https://localheinz.com"
},
"https://github.com/localheinz",
{
"type": "Twitter",
"url": "https://twitter.com/localheinz"
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[
{
"url": "https://localheinz.com",
"type": "Website"
},
"https://github.com/localheinz",
{
"url": "https://twitter.com/localheinz",
"type": "Twitter"
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"type": "array",
"items": {
"$ref": "#/definitions/url"
},
"definitions": {
"url": {
"anyOf": [
{
"$ref": "#/definitions/url-object"
},
{
"$ref": "#/definitions/url-string"
}
]
},
"url-object": {
"type": "object",
"additionalProperties": false,
"properties": {
"type": {
"type": "string"
},
"url": {
"type": "string"
}
}
},
"url-string": {
"type": "string"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "Andreas Möller",
"urls": [
"https://localheinz.com",
"https://github.com/localheinz",
"https://twitter.com/localheinz"
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"urls": [
"https://localheinz.com",
"https://github.com/localheinz",
"https://twitter.com/localheinz"
],
"name": "Andreas Möller"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"anyOf": [
{
"type": "object",
"properties": {
"name": {
"type": "string"
},
"urls": {
"type": "array",
"items": {
"type": "string"
}
}
},
"additionalProperties": false
},
{
"type": "object",
"properties": {
"name": {
"type": "string"
},
"url": {
"type": "string"
}
},
"additionalProperties": false
}
]
}

0 comments on commit 2039eb1

Please sign in to comment.