From 5e8e30186935624845fb8f8d76e364c642a85791 Mon Sep 17 00:00:00 2001 From: Beppe Catanese <1771700+gcatanese@users.noreply.github.com> Date: Sat, 4 Nov 2023 15:24:27 +0100 Subject: [PATCH] [POSTMAN] [BUG] Correct order of the Postman requests (#16983) * Make public for testing * Sort endpoints as they are added in the list/map * Test endpoints are sorted * Regenerate Postman sample --- .../languages/PostmanCollectionCodegen.java | 14 ++- .../postman/PostmanCollectionCodegenTest.java | 72 ++++++++++++++ .../schema/postman-collection/postman.json | 94 +++++++++---------- 3 files changed, 129 insertions(+), 51 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 56803edd4f7..028667f1870 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 @@ -75,9 +75,9 @@ public class PostmanCollectionCodegen extends DefaultCodegen implements CodegenC // operations grouped by tag - protected Map> codegenOperationsByTag = new HashMap<>(); + public Map> codegenOperationsByTag = new HashMap<>(); // list of operations - protected List codegenOperationsList = new ArrayList<>(); + public List codegenOperationsList = new ArrayList<>(); /** * Configures the type of generator. @@ -307,7 +307,7 @@ public class PostmanCollectionCodegen extends DefaultCodegen implements CodegenC * The map groups the CodegenOperations by tag as defined in the OpenAPI spec * @param codegenOperation Codegen operation instance */ - void addToMap(CodegenOperation codegenOperation){ + public void addToMap(CodegenOperation codegenOperation){ String key = null; if(codegenOperation.tags == null || codegenOperation.tags.isEmpty()) { @@ -325,10 +325,16 @@ public class PostmanCollectionCodegen extends DefaultCodegen implements CodegenC codegenOperationsByTag.put(key, list); + // sort requests by path + Collections.sort(list, Comparator.comparing(obj -> obj.path)); } - void addToList(CodegenOperation codegenOperation) { + public void addToList(CodegenOperation codegenOperation) { + codegenOperationsList.add(codegenOperation); + + // sort requests by path + Collections.sort(codegenOperationsList, Comparator.comparing(obj -> obj.path)); } String getResponseBody(CodegenResponse codegenResponse) { 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 afb200e4d5b..e5432d4ea6b 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 @@ -4,6 +4,7 @@ import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.node.ArrayNode; import com.fasterxml.jackson.databind.node.ObjectNode; +import io.swagger.v3.oas.models.tags.Tag; import org.junit.Assert; import org.junit.Test; import org.openapitools.codegen.*; @@ -16,6 +17,8 @@ import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; +import java.util.ArrayList; +import java.util.Arrays; import java.util.LinkedHashMap; import java.util.List; @@ -690,4 +693,73 @@ public class PostmanCollectionCodegenTest { } + @Test + public void testAddToList() { + + PostmanCollectionCodegen postmanCollectionCodegen = new PostmanCollectionCodegen(); + + CodegenOperation operationUsers = new CodegenOperation(); + operationUsers.path = "/users"; + postmanCollectionCodegen.addToList(operationUsers); + + CodegenOperation operationGroups = new CodegenOperation(); + operationGroups.path = "/groups"; + postmanCollectionCodegen.addToList(operationGroups); + + CodegenOperation operationUserId = new CodegenOperation(); + operationUserId.path = "/users/{id}"; + postmanCollectionCodegen.addToList(operationUserId); + + assertEquals(3, postmanCollectionCodegen.codegenOperationsList.size()); + // verify order + assertEquals("/groups", postmanCollectionCodegen.codegenOperationsList.get(0).path); + assertEquals("/users", postmanCollectionCodegen.codegenOperationsList.get(1).path); + assertEquals("/users/{id}", postmanCollectionCodegen.codegenOperationsList.get(2).path); + } + + @Test + public void testAddToMap() { + + PostmanCollectionCodegen postmanV2Generator = new PostmanCollectionCodegen(); + + CodegenOperation operationUsers = new CodegenOperation(); + operationUsers.path = "/users"; + operationUsers.tags = new ArrayList<>(Arrays.asList(new Tag().name("basic"))); + postmanV2Generator.addToMap(operationUsers); + + CodegenOperation operationGroups = new CodegenOperation(); + operationGroups.path = "/groups"; + operationGroups.tags = new ArrayList<>(Arrays.asList(new Tag().name("basic"))); + postmanV2Generator.addToMap(operationGroups); + + CodegenOperation operationUserId = new CodegenOperation(); + operationUserId.path = "/users/{id}"; + operationUserId.tags = new ArrayList<>(Arrays.asList(new Tag().name("basic"))); + postmanV2Generator.addToMap(operationUserId); + + // verify tag 'basic' + assertEquals(1, postmanV2Generator.codegenOperationsByTag.size()); + assertEquals(true, postmanV2Generator.codegenOperationsByTag.containsKey("basic")); + + List operations = postmanV2Generator.codegenOperationsByTag.get("basic"); + // verify order + assertEquals("/groups", operations.get(0).path); + assertEquals("/users", operations.get(1).path); + assertEquals("/users/{id}", operations.get(2).path); + } + + @Test + public void testAddToMapUsingDefaultTag() { + + PostmanCollectionCodegen postmanV2Generator = new PostmanCollectionCodegen(); + + CodegenOperation operationUsers = new CodegenOperation(); + operationUsers.path = "/users"; + postmanV2Generator.addToMap(operationUsers); + + // verify tag 'default' is used + assertEquals(1, postmanV2Generator.codegenOperationsByTag.size()); + assertEquals(true, postmanV2Generator.codegenOperationsByTag.containsKey("default")); + } + } diff --git a/samples/schema/postman-collection/postman.json b/samples/schema/postman-collection/postman.json index 263388f6a8c..bdcd5c9717b 100644 --- a/samples/schema/postman-collection/postman.json +++ b/samples/schema/postman-collection/postman.json @@ -192,6 +192,53 @@ "name": "basic", "item": [ { + "name": "/user", + "description": "Create a new user.", + "item": [ + { + "name": "Example request for Get User", + "request": { + "method": "POST", + "header": [ + { + "key": "Content-Type", + "value": "application/json", + "disabled": false + }, + { + "key": "Accept", + "value": "application/json", + "disabled": false + } + ], + "body": { + "mode": "raw", + "raw": "{\n \"id\" : 777,\n \"firstName\" : \"Alotta\",\n \"lastName\" : \"Rotta\",\n \"email\" : \"alotta.rotta@gmail.com\",\n \"dateOfBirth\" : \"1997-10-31\",\n \"emailVerified\" : true,\n \"createDate\" : \"2019-08-24\"\n}", + "options": { + "raw": { + "language": "json" + } + } + }, + "url": { + "raw": "{{baseUrl}}/user", + "host": [ + "{{baseUrl}}" + ], + "path": [ + "user" + ], + "variable": [ + ], + "query": [ + ] + }, + "description": "Create a new user." + } + } + ] + }, + { "name": "/users/", "description": "Retrieve the information of the user with the matching user ID.", "item": [ @@ -246,53 +293,6 @@ } } ] - }, - { - "name": "/user", - "description": "Create a new user.", - "item": [ - { - "name": "Example request for Get User", - "request": { - "method": "POST", - "header": [ - { - "key": "Content-Type", - "value": "application/json", - "disabled": false - }, - { - "key": "Accept", - "value": "application/json", - "disabled": false - } - ], - "body": { - "mode": "raw", - "raw": "{\n \"id\" : 777,\n \"firstName\" : \"Alotta\",\n \"lastName\" : \"Rotta\",\n \"email\" : \"alotta.rotta@gmail.com\",\n \"dateOfBirth\" : \"1997-10-31\",\n \"emailVerified\" : true,\n \"createDate\" : \"2019-08-24\"\n}", - "options": { - "raw": { - "language": "json" - } - } - }, - "url": { - "raw": "{{baseUrl}}/user", - "host": [ - "{{baseUrl}}" - ], - "path": [ - "user" - ], - "variable": [ - ], - "query": [ - ] - }, - "description": "Create a new user." - } - } - ] } ] }