Skip to content

Commit f321c7f

Browse files
committed
patch
1 parent 2d501e8 commit f321c7f

4 files changed

Lines changed: 67 additions & 4 deletions

File tree

documentation/docs/reference/api.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,16 @@ Once generated, copy the key and keep it safe. This key is used to authenticate
3737

3838
``<SNAPSHOT_ID>`` can be retrieved from the URL when you browse a snapshot from the repositories list or from the API when you list snapshots of a repository.
3939

40+
### Global query parameters
41+
42+
- ``normalize`` (optional): when set on a request, all keys in the JSON response are converted to lowercase recursively.
43+
44+
Example:
45+
46+
```bash
47+
curl --fail-with-body -L -s -X GET -H "Authorization: Bearer <APIKEY>" "https://repomanager.mydomain.net/api/v2/hosts/?normalize"
48+
```
49+
4050

4151
### Source repositories
4252

www/controllers/Api/Api.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,12 @@ private static function returnSuccess(array $results): void
177177
// TODO: remove 'return' key in favor of 'rc' key in future major release
178178
$results['return'] = $results['rc'];
179179
$results['note'] = "The 'return' key is deprecated and will be removed in a future major release (replaced by 'rc' key)";
180+
181+
// If the "normalize" query parameter is set, convert all result keys to lowercase
182+
if (isset($_GET['normalize'])) {
183+
$results = Serializer::normalize($results);
184+
}
185+
180186
http_response_code((int) $results['rc']);
181187
echo json_encode($results);
182188
exit;

www/controllers/Api/Serializer.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
namespace Controllers\Api;
4+
5+
class Serializer
6+
{
7+
/**
8+
* Recursively normalize the keys of an array to lowercase
9+
* so that the API always returns a consistent key format, no matter
10+
* whether the data comes from the database (PascalCase columns) or
11+
* is built by the backend (lowercase keys).
12+
*/
13+
public static function normalize($data)
14+
{
15+
// Only arrays have keys to normalize, return scalars as-is
16+
if (!is_array($data)) {
17+
return $data;
18+
}
19+
20+
$result = [];
21+
22+
foreach ($data as $key => $value) {
23+
// Recursively normalize nested arrays
24+
$value = self::normalize($value);
25+
26+
// Numeric keys (sequential lists) are kept as-is
27+
if (is_int($key)) {
28+
$result[$key] = $value;
29+
continue;
30+
}
31+
32+
$result[strtolower($key)] = $value;
33+
}
34+
35+
return $result;
36+
}
37+
}

www/update/database/5.12.2.php

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@
3434
throw new Exception('could not create new indexes for hosts table: ' . $e->getMessage());
3535
}
3636

37-
// Add new settings in settings table
3837
try {
3938
// Add 'compliance_threshold_count' column if not exists
4039
if (!$hostsDb->columnExist('settings', 'compliance_threshold_count')) {
@@ -45,18 +44,29 @@
4544
if (!$hostsDb->columnExist('settings', 'compliance_threshold_days')) {
4645
$hostsDb->exec("ALTER TABLE settings ADD COLUMN compliance_threshold_days INTEGER NOT NULL DEFAULT 30");
4746
}
47+
} catch (Exception $e) {
48+
throw new Exception('could not create new columns in hosts settings table: ' . $e->getMessage());
49+
}
4850

51+
try {
4952
// Overwrite settings with old values if they exist
50-
$hostsDb->exec("UPDATE settings SET compliance_threshold_count = (SELECT pkgs_count_considered_outdated FROM settings), compliance_threshold_days = 30");
53+
if ($hostsDb->columnExist('settings', 'pkgs_count_considered_outdated')) {
54+
$hostsDb->exec("UPDATE settings SET compliance_threshold_count = (SELECT pkgs_count_considered_outdated FROM settings)");
55+
}
56+
} catch (Exception $e) {
57+
throw new Exception('could not migrate old settings to new compliance_threshold_count in hosts settings table: ' . $e->getMessage());
58+
}
5159

52-
// Drop old settings columns
60+
try {
61+
// Drop old 'pkgs_count_considered_outdated' column from settings table
5362
if ($hostsDb->columnExist('settings', 'pkgs_count_considered_outdated')) {
5463
$hostsDb->exec("ALTER TABLE settings DROP COLUMN pkgs_count_considered_outdated");
5564
}
5665

66+
// Drop old 'pkgs_count_considered_critical' column from settings table
5767
if ($hostsDb->columnExist('settings', 'pkgs_count_considered_critical')) {
5868
$hostsDb->exec("ALTER TABLE settings DROP COLUMN pkgs_count_considered_critical");
5969
}
6070
} catch (Exception $e) {
61-
throw new Exception('could not add new settings columns in hosts settings table: ' . $e->getMessage());
71+
throw new Exception('could not drop old settings columns in hosts settings table: ' . $e->getMessage());
6272
}

0 commit comments

Comments
 (0)