forked from loafle/openapi-generator-original
[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:
parent
65bad61abb
commit
c9b934147a
@ -23,13 +23,9 @@ import org.openapitools.codegen.CodegenConstants;
|
|||||||
import org.openapitools.codegen.CodegenOperation;
|
import org.openapitools.codegen.CodegenOperation;
|
||||||
import org.openapitools.codegen.CodegenParameter;
|
import org.openapitools.codegen.CodegenParameter;
|
||||||
import org.openapitools.codegen.CodegenProperty;
|
import org.openapitools.codegen.CodegenProperty;
|
||||||
import org.openapitools.codegen.CodegenType;
|
|
||||||
import org.openapitools.codegen.DefaultCodegen;
|
import org.openapitools.codegen.DefaultCodegen;
|
||||||
import org.openapitools.codegen.SupportingFile;
|
|
||||||
import org.openapitools.codegen.utils.ModelUtils;
|
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 io.swagger.v3.oas.models.media.*;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
@ -702,6 +698,22 @@ public abstract class AbstractPhpCodegen extends DefaultCodegen implements Codeg
|
|||||||
return super.escapeText(input).trim();
|
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) {
|
protected String extractSimpleName(String phpClassName) {
|
||||||
if (phpClassName == null) {
|
if (phpClassName == null) {
|
||||||
return null;
|
return null;
|
||||||
|
@ -18,7 +18,6 @@
|
|||||||
package org.openapitools.codegen.languages;
|
package org.openapitools.codegen.languages;
|
||||||
|
|
||||||
import org.openapitools.codegen.*;
|
import org.openapitools.codegen.*;
|
||||||
import io.swagger.models.properties.*;
|
|
||||||
|
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
@ -133,6 +132,8 @@ public class PhpLumenServerCodegen extends AbstractPhpCodegen {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
escapeMediaType(operations);
|
||||||
|
|
||||||
return objs;
|
return objs;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -124,17 +124,7 @@ public class PhpSlimServerCodegen extends AbstractPhpCodegen {
|
|||||||
public Map<String, Object> postProcessOperationsWithModels(Map<String, Object> objs, List<Object> allModels) {
|
public Map<String, Object> postProcessOperationsWithModels(Map<String, Object> objs, List<Object> allModels) {
|
||||||
Map<String, Object> operations = (Map<String, Object>) objs.get("operations");
|
Map<String, Object> operations = (Map<String, Object>) objs.get("operations");
|
||||||
List<CodegenOperation> operationList = (List<CodegenOperation>) operations.get("operation");
|
List<CodegenOperation> operationList = (List<CodegenOperation>) operations.get("operation");
|
||||||
for (CodegenOperation op : operationList) {
|
escapeMediaType(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", "*_/_*");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return objs;
|
return objs;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -17,6 +17,7 @@
|
|||||||
|
|
||||||
package org.openapitools.codegen.php;
|
package org.openapitools.codegen.php;
|
||||||
|
|
||||||
|
import org.openapitools.codegen.CodegenOperation;
|
||||||
import org.testng.Assert;
|
import org.testng.Assert;
|
||||||
import org.testng.annotations.Test;
|
import org.testng.annotations.Test;
|
||||||
|
|
||||||
@ -24,6 +25,9 @@ import org.openapitools.codegen.CodegenConstants;
|
|||||||
import org.openapitools.codegen.CodegenType;
|
import org.openapitools.codegen.CodegenType;
|
||||||
import org.openapitools.codegen.languages.AbstractPhpCodegen;
|
import org.openapitools.codegen.languages.AbstractPhpCodegen;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.HashMap;
|
||||||
|
|
||||||
public class AbstractPhpCodegenTest {
|
public class AbstractPhpCodegenTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@ -79,6 +83,24 @@ public class AbstractPhpCodegenTest {
|
|||||||
Assert.assertEquals(codegen.additionalProperties().get(CodegenConstants.INVOKER_PACKAGE), "PHPinvoker");
|
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 {
|
private static class P_AbstractPhpCodegen extends AbstractPhpCodegen {
|
||||||
@Override
|
@Override
|
||||||
public CodegenType getTag() {
|
public CodegenType getTag() {
|
||||||
|
@ -81,28 +81,28 @@ $app->get('/v2/fake/jsonFormData', 'FakeApi@testJsonFormData');
|
|||||||
* post fakeOuterBooleanSerialize
|
* post fakeOuterBooleanSerialize
|
||||||
* Summary:
|
* Summary:
|
||||||
* Notes: Test serialization of outer boolean types
|
* Notes: Test serialization of outer boolean types
|
||||||
* Output-Formats: [*/*]
|
* Output-Formats: [*_/_*]
|
||||||
*/
|
*/
|
||||||
$app->post('/v2/fake/outer/boolean', 'FakeApi@fakeOuterBooleanSerialize');
|
$app->post('/v2/fake/outer/boolean', 'FakeApi@fakeOuterBooleanSerialize');
|
||||||
/**
|
/**
|
||||||
* post fakeOuterCompositeSerialize
|
* post fakeOuterCompositeSerialize
|
||||||
* Summary:
|
* Summary:
|
||||||
* Notes: Test serialization of object with outer number type
|
* Notes: Test serialization of object with outer number type
|
||||||
* Output-Formats: [*/*]
|
* Output-Formats: [*_/_*]
|
||||||
*/
|
*/
|
||||||
$app->post('/v2/fake/outer/composite', 'FakeApi@fakeOuterCompositeSerialize');
|
$app->post('/v2/fake/outer/composite', 'FakeApi@fakeOuterCompositeSerialize');
|
||||||
/**
|
/**
|
||||||
* post fakeOuterNumberSerialize
|
* post fakeOuterNumberSerialize
|
||||||
* Summary:
|
* Summary:
|
||||||
* Notes: Test serialization of outer number types
|
* Notes: Test serialization of outer number types
|
||||||
* Output-Formats: [*/*]
|
* Output-Formats: [*_/_*]
|
||||||
*/
|
*/
|
||||||
$app->post('/v2/fake/outer/number', 'FakeApi@fakeOuterNumberSerialize');
|
$app->post('/v2/fake/outer/number', 'FakeApi@fakeOuterNumberSerialize');
|
||||||
/**
|
/**
|
||||||
* post fakeOuterStringSerialize
|
* post fakeOuterStringSerialize
|
||||||
* Summary:
|
* Summary:
|
||||||
* Notes: Test serialization of outer string types
|
* Notes: Test serialization of outer string types
|
||||||
* Output-Formats: [*/*]
|
* Output-Formats: [*_/_*]
|
||||||
*/
|
*/
|
||||||
$app->post('/v2/fake/outer/string', 'FakeApi@fakeOuterStringSerialize');
|
$app->post('/v2/fake/outer/string', 'FakeApi@fakeOuterStringSerialize');
|
||||||
/**
|
/**
|
||||||
|
Loading…
x
Reference in New Issue
Block a user