Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public class PostmanCollectionCodegen extends DefaultCodegen implements CodegenC


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

Expand Down Expand Up @@ -319,22 +319,31 @@ public OperationsMap postProcessOperationsWithModels(OperationsMap objs, List<Mo
* @param codegenOperation Codegen operation instance
*/
public void addToMap(CodegenOperation codegenOperation) {

String key = null;
String tagName;
String tagDescription;
if (codegenOperation.tags == null || codegenOperation.tags.isEmpty()) {
key = "default";
tagName = "Default";
tagDescription = "Default tag";
} else {
key = codegenOperation.tags.get(0).getName();
tagName = codegenOperation.tags.get(0).getName();
tagDescription = codegenOperation.tags.get(0).getDescription();
if (tagDescription == null) {
tagDescription = tagName + " tag";
}
}

List<CodegenOperation> list = codegenOperationsByTag.get(key);
tagName = formatDescription(tagName);
Copy link
Copy Markdown
Contributor

@cubic-dev-ai cubic-dev-ai Bot Mar 14, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2: New folder name/description escaping is incomplete for JSON and can emit corrupted or invalid Postman JSON when tags contain backslashes/control characters.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/PostmanCollectionCodegen.java, line 335:

<comment>New folder name/description escaping is incomplete for JSON and can emit corrupted or invalid Postman JSON when tags contain backslashes/control characters.</comment>

<file context>
@@ -332,6 +332,9 @@ public void addToMap(CodegenOperation codegenOperation) {
             }
         }
 
+        tagName = formatDescription(tagName);
+        tagDescription = formatDescription(tagDescription);
+
</file context>
Fix with Cubic

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we can handle these edge cases in another PR

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Understood—thanks for the update; we can revisit these edge cases in a follow-up PR.

tagDescription = formatDescription(tagDescription);

PostmanRequestFolder folder = new PostmanRequestFolder(tagName, tagDescription);
Comment thread
cubic-dev-ai[bot] marked this conversation as resolved.
List<CodegenOperation> list = codegenOperationsByTag.get(folder);

if (list == null) {
list = new ArrayList<>();
}
list.add(codegenOperation);

codegenOperationsByTag.put(key, list);
codegenOperationsByTag.put(folder, list);

// sort requests by path
Collections.sort(list, Comparator.comparing(obj -> obj.path));
Expand Down Expand Up @@ -883,6 +892,30 @@ public String getPostmanType(CodegenProperty codegenProperty) {
}
}

@Getter
public static class PostmanRequestFolder {
private final String name;
private final String description;

public PostmanRequestFolder(String name, String description) {
this.name = name;
this.description = description;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
PostmanRequestFolder that = (PostmanRequestFolder) o;
return Objects.equals(name, that.name) && Objects.equals(description, that.description);
}

@Override
public int hashCode() {
return Objects.hash(name, description);
}
}

