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 @@ -2,6 +2,7 @@


import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.io.JsonStringEncoder;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
Expand Down Expand Up @@ -332,8 +333,8 @@ public void addToMap(CodegenOperation codegenOperation) {
}
}

tagName = formatDescription(tagName);
tagDescription = formatDescription(tagDescription);
tagName = escapeJsonString(tagName);
tagDescription = escapeJsonString(tagDescription);

PostmanRequestFolder folder = new PostmanRequestFolder(tagName, tagDescription);
List<CodegenOperation> list = codegenOperationsByTag.get(folder);
Expand Down Expand Up @@ -685,6 +686,13 @@ public String formatDescription(String description) {
return description;
}

String escapeJsonString(String value) {
if (value == null) {
return null;
}
return new String(JsonStringEncoder.getInstance().quoteAsString(value));
}

/**
* Extract all placeholders (string delimited by curly braces ie {{PLACEHOLDER}}) from the input string
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,38 @@ public void testTagDescriptionIsJsonEscaped() throws IOException {
assertEquals("Basic \"quoted\" tag\nsecond line", basicFolder.get("description").asText());
}

@Test
public void testTagDescriptionControlCharsAreJsonEscaped() 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/TagDescriptionControlCharsEscaping.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("desc with \\t tab, \\b backspace, \\f formfeed, \\r carriage-return, \\n line", basicFolder.get("description").asText());
}

@Test
public void testValidatePostmanJson() throws IOException {

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
openapi: 3.0.0
info:
title: Tag Description Control Chars 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: "desc with \\t tab, \\b backspace, \\f formfeed, \\r carriage-return, \\n line"
Loading