Skip to content

Feature/handling links field to api #187

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
4 commits merged into from
May 30, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public class {{classname}} {
*/
public okhttp3.Call {{operationId}}Call({{#allParams}}{{{dataType}}} {{paramName}}, {{/allParams}}final ProgressResponseBody.ProgressListener progressListener, final ProgressRequestBody.ProgressRequestListener progressRequestListener) throws ApiException {
SdkTracker sdkTracker = new SdkTracker();
Object localVarPostBody = {{#bodyParam}}sdkTracker.insertDeveloperIdTracker({{paramName}}, {{dataType}}.class.getSimpleName(), apiClient.merchantConfig.getRunEnvironment(), apiClient.merchantConfig.getDefaultDeveloperId()){{/bodyParam}}{{^bodyParam}}null{{/bodyParam}};
Object localVarPostBody = {{#bodyParam}}sdkTracker.insertDeveloperIdTracker({{paramName}}, {{{dataType}}}.class.getSimpleName(), apiClient.merchantConfig.getRunEnvironment(), apiClient.merchantConfig.getDefaultDeveloperId()){{/bodyParam}}{{^bodyParam}}null{{/bodyParam}};
{{^bodyParam}}
if ("{{httpMethod}}".equalsIgnoreCase("POST")) {
{{localVariablePrefix}}localVarPostBody = "{}";
Expand Down
28 changes: 10 additions & 18 deletions generator/cybersource_java_sdk_gen.bat
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,20 @@ rd /s /q ..\src\test
rd /s /q ..\target
rd /s /q ..\docs

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

REM Changing in Tmsv2customersEmbeddedDefaultPaymentInstrumentEmbeddedInstrumentIdentifierProcessingInformationAuthorizationOptionsInitiatorMerchantInitiatedTransaction.java

powershell -Command "(Get-Content ..\src\main\java\Model\Tmsv2customersEmbeddedDefaultPaymentInstrumentEmbeddedInstrumentIdentifierProcessingInformationAuthorizationOptionsInitiatorMerchantInitiatedTransaction.java) | ForEach-Object { $_ -creplace 'Tmsv2customersEmbeddedDefaultPaymentInstrumentEmbeddedInstrumentIdentifierProcessingInformationAuthorizationOptionsInitiatorMerchantInitiatedTransaction', 'Tmsv2customersEmbeddedDefaultPaymentInstrumentEmbeddedMerchantInitiatedTransaction'} | Set-Content ..\src\main\java\Model\Tmsv2customersEmbeddedDefaultPaymentInstrumentEmbeddedInstrumentIdentifierProcessingInformationAuthorizationOptionsInitiatorMerchantInitiatedTransaction.java"

powershell -Command "(Get-Content ..\src\main\java\Model\Tmsv2customersEmbeddedDefaultPaymentInstrumentEmbeddedInstrumentIdentifierProcessingInformationAuthorizationOptionsInitiatorMerchantInitiatedTransaction.java) | ForEach-Object { $_ -creplace 'tmsv2customersEmbeddedDefaultPaymentInstrumentEmbeddedInstrumentIdentifierProcessingInformationAuthorizationOptionsInitiatorMerchantInitiatedTransaction', 'tmsv2customersEmbeddedDefaultPaymentInstrumentEmbeddedMerchantInitiatedTransaction'} | Set-Content ..\src\main\java\Model\Tmsv2customersEmbeddedDefaultPaymentInstrumentEmbeddedInstrumentIdentifierProcessingInformationAuthorizationOptionsInitiatorMerchantInitiatedTransaction.java"

REM Changing in Tmsv2customersEmbeddedDefaultPaymentInstrumentEmbeddedInstrumentIdentifierProcessingInformationAuthorizationOptionsInitiator.java

powershell -Command "(Get-Content ..\src\main\java\Model\Tmsv2customersEmbeddedDefaultPaymentInstrumentEmbeddedInstrumentIdentifierProcessingInformationAuthorizationOptionsInitiator.java) | ForEach-Object { $_ -creplace 'Tmsv2customersEmbeddedDefaultPaymentInstrumentEmbeddedInstrumentIdentifierProcessingInformationAuthorizationOptionsInitiatorMerchantInitiatedTransaction', 'Tmsv2customersEmbeddedDefaultPaymentInstrumentEmbeddedMerchantInitiatedTransaction'} | Set-Content ..\src\main\java\Model\Tmsv2customersEmbeddedDefaultPaymentInstrumentEmbeddedInstrumentIdentifierProcessingInformationAuthorizationOptionsInitiator.java"
setlocal enabledelayedexpansion
python replaceFieldNamesForPaths.py -i cybersource-rest-spec.json > replaceFieldLogs.log
del replaceFieldLogs.log
endlocal

powershell -Command "(Get-Content ..\src\main\java\Model\Tmsv2customersEmbeddedDefaultPaymentInstrumentEmbeddedInstrumentIdentifierProcessingInformationAuthorizationOptionsInitiator.java) | ForEach-Object { $_ -creplace 'tmsv2customersEmbeddedDefaultPaymentInstrumentEmbeddedInstrumentIdentifierProcessingInformationAuthorizationOptionsInitiatorMerchantInitiatedTransaction', 'tmsv2customersEmbeddedDefaultPaymentInstrumentEmbeddedMerchantInitiatedTransaction'} | Set-Content ..\src\main\java\Model\Tmsv2customersEmbeddedDefaultPaymentInstrumentEmbeddedInstrumentIdentifierProcessingInformationAuthorizationOptionsInitiator.java"

REM Renaming Tmsv2customersEmbeddedDefaultPaymentInstrumentEmbeddedInstrumentIdentifierProcessingInformationAuthorizationOptionsInitiatorMerchantInitiatedTransaction.java

powershell Rename-Item ..\src\main\java\Model\Tmsv2customersEmbeddedDefaultPaymentInstrumentEmbeddedInstrumentIdentifierProcessingInformationAuthorizationOptionsInitiatorMerchantInitiatedTransaction.java Tmsv2customersEmbeddedDefaultPaymentInstrumentEmbeddedMerchantInitiatedTransaction.java

powershell Rename-Item ..\docs\Tmsv2customersEmbeddedDefaultPaymentInstrumentEmbeddedInstrumentIdentifierProcessingInformationAuthorizationOptionsInitiatorMerchantInitiatedTransaction.md Tmsv2customersEmbeddedDefaultPaymentInstrumentEmbeddedMerchantInitiatedTransaction.md
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

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') "

@REM replace sdkLinks fieldName to links for supporting links field name in request/response body
echo "starting of replacing the links keyword in PblPaymentLinksAllGet200Response.java model"
powershell -Command " Set-Content ..\src\main\java\Model\PblPaymentLinksAllGet200Response.java ((get-content ..\src\main\java\Model\PblPaymentLinksAllGet200Response.java -raw) -replace '@SerializedName\(\"sdkLinks\"\)', '@SerializedName(\"links\")')"
echo "completed the task of replacing the links keyword in PblPaymentLinksAllGet200Response.java model"

git checkout ..\src\main\java\Api\OAuthApi.java
git checkout ..\src\main\java\Model\AccessTokenResponse.java
git checkout ..\src\main\java\Model\CreateAccessTokenRequest.java
Expand Down
95 changes: 95 additions & 0 deletions generator/replaceFieldNamesForPaths.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
import json
import os
import argparse


def traverse_and_replace(obj, path, new_field_name):
if len(path) == 1:
if path[0] in obj:
keys = list(obj.keys())
index = keys.index(path[0])
obj[new_field_name] = obj.pop(path[0])
keys = list(obj.keys()) # Update keys after replacing the key
reordered = {k: obj[k] for k in keys[:index] + [new_field_name] + keys[index:]}
obj.clear()
obj.update(reordered)
else:
if path[0] in obj and isinstance(obj[path[0]], dict):
traverse_and_replace(obj[path[0]], path[1:], new_field_name)
elif path[0] in obj and isinstance(obj[path[0]], list):
if len(path) > 1 and isinstance(path[1], int) and path[1] < len(obj[path[0]]):
traverse_and_replace(obj[path[0]][path[1]], path[2:], new_field_name)

def replace_field_names(input_file_path, output_file_path, changes):
"""
Replace field names in the JSON file based on the given changes.

:param input_file: Path to the input JSON file.
:param output_file: Path to the output JSON file.
:param changes: List of dictionaries containing paths and new field names.
"""

with open(input_file_path, 'r', encoding='utf-8') as file:
data = json.load(file)

for change in changes:
key_path = change['path']
new_field_name = change['new_name']
traverse_and_replace(data, key_path, new_field_name)

with open(output_file_path, 'w', encoding='utf-8') as file:
json.dump(data, file, indent=4)

def convert_input_paths_to_changes(input_paths):
"""
Convert input paths to a list of changes with parsed paths and new field names.

:param input_paths: List of dictionaries containing string paths and new field names.
:return: List of dictionaries with parsed paths and new field names.
"""
changes = []
for item in input_paths:
path_str = item["path"]
path_parts = []
for part in path_str.split('.'):
if '[' in part and ']' in part:
field, index = part.split('[')
path_parts.append(field)
path_parts.append(int(index.strip(']')))
else:
path_parts.append(part)
changes.append({
"path": path_parts,
"new_name": item["new_field_name"]
})
return changes


if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Replace field names in a JSON file.")
parser.add_argument("-i", "--input", help="Path to the input JSON file.")
args = parser.parse_args()

input_file = args.input if args.input else "cybersource-rest-spec.json"
output_file = "cybersource-rest-spec-java.json"

inputPaths =[
{
"path": "paths./ipl/v2/payment-links.get.responses.200.schema.properties.links",
"new_field_name": "sdkLinks"
}

# example of how to add more paths
# ,{
# "path": "paths./pts/v2/payments.post.parameters[0].schema.properties.clientReferenceInformation.properties.code",
# "new_field_name": "sdkCode"
# }
]

# Convert inputPaths to changes
changes = convert_input_paths_to_changes(inputPaths)

script_dir = os.path.dirname(os.path.abspath(__file__))
input_file_path = os.path.join(script_dir, input_file)
output_file_path = os.path.join(script_dir, output_file)
replace_field_names(input_file_path, output_file_path, changes)
1 change: 1 addition & 0 deletions src/main/java/Model/PblPaymentLinksAllGet200Response.java
Original file line number Diff line number Diff line change
Expand Up @@ -173,3 +173,4 @@ private String toIndentedString(java.lang.Object o) {

}