Skip to content

Commit

Permalink
Add extension to the OpenAPI processor to automatically convert '*' t…
Browse files Browse the repository at this point in the history
…o a list of all properties, to save typing and avoid mistakes with newly added field.s
  • Loading branch information
BusterNeece committed Feb 26, 2025
1 parent 3c65655 commit 03cb352
Show file tree
Hide file tree
Showing 25 changed files with 72 additions and 146 deletions.
2 changes: 2 additions & 0 deletions backend/src/Console/Command/Dev/GenerateApiDocsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use App\Container\EnvironmentAwareTrait;
use App\Container\LoggerAwareTrait;
use App\OpenApi\AddXEnumNames;
use App\OpenApi\MakeAllFieldsRequired;
use App\Utilities\Types;
use App\Version;
use OpenApi\Annotations\OpenApi;
Expand Down Expand Up @@ -78,6 +79,7 @@ public function generate(

$pipeline = $generator->getProcessorPipeline();
$pipeline->add(new AddXEnumNames());
$pipeline->add(new MakeAllFieldsRequired());

return $generator->generate($finder);
}
Expand Down
5 changes: 1 addition & 4 deletions backend/src/Entity/Api/Account/ChangePassword.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,7 @@

#[OA\Schema(
schema: 'Api_Account_ChangePassword',
required: [
'current_password',
'new_password',
],
required: ['*'],
type: 'object'
)]
final readonly class ChangePassword
Expand Down
4 changes: 1 addition & 3 deletions backend/src/Entity/Api/Account/NewApiKey.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@

#[OA\Schema(
schema: 'Api_Account_NewApiKey',
required: [
'key',
],
required: ['*'],
type: 'object'
)]
final readonly class NewApiKey
Expand Down
4 changes: 1 addition & 3 deletions backend/src/Entity/Api/Account/TwoFactorStatus.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@

#[OA\Schema(
schema: 'Api_Account_TwoFactorStatus',
required: [
'two_factor_enabled',
],
required: ['*'],
type: 'object'
)]
final readonly class TwoFactorStatus
Expand Down
13 changes: 1 addition & 12 deletions backend/src/Entity/Api/Admin/AuditLog.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,7 @@

#[OA\Schema(
schema: 'Api_Admin_AuditLog',
required: [
'id',
'timestamp',
'operation',
'operation_text',
'class',
'identifier',
'target_class',
'target',
'user',
'changes',
],
required: ['*'],
type: 'object'
)]
final readonly class AuditLog
Expand Down
6 changes: 1 addition & 5 deletions backend/src/Entity/Api/Admin/AuditLogChangeset.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,7 @@

#[OA\Schema(
schema: 'Api_Admin_AuditLogChangeset',
required: [
'field',
'from',
'to',
],
required: ['*'],
type: 'object'
)]
final readonly class AuditLogChangeset
Expand Down
5 changes: 1 addition & 4 deletions backend/src/Entity/Api/Admin/Permission.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,7 @@

#[OA\Schema(
schema: 'Api_Admin_Permission',
required: [
'id',
'name',
],
required: ['*'],
type: 'object'
)]
final readonly class Permission
Expand Down
5 changes: 1 addition & 4 deletions backend/src/Entity/Api/Admin/Permissions.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,7 @@

#[OA\Schema(
schema: 'Api_Admin_Permissions',
required: [
'global',
'station',
],
required: ['*'],
type: 'object'
)]
final readonly class Permissions
Expand Down
6 changes: 1 addition & 5 deletions backend/src/Entity/Api/Admin/ServerStats/CpuStats.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,7 @@

#[OA\Schema(
schema: 'Api_Admin_ServerStats_CpuStats',
required: [
'total',
'cores',
'load',
],
required: ['*'],
type: 'object'
)]
final class CpuStats
Expand Down
8 changes: 1 addition & 7 deletions backend/src/Entity/Api/Admin/ServerStats/CpuStatsSection.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,7 @@

#[OA\Schema(
schema: 'Api_Admin_ServerStats_CpuStatsSection',
required: [
'name',
'usage',
'idle',
'io_wait',
'steal',
],
required: ['*'],
type: 'object'
)]
final readonly class CpuStatsSection
Expand Down
17 changes: 1 addition & 16 deletions backend/src/Entity/Api/Admin/ServerStats/MemoryStats.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,7 @@

#[OA\Schema(
schema: 'Api_Admin_ServerStats_MemoryStats',
required: [
'total_bytes',
'total_readable',
'free_bytes',
'free_readable',
'buffers_bytes',
'buffers_readable',
'cached_bytes',
'cached_readable',
'sReclaimable_bytes',
'sReclaimable_readable',
'shmem_bytes',
'shmem_readable',
'used_bytes',
'used_readable',
],
required: ['*'],
type: 'object'
)]
final readonly class MemoryStats
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,7 @@

#[OA\Schema(
schema: 'Api_Admin_ServerStats_NetworkInterfaceReceived',
required: [
'speed_bytes',
'speed_readable',
'packets',
'errs',
'drop',
'fifo',
'frame',
'compressed',
'multicast',
],
required: ['*'],
type: 'object'
)]
final readonly class NetworkInterfaceReceived
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,7 @@

