[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
This commit is contained in:
Beppe Catanese 2023-11-04 15:24:27 +01:00 committed by GitHub
parent f83cb60f27
commit 5e8e301869
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 129 additions and 51 deletions

View File

@ -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.
@ -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) {

View File

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

View File

@ -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."
}
}
]
}
]
}