Skip to content

Commit 682eb6d

Browse files
committed
Add support for specific per-schema corrections
1 parent 8db58df commit 682eb6d

4 files changed

Lines changed: 99 additions & 6 deletions

File tree

resources/schema-corrections.json

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{
2+
"us": {
3+
"mp": {
4+
"items": {
5+
"paths": {
6+
"\/v3\/items\/{id}": {
7+
"get": {
8+
"responses": {
9+
"200": {
10+
"content": {
11+
"application\/json": {
12+
"schema": {
13+
"$ref": "#\/components\/schemas\/ItemResponses"
14+
}
15+
}
16+
}
17+
}
18+
}
19+
}
20+
}
21+
},
22+
"schemas": {}
23+
}
24+
}
25+
}
26+
}

utils/constants.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
const DEFAULT_MODEL_DIR = 'Model';
1414

1515
const LOGFILE = __DIR__ . '/../generate.log';
16+
const SCHEMA_FIXES_FILE = RESOURCE_DIR . '/schema-corrections.json';
1617

1718
const BASIC_SCHEME_HEADER = 'Authorization';
1819
const ACCESS_TOKEN_HEADER = 'WM_SEC.ACCESS_TOKEN';

utils/customize-schemas.php

Lines changed: 43 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,18 @@
1212
*
1313
* @param string $path The path to the schema file
1414
* @param string $category The category code for the schema
15-
* @param string $name The human-readable API name for the schema
15+
* @param string $country The country code for the schema
16+
* @param string $apiCode The internal API code for the schema
17+
* @param string $apiName The human-readable API name for the schema
1618
* @return void
1719
*/
18-
function customizeSchema(string $path, string $category, string $name): void
19-
{
20+
function customizeSchema(
21+
string $path,
22+
string $category,
23+
string $country,
24+
string $apiCode,
25+
string $apiName
26+
): void {
2027
// There are auth headers that are the same on (nearly) every request, but which Walmart includes in every
2128
// single request schema. We remove them so that the SDK is easier to use, and will pass them in during the
2229
// request signing process when an endpoint is called.
@@ -78,7 +85,7 @@ function customizeSchema(string $path, string $category, string $name): void
7885
$security = [];
7986

8087
// Standardize tags based on our internal naming convention (derived from resources/apis.json)
81-
$verb['tags'] = [$name];
88+
$verb['tags'] = [$apiName];
8289

8390
// Update each operation's parameters and auth information
8491
foreach ($verb['parameters'] as $i => $parameter) {
@@ -176,6 +183,8 @@ function customizeSchema(string $path, string $category, string $name): void
176183
$schema['components']['schemas'] = replaceComponentInlineSchemas($componentSchemas);
177184
}
178185

186+
$schema = fixSchema($schema, $country, $category, $apiCode);
187+
179188
file_put_contents($path, json_encode($schema, JSON_PRETTY_PRINT));
180189
}
181190

@@ -345,14 +354,43 @@ function chooseContentType(array $content): string
345354
return array_key_first($content);
346355
}
347356

357+
/**
358+
* Applies specific model fixes from the resources/schema-corrections.json file to the current schema.
359+
*
360+
* @param array $schema The schema to apply the fixes to
361+
* @param string $country The country code for the schema
362+
* @param string $category The category code for the schema
363+
* @param string $code The internal API code for the schema
364+
*/
365+
function fixSchema(array $schema, string $country, string $category, string $code): array
366+
{
367+
$allFixes = json_decode(file_get_contents(SCHEMA_FIXES_FILE), true);
368+
$fixes = $allFixes[$country][$category][$code] ?? [];
369+
370+
if (isset($fixes['paths'])) {
371+
$schema['paths'] = array_merge_recursive_distinct(
372+
$schema['paths'],
373+
$fixes['paths']
374+
);
375+
}
376+
377+
return $schema;
378+
}
379+
348380
/**
349381
* Prepares schemas for api/model generation using janephp.
350382
*/
351383
function customizeSchemas(array $categories, array $countries, array $apiCodes): void
352384
{
353385
$schemas = schemas($categories, $countries, $apiCodes);
354386
foreach ($schemas as $schemaInfo) {
355-
customizeSchema($schemaInfo['path'], $schemaInfo['category'], $schemaInfo['api']['name']);
387+
customizeSchema(
388+
$schemaInfo['path'],
389+
$schemaInfo['category'],
390+
$schemaInfo['country'],
391+
$schemaInfo['api']['code'],
392+
$schemaInfo['api']['name'],
393+
);
356394
}
357395
}
358396

utils/helpers.php

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,10 +138,38 @@ function handleSchemaOpts(): array
138138
];
139139
}
140140

141-
// Get the library version
141+
/**
142+
* Get the current version code for the library.
143+
*
144+
* @return string
145+
*/
142146
function libVersion(): string
143147
{
144148
$configPath = RESOURCE_DIR . '/generator-config.json';
145149
$config = json_decode(file_get_contents($configPath), true);
146150
return $config['artifactVersion'];
147151
}
152+
153+
/**
154+
* Merge two arrays recursively, overwriting keys in the first array with keys from the second array.
155+
* Poached from https://www.php.net/manual/en/function.array-merge-recursive.php#92195
156+
*
157+
* @param array $array1
158+
* @param array $array2
159+
* @return array
160+
*/
161+
function array_merge_recursive_distinct(array &$array1, array &$array2): array
162+
{
163+
$merged = $array1;
164+
165+
foreach ($array2 as $key => &$value)
166+
{
167+
if (is_array($value) && isset($merged[$key]) && is_array($merged[$key])) {
168+
$merged[$key] = array_merge_recursive_distinct($merged[$key], $value);
169+
} else {
170+
$merged [$key] = $value;
171+
}
172+
}
173+
174+
return $merged;
175+
}

0 commit comments

Comments
 (0)