Skip to content

Commit 890ef34

Browse files
author
jessevz
committed
Fixed the swagger post parameter generation
1 parent 16b5129 commit 890ef34

File tree

6 files changed

+39
-4
lines changed

6 files changed

+39
-4
lines changed

src/inc/apiv2/common/AbstractBaseAPI.class.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -408,7 +408,7 @@ protected static function getExpandPermissions(string $expand): array
408408
DAccessControl::PUBLIC_ACCESS => array(LogEntry::PERM_READ),
409409

410410
// src/inc/defines/notifications.php
411-
DAccessControl::LOGIN_ACCESS => array(NotificationSetting::PERM_CREATE, NotificationSetting::PERM_READ, NotificationSetting::PERM_UPDATE, NotificationSetting::PERM_DELETE),
411+
DAccessControl::LOGIN_ACCESS => array(NotificationSetting::PERM_CREATE, NotificationSetting::PERM_READ, NotificationSetting::PERM_UPDATE, NotificationSetting::PERM_DELETE, LogEntry::PERM_CREATE, LogEntry::PERM_DELETE, LogEntry::PERM_UPDATE),
412412
);
413413

414414
/**
@@ -804,6 +804,16 @@ protected function validateData(array $data, array $features)
804804
}
805805
}
806806

807+
//function for automatic swagger doc generation
808+
function getAllPostParameters(array $features): array {
809+
$postFeatures = [];
810+
foreach($features as $key => $value) {
811+
if ($value['protected'] == False) {
812+
$postFeatures[$key] = $value;
813+
}
814+
}
815+
return $postFeatures;
816+
}
807817
/**
808818
* Validate incoming parameter keys
809819
*/

src/inc/apiv2/common/openAPISchema.routes.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
function typeLookup($feature): array {
1313
$type_format = null;
1414
$type_enum = null;
15+
$sub_type = null;
1516
if ($feature['type'] == 'int') {
1617
$type = "integer";
1718
} elseif ($feature['type'] == 'uint64') {
@@ -24,6 +25,7 @@ function typeLookup($feature): array {
2425
$type = "object";
2526
} elseif ($feature['type'] == 'array') {
2627
$type = "array";
28+
$sub_type = "integer"; //TODO: subtype is hardcoded because we only have int arrays
2729
} elseif ($feature['type'] == 'bool') {
2830
$type = "boolean";
2931
} elseif (str_starts_with($feature['type'], 'str(')) {
@@ -42,6 +44,7 @@ function typeLookup($feature): array {
4244
"type" => $type,
4345
"type_format" => $type_format,
4446
"type_enum" => $type_enum,
47+
"subtype" => $sub_type
4548
];
4649

4750
return $result;
@@ -184,6 +187,9 @@ function makeProperties($features, $skipPK=false): array {
184187
if ($ret["type_enum"] !== null) {
185188
$propertyVal[$feature['alias']]["enum"] = $ret["type_enum"];
186189
}
190+
if ($ret["subtype"] !== null) {
191+
$propertyVal[$feature['alias']]["items"]["type"] = $ret["subtype"];
192+
}
187193
}
188194
return $propertyVal;
189195
};
@@ -413,7 +419,7 @@ function makeDescription($isRelation, $method, $singleObject): string {
413419
$json_api_header = makeJsonApiHeader();
414420
$links = makeLinks($uri);
415421
$properties_return_post_patch = array_merge($json_api_header, $properties_return_post_patch);
416-
$properties_create = buildPatchPost(makeProperties($class->getCreateValidFeatures(), true));
422+
$properties_create = buildPatchPost(makeProperties($class->getAllPostParameters($class->getCreateValidFeatures(), true)));
417423
$properties_get = array_merge($json_api_header, $links, $properties_get_single, $included);
418424
$properties_patch = buildPatchPost(makeProperties($class->getPatchValidFeatures(), true));
419425

src/inc/apiv2/model/agents.routes.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public static function getToManyRelationships(): array {
7171
}
7272

7373
protected function createObject(array $data): int {
74-
assert(False, "Chunks cannot be created via API");
74+
assert(False, "Agents cannot be created via API");
7575
return -1;
7676
}
7777

src/inc/apiv2/model/crackertypes.routes.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,14 @@ public static function getToManyRelationships(): array {
3737
];
3838
}
3939

40+
function getAllPostParameters(array $features): array {
41+
42+
//for documentation purposes isChunkingAVailable has to be removed
43+
// because it is currently not setable by the user
44+
$features = parent::getAllPostParameters($features);
45+
unset($features[CrackerBinaryType::IS_CHUNKING_AVAILABLE]);
46+
return $features;
47+
}
4048

4149
protected function createObject(array $data): int {
4250
CrackerUtils::createBinaryType($data[CrackerBinaryType::TYPE_NAME]);

src/inc/apiv2/model/notifications.routes.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,19 @@ public static function getToOneRelationships(): array {
2828
];
2929
}
3030

31+
function getAllPostParameters(array $features): array {
32+
$features = parent::getAllPostParameters($features);
33+
unset($features[NotificationSetting::IS_ACTIVE]);
34+
return $features;
35+
}
3136

3237
public function getFormFields(): array {
3338
return ['actionFilter' => ['type' => 'str(256)']];
3439
}
3540

3641
protected function createObject(array $data): int {
3742
$dummyPost = [];
38-
switch (DNotificationType::getObjectType($data['action'])) {
43+
switch (DNotificationType::getObjectType($data[NotificationSetting::ACTION])) {
3944
case DNotificationObjectType::USER:
4045
$dummyPost['user'] = $data['actionFilter'];
4146
break;

src/inc/apiv2/model/users.routes.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,12 @@ protected function createObject($data): int {
9797
return $objects[0]->getId();
9898
}
9999

100+
function getAllPostParameters(array $features): array {
101+
102+
$features = parent::getAllPostParameters($features);
103+
unset($features[User::IS_VALID]);
104+
return $features;
105+
}
100106

101107
protected function deleteObject(object $object): void {
102108
UserUtils::deleteUser($object->getId(), $this->getCurrentUser());

0 commit comments

Comments
 (0)