Skip to content

Commit

Permalink
feat: implemented components
Browse files Browse the repository at this point in the history
  • Loading branch information
aizlee committed Sep 16, 2024
1 parent 4a8e6f9 commit c69c90b
Show file tree
Hide file tree
Showing 38 changed files with 587 additions and 521 deletions.
26 changes: 15 additions & 11 deletions src/Services/SwaggerService.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,9 @@ protected function generateEmptyData(): array
['url' => $this->getAppUrl() . $this->config['basePath']],
],
'paths' => [],
'definitions' => $this->config['definitions'],
'components' => [
'schemas' => $this->config['definitions'],
],
'info' => $this->prepareInfo($this->config['info'])
];

Expand Down Expand Up @@ -309,7 +311,7 @@ protected function saveResponseSchema(?array $content, string $definition): void
$this->saveObjectResponseDefinitions($content, $schemaProperties, $definition);
}

$this->data['definitions'][$definition] = [
$this->data['components']['schemas'][$definition] = [
'type' => $schemaType,
'properties' => $schemaProperties
];
Expand All @@ -331,7 +333,9 @@ protected function saveListResponseDefinitions(array $content, array &$schemaPro

protected function saveObjectResponseDefinitions(array $content, array &$schemaProperties, string $definition): void
{
$properties = Arr::get($this->data['definitions'], $definition, []);
$definitions = (!empty($this->data['components']['schemas'])) ? $this->data['components']['schemas'] : [];

$properties = Arr::get($definitions, $definition, []);

foreach ($content as $name => $value) {
$property = Arr::get($properties, "properties.{$name}", []);
Expand Down Expand Up @@ -397,7 +401,7 @@ protected function parseResponse($response)
$this->saveResponseSchema($content, $definition);

if (is_array($this->item['responses'][$code])) {
$this->item['responses'][$code]['content'][$produce]['schema']['$ref'] = "#/definitions/{$definition}";
$this->item['responses'][$code]['content'][$produce]['schema']['$ref'] = "#/components/schemas/{$definition}";
}
}

Expand Down Expand Up @@ -535,7 +539,7 @@ protected function savePostRequestParameters($actionName, $rules, array $attribu
'content' => [
$type => [
'schema' => [
"\$ref" => "#/definitions/{$actionName}Object",
"\$ref" => "#/components/schemas/{$actionName}Object",
],
],
],
Expand Down Expand Up @@ -573,7 +577,7 @@ protected function saveDefinitions($objectName, $rules, $attributes, array $anno
}

$data['example'] = $this->generateExample($data['properties']);
$this->data['definitions'][$objectName . 'Object'] = $data;
$this->data['components']['schemas'][$objectName . 'Object'] = $data;
}

protected function getParameterType(array $validation): string
Expand Down Expand Up @@ -614,8 +618,8 @@ protected function requestHasMoreProperties($actionName): bool
{
$requestParametersCount = count($this->request->all());

if (isset($this->data['definitions'][$actionName . 'Object']['properties'])) {
$objectParametersCount = count($this->data['definitions'][$actionName . 'Object']['properties']);
if (isset($this->data['components']['schemas'][$actionName . 'Object']['properties'])) {
$objectParametersCount = count($this->data['components']['schemas'][$actionName . 'Object']['properties']);
} else {
$objectParametersCount = 0;
}
Expand Down Expand Up @@ -993,11 +997,11 @@ protected function mergeOpenAPIDocs(array &$documentation, array $additionalDocu
}
}

$definitions = array_keys($additionalDocumentation['definitions']);
$definitions = array_keys($additionalDocumentation['components']['schemas']);

foreach ($definitions as $definition) {
if (empty($documentation['definitions'][$definition])) {
$documentation['definitions'][$definition] = $additionalDocumentation['definitions'][$definition];
if (empty($documentation['components']['schemas'][$definition])) {
$documentation['components']['schemas'][$definition] = $additionalDocumentation['components']['schemas'][$definition];
}
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/Validators/SwaggerSpecValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class SwaggerSpecValidator
];

public const REQUIRED_FIELDS = [
'definition' => ['type'],
'components' => ['type'],
'doc' => ['openapi', 'info', 'paths'],
'info' => ['title', 'version'],
'item' => ['type'],
Expand Down Expand Up @@ -145,10 +145,10 @@ protected function validatePaths(): void

protected function validateDefinitions(): void
{
$definitions = Arr::get($this->doc, 'definitions', []);
$definitions = Arr::get($this->doc, 'components.schemas', []);

foreach ($definitions as $index => $definition) {
$this->validateFieldsPresent(self::REQUIRED_FIELDS['definition'], "definitions.{$index}");
$this->validateFieldsPresent(self::REQUIRED_FIELDS['components'], "components.schemas.{$index}");
}
}

Expand Down Expand Up @@ -431,7 +431,7 @@ protected function validateRefs(): void
!empty($refFilename)
? json_decode(file_get_contents($refFilename), true)
: $this->doc,
$refParentKey
str_replace('/', '.', $refParentKey),
);

if (!empty($missingRefs)) {
Expand Down
2 changes: 1 addition & 1 deletion tests/SwaggerServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ public static function getConstructorInvalidTmpData(): array
[
'tmpDoc' => 'documentation/invalid_format__missing_field__definition_type',
'exception' => MissingFieldException::class,
'exceptionMessage' => "Validation failed. 'definitions.authloginObject' should have "
'exceptionMessage' => "Validation failed. 'components.schemas.authloginObject' should have "
. "required fields: type.",
],
[
Expand Down
40 changes: 21 additions & 19 deletions tests/fixtures/AutoDocControllerTest/tmp_data.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"content": {
"application/json": {
"schema": {
"$ref": "#/definitions/authloginObject"
"$ref": "#/components/schemas/authloginObject"
}
},
"required": true,
Expand Down Expand Up @@ -101,26 +101,28 @@
}
}
},
"definitions": {
"authloginObject": {
"type": "object",
"properties": {
"email": {
"type": "string",
"description": "2"
"components": {
"schemas": {
"authloginObject": {
"type": "object",
"properties": {
"email": {
"type": "string",
"description": "2"
},
"password": {
"type": "string",
"description": ""
}
},
"password": {
"type": "string",
"description": ""
"required": [
"email",
"password"
],
"example": {
"email": "[email protected]",
"password": "123"
}
},
"required": [
"email",
"password"
],
"example": {
"email": "[email protected]",
"password": "123"
}
}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"content": {
"application/json": {
"schema": {
"$ref": "#/definitions/authloginObject"
"$ref": "#/components/schemas/authloginObject"
}
},
"required": true,
Expand Down Expand Up @@ -114,7 +114,7 @@
"content": {
"application/json": {
"schema": {
"$ref": "#/definitions/authloginObject"
"$ref": "#/components/schemas/authloginObject"
}
},
"required": true,
Expand Down Expand Up @@ -210,7 +210,7 @@
"content": {
"application/json": {
"schema": {
"$ref": "#/definitions/apiusersObject"
"$ref": "#/components/schemas/apiusersObject"
}
},
"required": true,
Expand Down Expand Up @@ -290,42 +290,44 @@
}
}
},
"definitions": {
"authloginObject": {
"type": "object",
"properties": {
"email": {
"type": "string",
"description": "2"
"components": {
"schemas": {
"authloginObject": {
"type": "object",
"properties": {
"email": {
"type": "string",
"description": "2"
},
"password": {
"type": "string",
"description": ""
}
},
"password": {
"type": "string",
"description": ""
"required": [
"email",
"password"
],
"example": {
"email": "[email protected]",
"password": "123"
}
},
"required": [
"email",
"password"
],
"example": {
"email": "[email protected]",
"password": "123"
}
},
"apiusersObject": {
"type": "object",
"properties": {
"query": {
"type": "string",
"description": ""
"apiusersObject": {
"type": "object",
"properties": {
"query": {
"type": "string",
"description": ""
}
},
"required": [
"query"
],
"example": {
"first_name": "andrey",
"last_name": "voronin"
}
},
"required": [
"query"
],
"example": {
"first_name": "andrey",
"last_name": "voronin"
}
}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
"content": {
"application/json": {
"schema": {
"$ref": "#/definitions/getUsersroles200ResponseObject",
"$ref": "#/components/schemas/getUsersroles200ResponseObject",
"type": "object"
},
"example": [
Expand Down Expand Up @@ -90,16 +90,18 @@
}
}
},
"definitions": {
"getUsersroles200ResponseObject": {
"type": "array",
"properties": {
"items": {
"allOf": [
{
"type": "array"
}
]
"components": {
"schemas": {
"getUsersroles200ResponseObject": {
"type": "array",
"properties": {
"items": {
"allOf": [
{
"type": "array"
}
]
}
}
}
}
Expand Down
40 changes: 21 additions & 19 deletions tests/fixtures/LocalDriverTest/tmp_data.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"content": {
"application/json": {
"schema": {
"$ref": "#/definitions/authloginObject"
"$ref": "#/components/schemas/authloginObject"
}
},
"required": true,
Expand Down Expand Up @@ -103,26 +103,28 @@
}
}
},
"definitions": {
"authloginObject": {
"type": "object",
"properties": {
"email": {
"type": "string",
"description": "2"
"components": {
"schemas": {
"authloginObject": {
"type": "object",
"properties": {
"email": {
"type": "string",
"description": "2"
},
"password": {
"type": "string",
"description": ""
}
},
"password": {
"type": "string",
"description": ""
"required": [
"email",
"password"
],
"example": {
"email": "[email protected]",
"password": "123"
}
},
"required": [
"email",
"password"
],
"example": {
"email": "[email protected]",
"password": "123"
}
}
},
Expand Down
2 changes: 1 addition & 1 deletion tests/fixtures/LocalDriverTest/tmp_data_non_formatted.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"openapi":"3.1.0","servers":[{"url":"http:\/\/localhost"}],"paths":{"\/auth\/login":{"post":{"tags":["auth"],"consumes":["application\/json"],"produces":["application\/json"],"parameters":[],"requestBody":{"content":{"application\/json":{"schema":{"$ref":"#\/definitions\/authloginObject"}},"required":true,"description":""}},"responses":{"200":{"description":"Operation successfully done","content":{"application\/json":{"schema":{"type":"object"},"example":{"token":"some_token","user":{"id":2,"email":"[email protected]","deleted_at":null,"created_at":"2017-11-16 06:08:34","updated_at":"2018-01-01 00:00:00","role_id":2,"state":"confirmed","reset_password_hash":null,"failed_auth_attempts":0,"last_auth_attempt":"2018-01-01 00:00:00","first_name":"user","last_name":null,"set_password_hash_created_at":null,"full_name":"user","new_email":"[email protected]","is_email_verified":true,"role":{"id":2,"name":"client","created_at":null,"updated_at":null,"settable":true}},"ttl":60,"refresh_ttl":20160}}}},"401":{"description":"Unauthorized","content":{"application\/json":{"schema":{"type":"object"},"example":{"error":"You have entered an incorrect credentials."}}}},"400":{"description":"Bad Request","content":{"application\/json":{"schema":{"type":"object"},"example":{"error":"The limit of failed authorization attempts has been reached. You can't login in next 50 minutes."}}}}},"security":[],"description":"","summary":"login"}}},"definitions":{"authloginObject":{"type":"object","properties":{"email":{"type":"string","description":"2"},"password":{"type":"string","description":""}},"required":["email","password"],"example":{"email":"[email protected]","password":"123"}}},"info":{"description":"This is automatically collected documentation","version":"0.0.0","title":"Project Title","termsOfService":"","contact":{"email":"[email protected]"}}}
{"openapi":"3.1.0","servers":[{"url":"http:\/\/localhost"}],"paths":{"\/auth\/login":{"post":{"tags":["auth"],"consumes":["application\/json"],"produces":["application\/json"],"parameters":[],"requestBody":{"content":{"application\/json":{"schema":{"$ref":"#\/components\/schemas\/authloginObject"}},"required":true,"description":""}},"responses":{"200":{"description":"Operation successfully done","content":{"application\/json":{"schema":{"type":"object"},"example":{"token":"some_token","user":{"id":2,"email":"[email protected]","deleted_at":null,"created_at":"2017-11-16 06:08:34","updated_at":"2018-01-01 00:00:00","role_id":2,"state":"confirmed","reset_password_hash":null,"failed_auth_attempts":0,"last_auth_attempt":"2018-01-01 00:00:00","first_name":"user","last_name":null,"set_password_hash_created_at":null,"full_name":"user","new_email":"[email protected]","is_email_verified":true,"role":{"id":2,"name":"client","created_at":null,"updated_at":null,"settable":true}},"ttl":60,"refresh_ttl":20160}}}},"401":{"description":"Unauthorized","content":{"application\/json":{"schema":{"type":"object"},"example":{"error":"You have entered an incorrect credentials."}}}},"400":{"description":"Bad Request","content":{"application\/json":{"schema":{"type":"object"},"example":{"error":"The limit of failed authorization attempts has been reached. You can't login in next 50 minutes."}}}}},"security":[],"description":"","summary":"login"}}},"components":{"schemas":{"authloginObject":{"type":"object","properties":{"email":{"type":"string","description":"2"},"password":{"type":"string","description":""}},"required":["email","password"],"example":{"email":"[email protected]","password":"123"}}}},"info":{"description":"This is automatically collected documentation","version":"0.0.0","title":"Project Title","termsOfService":"","contact":{"email":"[email protected]"}}}
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,9 @@
}
}
},
"definitions": {},
"components": {
"schemas": {}
},
"info": {
"description": "This is automatically collected documentation",
"version": "0.0.0",
Expand Down
Loading

0 comments on commit c69c90b

Please sign in to comment.