Skip to content

Commit

Permalink
Clear useless response schemas
Browse files Browse the repository at this point in the history
  • Loading branch information
yceruto committed Dec 2, 2023
1 parent d543db5 commit f767844
Show file tree
Hide file tree
Showing 14 changed files with 142 additions and 660 deletions.
55 changes: 55 additions & 0 deletions src/OpenApi/Processor/CleanupAnnotations.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,14 @@
namespace OpenSolid\OpenApiBundle\OpenApi\Processor;

use OpenApi\Analysis;
use OpenApi\Annotations\Operation;
use OpenApi\Generator;
use OpenApi\Processors\ProcessorInterface;
use OpenSolid\OpenApiBundle\Routing\Attribute\Delete;
use OpenSolid\OpenApiBundle\Routing\Attribute\Get;
use OpenSolid\OpenApiBundle\Routing\Attribute\Patch;
use OpenSolid\OpenApiBundle\Routing\Attribute\Post;
use OpenSolid\OpenApiBundle\Routing\Attribute\Put;

readonly class CleanupAnnotations implements ProcessorInterface
{
Expand All @@ -13,6 +19,7 @@
public function __invoke(Analysis $analysis): void
{
$this->removeComponentsDuplicatedResponses($analysis);
$this->removeComponentsUselessResponses($analysis);
$this->removeComponentsUselessParameters($analysis);
$this->removeComponentsUselessSchemas($analysis);
}
Expand Down Expand Up @@ -42,6 +49,50 @@ protected function removeComponentsDuplicatedResponses(Analysis $analysis): void
}
}

protected function removeComponentsUselessResponses(Analysis $analysis): void
{
if (null === $openapi = $analysis->openapi) {
return;
}

if (Generator::isDefault($openapi->components)) {
return;
}

foreach ($openapi->components->responses as $i => $response) {
if (!Generator::isDefault($openapi->paths)) {
foreach ($openapi->paths as $pathItem) {
/** @var Operation[]|Post[]|Get[]|Put[]|Patch[]|Delete[] $methods */
$methods = [
$pathItem->post,
$pathItem->get,
$pathItem->put,
$pathItem->patch,
$pathItem->delete,
];
foreach ($methods as $method) {
if (Generator::isDefault($method) || Generator::isDefault($method->responses)) {
continue;
}

foreach ($method->responses as $res) {
if ((int) $res->response === (int) $response->response) {
continue 4;
}
}
}
}
}

$this->detachAnnotationRecursively($response, $analysis);
unset($openapi->components->responses[$i]);
}

if ([] === $openapi->components->responses) {
$openapi->components->responses = Generator::UNDEFINED;
}
}

