From 1ce73ac31a9482591812775a705564c68cf70d0f Mon Sep 17 00:00:00 2001 From: Robert Zondervan Date: Mon, 13 Jan 2020 14:56:29 +0100 Subject: [PATCH] New SwaggerDecorator --- api/src/Swagger/SwaggerDecorator.php | 169 ++++++++++++++++----------- 1 file changed, 101 insertions(+), 68 deletions(-) diff --git a/api/src/Swagger/SwaggerDecorator.php b/api/src/Swagger/SwaggerDecorator.php index b0610232..acb8988e 100644 --- a/api/src/Swagger/SwaggerDecorator.php +++ b/api/src/Swagger/SwaggerDecorator.php @@ -23,13 +23,13 @@ final class SwaggerDecorator implements NormalizerInterface private $camelCaseToSnakeCaseNameConverter; public function __construct( - NormalizerInterface $decorated, - ParameterBagInterface $params, - CacheInterface $cache, - EntityManagerInterface $em, - AnnotationReader $annotationReader, - CamelCaseToSnakeCaseNameConverter $camelCaseToSnakeCaseNameConverter - ) { + NormalizerInterface $decorated, + ParameterBagInterface $params, + CacheInterface $cache, + EntityManagerInterface $em, + AnnotationReader $annotationReader, + CamelCaseToSnakeCaseNameConverter $camelCaseToSnakeCaseNameConverter + ) { $this->decorated = $decorated; $this->params = $params; $this->cash = $cache; @@ -63,17 +63,17 @@ public function normalize($object, $format = null, array $context = []) // Lets add JWT-Oauth $docs['securityDefinitions']['JWT-Oauth'] = [ - 'type' => 'oauth2', - 'authorizationUrl'=> 'http://petstore.swagger.io/api/oauth/dialog', - 'flow' => 'implicit', - 'scopes' => [], //scopes will be filled later autmaticly + 'type' => 'oauth2', + 'authorizationUrl'=> 'http://petstore.swagger.io/api/oauth/dialog', + 'flow' => 'implicit', + 'scopes' => [], //scopes will be filled later autmaticly ]; $docs['securityDefinitions']['JWT-Token'] = [ - 'type' => 'apiKey', - 'in' => 'header', // can be "header", "query" or "cookie" - 'name' => 'Authorization', // name of the header, query parameter or cookie - 'scopes'=> [], //scopes will be filled later autmaticly + 'type' => 'apiKey', + 'in' => 'header', // can be "header", "query" or "cookie" + 'name' => 'Authorization', // name of the header, query parameter or cookie + 'scopes'=> [], //scopes will be filled later autmaticly ]; // Lets get al the entities known to doctrine @@ -115,7 +115,7 @@ public function normalize($object, $format = null, array $context = []) $entityDocs = $this->getAdditionalEntityDocs($entity); // Only run if we have aditional docs if(array_key_exists('properties',$entityDocs)){ - $additionalDocs = array_merge($additionalDocs, $entityDocs['properties']); + $additionalDocs = array_merge($additionalDocs, $entityDocs['properties']); } // Security @@ -196,16 +196,16 @@ public function normalize($object, $format = null, array $context = []) // NLX loging headers $call['parameters'][] = [ - 'name' => 'Authorization', - 'description' => 'The JWT of the entity performing the request', - 'in' => 'header', + 'name' => 'Authorization', + 'description' => 'The JWT of the entity performing the request', + 'in' => 'header', ]; // NLX loging headers $call['parameters'][] = [ - 'name' => 'API-Version', - 'description' => 'The version of the API conform [Landelijke API-strategie.](https://geonovum.github.io/KP-APIs/#versioning)', - 'example' => '1.0.1', - 'in' => 'header', + 'name' => 'API-Version', + 'description' => 'The version of the API conform [Landelijke API-strategie.](https://geonovum.github.io/KP-APIs/#versioning)', + 'example' => '1.0.1', + 'in' => 'header', ]; /* // NLX loging headers @@ -223,33 +223,33 @@ public function normalize($object, $format = null, array $context = []) */ // NLX loging headers $call['parameters'][] = [ - 'name' => 'X-NLX-Logrecord-ID', - 'description' => 'A globally unique id of the request, which makes a request traceable throughout the network.', - 'in' => 'header', + 'name' => 'X-NLX-Logrecord-ID', + 'description' => 'A globally unique id of the request, which makes a request traceable throughout the network.', + 'in' => 'header', ]; // NLX loging headers $call['parameters'][] = [ - 'name' => 'X-NLX-Request-Process-Id', - 'description' => 'A process id for purpose registration (doelbinding)', - 'in' => 'header', + 'name' => 'X-NLX-Request-Process-Id', + 'description' => 'A process id for purpose registration (doelbinding)', + 'in' => 'header', ]; // NLX loging headers $call['parameters'][] = [ - 'name' => 'X-NLX-Request-Data-Elements', - 'description' => 'A list of requested data elements', - 'in' => 'header', + 'name' => 'X-NLX-Request-Data-Elements', + 'description' => 'A list of requested data elements', + 'in' => 'header', ]; // NLX loging headers $call['parameters'][] = [ - 'name' => 'X-NLX-Request-Data-Subject', - 'description' => 'A key-value list of data subjects related to this request. e.g. `bsn=12345678, kenteken=ab-12-fg`', - 'in' => 'header', + 'name' => 'X-NLX-Request-Data-Subject', + 'description' => 'A key-value list of data subjects related to this request. e.g. `bsn=12345678, kenteken=ab-12-fg`', + 'in' => 'header', ]; // NLX loging headers $call['parameters'][] = [ - 'name' => 'X-NLX-Audit-Clarification', - 'description' => 'A clarification as to why a request has been made (doelbinding)', - 'in' => 'header', + 'name' => 'X-NLX-Audit-Clarification', + 'description' => 'A clarification as to why a request has been made (doelbinding)', + 'in' => 'header', ]; if ($method == 'get') { @@ -259,48 +259,48 @@ public function normalize($object, $format = null, array $context = []) // WEBSUB header $call['parameters'][] = [ - 'name' => 'Link', - 'description' => 'A [websub](https://www.w3.org/TR/websub/#discovery) header like ; rel="hub"', - 'in' => 'header', + 'name' => 'Link', + 'description' => 'A [websub](https://www.w3.org/TR/websub/#discovery) header like ; rel="hub"', + 'in' => 'header', ]; // Lets add the extend functionality $call['parameters'][] = [ - 'name' => 'extend[]', - 'required' => false, - 'description' => 'An array of nested objects to include in the return object', - 'in' => 'query', - 'schema' => ['type'=>'array'], + 'name' => 'extend[]', + 'required' => false, + 'description' => 'An array of nested objects to include in the return object', + 'in' => 'query', + 'schema' => ['type'=>'array'], ]; // Lets add the fields functionality $call['parameters'][] = [ - 'name' => 'fields[]', - 'required' => false, - 'description' => 'An array of fields to return in output, wil return all fields is not supplied', - 'in' => 'query', - 'schema' => ['type'=>'array'], + 'name' => 'fields[]', + 'required' => false, + 'description' => 'An array of fields to return in output, wil return all fields is not supplied', + 'in' => 'query', + 'schema' => ['type'=>'array'], ]; // Lets add some time travel $call['parameters'][] = [ - 'name' => 'validOn', - 'required' => false, - 'description' => 'Returns object as valid on a given date time', - 'schema' => ['type'=>'string', 'format' => 'date-time'], - 'in' => 'query', + 'name' => 'validOn', + 'required' => false, + 'description' => 'Returns object as valid on a given date time', + 'schema' => ['type'=>'string', 'format' => 'date-time'], + 'in' => 'query', ]; $call['parameters'][] = [ - 'name' => 'validFrom', - 'required' => false, - 'description' => 'Returns objects valid from a given date time', - 'schema' => ['type'=>'string', 'format' => 'date-time'], - 'in' => 'query', + 'name' => 'validFrom', + 'required' => false, + 'description' => 'Returns objects valid from a given date time', + 'schema' => ['type'=>'string', 'format' => 'date-time'], + 'in' => 'query', ]; $call['parameters'][] = [ - 'name' => 'validUntil', - 'required' => false, - 'description' => 'Returns objects valid until a given date time', - 'schema' => ['type'=>'string', 'format' => 'date-time'], - 'in' => 'query', + 'name' => 'validUntil', + 'required' => false, + 'description' => 'Returns objects valid until a given date time', + 'schema' => ['type'=>'string', 'format' => 'date-time'], + 'in' => 'query', ]; } } @@ -418,13 +418,20 @@ private function getAdditionalEntityDocs($entity) $atributes = []; $groups = []; + foreach ($tags as $tag) { $name = $tag->getName(); $description = $tag->getDescription(); - // - //$description = (string) $description; switch ($name) { + + // Description + case 'var': + $atributes['description'] = (string) $description; + $atributes['type'] = (string) $tag->getType(); + + break; + // Docblocks case 'example': $atributes['example'] = (string) $description; @@ -437,22 +444,48 @@ private function getAdditionalEntityDocs($entity) break; // Constrainds (Validation) + case "Assert\Date": + $atributes['type'] = "string"; + $atributes['format'] = 'date'; + $atributes['example'] = \date('Y-m-d'); + break; + case "Assert\DateTime": + $atributes['type'] = "string"; + $atributes['format'] = 'date-time'; + $atributes['example'] = \date('Y-m-d H:i:s'); + break; + case "Assert\Time": + $atributes['type'] = "string"; + $atributes['format'] = 'time'; + $atributes['example'] = \date('H:i:s'); + break; + case "Assert\Timezone": + $atributes['type'] = "string"; + $atributes['format'] = 'timezone'; + $atributes['example'] = 'America/New_York'; + break; case "Assert\Uuid": + $atributes['type'] = "string"; $atributes['format'] = 'uuid'; break; case "Assert\Email": + $atributes['type'] = "string"; $atributes['format'] = 'email'; break; case "Assert\Url": + $atributes['type'] = "string"; $atributes['format'] = 'url'; break; case "Assert\Regex": + $atributes['type'] = "string"; $atributes['format'] = 'regex'; break; case "Assert\Ip": + $atributes['type'] = "string"; $atributes['format'] = 'ip'; break; case "Assert\Json": + $atributes['type'] = "string"; $atributes['format'] = 'json'; break; case "Assert\Choice":