From b4f9d8103f99faa26830c53eb792935499f86b65 Mon Sep 17 00:00:00 2001 From: Beppe Catanese <1771700+gcatanese@users.noreply.github.com> Date: Sun, 15 Oct 2023 03:35:57 +0200 Subject: [PATCH] [Postman Collection] Fix path parameter syntax (#16827) * Format Postman request url * Generate schema --- .../languages/PostmanCollectionCodegen.java | 14 +++++++++++++- .../postman/PostmanCollectionCodegenTest.java | 8 ++++++++ samples/schema/postman-collection/postman.json | 18 +++++++++--------- 3 files changed, 30 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 e0e1da0d77e..56803edd4f7 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 @@ -217,9 +217,12 @@ public class PostmanCollectionCodegen extends DefaultCodegen implements CodegenC for(CodegenOperation codegenOperation : opList) { 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); } - codegenOperation.summary = getSummary(codegenOperation); // request headers @@ -503,6 +506,15 @@ public class PostmanCollectionCodegen extends DefaultCodegen implements CodegenC } + // convert path from /users/{id} to /users/:id + String replacesBracesInPath(String path) { + + String s = path.replace("{", ":"); + s = s.replace("}", ""); + + return s; + } + public String extractExampleByName(String ref) { return ref.substring(ref.lastIndexOf("/") + 1); } 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 aa92b6a095f..afb200e4d5b 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 @@ -73,6 +73,8 @@ public class PostmanCollectionCodegenTest { // verify request name (from summary) assertFileContains(path, "\"name\": \"Get User\""); + // verify request endpoint + TestUtils.assertFileContains(path, "\"name\": \"/users/:userId\""); } @@ -155,6 +157,9 @@ public class PostmanCollectionCodegenTest { TestUtils.assertFileContains(path, "key\": \"groupId\", \"value\": \"1\", \"type\": \"number\""); + // verify request endpoint + TestUtils.assertFileContains(path, "\"name\": \"/users/{{userId}}\""); + } @Test @@ -187,6 +192,9 @@ public class PostmanCollectionCodegenTest { assertFileContains(path, "{{MY_VAR_NAME}}"); + // verify request endpoint + TestUtils.assertFileContains(path, "\"name\": \"/users/{{userId}}\""); + } @Test diff --git a/samples/schema/postman-collection/postman.json b/samples/schema/postman-collection/postman.json index c7f6b04c752..263388f6a8c 100644 --- a/samples/schema/postman-collection/postman.json +++ b/samples/schema/postman-collection/postman.json @@ -13,7 +13,7 @@ "name": "default", "item": [ { - "name": "/users/{userId} (DEPRECATED)", + "name": "/users/:userId (DEPRECATED)", "description": "Update the information of an existing user.", "item": [ { @@ -52,13 +52,13 @@ } }, "url": { - "raw": "{{baseUrl}}/users/{userId}", + "raw": "{{baseUrl}}/users/:userId", "host": [ "{{baseUrl}}" ], "path": [ "users", - "{userId}" + ":userId" ], "variable": [ { @@ -81,7 +81,7 @@ "name": "advanced", "item": [ { - "name": "/groups/{groupId}", + "name": "/groups/:groupId", "description": "Get group of users", "item": [ { @@ -105,13 +105,13 @@ } }, "url": { - "raw": "{{baseUrl}}/groups/{groupId}", + "raw": "{{baseUrl}}/groups/:groupId", "host": [ "{{baseUrl}}" ], "path": [ "groups", - "{groupId}" + ":groupId" ], "variable": [ { @@ -129,7 +129,7 @@ ] }, { - "name": "/users/{userId}", + "name": "/users/:userId", "description": "Retrieve the information of the user with the matching user ID.", "item": [ { @@ -163,13 +163,13 @@ } }, "url": { - "raw": "{{baseUrl}}/users/{userId}", + "raw": "{{baseUrl}}/users/:userId", "host": [ "{{baseUrl}}" ], "path": [ "users", - "{userId}" + ":userId" ], "variable": [ {