Skip to content

Commit

Permalink
OpenAPI: add Link object and add missing properties of existing objects
Browse files Browse the repository at this point in the history
Signed-off-by: Roman Ondráček <[email protected]>
  • Loading branch information
Roman3349 authored and f3l1x committed Jul 17, 2023
1 parent 1a9a83c commit ad2565b
Show file tree
Hide file tree
Showing 21 changed files with 4,009 additions and 78 deletions.
6 changes: 3 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ phpstan:
vendor/bin/phpstan analyse -c phpstan.neon

tests:
vendor/bin/tester -s -p php --colors 1 -C tests/Cases
vendor/bin/tester -s -p php --colors 1 -C -d memory_limit=512M tests/Cases

coverage:
ifdef GITHUB_ACTION
vendor/bin/tester -s -p phpdbg --colors 1 -C --coverage coverage.xml --coverage-src src tests/Cases
vendor/bin/tester -s -p phpdbg --colors 1 -C -d memory_limit=512M --coverage coverage.xml --coverage-src src tests/Cases
else
vendor/bin/tester -s -p phpdbg --colors 1 -C --coverage coverage.html --coverage-src src tests/Cases
vendor/bin/tester -s -p phpdbg --colors 1 -C -d memory_limit=512M --coverage coverage.html --coverage-src src tests/Cases
endif
1 change: 0 additions & 1 deletion phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ parameters:
path: %currentWorkingDirectory%/src/OpenApi/SchemaDefinition/Entity/EntityAdapter.php

- '#Property Apitte\\OpenApi\\Schema\\(\w+)::\$(\w+) is never read, only written.#'
- '#Property Apitte\\OpenApi\\Schema\\(\w+)::\$(\w+) is unused.#'

# This should not happen because null is returned on error
- '#Method Apitte\\Core\\Utils\\Helpers::slashless\(\) should return string but returns string\|null\.#'
Expand Down
33 changes: 20 additions & 13 deletions src/OpenApi/Schema/Components.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public static function fromArray(array $data): Components

if (isset($data['responses'])) {
foreach ($data['responses'] as $responseKey => $responseData) {
$components->setResponse($responseKey, Response::fromArray($responseData));
$components->setResponse((string) $responseKey, Response::fromArray($responseData));
}
}

Expand All @@ -62,22 +62,20 @@ public static function fromArray(array $data): Components
}
}

if (isset($data['requestBodies'])) {
foreach ($data['requestBodies'] as $requestBodyKey => $requestBodyData) {
$components->setRequestBody($requestBodyKey, RequestBody::fromArray($requestBodyData));
}
foreach ($data['requestBodies'] ?? [] as $requestBodyKey => $requestBodyData) {
$components->setRequestBody($requestBodyKey, RequestBody::fromArray($requestBodyData));
}

if (isset($data['headers'])) {
foreach ($data['headers'] as $headerKey => $headerData) {
$components->setHeader($headerKey, Header::fromArray($headerData));
}
foreach ($data['headers'] ?? [] as $headerKey => $headerData) {
$components->setHeader($headerKey, Header::fromArray($headerData));
}

if (isset($data['securitySchemes'])) {
foreach ($data['securitySchemes'] as $securitySchemeKey => $securitySchemeData) {
$components->setSecurityScheme($securitySchemeKey, SecurityScheme::fromArray($securitySchemeData));
}
foreach ($data['securitySchemes'] ?? [] as $securitySchemeKey => $securitySchemeData) {
$components->setSecurityScheme($securitySchemeKey, SecurityScheme::fromArray($securitySchemeData));
}

foreach ($data['links'] ?? [] as $linkKey => $linkData) {
$components->setLink($linkKey, Link::fromArray($linkData));
}

return $components;
Expand Down Expand Up @@ -118,6 +116,11 @@ public function setSecurityScheme(string $name, SecurityScheme|Reference $securi
$this->securitySchemes[$name] = $securityScheme;
}

public function setLink(string $name, Link|Reference $link): void
{
$this->links[$name] = $link;
}

/**
* @return mixed[]
*/
Expand Down Expand Up @@ -152,6 +155,10 @@ public function toArray(): array
$data['securitySchemes'][$securitySchemeKey] = $securityScheme->toArray();
}

foreach ($this->links as $linkKey => $link) {
$data['links'][$linkKey] = $link->toArray();
}

return $data;
}

Expand Down
73 changes: 72 additions & 1 deletion src/OpenApi/Schema/Header.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,39 @@ class Header

private ?string $description = null;

private ?bool $required = null;

private ?bool $deprecated = null;

private ?bool $allowEmptyValue = null;

private Schema|Reference|null $schema = null;

private mixed $example = null;

/** @var mixed[] */
private array $examples = [];

/**
* @param mixed[] $data
*/
public static function fromArray(array $data): Header
{
$header = new Header();
$header->setDescription($data['description'] ?? null);
$header->setSchema(isset($data['schema']) ? Schema::fromArray($data['schema']) : null);
$header->setRequired($data['required'] ?? null);
$header->setDeprecated($data['deprecated'] ?? null);
$header->setAllowEmptyValue($data['allowEmptyValue'] ?? null);
if (isset($data['schema'])) {
if (isset($data['schema']['$ref'])) {
$header->setSchema(Reference::fromArray($data['schema']));
} else {
$header->setSchema(Schema::fromArray($data['schema']));
}
}

$header->setExample($data['example'] ?? null);
$header->setExamples($data['examples'] ?? []);

return $header;
}
Expand All @@ -31,10 +54,30 @@ public function toArray(): array
$data['description'] = $this->description;
}

