forked from loafle/openapi-generator-original
[php] Fix Composer 2 name error (#10002)
* Fix obvious typo * Add function to build Composer package name * Add composerPackageName codegen property * Use package name if it's not empty * Refresh samples
This commit is contained in:
parent
9e314d165b
commit
fe9636e9d9
@ -301,7 +301,7 @@ public class CodegenConfigurator {
|
||||
|
||||
public CodegenConfigurator setGitUserId(String gitUserId) {
|
||||
if (StringUtils.isNotEmpty(gitUserId)) {
|
||||
addAdditionalProperty(CodegenConstants.GIT_HOST, gitUserId);
|
||||
addAdditionalProperty(CodegenConstants.GIT_USER_ID, gitUserId);
|
||||
}
|
||||
generatorSettingsBuilder.withGitUserId(gitUserId);
|
||||
return this;
|
||||
|
@ -29,6 +29,7 @@ import org.slf4j.LoggerFactory;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.*;
|
||||
import java.util.regex.Pattern;
|
||||
import java.util.regex.Matcher;
|
||||
import static org.openapitools.codegen.utils.StringUtils.camelize;
|
||||
import static org.openapitools.codegen.utils.StringUtils.underscore;
|
||||
@ -190,6 +191,18 @@ public abstract class AbstractPhpCodegen extends DefaultCodegen implements Codeg
|
||||
this.setParameterNamingConvention((String) additionalProperties.get(VARIABLE_NAMING_CONVENTION));
|
||||
}
|
||||
|
||||
if (additionalProperties.containsKey(CodegenConstants.GIT_USER_ID)) {
|
||||
this.setGitUserId((String) additionalProperties.get(CodegenConstants.GIT_USER_ID));
|
||||
}
|
||||
|
||||
if (additionalProperties.containsKey(CodegenConstants.GIT_REPO_ID)) {
|
||||
this.setGitRepoId((String) additionalProperties.get(CodegenConstants.GIT_REPO_ID));
|
||||
}
|
||||
|
||||
if (!this.getComposerPackageName().isEmpty()) {
|
||||
additionalProperties.put("composerPackageName", this.getComposerPackageName());
|
||||
}
|
||||
|
||||
additionalProperties.put("escapedInvokerPackage", invokerPackage.replace("\\", "\\\\"));
|
||||
|
||||
// make api and model src path available in mustache template
|
||||
@ -758,4 +771,22 @@ public abstract class AbstractPhpCodegen extends DefaultCodegen implements Codeg
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get Composer package name based on GIT_USER_ID and GIT_REPO_ID.
|
||||
*
|
||||
* @return package name or empty string on fail
|
||||
*/
|
||||
public String getComposerPackageName() {
|
||||
String packageName = this.getGitUserId() + "/" + this.getGitRepoId();
|
||||
if (
|
||||
packageName.contentEquals("/")
|
||||
|| packageName.contentEquals("null/null")
|
||||
|| !Pattern.matches("^[a-z0-9]([_.-]?[a-z0-9]+)*/[a-z0-9](([_.]?|-{0,2})[a-z0-9]+)*$", packageName)
|
||||
) {
|
||||
return "";
|
||||
}
|
||||
|
||||
return packageName;
|
||||
}
|
||||
}
|
||||
|
@ -1,5 +1,7 @@
|
||||
{
|
||||
"name": "{{gitUserId}}/{{gitRepoId}}",
|
||||
{{#composerPackageName}}
|
||||
"name": "{{composerPackageName}}",
|
||||
{{/composerPackageName}}
|
||||
{{#artifactVersion}}
|
||||
"version": "{{artifactVersion}}",
|
||||
{{/artifactVersion}}
|
||||
|
@ -22,6 +22,7 @@ import org.openapitools.codegen.CodegenOperation;
|
||||
import org.openapitools.codegen.CodegenType;
|
||||
import org.openapitools.codegen.languages.AbstractPhpCodegen;
|
||||
import org.testng.Assert;
|
||||
import org.testng.annotations.DataProvider;
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
import java.util.Arrays;
|
||||
@ -100,6 +101,27 @@ public class AbstractPhpCodegenTest {
|
||||
Assert.assertEquals(codegenOperation.produces.get(1).get("mediaType"), "application/json");
|
||||
}
|
||||
|
||||
@Test(dataProvider = "composerNames", description = "Issue #9998")
|
||||
public void testGetComposerPackageName(String gitUserId, String gitRepoId, String result) {
|
||||
final AbstractPhpCodegen codegen = new P_AbstractPhpCodegen();
|
||||
codegen.processOpts();
|
||||
|
||||
codegen.setGitUserId(gitUserId);
|
||||
codegen.setGitRepoId(gitRepoId);
|
||||
Assert.assertEquals(codegen.getComposerPackageName(), result);
|
||||
}
|
||||
|
||||
@DataProvider(name = "composerNames")
|
||||
public static Object[][] composerNames() {
|
||||
return new Object[][] {
|
||||
{"", "", ""},
|
||||
{"null", "null", ""},
|
||||
{"GIT_REPO_ID", "GIT_USER_ID", ""},
|
||||
{"git_repo_id", "git_user_id", "git_repo_id/git_user_id"},
|
||||
{"foo", "bar", "foo/bar"},
|
||||
};
|
||||
}
|
||||
|
||||
private static class P_AbstractPhpCodegen extends AbstractPhpCodegen {
|
||||
@Override
|
||||
public CodegenType getTag() {
|
||||
|
@ -1,5 +1,4 @@
|
||||
{
|
||||
"name": "GIT_USER_ID/GIT_REPO_ID",
|
||||
"description": "This spec is mainly for testing Petstore server and contains fake endpoints, models. Please do not use this for any other purpose. Special characters: \" \\",
|
||||
"keywords": [
|
||||
"openapitools",
|
||||
|
Loading…
x
Reference in New Issue
Block a user