Skip to content

Commit ccd8c4f

Browse files
committed
adding changes to handle links field through automated process
1 parent 823bcc5 commit ccd8c4f

File tree

2 files changed

+105
-0
lines changed

2 files changed

+105
-0
lines changed

generator/cybersource_java_sdk_gen.bat

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@ rd /s /q ..\src\test
77
rd /s /q ..\target
88
rd /s /q ..\docs
99

10+
setlocal enabledelayedexpansion
11+
python replaceFieldNamesForPaths.py -i cybersource-rest-spec.json > replaceFieldLogs.log
12+
del replaceFieldLogs.log
13+
endlocal
14+
1015
java -jar swagger-codegen-cli-2.4.38.jar generate -t cybersource-java-template\libraries\okhttp-gson -i cybersource-rest-spec-java.json -l java -o ../ -c cybersource-java-config.json
1116

1217
REM Changing in Tmsv2customersEmbeddedDefaultPaymentInstrumentEmbeddedInstrumentIdentifierProcessingInformationAuthorizationOptionsInitiatorMerchantInitiatedTransaction.java
@@ -29,6 +34,11 @@ powershell Rename-Item ..\docs\Tmsv2customersEmbeddedDefaultPaymentInstrumentEmb
2934

3035
powershell -Command " Set-Content ..\src\main\java\Api\SecureFileShareApi.java ((get-content ..\src\main\java\Api\SecureFileShareApi.java -raw) -replace '\*_\/_\*;charset=utf-8', '*/*;charset=utf-8') "
3136

37+
@REM replace sdkLinks fieldName to links for supporting links field name in request/response body
38+
echo "starting of replacing the links keyword in PblPaymentLinksAllGet200Response.java model"
39+
powershell -Command " Set-Content ..\src\main\java\Model\PblPaymentLinksAllGet200Response.java ((get-content ..\src\main\java\Model\PblPaymentLinksAllGet200Response.java -raw) -replace '@SerializedName\(\"sdkLinks\"\)', '@SerializedName(\"links\")')"
40+
echo "completed the task of replacing the links keyword in PblPaymentLinksAllGet200Response.java model"
41+
3242
git checkout ..\src\main\java\Api\OAuthApi.java
3343
git checkout ..\src\main\java\Model\AccessTokenResponse.java
3444
git checkout ..\src\main\java\Model\CreateAccessTokenRequest.java
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
import json
2+
import os
3+
import argparse
4+
5+
6+
def traverse_and_replace(obj, path, new_field_name):
7+
if len(path) == 1:
8+
if path[0] in obj:
9+
keys = list(obj.keys())
10+
index = keys.index(path[0])
11+
obj[new_field_name] = obj.pop(path[0])
12+
keys = list(obj.keys()) # Update keys after replacing the key
13+
reordered = {k: obj[k] for k in keys[:index] + [new_field_name] + keys[index:]}
14+
obj.clear()
15+
obj.update(reordered)
16+
else:
17+
if path[0] in obj and isinstance(obj[path[0]], dict):
18+
traverse_and_replace(obj[path[0]], path[1:], new_field_name)
19+
elif path[0] in obj and isinstance(obj[path[0]], list):
20+
if len(path) > 1 and isinstance(path[1], int) and path[1] < len(obj[path[0]]):
21+
traverse_and_replace(obj[path[0]][path[1]], path[2:], new_field_name)
22+
23+
def replace_field_names(input_file_path, output_file_path, changes):
24+
"""
25+
Replace field names in the JSON file based on the given changes.
26+
27+
:param input_file: Path to the input JSON file.
28+
:param output_file: Path to the output JSON file.
29+
:param changes: List of dictionaries containing paths and new field names.
30+
"""
31+
32+
with open(input_file_path, 'r', encoding='utf-8') as file:
33+
data = json.load(file)
34+
35+
for change in changes:
36+
key_path = change['path']
37+
new_field_name = change['new_name']
38+
traverse_and_replace(data, key_path, new_field_name)
39+
40+
with open(output_file_path, 'w', encoding='utf-8') as file:
41+
json.dump(data, file, indent=4)
42+
43+
def convert_input_paths_to_changes(input_paths):
44+
"""
45+
Convert input paths to a list of changes with parsed paths and new field names.
46+
47+
:param input_paths: List of dictionaries containing string paths and new field names.
48+
:return: List of dictionaries with parsed paths and new field names.
49+
"""
50+
changes = []
51+
for item in input_paths:
52+
path_str = item["path"]
53+
path_parts = []
54+
for part in path_str.split('.'):
55+
if '[' in part and ']' in part:
56+
field, index = part.split('[')
57+
path_parts.append(field)
58+
path_parts.append(int(index.strip(']')))
59+
else:
60+
path_parts.append(part)
61+
changes.append({
62+
"path": path_parts,
63+
"new_name": item["new_field_name"]
64+
})
65+
return changes
66+
67+
68+
if __name__ == "__main__":
69+
parser = argparse.ArgumentParser(description="Replace field names in a JSON file.")
70+
parser.add_argument("-i", "--input", help="Path to the input JSON file.")
71+
args = parser.parse_args()
72+
73+
input_file = args.input if args.input else "cybersource-rest-spec.json"
74+
output_file = "cybersource-rest-spec-java.json"
75+
76+
inputPaths =[
77+
{
78+
"path": "paths./ipl/v2/payment-links.get.responses.200.schema.properties.links",
79+
"new_field_name": "sdkLinks"
80+
}
81+
82+
# example of how to add more paths
83+
# ,{
84+
# "path": "paths./pts/v2/payments.post.parameters[0].schema.properties.clientReferenceInformation.properties.code",
85+
# "new_field_name": "sdkCode"
86+
# }
87+
]
88+
89+
# Convert inputPaths to changes
90+
changes = convert_input_paths_to_changes(inputPaths)
91+
92+
script_dir = os.path.dirname(os.path.abspath(__file__))
93+
input_file_path = os.path.join(script_dir, input_file)
94+
output_file_path = os.path.join(script_dir, output_file)
95+
replace_field_names(input_file_path, output_file_path, changes)

0 commit comments

Comments
 (0)