Skip to content

Commit e62c393

Browse files
committed
fix(merge-specs): Use objects instead of associative arrays to fix empty JSON objects
Signed-off-by: provokateurin <[email protected]>
1 parent 5b41dea commit e62c393

File tree

1 file changed

+63
-53
lines changed

1 file changed

+63
-53
lines changed

merge-specs.php

Lines changed: 63 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -84,96 +84,106 @@
8484
$data
8585
['paths']
8686
['/ocs/v2.php/cloud/capabilities']
87-
['get']
88-
['responses']
89-
['200']
90-
['content']
91-
['application/json']
92-
['schema']
93-
['properties']
94-
['ocs']
95-
['properties']
96-
['data']
97-
['properties']
98-
['capabilities']
99-
['anyOf']
100-
= array_map(fn (string $capability): array => ['$ref' => '#/components/schemas/' . $capability], $capabilities);
101-
102-
function loadSpec(string $path): array {
103-
return rewriteRefs(json_decode(file_get_contents($path), true));
87+
->get
88+
->responses
89+
->{'200'}
90+
->content
91+
->{'application/json'}
92+
->schema
93+
->properties
94+
->ocs
95+
->properties
96+
->data
97+
->properties
98+
->capabilities
99+
->anyOf
100+
= array_map(static fn (string $capability): array => ['$ref' => '#/components/schemas/' . $capability], $capabilities);
101+
102+
function loadSpec(string $path): object {
103+
return rewriteRefs(json_decode(file_get_contents($path), false, 512, JSON_THROW_ON_ERROR));
104104
}
105105

106-
function rewriteRefs(array $spec): array {
107-
$readableAppID = Helpers::generateReadableAppID($spec['info']['title']);
108-
array_walk_recursive($spec, function (mixed &$item, string $key) use ($readableAppID): void {
106+
function rewriteRefs(object $spec): object {
107+
$readableAppID = Helpers::generateReadableAppID($spec->info->title);
108+
object_walk_recursive($spec, static function (mixed &$item, string $key) use ($readableAppID): void {
109109
if ($key === '$ref' && $item !== '#/components/schemas/OCSMeta') {
110110
$item = str_replace('#/components/schemas/', '#/components/schemas/' . $readableAppID, $item);
111111
}
112112
});
113113
return $spec;
114114
}
115115

116-
function collectCapabilities(array $spec): array {
116+
function object_walk_recursive(object $object, \Closure $callback): void {
117+
foreach (array_keys(get_object_vars($object)) as $key) {
118+
$callback($object->{$key}, $key);
119+
if (is_object($object->{$key})) {
120+
object_walk_recursive($object->{$key}, $callback);
121+
}
122+
if (is_array($object->{$key})) {
123+
foreach ($object->{$key} as $item) {
124+
if (is_object($item)) {
125+
object_walk_recursive($item, $callback);
126+
}
127+
}
128+
}
129+
}
130+
}
131+
132+
function collectCapabilities(object $spec): array {
117133
$capabilities = [];
118-
$readableAppID = Helpers::generateReadableAppID($spec['info']['title']);
119-
foreach (array_keys($spec['components']['schemas']) as $name) {
120-
if ($name == 'Capabilities' || $name == 'PublicCapabilities') {
134+
$readableAppID = Helpers::generateReadableAppID($spec->info->title);
135+
foreach (array_keys(get_object_vars($spec->components->schemas)) as $name) {
136+
if ($name === 'Capabilities' || $name === 'PublicCapabilities') {
121137
$capabilities[] = $readableAppID . $name;
122138
}
123139
}
124140

125141
return $capabilities;
126142
}
127143

128-
function rewriteSchemaNames(array $spec): array {
129-
$schemas = $spec['components']['schemas'];
130-
$readableAppID = Helpers::generateReadableAppID($spec['info']['title']);
144+
function rewriteSchemaNames(object $spec): array {
145+
$schemas = get_object_vars($spec->components->schemas);
146+
$readableAppID = Helpers::generateReadableAppID($spec->info->title);
131147
return array_combine(
132-
array_map(fn (string $key): string => $key === 'OCSMeta' ? $key : $readableAppID . $key, array_keys($schemas)),
148+
array_map(static fn (string $key): string => $key === 'OCSMeta' ? $key : $readableAppID . $key, array_keys($schemas)),
133149
array_values($schemas),
134150
);
135151
}
136152

137-
function rewriteTags(array $spec): array {
138-
return array_map(function (array $tag) use ($spec) {
139-
$tag['name'] = $spec['info']['title'] . '/' . $tag['name'];
153+
function rewriteTags(object $spec): array {
154+
return array_map(static function (object $tag) use ($spec) {
155+
$tag->name = $spec->info->title . '/' . $tag->name;
140156
return $tag;
141-
}, $spec['tags']);
157+
}, $spec->tags);
142158
}
143159

144-
function rewriteOperations(array $spec): array {
160+
function rewriteOperations(object $spec): array {
145161
global $firstStatusCode;
146162

147-
foreach (array_keys($spec['paths']) as $path) {
148-
foreach (array_keys($spec['paths'][$path]) as $method) {
163+
foreach (array_keys(get_object_vars($spec->paths)) as $path) {
164+
foreach (array_keys(get_object_vars($spec->paths->{$path})) as $method) {
149165
if (!in_array($method, ['delete', 'get', 'post', 'put', 'patch', 'options'])) {
150166
continue;
151167
}
152-
$operation = &$spec['paths'][$path][$method];
153-
if (array_key_exists('operationId', $operation)) {
154-
$operation['operationId'] = $spec['info']['title'] . '-' . $operation['operationId'];
168+
$operation = &$spec->paths->{$path}->{$method};
169+
if (property_exists($operation, 'operationId')) {
170+
$operation->operationId = $spec->info->title . '-' . $operation->operationId;
155171
}
156-
if (array_key_exists('tags', $operation)) {
157-
$operation['tags'] = array_map(fn (string $tag): string => $spec['info']['title'] . '/' . $tag, $operation['tags']);
172+
if (property_exists($operation, 'tags')) {
173+
$operation->tags = array_map(static fn (string $tag): string => $spec->info->title . '/' . $tag, $operation->tags);
158174
} else {
159-
$operation['tags'] = [$spec['info']['title']];
175+
$operation->tags = [$spec->info->title];
160176
}
161-
if ($firstStatusCode && array_key_exists('responses', $operation)) {
177+
if ($firstStatusCode && property_exists($operation, 'responses')) {
162178
/** @var string $value */
163-
$value = array_key_first($operation['responses']);
164-
$operation['responses'] = [$value => $operation['responses'][$value]];
165-
}
166-
if (array_key_exists('security', $operation)) {
167-
$counter = count($operation['security']);
168-
for ($i = 0; $i < $counter; $i++) {
169-
if (count($operation['security'][$i]) == 0) {
170-
$operation['security'][$i] = new stdClass(); // When reading {} will be converted to [], so we have to fix it
171-
}
172-
}
179+
$value = array_key_first(get_object_vars($operation->responses));
180+
$response = $operation->responses->{$value};
181+
$operation->responses = new stdClass();
182+
$operation->responses->{$value} = $response;
173183
}
174184
}
175185
}
176-
return $spec['paths'];
186+
return get_object_vars($spec->paths);
177187
}
178188

179189
file_put_contents($mergedSpecPath, json_encode($data, Helpers::jsonFlags()) . "\n");

0 commit comments

Comments
 (0)