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: allOf match logic #67

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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
43 changes: 29 additions & 14 deletions src/Base/Body.php
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,18 @@ function () use ($name, $schemaArray, $body, $type)
*/
public function matchObjectProperties($name, $schemaArray, $body)
{
if (!isset($schemaArray[self::SWAGGER_ADDITIONAL_PROPERTIES])) {
$schemaArray[self::SWAGGER_ADDITIONAL_PROPERTIES] = true;
}

if (is_bool($schemaArray[self::SWAGGER_ADDITIONAL_PROPERTIES])) {
if ($schemaArray[self::SWAGGER_ADDITIONAL_PROPERTIES]) {
$schemaArray[self::SWAGGER_ADDITIONAL_PROPERTIES] = [];
} else {
unset($schemaArray[self::SWAGGER_ADDITIONAL_PROPERTIES]);
}
}

if (isset($schemaArray[self::SWAGGER_ADDITIONAL_PROPERTIES]) && !isset($schemaArray[self::SWAGGER_PROPERTIES])) {
$schemaArray[self::SWAGGER_PROPERTIES] = [];
}
Expand Down Expand Up @@ -341,7 +353,7 @@ public function matchObjectProperties($name, $schemaArray, $body)
/**
* @param string $name
* @param array $schemaArray
* @param array $body
* @param array|string $body
* @return bool
* @throws DefinitionNotFoundException
* @throws InvalidDefinitionException
Expand All @@ -351,6 +363,10 @@ public function matchObjectProperties($name, $schemaArray, $body)
*/
protected function matchSchema($name, $schemaArray, $body)
{
if ($schemaArray === []) {
return true;
}

// Match Single Types
if ($this->matchTypes($name, $schemaArray, $body)) {
return true;
Expand All @@ -362,25 +378,19 @@ protected function matchSchema($name, $schemaArray, $body)

// Get References and try to match it again
if (isset($schemaArray['$ref']) && !is_array($schemaArray['$ref'])) {
$defintion = $this->schema->getDefinition($schemaArray['$ref']);
return $this->matchSchema($schemaArray['$ref'], $defintion, $body);
}

// Match object properties
if ($this->matchObjectProperties($name, $schemaArray, $body)) {
return true;
$definition = $this->schema->getDefinition($schemaArray['$ref']);
return $this->matchSchema($schemaArray['$ref'], $definition, $body);
}

if (isset($schemaArray['allOf'])) {
$allOfSchemas = $schemaArray['allOf'];
foreach ($allOfSchemas as &$schema) {
if (isset($schema['$ref'])) {
$schema = $this->schema->getDefinition($schema['$ref']);
foreach ($allOfSchemas as $schema) {
if (!$this->matchSchema($name, $schema, $body)) {
return false;
}
}
unset($schema);
$mergedSchema = array_merge_recursive(...$allOfSchemas);
return $this->matchSchema($name, $mergedSchema, $body);

return true;
}

if (isset($schemaArray['oneOf'])) {
Expand All @@ -400,6 +410,11 @@ protected function matchSchema($name, $schemaArray, $body)
return $matched;
}

// Match object properties
if ($this->matchObjectProperties($name, $schemaArray, $body)) {
return true;
}

/**
* OpenApi 2.0 does not describe ANY object value
* But there is hack that makes ANY object possible, described in link below
Expand Down
4 changes: 4 additions & 0 deletions tests/OpenApiResponseBodyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,10 @@ public function testMatchAllOf()

$responseParameter = $this->openApiSchema2()->getResponseParameters('/v2/allofref', 'get', 200);
$this->assertTrue($responseParameter->match($body));

// password is not required
$responseParameter = $this->openApiSchema2()->getResponseParameters('/v2/nestedallofref', 'get', 200);
$this->assertTrue($responseParameter->match($body));
}

public function testResponseDefault()
Expand Down
1 change: 1 addition & 0 deletions tests/OpenApiSchemaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,7 @@ public function testGetDefinition()
{
$expected = [
"type" => "object",
"additionalProperties" => false,
"properties" => [
"id" => [
"type" => "integer",
Expand Down
1 change: 1 addition & 0 deletions tests/SwaggerSchemaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,7 @@ public function testGetDefinition()
$this->assertEquals(
[
"type" => "object",
"additionalProperties" => false,
"properties" => [
"id" => [
"type" => "integer",
Expand Down
1 change: 1 addition & 0 deletions tests/example/openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -973,6 +973,7 @@
"schemas": {
"Order": {
"type": "object",
"additionalProperties": false,
"properties": {
"id": {
"type": "integer",
Expand Down
50 changes: 48 additions & 2 deletions tests/example/openapi2.json
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@
"summary": "Get a response that should match two schemas",
"responses": {
"200": {
"description": "Returns any value",
"description": "Returns name and email",
"content": {
"application/json": {
"schema": {
Expand Down Expand Up @@ -161,7 +161,7 @@
"summary": "Get a response that should match two schemas",
"responses": {
"200": {
"description": "Returns any value",
"description": "Returns name and email",
"content": {
"application/json": {
"schema": {
Expand All @@ -180,6 +180,39 @@
}
}
}
},
"/nestedallofref": {
"get": {
"tags": [
"general"
],
"summary": "Get a response that should match two schemas",
"responses": {
"200": {
"description": "Returns name, email and password",
"content": {
"application/json": {
"schema": {
"type": "object",
"allOf": [
{
"$ref": "#/components/schemas/NameEmailObject"
},
{
"type": "object",
"properties": {
"password": {
"type": "string"
}
}
}
]
}
}
}
}
}
}
}
},
"externalDocs": {
Expand Down Expand Up @@ -222,18 +255,31 @@
},
"AnyValue": {},
"NameObject": {
"required": ["name"],
"properties": {
"name": {
"type": "string"
}
}
},
"EmailObject": {
"required": ["email"],
"properties": {
"email": {
"type": "string"
}
}
},
"NameEmailObject": {
"type": "object",
"allOf": [
{
"$ref": "#/components/schemas/NameObject"
},
{
"$ref": "#/components/schemas/EmailObject"
}
]
}
}
}
Expand Down
1 change: 1 addition & 0 deletions tests/example/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -887,6 +887,7 @@
"definitions": {
"Order": {
"type": "object",
"additionalProperties": false,
"properties": {
"id": {
"type": "integer",
Expand Down