Skip to content

Commit

Permalink
[POSTMAN] [BUG] Correct order of the Postman requests (#16983)
Browse files Browse the repository at this point in the history
* Make public for testing

* Sort endpoints as they are added in the list/map

* Test endpoints are sorted

* Regenerate Postman sample
  • Loading branch information
gcatanese committed Nov 4, 2023
1 parent f83cb60 commit 5e8e301
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,9 @@ public class PostmanCollectionCodegen extends DefaultCodegen implements CodegenC


// operations grouped by tag
protected Map<String, List<CodegenOperation>> codegenOperationsByTag = new HashMap<>();
public Map<String, List<CodegenOperation>> codegenOperationsByTag = new HashMap<>();
// list of operations
protected List<CodegenOperation> codegenOperationsList = new ArrayList<>();
public List<CodegenOperation> codegenOperationsList = new ArrayList<>();

/**
* Configures the type of generator.
Expand Down Expand Up @@ -307,7 +307,7 @@ public OperationsMap postProcessOperationsWithModels(OperationsMap objs, List<Mo
* 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()) {
Expand All @@ -325,10 +325,16 @@ void addToMap(CodegenOperation codegenOperation){

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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
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.*;
Expand All @@ -16,6 +17,8 @@
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;

Expand Down Expand Up @@ -690,4 +693,73 @@ public void convertNestedLinkedHashMapToJson() {

}

@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<CodegenOperation> 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"));
}

}
62 changes: 31 additions & 31 deletions samples/schema/postman-collection/postman.json
Original file line number Diff line number Diff line change
Expand Up @@ -192,104 +192,104 @@
"name": "basic",
"item": [
{
"name": "/users/",
"description": "Retrieve the information of the user with the matching user ID.",
"name": "/user",
"description": "Create a new user.",
"item": [
{
"name": "Get User Info by Query Param",
"name": "Example request for Get User",
"request": {
"method": "GET",
"method": "POST",
"header": [
{
"key": "Accept",
"key": "Content-Type",
"value": "application/json",
"disabled": false
},
{
"key": "Custom-Header",
"value": "",
"disabled": true
},
{
"key": "Another-Custom-Header",
"value": "abc",
"key": "Accept",
"value": "application/json",
"disabled": false
}
],
"body": {
"mode": "raw",
"raw": "",
"raw": "{\n \"id\" : 777,\n \"firstName\" : \"Alotta\",\n \"lastName\" : \"Rotta\",\n \"email\" : \"[email protected]\",\n \"dateOfBirth\" : \"1997-10-31\",\n \"emailVerified\" : true,\n \"createDate\" : \"2019-08-24\"\n}",
"options": {
"raw": {
"language": "json"
}
}
},
"url": {
"raw": "{{baseUrl}}/users/",
"raw": "{{baseUrl}}/user",
"host": [
"{{baseUrl}}"
],
"path": [
"users"
"user"
],
"variable": [
],
"query": [
{
"key": "pUserId",
"value": "888"
}
]
},
"description": "Retrieve the information of the user with the matching user ID."
"description": "Create a new user."
}
}
]
},
{
"name": "/user",
"description": "Create a new user.",
"name": "/users/",
"description": "Retrieve the information of the user with the matching user ID.",
"item": [
{
"name": "Example request for Get User",
"name": "Get User Info by Query Param",
"request": {
"method": "POST",
"method": "GET",
"header": [
{
"key": "Content-Type",
"key": "Accept",
"value": "application/json",
"disabled": false
},
{
"key": "Accept",
"value": "application/json",
"key": "Custom-Header",
"value": "",
"disabled": true
},
{
"key": "Another-Custom-Header",
"value": "abc",
"disabled": false
}
],
"body": {
"mode": "raw",
"raw": "{\n \"id\" : 777,\n \"firstName\" : \"Alotta\",\n \"lastName\" : \"Rotta\",\n \"email\" : \"[email protected]\",\n \"dateOfBirth\" : \"1997-10-31\",\n \"emailVerified\" : true,\n \"createDate\" : \"2019-08-24\"\n}",
"raw": "",
"options": {
"raw": {
"language": "json"
}
}
},
"url": {
"raw": "{{baseUrl}}/user",
"raw": "{{baseUrl}}/users/",
"host": [
"{{baseUrl}}"
],
"path": [
"user"
"users"
],
"variable": [
],
"query": [
{
"key": "pUserId",
"value": "888"
}
]
},
"description": "Create a new user."
"description": "Retrieve the information of the user with the matching user ID."
}
}
]
Expand Down

0 comments on commit 5e8e301

Please sign in to comment.