Skip to content

Commit

Permalink
Merge pull request #57 from gcatanese/order-endpoints
Browse files Browse the repository at this point in the history
Order endpoints by path
  • Loading branch information
gcatanese authored Nov 4, 2023
2 parents ca471fb + 2b133d8 commit fcf7908
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -293,10 +293,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) {
codegenOperationsList.add(codegenOperation);

// sort requests by path
Collections.sort(codegenOperationsList, Comparator.comparing(obj -> obj.path));
}

String getResponseBody(CodegenResponse codegenResponse) {
Expand Down Expand Up @@ -365,6 +371,8 @@ List<PostmanRequestItem> getPostmanRequests(CodegenOperation codegenOperation) {
return items;
}



// split, trim
void extractPostmanVariableNames(String postmanVariablesCsv) {
postmanVariableNames = postmanVariablesCsv.split("-");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@

import com.fasterxml.jackson.databind.ObjectMapper;
import com.tweesky.cloudtools.codegen.model.PostmanRequestItem;
import io.swagger.v3.oas.models.tags.Tag;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
import org.junit.Assert;
import org.junit.Test;
import org.openapitools.codegen.ClientOptInput;
import org.openapitools.codegen.CodegenOperation;
import org.openapitools.codegen.DefaultGenerator;
import org.openapitools.codegen.config.CodegenConfigurator;

Expand All @@ -19,6 +21,7 @@
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import static org.junit.Assert.assertEquals;
Expand Down Expand Up @@ -207,7 +210,6 @@ public void testVariablesInRequestExample() throws IOException, ParseException {
// verify request endpoint
TestUtils.assertFileContains(path, "\"name\": \"/users/{{userId}}\"");


}
@Test
public void testVariableThatDoesNotExist() throws IOException, ParseException {
Expand Down Expand Up @@ -530,4 +532,72 @@ public void testFormatDescription() throws Exception {
assertEquals(EXPECTED, new PostmanV2Generator().formatDescription(DESCRIPTION));
}

@Test
public void testAddToList() {

PostmanV2Generator postmanV2Generator = new PostmanV2Generator();

CodegenOperation operationUsers = new CodegenOperation();
operationUsers.path = "/users";
postmanV2Generator.addToList(operationUsers);

CodegenOperation operationGroups = new CodegenOperation();
operationGroups.path = "/groups";
postmanV2Generator.addToList(operationGroups);

CodegenOperation operationUserId = new CodegenOperation();
operationUserId.path = "/users/{id}";
postmanV2Generator.addToList(operationUserId);

assertEquals(3, postmanV2Generator.codegenOperationsList.size());
// verify order
assertEquals("/groups", postmanV2Generator.codegenOperationsList.get(0).path);
assertEquals("/users", postmanV2Generator.codegenOperationsList.get(1).path);
assertEquals("/users/{id}", postmanV2Generator.codegenOperationsList.get(2).path);
}

@Test
public void testAddToMap() {

PostmanV2Generator postmanV2Generator = new PostmanV2Generator();

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() {

PostmanV2Generator postmanV2Generator = new PostmanV2Generator();

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

0 comments on commit fcf7908

Please sign in to comment.