Skip to content

Commit

Permalink
Symfony generator enhancements (#6016)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
ksm2 authored and wing328 committed Jul 10, 2017
1 parent ca98803 commit d522236
Show file tree
Hide file tree
Showing 10 changed files with 555 additions and 191 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -198,10 +198,10 @@ public void processOpts() {
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);
Expand Down Expand Up @@ -261,32 +261,32 @@ public String escapeReservedWord(String name) {

@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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -66,9 +65,9 @@ public SymfonyServerCodegen() {
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");
Expand Down Expand Up @@ -153,7 +152,7 @@ public void setBundleName(String bundleName) {


public String controllerFileFolder() {
return (outputFolder + "/" + toPackagePath(controllerPackage, srcBasePath));
return (outputFolder + File.separator + toPackagePath(controllerPackage, srcBasePath));
}

@Override
Expand Down Expand Up @@ -219,16 +218,6 @@ public void processOpts() {
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);
Expand All @@ -241,13 +230,13 @@ public void processOpts() {
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);
Expand All @@ -256,15 +245,18 @@ public void processOpts() {
// 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"));
Expand All @@ -281,6 +273,7 @@ public Map<String, Object> postProcessOperations(Map<String, Object> objs) {
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) {
Expand Down Expand Up @@ -310,9 +303,15 @@ public Map<String, Object> postProcessOperations(Map<String, Object> objs) {
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;
}
Expand All @@ -332,11 +331,16 @@ public Map<String, Object> postProcessModels(Map<String, Object> objs) {
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");
}
}
}

Expand All @@ -355,12 +359,12 @@ public String escapeReservedWord(String name) {

@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) {
Expand Down Expand Up @@ -429,14 +433,14 @@ public String toApiName(String name) {
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) {
Expand Down
Loading

0 comments on commit d522236

Please sign in to comment.