protected function removeComponentsUselessParameters(Analysis $analysis): void
{
if (null === $openapi = $analysis->openapi) {
Expand Down Expand Up @@ -86,5 +137,9 @@ protected function removeComponentsUselessSchemas(Analysis $analysis): void
$this->detachAnnotationRecursively($schema, $analysis);
unset($openapi->components->schemas[$i]);
}

if ([] === $openapi->components->schemas) {
$openapi->components->schemas = Generator::UNDEFINED;
}
}
}
131 changes: 1 addition & 130 deletions tests/Functional/App/ConditionalPathAction/Output/doc.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,134 +10,5 @@
}
],
"paths": {},
"components": {
"schemas": {
"BadRequestErrorView": {
"properties": {
"type": {
"type": "string",
"default": "https://tools.ietf.org/html/rfc2616#section-10"
},
"title": {
"type": "string",
"default": "Bad Request"
},
"status": {
"type": "integer",
"default": 400
},
"detail": {
"type": "string"
}
},
"type": "object",
"readOnly": true
},
"NotFoundErrorView": {
"properties": {
"type": {
"type": "string",
"default": "https://tools.ietf.org/html/rfc2616#section-10"
},
"title": {
"type": "string",
"default": "Not Found"
},
"status": {
"type": "integer",
"default": 404
},
"detail": {
"type": "string"
}
},
"type": "object",
"readOnly": true
},
"ValidationErrorView": {
"properties": {
"type": {
"type": "string",
"default": "https://symfony.com/errors/validation"
},
"title": {
"type": "string",
"default": "Validation Failed"
},
"status": {
"type": "integer",
"default": 422
},
"detail": {
"type": "string"
},
"violations": {
"type": "array",
"items": {
"$ref": "#/components/schemas/ValidationViolationErrorView"
}
}
},
"type": "object",
"readOnly": true
},
"ValidationViolationErrorView": {
"properties": {
"propertyPath": {
"type": "string"
},
"title": {
"type": "string"
},
"template": {
"type": "string"
},
"parameters": {
"type": "object",
"additionalProperties": {
"type": "string"
}
},
"type": {
"type": "string",
"example": "urn:uuid:9ff3fdc4-b214-49db-8718-39c315e33d45"
}
},
"type": "object",
"readOnly": true
}
},
"responses": {
"400": {
"description": "Bad request",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/BadRequestErrorView"
}
}
}
},
"404": {
"description": "Not found",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/NotFoundErrorView"
}
}
}
},
"422": {
"description": "Validation error",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/ValidationErrorView"
}
}
}
}
}
}
"components": {}
}
37 changes: 3 additions & 34 deletions tests/Functional/App/CustomResponseSpec/Output/doc.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,27 +39,6 @@
},
"components": {
"schemas": {
"CustomBadRequestErrorView": {
"properties": {
"type": {
"type": "string",
"default": "https://tools.ietf.org/html/rfc2616#section-10"
},
"title": {
"type": "string",
"default": "Custom Bad Request"
},
"status": {
"type": "integer",
"default": 400
},
"detail": {
"type": "string"
}
},
"type": "object",
"readOnly": true
},
"PostResourceBody": {
"required": [
"name"
Expand All @@ -73,19 +52,19 @@
"type": "object",
"writeOnly": true
},
"NotFoundErrorView": {
"CustomBadRequestErrorView": {
"properties": {
"type": {
"type": "string",
"default": "https://tools.ietf.org/html/rfc2616#section-10"
},
"title": {
"type": "string",
"default": "Not Found"
"default": "Custom Bad Request"
},
"status": {
"type": "integer",
"default": 404
"default": 400
},
"detail": {
"type": "string"
Expand Down Expand Up @@ -158,16 +137,6 @@
}
}
},
"404": {
"description": "Not found",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/NotFoundErrorView"
}
}
}
},
"422": {
"description": "Validation error",
"content": {
Expand Down
62 changes: 0 additions & 62 deletions tests/Functional/App/DeleteResourceAction/Output/doc.json
Original file line number Diff line number Diff line change
Expand Up @@ -81,58 +81,6 @@
},
"type": "object",
"readOnly": true
},
"ValidationErrorView": {
"properties": {
"type": {
"type": "string",
"default": "https://symfony.com/errors/validation"
},
"title": {
"type": "string",
"default": "Validation Failed"
},
"status": {
"type": "integer",
"default": 422
},
"detail": {
"type": "string"
},
"violations": {
"type": "array",
"items": {
"$ref": "#/components/schemas/ValidationViolationErrorView"
}
}
},
"type": "object",
"readOnly": true
},
"ValidationViolationErrorView": {
"properties": {
"propertyPath": {
"type": "string"
},
"title": {
"type": "string"
},
"template": {
"type": "string"
},
"parameters": {
"type": "object",
"additionalProperties": {
"type": "string"
}
},
"type": {
"type": "string",
"example": "urn:uuid:9ff3fdc4-b214-49db-8718-39c315e33d45"
}
},
"type": "object",
"readOnly": true
}
},
"responses": {
Expand All @@ -155,16 +103,6 @@
}
}
}
},
"422": {
"description": "Validation error",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/ValidationErrorView"
}
}
}
}
}
}
Expand Down
Loading

0 comments on commit f767844

Please sign in to comment.