Skip to content

Commit 425ff3d

Browse files
author
Nael Rashdeen
committed
[PHP][Symfony] Improve the implementation
Closes #6614
1 parent 4327746 commit 425ff3d

19 files changed

+623
-1009
lines changed

modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/SymfonyServerCodegen.java

Lines changed: 36 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ public SymfonyServerCodegen() {
110110
defaultIncludes = new HashSet<String>(
111111
Arrays.asList(
112112
"\\DateTime",
113-
"\\SplFileObject"
113+
"UploadedFile"
114114
)
115115
);
116116

@@ -134,7 +134,7 @@ public SymfonyServerCodegen() {
134134
typeMapping.put("boolean", "bool");
135135
typeMapping.put("Date", "\\DateTime");
136136
typeMapping.put("DateTime", "\\DateTime");
137-
typeMapping.put("file", "\\SplFileObject");
137+
typeMapping.put("file", "UploadedFile");
138138
typeMapping.put("map", "array");
139139
typeMapping.put("array", "array");
140140
typeMapping.put("list", "array");
@@ -275,10 +275,16 @@ public void processOpts() {
275275
supportingFiles.add(new SupportingFile("Extension.mustache", dependencyInjectionDir, bundleExtensionName + ".php"));
276276
supportingFiles.add(new SupportingFile("ApiPass.mustache", dependencyInjectionDir + File.separator + "Compiler", bundleName + "ApiPass.php"));
277277
supportingFiles.add(new SupportingFile("ApiServer.mustache", toPackagePath(apiPackage, srcBasePath), "ApiServer.php"));
278-
supportingFiles.add(new SupportingFile("ModelSerializer.mustache", toPackagePath(modelPackage, srcBasePath), "ModelSerializer.php"));
279-
supportingFiles.add(new SupportingFile("ModelInterface.mustache", toPackagePath(modelPackage, srcBasePath), "ModelInterface.php"));
280-
supportingFiles.add(new SupportingFile("StrictJsonDeserializationVisitor.mustache", toPackagePath(servicePackage, srcBasePath), "StrictJsonDeserializationVisitor.php"));
281-
supportingFiles.add(new SupportingFile("TypeMismatchException.mustache", toPackagePath(servicePackage, srcBasePath), "TypeMismatchException.php"));
278+
279+
// Serialization components
280+
supportingFiles.add(new SupportingFile("serialization/SerializerInterface.mustache", toPackagePath(servicePackage, srcBasePath), "SerializerInterface.php"));
281+
supportingFiles.add(new SupportingFile("serialization/JmsSerializer.mustache", toPackagePath(servicePackage, srcBasePath), "JmsSerializer.php"));
282+
supportingFiles.add(new SupportingFile("serialization/StrictJsonDeserializationVisitor.mustache", toPackagePath(servicePackage, srcBasePath), "StrictJsonDeserializationVisitor.php"));
283+
supportingFiles.add(new SupportingFile("serialization/TypeMismatchException.mustache", toPackagePath(servicePackage, srcBasePath), "TypeMismatchException.php"));
284+
// Validation components
285+
supportingFiles.add(new SupportingFile("validation/ValidatorInterface.mustache", toPackagePath(servicePackage, srcBasePath), "ValidatorInterface.php"));
286+
supportingFiles.add(new SupportingFile("validation/SymfonyValidator.mustache", toPackagePath(servicePackage, srcBasePath), "SymfonyValidator.php"));
287+
282288
supportingFiles.add(new SupportingFile("routing.mustache", configDir, "routing.yml"));
283289
supportingFiles.add(new SupportingFile("services.mustache", configDir, "services.yml"));
284290
supportingFiles.add(new SupportingFile("composer.mustache", getPackagePath(), "composer.json"));
@@ -331,6 +337,14 @@ public Map<String, Object> postProcessOperations(Map<String, Object> objs) {
331337
if (!typeHint.isEmpty()) {
332338
param.vendorExtensions.put("x-parameterType", typeHint);
333339
}
340+
341+
// Quote default values for strings
342+
// @todo: The default values for headers, forms and query params are handled
343+
// in DefaultCodegen fromParameter with no real possibility to override
344+
// the functionality. Thus we are handling quoting of string values here
345+
if (param.dataType.equals("string") && param.defaultValue != null && !param.defaultValue.isEmpty()) {
346+
param.defaultValue = "'"+param.defaultValue+"'";
347+
}
334348
}
335349

336350
for (CodegenResponse response : op.responses) {
@@ -363,19 +377,15 @@ public Map<String, Object> postProcessModels(Map<String, Object> objs) {
363377
ArrayList<Object> modelsArray = (ArrayList<Object>) objs.get("models");
364378
Map<String, Object> models = (Map<String, Object>) modelsArray.get(0);
365379
CodegenModel model = (CodegenModel) models.get("model");
366-
HashSet<String> imports = new HashSet<>();
367380

368381
// Simplify model var type
369382
for (CodegenProperty var : model.vars) {
370383
if (var.datatype != null) {
371-
final String importType = var.datatype.replaceFirst("\\[\\]$", "");
372-
final String dataType = extractSimpleName(var.datatype);
373-
final boolean isScalarType = typeMapping.containsValue(importType);
374-
var.vendorExtensions.put("x-fullType", var.datatype);
375-
if (!isScalarType) {
376-
var.vendorExtensions.put("x-typeAnnotation", dataType.endsWith("[]") ? "array" : dataType);
377-
imports.add(importType);
378-
var.datatype = dataType;
384+
// Determine if the paramter type is supported as a type hint and make it available
385+
// to the templating engine
386+
String typeHint = getTypeHint(var.datatype);
387+
if (!typeHint.isEmpty()) {
388+
var.vendorExtensions.put("x-parameterType", typeHint);
379389
}
380390

381391
if (var.isBoolean) {
@@ -384,8 +394,6 @@ public Map<String, Object> postProcessModels(Map<String, Object> objs) {
384394
}
385395
}
386396

387-
objs.put("useStatements", new ArrayList<>(imports));
388-
389397
return objs;
390398
}
391399

@@ -494,6 +502,17 @@ public String toEnumValue(String value, String datatype) {
494502
}
495503
}
496504

505+
/**
506+
* Return the regular expression/JSON schema pattern (http://json-schema.org/latest/json-schema-validation.html#anchor33)
507+
*
508+
* @param pattern the pattern (regular expression)
509+
* @return properly-escaped pattern
510+
*/
511+
@Override
512+
public String toRegularExpression(String pattern) {
513+
return escapeText(pattern);
514+
}
515+
497516
public String toApiName(String name) {
498517
if (name.isEmpty()) {
499518
return "DefaultApiInterface";

0 commit comments

Comments
 (0)