Symfony generator enhancements (#6016)

* Fix error in Symfony models #5985

* Parse Symfony params #5985

* Implement auth metods in Symfony #5985

* Make "get" to "is" in Symfony's booleans #5985

* Use `camelize` instead of `initialCaps` in Symfony #5985

* Use File.separator instead of "/" in PHP/Symfony #5985

* Improve README generation for Symfony #5985

* Create an options test for Symfony #5985
This commit is contained in:
Konstantin Simon Maria Möllers
2017-07-10 18:34:43 +02:00
committed by wing328
parent ca988039cc
commit d522236cec
10 changed files with 547 additions and 183 deletions

View File

@@ -45,8 +45,8 @@ public abstract class AbstractPhpCodegen extends DefaultCodegen implements Codeg
protected String apiDirName = "Api";
protected String modelDirName = "Model";
protected String variableNamingConvention= "snake_case";
protected String apiDocPath = docsBasePath + "/" + apiDirName;
protected String modelDocPath = docsBasePath + "/" + modelDirName;
protected String apiDocPath = docsBasePath + File.separator + apiDirName;
protected String modelDocPath = docsBasePath + File.separator + modelDirName;
public AbstractPhpCodegen() {
super();
@@ -198,10 +198,10 @@ public abstract class AbstractPhpCodegen extends DefaultCodegen implements Codeg
additionalProperties.put("escapedInvokerPackage", invokerPackage.replace("\\", "\\\\"));
// make api and model src path available in mustache template
additionalProperties.put("apiSrcPath", "./" + toSrcPath(apiPackage, srcBasePath));
additionalProperties.put("modelSrcPath", "./" + toSrcPath(modelPackage, srcBasePath));
additionalProperties.put("apiTestPath", "./" + testBasePath + "/" + apiDirName);
additionalProperties.put("modelTestPath", "./" + testBasePath + "/" + modelDirName);
additionalProperties.put("apiSrcPath", "." + File.separator + toSrcPath(apiPackage, srcBasePath));
additionalProperties.put("modelSrcPath", "." + File.separator + toSrcPath(modelPackage, srcBasePath));
additionalProperties.put("apiTestPath", "." + File.separator + testBasePath + File.separator + apiDirName);
additionalProperties.put("modelTestPath", "." + File.separator + testBasePath + File.separator + modelDirName);
// make api and model doc path available in mustache template
additionalProperties.put("apiDocPath", apiDocPath);
@@ -261,32 +261,32 @@ public abstract class AbstractPhpCodegen extends DefaultCodegen implements Codeg
@Override
public String apiFileFolder() {
return (outputFolder + "/" + toPackagePath(apiPackage, srcBasePath));
return (outputFolder + File.separator + toPackagePath(apiPackage, srcBasePath));
}
@Override
public String modelFileFolder() {
return (outputFolder + "/" + toPackagePath(modelPackage, srcBasePath));
return (outputFolder + File.separator + toPackagePath(modelPackage, srcBasePath));
}
@Override
public String apiTestFileFolder() {
return (outputFolder + "/" + getPackagePath() + "/" + testBasePath + "/" + apiDirName);
return (outputFolder + File.separator + getPackagePath() + File.separator + testBasePath + File.separator + apiDirName);
}
@Override
public String modelTestFileFolder() {
return (outputFolder + "/" + getPackagePath() + "/" + testBasePath + "/" + modelDirName);
return (outputFolder + File.separator + getPackagePath() + File.separator + testBasePath + File.separator + modelDirName);
}
@Override
public String apiDocFileFolder() {
return (outputFolder + "/" + getPackagePath() + "/" + apiDocPath);
return (outputFolder + File.separator + getPackagePath() + File.separator + apiDocPath);
}
@Override
public String modelDocFileFolder() {
return (outputFolder + "/" + getPackagePath() + "/" + modelDocPath);
return (outputFolder + File.separator + getPackagePath() + File.separator + modelDocPath);
}
@Override

View File

@@ -16,7 +16,6 @@ public class SymfonyServerCodegen extends AbstractPhpCodegen implements CodegenC
@SuppressWarnings("hiding")
static Logger LOGGER = LoggerFactory.getLogger(SymfonyServerCodegen.class);
public static final String VARIABLE_NAMING_CONVENTION = "variableNamingConvention";
public static final String BUNDLE_NAME = "bundleName";
public static final String COMPOSER_VENDOR_NAME = "composerVendorName";
public static final String COMPOSER_PROJECT_NAME = "composerProjectName";
@@ -66,9 +65,9 @@ public class SymfonyServerCodegen extends AbstractPhpCodegen implements CodegenC
setBundleName("SwaggerServer");
packagePath = "SymfonyBundle-php";
modelDirName = "Model";
docsBasePath = "Resources/docs";
apiDocPath = docsBasePath + "/" + apiDirName;
modelDocPath = docsBasePath + "/" + modelDirName;
docsBasePath = "Resources" + File.separator + "docs";
apiDocPath = docsBasePath + File.separator + apiDirName;
modelDocPath = docsBasePath + File.separator + modelDirName;
outputFolder = "generated-code" + File.separator + "php";
apiTemplateFiles.put("api_controller.mustache", ".php");
modelTestTemplateFiles.put("model_test.mustache", ".php");
@@ -153,7 +152,7 @@ public class SymfonyServerCodegen extends AbstractPhpCodegen implements CodegenC
public String controllerFileFolder() {
return (outputFolder + "/" + toPackagePath(controllerPackage, srcBasePath));
return (outputFolder + File.separator + toPackagePath(controllerPackage, srcBasePath));
}
@Override
@@ -219,16 +218,6 @@ public class SymfonyServerCodegen extends AbstractPhpCodegen implements CodegenC
additionalProperties.put(COMPOSER_VENDOR_NAME, composerVendorName);
}
if (additionalProperties.containsKey(CodegenConstants.ARTIFACT_VERSION)) {
this.setArtifactVersion((String) additionalProperties.get(CodegenConstants.ARTIFACT_VERSION));
} else {
additionalProperties.put(CodegenConstants.ARTIFACT_VERSION, artifactVersion);
}
if (additionalProperties.containsKey(VARIABLE_NAMING_CONVENTION)) {
this.setParameterNamingConvention((String) additionalProperties.get(VARIABLE_NAMING_CONVENTION));
}
additionalProperties.put("escapedInvokerPackage", invokerPackage.replace("\\", "\\\\"));
additionalProperties.put("controllerPackage", controllerPackage);
additionalProperties.put("apiTestsPackage", apiTestsPackage);
@@ -241,13 +230,13 @@ public class SymfonyServerCodegen extends AbstractPhpCodegen implements CodegenC
additionalProperties.put("bundleAlias", bundleAlias);
// make api and model src path available in mustache template
additionalProperties.put("apiSrcPath", "./" + toSrcPath(apiPackage, srcBasePath));
additionalProperties.put("modelSrcPath", "./" + toSrcPath(modelPackage, srcBasePath));
additionalProperties.put("testsSrcPath", "./" + toSrcPath(testsPackage, srcBasePath));
additionalProperties.put("apiTestsSrcPath", "./" + toSrcPath(apiTestsPackage, srcBasePath));
additionalProperties.put("modelTestsSrcPath", "./" + toSrcPath(modelTestsPackage, srcBasePath));
additionalProperties.put("apiTestPath", "./" + testsDirName + "/" + apiDirName);
additionalProperties.put("modelTestPath", "./" + testsDirName + "/" + modelDirName);
additionalProperties.put("apiSrcPath", "." + File.separator + toSrcPath(apiPackage, srcBasePath));
additionalProperties.put("modelSrcPath", "." + File.separator + toSrcPath(modelPackage, srcBasePath));
additionalProperties.put("testsSrcPath", "." + File.separator + toSrcPath(testsPackage, srcBasePath));
additionalProperties.put("apiTestsSrcPath", "." + File.separator + toSrcPath(apiTestsPackage, srcBasePath));
additionalProperties.put("modelTestsSrcPath", "." + File.separator + toSrcPath(modelTestsPackage, srcBasePath));
additionalProperties.put("apiTestPath", "." + File.separator + testsDirName + File.separator + apiDirName);
additionalProperties.put("modelTestPath", "." + File.separator + testsDirName + File.separator + modelDirName);
// make api and model doc path available in mustache template
additionalProperties.put("apiDocPath", apiDocPath);
@@ -256,15 +245,18 @@ public class SymfonyServerCodegen extends AbstractPhpCodegen implements CodegenC
// make test path available in mustache template
additionalProperties.put("testsDirName", testsDirName);
final String configDir = getPackagePath() + File.separator + "Resources" + File.separator + "config";
final String dependencyInjectionDir = getPackagePath() + File.separator + "DependencyInjection";
supportingFiles.add(new SupportingFile("Controller.mustache", toPackagePath(controllerPackage, srcBasePath), "Controller.php"));
supportingFiles.add(new SupportingFile("Bundle.mustache", getPackagePath(), bundleClassName + ".php"));
supportingFiles.add(new SupportingFile("Extension.mustache", getPackagePath() + "/DependencyInjection", bundleExtensionName + ".php"));
supportingFiles.add(new SupportingFile("ApiPass.mustache", getPackagePath() + "/DependencyInjection/Compiler", bundleName + "ApiPass.php"));
supportingFiles.add(new SupportingFile("Extension.mustache", dependencyInjectionDir, bundleExtensionName + ".php"));
supportingFiles.add(new SupportingFile("ApiPass.mustache", dependencyInjectionDir + File.separator + "Compiler", bundleName + "ApiPass.php"));
supportingFiles.add(new SupportingFile("ApiServer.mustache", toPackagePath(apiPackage, srcBasePath), "ApiServer.php"));
supportingFiles.add(new SupportingFile("ModelSerializer.mustache", toPackagePath(modelPackage, srcBasePath), "ModelSerializer.php"));
supportingFiles.add(new SupportingFile("ModelInterface.mustache", toPackagePath(modelPackage, srcBasePath), "ModelInterface.php"));
supportingFiles.add(new SupportingFile("routing.mustache", getPackagePath() + "/Resources/config", "routing.yml"));
supportingFiles.add(new SupportingFile("services.mustache", getPackagePath() + "/Resources/config", "services.yml"));
supportingFiles.add(new SupportingFile("routing.mustache", configDir, "routing.yml"));
supportingFiles.add(new SupportingFile("services.mustache", configDir, "services.yml"));
supportingFiles.add(new SupportingFile("composer.mustache", getPackagePath(), "composer.json"));
supportingFiles.add(new SupportingFile("autoload.mustache", getPackagePath(), "autoload.php"));
supportingFiles.add(new SupportingFile("README.mustache", getPackagePath(), "README.md"));
@@ -281,6 +273,7 @@ public class SymfonyServerCodegen extends AbstractPhpCodegen implements CodegenC
operations.put("controllerName", toControllerName((String) operations.get("pathPrefix")));
operations.put("symfonyService", toSymfonyService((String) operations.get("pathPrefix")));
HashSet<CodegenSecurity> authMethods = new HashSet<>();
HashSet<String> imports = new HashSet<>();
List<CodegenOperation> operationList = (List<CodegenOperation>) operations.get("operation");
for (CodegenOperation op : operationList) {
@@ -310,9 +303,15 @@ public class SymfonyServerCodegen extends AbstractPhpCodegen implements CodegenC
imports.add(exception);
}
}
// Add operation's authentication methods to whole interface
if (op.authMethods != null) {
authMethods.addAll(op.authMethods);
}
}
operations.put("imports", new ArrayList<>(imports));
operations.put("authMethods", authMethods);
return objs;
}
@@ -332,11 +331,16 @@ public class SymfonyServerCodegen extends AbstractPhpCodegen implements CodegenC
final String importType = var.datatype.replaceFirst("\\[\\]$", "");
final String dataType = extractSimpleName(var.datatype);
final boolean isScalarType = typeMapping.containsValue(importType);
var.vendorExtensions.put("x-fullType", var.datatype);
if (!isScalarType) {
var.vendorExtensions.put("x-typeAnnotation", dataType.endsWith("[]") ? "array" : dataType);
imports.add(importType);
var.datatype = dataType;
}
if (var.isBoolean) {
var.getter = var.getter.replaceAll("^get", "is");
}
}
}
@@ -355,12 +359,12 @@ public class SymfonyServerCodegen extends AbstractPhpCodegen implements CodegenC
@Override
public String apiTestFileFolder() {
return (outputFolder + "/" + toPackagePath(apiTestsPackage, srcBasePath));
return (outputFolder + File.separator + toPackagePath(apiTestsPackage, srcBasePath));
}
@Override
public String modelTestFileFolder() {
return (outputFolder + "/" + toPackagePath(modelTestsPackage, srcBasePath));
return (outputFolder + File.separator + toPackagePath(modelTestsPackage, srcBasePath));
}
public void setComposerVendorName(String composerVendorName) {
@@ -429,14 +433,14 @@ public class SymfonyServerCodegen extends AbstractPhpCodegen implements CodegenC
if (name.isEmpty()) {
return "DefaultApiInterface";
}
return initialCaps(name) + "ApiInterface";
return camelize(name, false) + "ApiInterface";
}
protected String toControllerName(String name) {
if (name.isEmpty()) {
return "DefaultController";
}
return initialCaps(name) + "Controller";
return camelize(name, false) + "Controller";
}
protected String toSymfonyService(String name) {