Fix NPE in StringUtils.camelize (#15957)

* fix NPE in StringUtils.camelize

* add comment

* add test file
This commit is contained in:
William Cheng
2023-06-30 13:19:11 +08:00
committed by GitHub
parent 8add9119df
commit 63b2c79565
3 changed files with 65 additions and 47 deletions

View File

@@ -20,7 +20,6 @@ import java.util.regex.Pattern;
import static org.openapitools.codegen.utils.CamelizeOption.UPPERCASE_FIRST_CHAR;
public class StringUtils {
/**
* Set the cache size (entry count) of the sanitizedNameCache, camelizedWordsCache and underscoreWordsCache.
*/
@@ -139,7 +138,7 @@ public class StringUtils {
// Replace all slashes with dots (package separator)
Matcher m = camelizeSlashPattern.matcher(word);
while (m.find()) {
word = m.replaceFirst("." + m.group(1)/*.toUpperCase()*/);
word = m.replaceFirst("." + m.group(1).replace("\\", "\\\\")/*.toUpperCase()*/);
m = camelizeSlashPattern.matcher(word);
}

View File

@@ -1,55 +1,29 @@
/*
package org.openapitools.codegen.markdown;
import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import org.apache.commons.io.FileUtils;
import org.openapitools.codegen.DefaultGenerator;
import org.openapitools.codegen.config.CodegenConfigurator;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.Operation;
import org.openapitools.codegen.CodegenOperation;
import org.openapitools.codegen.CodegenParameter;
import org.openapitools.codegen.TestUtils;
import org.openapitools.codegen.languages.MarkdownDocumentationCodegen;
import org.testng.Assert;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
public class MarkdownSampleGeneratorTest {
private File outputTempDirectory;
private List<File> generatedFiles;
@BeforeClass
public void beforeClassGenerateTestMarkup() throws Exception {
// set line break to \n across all platforms
System.setProperty("line.separator", "\n");
@Test(description = "test special characters in MIME type")
public void testSpecialCharactersInMimeType() throws Exception {
// for https://github.com/OpenAPITools/openapi-generator/issues/15923
// without the fix, the following will throw exception "IllegalArgument character to be escaped is missing"
final OpenAPI openAPI = TestUtils.parseFlattenSpec("src/test/resources/3_0/issue_15923.yaml");
final MarkdownDocumentationCodegen codegen = new MarkdownDocumentationCodegen();
codegen.setOpenAPI(openAPI);
this.outputTempDirectory = Files.createTempDirectory("test-markdown-sample-generator.").toFile();
final CodegenConfigurator configurator = new CodegenConfigurator().setGeneratorName("markdown")
.setInputSpec("src/test/resources/3_0/markdown/issue_6096.yaml")
.setOutputDir(outputTempDirectory.getAbsolutePath());
DefaultGenerator generator = new DefaultGenerator();
this.generatedFiles = generator.opts(configurator.toClientOptInput()).generate();
final String requestPath = "/v1/MyRequest";
Operation textOperation = openAPI.getPaths().get(requestPath).getPut();
CodegenOperation operation = codegen.fromOperation(requestPath, "put", textOperation, null);
CodegenParameter codegenParameter = operation.allParams.get(0);
Assert.assertNotNull(codegenParameter);
}
@Test
public void testSampleMarkdownGeneration() throws IOException {
Path expectedFiles = new File("src/test/resources/3_0/markdown/expected/").toPath();
for (File generated : this.generatedFiles) {
if (!generated.toString().endsWith(".md")) {
continue;
}
Path expectedPath = this.outputTempDirectory.toPath().relativize(generated.toPath());
File expected = expectedFiles.resolve(expectedPath).toFile();
Assert.assertTrue(expected.exists(), "Could not find " + expected);
Assert.assertEquals(FileUtils.readFileToString(generated, StandardCharsets.UTF_8).replace("\n", "").replace("\r", ""),
FileUtils.readFileToString(expected, StandardCharsets.UTF_8).replace("\n", "").replace("\r", ""));
}
}
}
*/
}

View File

@@ -0,0 +1,45 @@
openapi: 3.0.1
info:
title: Bug
description: Web API
version: v1
paths:
/v1/MyRequest:
put:
tags:
- MyRequest
summary: Update the test
operationId: test
requestBody:
content:
application/json:
schema:
$ref: '#/components/schemas/TestEntity'
text/json:
schema:
$ref: '#/components/schemas/TestEntity'
application/*+json:
schema:
$ref: '#/components/schemas/TestEntity'
responses:
'200':
description: Success
content:
text/plain:
schema:
$ref: '#/components/schemas/TestEntity'
application/json:
schema:
$ref: '#/components/schemas/TestEntity'
text/json:
schema:
$ref: '#/components/schemas/TestEntity'
components:
schemas:
TestEntity:
type: object
properties:
name:
type: string
nullable: true
additionalProperties: false