[PHP] Escape media type (#615)

* Move escapeMediaType() to AbstractPhpCodegen

* Escape media type

* Update samples

- bin/php-lumen-petstore-server.sh

* Refactoring: remove unused 'import'
This commit is contained in:
Akihito Nakano 2018-07-26 22:26:01 +09:00 committed by William Cheng
parent 65bad61abb
commit c9b934147a
5 changed files with 45 additions and 20 deletions

View File

@ -23,13 +23,9 @@ import org.openapitools.codegen.CodegenConstants;
import org.openapitools.codegen.CodegenOperation;
import org.openapitools.codegen.CodegenParameter;
import org.openapitools.codegen.CodegenProperty;
import org.openapitools.codegen.CodegenType;
import org.openapitools.codegen.DefaultCodegen;
import org.openapitools.codegen.SupportingFile;
import org.openapitools.codegen.utils.ModelUtils;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.Operation;
import io.swagger.v3.oas.models.media.*;
import java.io.File;
@ -702,6 +698,22 @@ public abstract class AbstractPhpCodegen extends DefaultCodegen implements Codeg
return super.escapeText(input).trim();
}
public void escapeMediaType(List<CodegenOperation> operationList) {
for (CodegenOperation op : operationList) {
if (!op.hasProduces) {
continue;
}
List<Map<String, String>> c = op.produces;
for (Map<String, String> mediaType : c) {
// "*/*" causes a syntax error
if ("*/*".equals(mediaType.get("mediaType"))) {
mediaType.put("mediaType", "*_/_*");
}
}
}
}
protected String extractSimpleName(String phpClassName) {
if (phpClassName == null) {
return null;

View File

@ -18,7 +18,6 @@
package org.openapitools.codegen.languages;
import org.openapitools.codegen.*;
import io.swagger.models.properties.*;
import java.util.*;
import java.io.File;
@ -133,6 +132,8 @@ public class PhpLumenServerCodegen extends AbstractPhpCodegen {
}
});
escapeMediaType(operations);
return objs;
}
}

View File

@ -124,17 +124,7 @@ public class PhpSlimServerCodegen extends AbstractPhpCodegen {
public Map<String, Object> postProcessOperationsWithModels(Map<String, Object> objs, List<Object> allModels) {
Map<String, Object> operations = (Map<String, Object>) objs.get("operations");
List<CodegenOperation> operationList = (List<CodegenOperation>) operations.get("operation");
for (CodegenOperation op : operationList) {
if (op.hasProduces) {
// need to escape */* values because they breakes current mustaches
List<Map<String, String>> c = op.produces;
for (Map<String, String> mediaType : c) {
if ("*/*".equals(mediaType.get("mediaType"))) {
mediaType.put("mediaType", "*_/_*");
}
}
}
}
escapeMediaType(operationList);
return objs;
}

View File

@ -17,6 +17,7 @@
package org.openapitools.codegen.php;
import org.openapitools.codegen.CodegenOperation;
import org.testng.Assert;
import org.testng.annotations.Test;
@ -24,6 +25,9 @@ import org.openapitools.codegen.CodegenConstants;
import org.openapitools.codegen.CodegenType;
import org.openapitools.codegen.languages.AbstractPhpCodegen;
import java.util.Arrays;
import java.util.HashMap;
public class AbstractPhpCodegenTest {
@Test
@ -79,6 +83,24 @@ public class AbstractPhpCodegenTest {
Assert.assertEquals(codegen.additionalProperties().get(CodegenConstants.INVOKER_PACKAGE), "PHPinvoker");
}
@Test
public void testEscapeMediaType() throws Exception {
HashMap<String, String> all = new HashMap<>();
all.put("mediaType", "*/*");
HashMap<String, String> applicationJson = new HashMap<>();
applicationJson.put("mediaType", "application/json");
CodegenOperation codegenOperation = new CodegenOperation();
codegenOperation.hasProduces = true;
codegenOperation.produces = Arrays.asList(all, applicationJson);
final AbstractPhpCodegen codegen = new P_AbstractPhpCodegen();
codegen.escapeMediaType(Arrays.asList(codegenOperation));
Assert.assertEquals(codegenOperation.produces.get(0).get("mediaType"), "*_/_*");
Assert.assertEquals(codegenOperation.produces.get(1).get("mediaType"), "application/json");
}
private static class P_AbstractPhpCodegen extends AbstractPhpCodegen {
@Override
public CodegenType getTag() {

View File

@ -81,28 +81,28 @@ $app->get('/v2/fake/jsonFormData', 'FakeApi@testJsonFormData');
* post fakeOuterBooleanSerialize
* Summary:
* Notes: Test serialization of outer boolean types
* Output-Formats: [*/*]
* Output-Formats: [*_/_*]
*/
$app->post('/v2/fake/outer/boolean', 'FakeApi@fakeOuterBooleanSerialize');
/**
* post fakeOuterCompositeSerialize
* Summary:
* Notes: Test serialization of object with outer number type
* Output-Formats: [*/*]
* Output-Formats: [*_/_*]
*/
$app->post('/v2/fake/outer/composite', 'FakeApi@fakeOuterCompositeSerialize');
/**
* post fakeOuterNumberSerialize
* Summary:
* Notes: Test serialization of outer number types
* Output-Formats: [*/*]
* Output-Formats: [*_/_*]
*/
$app->post('/v2/fake/outer/number', 'FakeApi@fakeOuterNumberSerialize');
/**
* post fakeOuterStringSerialize
* Summary:
* Notes: Test serialization of outer string types
* Output-Formats: [*/*]
* Output-Formats: [*_/_*]
*/
$app->post('/v2/fake/outer/string', 'FakeApi@fakeOuterStringSerialize');
/**