Skip to content

Commit 94480fb

Browse files
committed
adding changes for handling links by sdklinks in spec
1 parent 6995948 commit 94480fb

File tree

3 files changed

+110
-0
lines changed

3 files changed

+110
-0
lines changed

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+

generator/cybersource_dotnet_sdk_gen.bat

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

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

1116
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 $_ }"
@@ -116,6 +121,12 @@ del ..\cybersource-rest-client-dotnet.sln
116121
powershell Rename-Item ..\CyberSource.sln cybersource-rest-client-dotnet.sln
117122

118123
xcopy ..\src\cybersource ..\ /s /e /y /exclude:excludelist.txt
124+
125+
@REM replace sdkLinks fieldName to links for supporting links field name in request/response body
126+
echo "starting of replacing the links keyword in PblPaymentLinksAllGet200Response.cs model"
127+
powershell -Command "Set-Content ..\Model\PblPaymentLinksAllGet200Response.cs ((Get-Content ..\Model\PblPaymentLinksAllGet200Response.cs -Raw) -replace '\[DataMember\(Name=\"sdkLinks\", EmitDefaultValue=false\)\]', '[DataMember(Name=\"links\", EmitDefaultValue=false)]')"
128+
echo "completed the task of replacing the links keyword in PblPaymentLinksAllGet200Response.cs model"
129+
119130
git checkout ..\README.md
120131
git checkout ..\Api\OAuthApi.cs
121132
git checkout ..\Model\AccessTokenResponse.cs
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)