Fix regexp error on php-slim (#2604)

* Add tests to reproduce the issue

* Use simple replacement instead of regexp

* Add tests to reproduce the issue (apiPackage)

* Use simple replacement instead of regexp (apiPackage)

* Replace a slash with File.separator (addressing the issue on windows)

* Tweak (windows)

* Tweak (windows)

* Use StringUtils#remove***() instead of regexp
This commit is contained in:
Akihito Nakano 2019-04-11 08:01:24 +09:00 committed by GitHub
parent 3036d8fd20
commit b797662eaf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 63 additions and 16 deletions

View File

@ -632,19 +632,19 @@ public class DefaultCodegen implements CodegenConfig {
}
public String apiFileFolder() {
return outputFolder + "/" + apiPackage().replace('.', '/');
return outputFolder + File.separator + apiPackage().replace('.', File.separatorChar);
}
public String modelFileFolder() {
return outputFolder + "/" + modelPackage().replace('.', '/');
return outputFolder + File.separator + modelPackage().replace('.', File.separatorChar);
}
public String apiTestFileFolder() {
return outputFolder + "/" + testPackage().replace('.', '/');
return outputFolder + File.separator + testPackage().replace('.', File.separatorChar);
}
public String modelTestFileFolder() {
return outputFolder + "/" + testPackage().replace('.', '/');
return outputFolder + File.separator + testPackage().replace('.', File.separatorChar);
}
public String apiDocFileFolder() {

View File

@ -29,6 +29,7 @@ import org.slf4j.LoggerFactory;
import java.io.File;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import static org.openapitools.codegen.utils.StringUtils.camelize;
import static org.openapitools.codegen.utils.StringUtils.underscore;
@ -225,16 +226,18 @@ public abstract class AbstractPhpCodegen extends DefaultCodegen implements Codeg
public String toSrcPath(String packageName, String basePath) {
packageName = packageName.replace(invokerPackage, ""); // FIXME: a parameter should not be assigned. Also declare the methods parameters as 'final'.
if (basePath != null && basePath.length() > 0) {
basePath = basePath.replaceAll("[\\\\/]?$", "") + '/'; // FIXME: a parameter should not be assigned. Also declare the methods parameters as 'final'.
basePath = basePath.replaceAll("[\\\\/]?$", "") + File.separator; // FIXME: a parameter should not be assigned. Also declare the methods parameters as 'final'.
}
return (basePath
// Replace period, backslash, forward slash with file separator in package name
+ packageName.replaceAll("[\\.\\\\/]", Matcher.quoteReplacement("/"))
// Trim prefix file separators from package path
.replaceAll("^/", ""))
// Trim trailing file separators from the overall path
.replaceAll("/$", "");
// Trim prefix file separators from package path
String packagePath = StringUtils.removeStart(
// Replace period, backslash, forward slash with file separator in package name
packageName.replaceAll("[\\.\\\\/]", Matcher.quoteReplacement("/")),
File.separator
);
// Trim trailing file separators from the overall path
return StringUtils.removeEnd(basePath + packagePath, File.separator);
}
@Override

View File

@ -21,6 +21,7 @@ import io.swagger.v3.oas.models.Operation;
import io.swagger.v3.oas.models.security.SecurityScheme;
import io.swagger.v3.oas.models.servers.Server;
import org.apache.commons.lang3.StringEscapeUtils;
import org.apache.commons.lang3.StringUtils;
import org.openapitools.codegen.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -94,18 +95,18 @@ public class PhpSlimServerCodegen extends AbstractPhpCodegen {
@Override
public String apiFileFolder() {
if (apiPackage.matches("^" + invokerPackage + "\\\\*(.+)")) {
if (apiPackage.startsWith(invokerPackage + "\\")) {
// need to strip out invokerPackage from path
return (outputFolder + File.separator + toSrcPath(apiPackage.replaceFirst("^" + invokerPackage + "\\\\*(.+)", "$1"), srcBasePath));
return (outputFolder + File.separator + toSrcPath(StringUtils.removeStart(apiPackage, invokerPackage + "\\"), srcBasePath));
}
return (outputFolder + File.separator + toSrcPath(apiPackage, srcBasePath));
}
@Override
public String modelFileFolder() {
if (modelPackage.matches("^" + invokerPackage + "\\\\*(.+)")) {
if (modelPackage.startsWith(invokerPackage + "\\")) {
// need to strip out invokerPackage from path
return (outputFolder + File.separator + toSrcPath(modelPackage.replaceFirst("^" + invokerPackage + "\\\\*(.+)", "$1"), srcBasePath));
return (outputFolder + File.separator + toSrcPath(StringUtils.removeStart(modelPackage, invokerPackage + "\\"), srcBasePath));
}
return (outputFolder + File.separator + toSrcPath(modelPackage, srcBasePath));
}

View File

@ -18,8 +18,11 @@ package org.openapitools.codegen.slim;
import org.openapitools.codegen.languages.PhpSlimServerCodegen;
import org.testng.Assert;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import java.io.File;
public class PhpSlimServerCodegenTest {
@Test
@ -43,4 +46,44 @@ public class PhpSlimServerCodegenTest {
Assert.assertEquals(codegen.encodePath("/user/{name}[/{id:[0-9]+}]"), "/user/{name}[/{id:[0-9]+}]");
Assert.assertEquals(codegen.encodePath("/fixedRoutePart/{varName}[/moreFixed/{varName2:\\d+}]"), "/fixedRoutePart/{varName}[/moreFixed/{varName2:\\d+}]");
}
@Test(dataProvider = "modelFileFolderProvider")
public void modelFileFolder(String modelPackage, String invokerPackage, String expected) {
final PhpSlimServerCodegen codegen = new PhpSlimServerCodegen();
codegen.setModelPackage(modelPackage);
codegen.setInvokerPackage(invokerPackage);
Assert.assertEquals(codegen.modelFileFolder(), expected);
}
@DataProvider(name = "modelFileFolderProvider")
public Object[][] modelFileFolderProvider() {
return new Object[][] {
// {modelPackage, invokerPackage, expected}
{"Model", "Invoker", "generated-code/slim/lib/Model".replace('/', File.separatorChar)},
{"Petstore", "Petstore", "generated-code/slim/lib".replace('/', File.separatorChar)},
{"Package\\SubPackage\\Model", "Package\\SubPackage", "generated-code/slim/lib/Model".replace('/', File.separatorChar)},
{"Websupport\\InvoiceValidation\\Model", "Websupport\\InvoiceValidation", "generated-code/slim/lib/Model".replace('/', File.separatorChar)},
};
}
@Test(dataProvider = "apiFileFolderProvider")
public void apiFileFolder(String modelPackage, String invokerPackage, String expected) {
final PhpSlimServerCodegen codegen = new PhpSlimServerCodegen();
codegen.setApiPackage(modelPackage);
codegen.setInvokerPackage(invokerPackage);
Assert.assertEquals(codegen.apiFileFolder(), expected);
}
@DataProvider(name = "apiFileFolderProvider")
public Object[][] apiFileFolderProvider() {
return new Object[][] {
// {apiPackage, invokerPackage, expected}
{"Api", "Invoker", "generated-code/slim/lib/Api".replace('/', File.separatorChar)},
{"Petstore", "Petstore", "generated-code/slim/lib".replace('/', File.separatorChar)},
{"Package\\SubPackage\\Api", "Package\\SubPackage", "generated-code/slim/lib/Api".replace('/', File.separatorChar)},
{"Websupport\\InvoiceValidation\\Api", "Websupport\\InvoiceValidation", "generated-code/slim/lib/Api".replace('/', File.separatorChar)},
};
}
}