Skip to content

Commit

Permalink
Merge pull request #62 from gcatanese/set-path-parameters-values
Browse files Browse the repository at this point in the history
Set path parameters values
  • Loading branch information
gcatanese authored Nov 14, 2023
2 parents f74b3d7 + 000ff2b commit 79341c0
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -192,25 +192,28 @@ public OperationsMap postProcessOperationsWithModels(OperationsMap objs, List<Mo

for(CodegenOperation codegenOperation : opList) {

// use Postman notation for path parameter
codegenOperation.path = replacesBracesInPath(codegenOperation.path);

if(pathParamsAsVariables) {
// create Postman variable from path parameter
codegenOperation.path = doubleCurlyBraces(codegenOperation.path);
} else {
// use Postman notation for path parameter
codegenOperation.path = replacesBracesInPath(codegenOperation.path);
// ad-hoc customisation for specific path parameters:
// companyId: set value as YOUR_COMPANY_ACCOUNT env variable
// merchantId: set value as YOUR_MERCHANT_ACCOUNT env variable
if(codegenOperation.path.contains(":companyId") || codegenOperation.path.contains(":merchantId")) {
for(CodegenParameter codegenParameter : codegenOperation.pathParams) {
if(codegenParameter.paramName.equalsIgnoreCase("companyId")) {
// set default value for `companyId` path parameter
codegenParameter.defaultValue = "{{YOUR_COMPANY_ACCOUNT}}";
}
if(codegenParameter.paramName.equalsIgnoreCase("merchantId")) {
// set default value for `merchantId` path parameter
codegenParameter.defaultValue = "{{YOUR_MERCHANT_ACCOUNT}}";
}
// set value of path parameter with corresponding env variable
for(CodegenParameter codegenParameter : codegenOperation.pathParams) {
codegenParameter.defaultValue = "{{" + codegenParameter.paramName + "}}";
}
}

// ad-hoc customisation for specific path parameters:
// companyId: set value as YOUR_COMPANY_ACCOUNT env variable
// merchantId: set value as YOUR_MERCHANT_ACCOUNT env variable
if(codegenOperation.path.contains(":companyId") || codegenOperation.path.contains(":merchantId")) {
for(CodegenParameter codegenParameter : codegenOperation.pathParams) {
if(codegenParameter.paramName.equalsIgnoreCase("companyId")) {
// set default value for `companyId` path parameter
codegenParameter.defaultValue = "{{YOUR_COMPANY_ACCOUNT}}";
}
if(codegenParameter.paramName.equalsIgnoreCase("merchantId")) {
// set default value for `merchantId` path parameter
codegenParameter.defaultValue = "{{YOUR_MERCHANT_ACCOUNT}}";
}
}
}
Expand Down Expand Up @@ -496,17 +499,6 @@ public String escapeQuotationMark(String input) {
return input.replace("\"", "\\\"");
}

String doubleCurlyBraces(String str) {

// remove doublebraces first
String s = str.replace("{{", "{").replace("}}", "}");
// change all singlebraces to doublebraces
s = s.replace("{", "{{").replace("}", "}}");

return s;

}

// convert path from /users/{id} to /users/:id
String replacesBracesInPath(String path) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ public void testVariables() throws IOException, ParseException {
"key\": \"groupId\", \"value\": \"1\", \"type\": \"number\"");

// verify request endpoint
TestUtils.assertFileContains(path, "\"name\": \"/users/{{userId}}\"");
TestUtils.assertFileContains(path, "\"name\": \"/users/:userId\"");

}

Expand Down Expand Up @@ -208,7 +208,7 @@ public void testVariablesInRequestExample() throws IOException, ParseException {
TestUtils.assertFileContains(path, "{{MY_VAR_NAME}}");

// verify request endpoint
TestUtils.assertFileContains(path, "\"name\": \"/users/{{userId}}\"");
TestUtils.assertFileContains(path, "\"name\": \"/users/:userId\"");

}
@Test
Expand Down Expand Up @@ -315,7 +315,7 @@ public void testNamingRequestsWithUrl() throws IOException, ParseException {
Path path = Paths.get(output + "/postman.json");
TestUtils.assertFileExists(path);
// verify request name (from path)
TestUtils.assertFileContains(path, "\"name\": \"/users/{{userId}}\"");
TestUtils.assertFileContains(path, "\"name\": \"/users/:userId\"");
}

@Test
Expand Down Expand Up @@ -396,20 +396,6 @@ public void testHeaderParameters() throws IOException, ParseException {
TestUtils.assertFileContains(path, "{ \"key\": \"Another-Custom-Header\", \"value\": \"abc\", \"disabled\": false");
}

@Test
public void doubleCurlyBraces() {
String str = "/api/{var}/archive";

assertEquals("/api/{{var}}/archive", new PostmanV2Generator().doubleCurlyBraces(str));
}

@Test
public void doubleCurlyBracesNoChanges() {
String str = "/api/{{var}}/archive";

assertEquals("/api/{{var}}/archive", new PostmanV2Generator().doubleCurlyBraces(str));
}

@Test
public void extractExampleByName() {
String str = "#/components/examples/get-user-basic";
Expand Down

0 comments on commit 79341c0

Please sign in to comment.