#[OA\Schema(
schema: 'Api_Admin_ServerStats_NetworkInterfaceStats',
required: [
'interface_name',
'received',
'transmitted',
],
required: ['*'],
type: 'object'
)]
final readonly class NetworkInterfaceStats
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,7 @@

#[OA\Schema(
schema: 'Api_Admin_ServerStats_NetworkInterfaceTransmitted',
required: [
'speed_bytes',
'speed_readable',
'packets',
'errs',
'drop',
'fifo',
'frame',
'carrier',
'compressed',
],
required: ['*'],
type: 'object'
)]
final class NetworkInterfaceTransmitted
Expand Down
8 changes: 1 addition & 7 deletions backend/src/Entity/Api/Admin/ServerStats/ServerStats.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,7 @@

#[OA\Schema(
schema: 'Api_Admin_ServerStats',
required: [
'cpu',
'memory',
'swap',
'disk',
'network',
],
required: ['*'],
type: 'object'
)]
final readonly class ServerStats
Expand Down
9 changes: 1 addition & 8 deletions backend/src/Entity/Api/Admin/ServerStats/StorageStats.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,7 @@

#[OA\Schema(
schema: 'Api_Admin_ServerStats_StorageStats',
required: [
'total_bytes',
'total_readable',
'free_bytes',
'free_readable',
'used_bytes',
'used_readable',
],
required: ['*'],
type: 'object'
)]
final class StorageStats
Expand Down
9 changes: 1 addition & 8 deletions backend/src/Entity/Api/Error.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,7 @@

#[OA\Schema(
schema: 'Api_Error',
required: [
'code',
'type',
'message',
'formatted_message',
'extra_data',
'success',
],
required: ['*'],
type: 'object'
)]
final readonly class Error
Expand Down
6 changes: 1 addition & 5 deletions backend/src/Entity/Api/LogContents.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,7 @@

#[OA\Schema(
schema: 'Api_LogContents',
required: [
'contents',
'eof',
'position',
],
required: ['*'],
type: 'object',
readOnly: true
)]
Expand Down
8 changes: 1 addition & 7 deletions backend/src/Entity/Api/LogType.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,7 @@

#[OA\Schema(
schema: 'Api_LogType',
required: [
'key',
'name',
'path',
'tail',
'links',
],
required: ['*'],
type: 'object',
readOnly: true
)]
Expand Down
6 changes: 1 addition & 5 deletions backend/src/Entity/Api/Status.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,7 @@

#[OA\Schema(
schema: 'Api_Status',
required: [
'success',
'message',
'formatted_message',
],
required: ['*'],
type: 'object'
)]
readonly class Status
Expand Down
5 changes: 1 addition & 4 deletions backend/src/Entity/Api/SystemStatus.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,7 @@

#[OA\Schema(
schema: 'Api_SystemStatus',
required: [
'online',
'timestamp',
],
required: ['*'],
type: 'object'
)]
final readonly class SystemStatus
Expand Down
8 changes: 1 addition & 7 deletions backend/src/Entity/Api/Time.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,7 @@

#[OA\Schema(
schema: 'Api_Time',
required: [
'timestamp',
'utc_datetime',
'utc_date',
'utc_time',
'utc_json',
],
required: ['*'],
type: 'object'
)]
final readonly class Time
Expand Down
5 changes: 1 addition & 4 deletions backend/src/Entity/Api/UploadFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,7 @@

#[OA\Schema(
schema: 'Api_UploadFile',
required: [
'path',
'file',
],
required: ['*'],
type: 'object'
)]
final readonly class UploadFile
Expand Down
48 changes: 48 additions & 0 deletions backend/src/OpenApi/MakeAllFieldsRequired.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

declare(strict_types=1);

namespace App\OpenApi;

use OpenApi\Analysis;
use OpenApi\Annotations as OA;
use OpenApi\Generator;

/**
* Expand '*' in the `required` field to be every field in the schema.
*/
class MakeAllFieldsRequired
{
public function __invoke(Analysis $analysis): void
{
/** @var OA\Schema[] $schemas */
$schemas = $analysis->getAnnotationsOfType(OA\Schema::class, true);

foreach ($schemas as $schema) {
if ($schema->required === ['*']) {
$schema->required = $this->getPropertyNames($schema);
}
}
}

protected function getPropertyNames(OA\Schema $schema): array
{
$propertyNames = [];

if (!Generator::isDefault($schema->allOf)) {
foreach ($schema->allOf as $item) {
$propertyNames = array_merge($propertyNames, $this->getPropertyNames($item));
}
}

if (!Generator::isDefault($schema->properties)) {
foreach ($schema->properties as $property) {
if (!Generator::isDefault($property->property)) {
$propertyNames[] = $property->property;
}
}
}

return $propertyNames;
}
}
1 change: 0 additions & 1 deletion web/static/openapi.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4472,7 +4472,6 @@ components:
- name
- path
- tail
- links
type: object
readOnly: true
allOf:
Expand Down

0 comments on commit 03cb352

Please sign in to comment.