From 09060c6c22dbad88beedd5713e14d6cb6036119e Mon Sep 17 00:00:00 2001 From: Beppe Catanese <1771700+gcatanese@users.noreply.github.com> Date: Sat, 11 Nov 2023 17:08:07 +0100 Subject: [PATCH] Use postman notation (#17028) * use Postman notation for path parameter * Regenerate samples --- .../languages/PostmanCollectionCodegen.java | 14 +++++++++----- .../resources/postman-collection/item.mustache | 2 +- .../postman/PostmanCollectionCodegenTest.java | 13 ++++++++++--- samples/schema/postman-collection/postman.json | 2 +- 4 files changed, 21 insertions(+), 10 deletions(-) diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/PostmanCollectionCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/PostmanCollectionCodegen.java index 028667f1870..71d5609cc4c 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/PostmanCollectionCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/PostmanCollectionCodegen.java @@ -131,6 +131,7 @@ public class PostmanCollectionCodegen extends DefaultCodegen implements CodegenC @Override public void postProcessParameter(CodegenParameter parameter) { + // create Postman variable from every path parameter if(pathParamsAsVariables && parameter.isPathParam) { variables.add(new PostmanVariable() .addName(parameter.paramName) @@ -216,13 +217,16 @@ public class PostmanCollectionCodegen extends DefaultCodegen implements CodegenC 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); + // set value of path parameter with corresponding env variable + for(CodegenParameter codegenParameter : codegenOperation.pathParams) { + codegenParameter.defaultValue = "{{" + codegenParameter.paramName + "}}"; + } } + codegenOperation.summary = getSummary(codegenOperation); // request headers diff --git a/modules/openapi-generator/src/main/resources/postman-collection/item.mustache b/modules/openapi-generator/src/main/resources/postman-collection/item.mustache index 416e0d8cdad..a9cc9c49da7 100644 --- a/modules/openapi-generator/src/main/resources/postman-collection/item.mustache +++ b/modules/openapi-generator/src/main/resources/postman-collection/item.mustache @@ -39,7 +39,7 @@ {{#pathParams}} { "key": "{{paramName}}", - "value": "", + "value": "{{defaultValue}}", "description": "{{description}}" }{{^-last}},{{/-last}} {{/pathParams}} diff --git a/modules/openapi-generator/src/test/java/org/openapitools/codegen/postman/PostmanCollectionCodegenTest.java b/modules/openapi-generator/src/test/java/org/openapitools/codegen/postman/PostmanCollectionCodegenTest.java index e5432d4ea6b..f5e2366e966 100644 --- a/modules/openapi-generator/src/test/java/org/openapitools/codegen/postman/PostmanCollectionCodegenTest.java +++ b/modules/openapi-generator/src/test/java/org/openapitools/codegen/postman/PostmanCollectionCodegenTest.java @@ -161,7 +161,9 @@ public class PostmanCollectionCodegenTest { "key\": \"groupId\", \"value\": \"1\", \"type\": \"number\""); // verify request endpoint - TestUtils.assertFileContains(path, "\"name\": \"/users/{{userId}}\""); + TestUtils.assertFileContains(path, "\"name\": \"/users/:userId\""); + // verify path parameter value + TestUtils.assertFileContains(path, "key\": \"userId\", \"value\": \"{{userId}}\","); } @@ -196,7 +198,9 @@ public class PostmanCollectionCodegenTest { assertFileContains(path, "{{MY_VAR_NAME}}"); // verify request endpoint - TestUtils.assertFileContains(path, "\"name\": \"/users/{{userId}}\""); + TestUtils.assertFileContains(path, "\"name\": \"/users/:userId\""); + // verify path parameter value + TestUtils.assertFileContains(path, "key\": \"userId\", \"value\": \"{{userId}}\","); } @@ -303,7 +307,10 @@ public class PostmanCollectionCodegenTest { Path path = Paths.get(output + "/postman.json"); assertFileExists(path); // verify request name (from path) - assertFileContains(path, "\"name\": \"/users/{{userId}}\""); + assertFileContains(path, "\"name\": \"/users/:userId\""); + // verify path parameter value + TestUtils.assertFileContains(path, "key\": \"userId\", \"value\": \"{{userId}}\","); + } @Test diff --git a/samples/schema/postman-collection/postman.json b/samples/schema/postman-collection/postman.json index bdcd5c9717b..8a39ae60676 100644 --- a/samples/schema/postman-collection/postman.json +++ b/samples/schema/postman-collection/postman.json @@ -116,7 +116,7 @@ "variable": [ { "key": "groupId", - "value": "", + "value": "1", "description": "group Id" } ],