if ($this->required !== null) {
$data['required'] = $this->required;
}

if ($this->deprecated !== null) {
$data['deprecated'] = $this->deprecated;
}

if ($this->allowEmptyValue !== null) {
$data['allowEmptyValue'] = $this->allowEmptyValue;
}

if ($this->schema !== null) {
$data['schema'] = $this->schema->toArray();
}

if ($this->example !== null) {
$data['example'] = $this->example;
}

if ($this->examples !== []) {
$data['examples'] = $this->examples;
}

return $data;
}

Expand All @@ -43,9 +86,37 @@ public function setDescription(?string $description): void
$this->description = $description;
}

public function setRequired(?bool $required): void
{
$this->required = $required;
}

public function setDeprecated(?bool $deprecated): void
{
$this->deprecated = $deprecated;
}

public function setAllowEmptyValue(?bool $allowEmptyValue): void
{
$this->allowEmptyValue = $allowEmptyValue;
}

public function setSchema(Schema|Reference|null $schema): void
{
$this->schema = $schema;
}

public function setExample(mixed $example): void
{
$this->example = $example;
}

/**
* @param mixed[] $examples
*/
public function setExamples(array $examples): void
{
$this->examples = $examples;
}

}
18 changes: 18 additions & 0 deletions src/OpenApi/Schema/License.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ class License

private string $name;

private ?string $identifier = null;

private ?string $url = null;

public function __construct(string $name)
Expand All @@ -20,6 +22,7 @@ public function __construct(string $name)
public static function fromArray(array $data): License
{
$license = new License($data['name']);
$license->setIdentifier($data['identifier'] ?? null);
$license->setUrl($data['url'] ?? null);

return $license;
Expand All @@ -33,6 +36,11 @@ public function toArray(): array
$data = [];
$data['name'] = $this->name;

// Optional SPDX identifier
if ($this->identifier !== null) {
$data['identifier'] = $this->identifier;
}

// Optional url
if ($this->url !== null) {
$data['url'] = $this->url;
Expand All @@ -41,6 +49,11 @@ public function toArray(): array
return $data;
}

public function setIdentifier(?string $identifier): void
{
$this->identifier = $identifier;
}

public function setUrl(?string $url): void
{
$this->url = $url;
Expand All @@ -51,6 +64,11 @@ public function getName(): string
return $this->name;
}

public function getIdentifier(): ?string
{
return $this->identifier;
}

public function getUrl(): ?string
{
return $this->url;
Expand Down
89 changes: 88 additions & 1 deletion src/OpenApi/Schema/Link.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,99 @@
class Link
{

private ?string $operationRef = null;

private ?string $operationId = null;

/** @var mixed[] */
private array $parameters = [];

private mixed $requestBody = null;

private ?string $description = null;

private ?Server $server = null;

/**
* @param mixed[] $data
*/
public static function fromArray(array $data): Link
{
$link = new Link();
$link->setOperationRef($data['operationRef'] ?? null);
$link->setOperationId($data['operationId'] ?? null);
$link->setParameters($data['parameters'] ?? []);
$link->setRequestBody($data['requestBody'] ?? null);
$link->setDescription($data['description'] ?? null);
$link->setServer(isset($data['server']) ? Server::fromArray($data['server']) : null);

return $link;
}

public function setOperationRef(?string $operationRef): void
{
$this->operationRef = $operationRef;
}

public function setOperationId(?string $operationId): void
{
$this->operationId = $operationId;
}

/**
* @param mixed[] $parameters
*/
public function setParameters(array $parameters): void
{
$this->parameters = $parameters;
}

public function setRequestBody(mixed $requestBody): void
{
$this->requestBody = $requestBody;
}

public function setDescription(?string $description): void
{
$this->description = $description;
}

public function setServer(?Server $server): void
{
$this->server = $server;
}

/**
* @return mixed[]
*/
public function toArray(): array
{
return [];
$data = [];
if ($this->operationRef !== null) {
$data['operationRef'] = $this->operationRef;
}

if ($this->operationId !== null) {
$data['operationId'] = $this->operationId;
}

if ($this->parameters !== []) {
$data['parameters'] = $this->parameters;
}

if ($this->requestBody !== null) {
$data['requestBody'] = $this->requestBody;
}

if ($this->description !== null) {
$data['description'] = $this->description;
}

if ($this->server !== null) {
$data['server'] = $this->server->toArray();
}

return $data;
}

}
4 changes: 2 additions & 2 deletions src/OpenApi/Schema/MediaType.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public static function fromArray(array $data): MediaType
$mediaType = new MediaType();
if (isset($data['schema'])) {
if (isset($data['schema']['$ref'])) {
$mediaType->setSchema(new Reference($data['schema']['$ref']));
$mediaType->setSchema(Reference::fromArray($data['schema']));
} else {
$mediaType->setSchema(Schema::fromArray($data['schema']));
}
Expand All @@ -30,7 +30,7 @@ public static function fromArray(array $data): MediaType
if (isset($data['examples'])) {
foreach ($data['examples'] as $name => $example) {
if (isset($example['$ref'])) {
$mediaType->addExample($name, new Reference($example['$ref']));
$mediaType->addExample($name, Reference::fromArray($example));
} else {
$mediaType->addExample($name, Example::fromArray($example));
}
Expand Down
Loading

0 comments on commit ad2565b

Please sign in to comment.