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 @@ -608,7 +608,7 @@ private String constructExampleCode(CodegenParameter codegenParameter, HashMap<S
} else if (codegenParameter.isPrimitiveType) { // primitive type
if (codegenParameter.isString) {
if (!StringUtils.isEmpty(codegenParameter.example) && !"null".equals(codegenParameter.example)) {
return "\"" + codegenParameter.example + "\"";
return "\"" + escapeText(codegenParameter.example) + "\"";
} else {
return "\"" + codegenParameter.paramName + "_example\"";
}
Expand Down Expand Up @@ -640,7 +640,7 @@ private String constructExampleCode(CodegenParameter codegenParameter, HashMap<S
return constructExampleCode(modelMaps.get(codegenParameter.dataType), modelMaps, processedModelMap, 0);
} else if (codegenParameter.isEmail) { // email
if (!StringUtils.isEmpty(codegenParameter.example) && !"null".equals(codegenParameter.example)) {
return "\"" + codegenParameter.example + "\"";
return "\"" + escapeText(codegenParameter.example) + "\"";
} else {
return "\"" + codegenParameter.paramName + "@example.com\"";
}
Expand Down Expand Up @@ -681,7 +681,7 @@ private String constructExampleCode(CodegenProperty codegenProperty, HashMap<Str
} else if (codegenProperty.isPrimitiveType) { // primitive type
if (codegenProperty.isString) {
if (!StringUtils.isEmpty(codegenProperty.example) && !"null".equals(codegenProperty.example)) {
return "\"" + codegenProperty.example + "\"";
return "\"" + escapeText(codegenProperty.example) + "\"";
} else {
return "\"" + codegenProperty.name + "_example\"";
}
Expand Down Expand Up @@ -714,7 +714,7 @@ private String constructExampleCode(CodegenProperty codegenProperty, HashMap<Str
return constructExampleCode(modelMaps.get(codegenProperty.dataType), modelMaps, processedModelMap, depth + 1);
} else if (codegenProperty.isEmail) { // email
if (!StringUtils.isEmpty(codegenProperty.example) && !"null".equals(codegenProperty.example)) {
return "\"" + codegenProperty.example + "\"";
return "\"" + escapeText(codegenProperty.example) + "\"";
} else {
return "\"" + codegenProperty.name + "@example.com\"";
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -461,4 +461,27 @@ public void testArrayDefaultValue() throws IOException {
TestUtils.assertFileContains(apiPath, defaultEnumArrayString);
TestUtils.assertFileContains(apiPath, defaultValueString);
}

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

final CodegenConfigurator configurator = new CodegenConfigurator()
.setGeneratorName("go")
.setInputSpec("src/test/resources/3_0/go/petstore-with-special-chars-in-examples.yaml")
.setOutputDir(output.getAbsolutePath().replace("\\", "/"));

DefaultGenerator generator = new DefaultGenerator();
List<File> files = generator.opts(configurator.toClientOptInput()).generate();
files.forEach(File::deleteOnExit);

Path docPath = Paths.get(output + "/docs/TestAPI.md");
// Verify that quotes are properly escaped in parameter examples
TestUtils.assertFileContains(docPath, "stringWithQuotes := \"John \\\"Johnny\\\" Doe\"");
// Verify that backslashes are properly escaped in parameter examples
TestUtils.assertFileContains(docPath, "stringWithBackslash := \"C:\\\\path\\\\to\\\\file\"");
// Verify that quotes are properly escaped in email parameter examples
TestUtils.assertFileContains(docPath, "emailWithQuotes := \"test\\\"user@example.com\"");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
openapi: 3.0.0
info:
title: Test Escaping in Examples
version: 1.0.0
paths:
/test:
get:
operationId: testEscaping
tags:
- test
parameters:
- name: stringWithQuotes
in: query
required: true
schema:
type: string
example: 'John "Johnny" Doe'
- name: stringWithBackslash
in: query
required: true
schema:
type: string
example: 'C:\path\to\file'
- name: emailWithQuotes
in: query
required: true
schema:
type: string
format: email
example: 'test"user@example.com'
responses:
'200':
description: Success
content:
application/json:
schema:
$ref: '#/components/schemas/TestResponse'
components:
schemas:
TestResponse:
type: object
properties:
name:
type: string
example: 'Name with "quotes" inside'
path:
type: string
example: 'C:\Windows\System32'
email:
type: string
format: email
example: 'user"name@example.com'
Loading