Skip to content

Commit fcb0c49

Browse files
committed
adding changes to handle links and _links using sdklinks in sdk
1 parent 9fd916a commit fcb0c49

File tree

3 files changed

+109
-0
lines changed

3 files changed

+109
-0
lines changed

cybersource-rest-client-netstandard/cybersource-rest-client-netstandard/Model/PblPaymentLinksAllGet200Response.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,3 +175,4 @@ public override int GetHashCode()
175175
}
176176

177177
}
178+

cybersource-rest-client-netstandard/cybersource-rest-client-netstandard/generator/cybersource_dotnet_sdk_gen.bat

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@ rd /s /q ..\docs
77
rd /s /q ..\..\cybersource-rest-client-netstandard.Test\Api
88
rd /s /q ..\..\cybersource-rest-client-netstandard.Test\Model
99

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

1217
powershell -Command "Get-ChildItem '..\..\..\src\CyberSource\Api\*.cs' -Recurse | ForEach-Object { (Get-Content $_).Replace('Method.POST','Method.Post').Replace('Method.GET','Method.Get').Replace('Method.PATCH','Method.Patch').Replace('Method.DELETE','Method.Delete').Replace('Method.PUT','Method.Put') | Set-Content $_ }"
@@ -131,6 +136,11 @@ robocopy ..\..\..\src\cybersource.test ..\..\cybersource-rest-client-netstandard
131136

132137
robocopy ..\..\..\docs ..\docs /S
133138

139+
@REM replace sdkLinks fieldName to links for supporting links field name in request/response body
140+
echo "starting of replacing the links keyword in PblPaymentLinksAllGet200Response.cs model"
141+
powershell -Command "Set-Content ..\Model\PblPaymentLinksAllGet200Response.cs ((Get-Content ..\Model\PblPaymentLinksAllGet200Response.cs -Raw) -replace '\[DataMember\(Name=\"sdkLinks\", EmitDefaultValue=false\)\]', '[DataMember(Name=\"links\", EmitDefaultValue=false)]')"
142+
echo "completed the task of replacing the links keyword in PblPaymentLinksAllGet200Response.cs model"
143+
134144
del ..\..\..\CyberSource.sln
135145
del ..\..\..\*ignore
136146
git checkout ..\..\..\.gitignore
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
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+
parser.add_argument("-o", "--output", help="Path to the output JSON file.")
72+
args = parser.parse_args()
73+
74+
input_file = args.input if args.input else "cybersource-rest-spec.json"
75+
76+
output_file = args.output if args.output else "cybersource-rest-spec-output.json"
77+
78+
79+
inputPaths =[
80+
{
81+
"path": "paths./ipl/v2/payment-links.get.responses.200.schema.properties.links",
82+
"new_field_name": "sdkLinks"
83+
}
84+
85+
# example of how to add more paths
86+
# ,{
87+
# "path": "paths./pts/v2/payments.post.parameters[0].schema.properties.clientReferenceInformation.properties.code",
88+
# "new_field_name": "sdkCode"
89+
# }
90+
]
91+
92+
# Convert inputPaths to changes
93+
changes = convert_input_paths_to_changes(inputPaths)
94+
95+
script_dir = os.path.dirname(os.path.abspath(__file__))
96+
input_file_path = os.path.join(script_dir, input_file)
97+
output_file_path = os.path.join(script_dir, output_file)
98+
replace_field_names(input_file_path, output_file_path, changes)

0 commit comments

Comments
 (0)