Skip to content

Commit

Permalink
Merge pull request #15 from ConductionNL/dev-robert
Browse files Browse the repository at this point in the history
New SwaggerDecorator
  • Loading branch information
rubenvdlinde authored Jan 13, 2020
2 parents 6ff55fc + 1ce73ac commit b142f5f
Showing 1 changed file with 101 additions and 68 deletions.
169 changes: 101 additions & 68 deletions api/src/Swagger/SwaggerDecorator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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') {
Expand All @@ -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 <https://hub.example.com/>; rel="hub"',
'in' => 'header',
'name' => 'Link',
'description' => 'A [websub](https://www.w3.org/TR/websub/#discovery) header like <https://hub.example.com/>; 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',
];
}
}
Expand Down Expand Up @@ -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;
Expand All @@ -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":
Expand Down

0 comments on commit b142f5f

Please sign in to comment.