// Supporting models
@Getter
@Setter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
{{#codegenOperationsByTag}}
{{#entrySet}}
{
"name": "{{key}}",
"name": "{{{key.name}}}",
"description": "{{{key.description}}}",
"item": [{{#value}}
{{>item}}{{^-last}},{{/-last}}{{/value}}
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import static org.junit.jupiter.api.Assertions.*;
import static org.openapitools.codegen.TestUtils.*;
Expand Down Expand Up @@ -76,6 +78,21 @@ public void testBasicGeneration() throws IOException {
// verify request endpoint
TestUtils.assertFileContains(path, "\"name\": \"/users/:userId\"");

ObjectMapper objectMapper = new ObjectMapper();
JsonNode root = objectMapper.readTree(new File(output + "/postman.json"));
JsonNode folders = root.get("item");

JsonNode basicFolder = null;
for (JsonNode folder : folders) {
if ("basic".equals(folder.get("name").asText())) {
basicFolder = folder;
break;
}
}

assertNotNull(basicFolder);
assertEquals("Basic tag", basicFolder.get("description").asText());

}

@Test
Expand All @@ -101,6 +118,38 @@ public void testBasicGenerationJson() throws IOException {
assertFileContains(path, "\"schema\": \"https://schema.getpostman.com/json/collection/v2.1.0/collection.json\"");
}

@Test
public void testTagDescriptionIsJsonEscaped() throws IOException {
File output = Files.createTempDirectory("postmantest_").toFile();
output.deleteOnExit();

final CodegenConfigurator configurator = new CodegenConfigurator()
.setGeneratorName("postman-collection")
.setInputSpec("src/test/resources/3_0/postman-collection/TagDescriptionEscaping.yaml")
.setOutputDir(output.getAbsolutePath().replace("\\", "/"));

final ClientOptInput clientOptInput = configurator.toClientOptInput();
DefaultGenerator generator = new DefaultGenerator();
List<File> files = generator.opts(clientOptInput).generate();

files.forEach(File::deleteOnExit);

ObjectMapper objectMapper = new ObjectMapper();
JsonNode root = objectMapper.readTree(new File(output + "/postman.json"));
JsonNode folders = root.get("item");

JsonNode basicFolder = null;
for (JsonNode folder : folders) {
if ("basic".equals(folder.get("name").asText())) {
basicFolder = folder;
break;
}
}

assertNotNull(basicFolder);
assertEquals("Basic \"quoted\" tag\nsecond line", basicFolder.get("description").asText());
}

@Test
public void testValidatePostmanJson() throws IOException {

Expand Down Expand Up @@ -702,24 +751,26 @@ public void testAddToMap() {

CodegenOperation operationUsers = new CodegenOperation();
operationUsers.path = "/users";
operationUsers.tags = new ArrayList<>(Arrays.asList(new Tag().name("basic")));
operationUsers.tags = new ArrayList<>(Arrays.asList(new Tag().name("basic").description("Basic tag")));
postmanV2Generator.addToMap(operationUsers);

CodegenOperation operationGroups = new CodegenOperation();
operationGroups.path = "/groups";
operationGroups.tags = new ArrayList<>(Arrays.asList(new Tag().name("basic")));
operationGroups.tags = new ArrayList<>(Arrays.asList(new Tag().name("basic").description("Basic tag")));
postmanV2Generator.addToMap(operationGroups);

CodegenOperation operationUserId = new CodegenOperation();
operationUserId.path = "/users/{id}";
operationUserId.tags = new ArrayList<>(Arrays.asList(new Tag().name("basic")));
operationUserId.tags = new ArrayList<>(Arrays.asList(new Tag().name("basic").description("Basic tag")));
postmanV2Generator.addToMap(operationUserId);

PostmanCollectionCodegen.PostmanRequestFolder folder = new PostmanCollectionCodegen.PostmanRequestFolder("basic", "Basic tag");

// verify tag 'basic'
assertEquals(1, postmanV2Generator.codegenOperationsByTag.size());
assertEquals(true, postmanV2Generator.codegenOperationsByTag.containsKey("basic"));
assertEquals(true, postmanV2Generator.codegenOperationsByTag.containsKey(folder));

List<CodegenOperation> operations = postmanV2Generator.codegenOperationsByTag.get("basic");
List<CodegenOperation> operations = postmanV2Generator.codegenOperationsByTag.get(folder);
// verify order
assertEquals("/groups", operations.get(0).path);
assertEquals("/users", operations.get(1).path);
Expand All @@ -737,7 +788,18 @@ public void testAddToMapUsingDefaultTag() {

// verify tag 'default' is used
assertEquals(1, postmanV2Generator.codegenOperationsByTag.size());
assertEquals(true, postmanV2Generator.codegenOperationsByTag.containsKey("default"));
assertEquals(true, postmanV2Generator.codegenOperationsByTag.containsKey(new PostmanCollectionCodegen.PostmanRequestFolder("Default", "Default tag")));
}

@Test
public void testPostmanRequestFolderInMap() {
PostmanCollectionCodegen.PostmanRequestFolder folder1 = new PostmanCollectionCodegen.PostmanRequestFolder("test", "descr");
Map<PostmanCollectionCodegen.PostmanRequestFolder, String> map = new HashMap<>();
map.put(folder1, "folder1");
PostmanCollectionCodegen.PostmanRequestFolder folder2 = new PostmanCollectionCodegen.PostmanRequestFolder("test", "descr");

assertTrue(map.containsKey(folder2));
assertEquals("folder1", map.get(folder2));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
openapi: 3.0.0
info:
title: Tag Description Escaping
version: '1.0'
servers:
- url: 'http://localhost:5001'
paths:
'/users/{userId}':
get:
summary: Get User
operationId: get-users-userId
tags:
- basic
parameters:
- description: Unique identifier of the user
name: userId
in: path
required: true
schema:
type: string
responses:
'200':
description: User Found
content:
application/json:
schema:
type: object
properties:
id:
type: integer
tags:
- name: basic
description: |-
Basic "quoted" tag
second line
Loading
Loading