Skip to content

[PHP] Escape media type #615

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jul 26, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,9 @@
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;
Expand Down Expand Up @@ -702,6 +698,22 @@ public String escapeText(String input) {
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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -133,6 +132,8 @@ public int compare(CodegenOperation lhs, CodegenOperation rhs) {
}
});

escapeMediaType(operations);

return objs;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -124,17 +124,7 @@ public void processOpts() {
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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,17 @@

package org.openapitools.codegen.php;

import org.openapitools.codegen.CodegenOperation;
import org.testng.Assert;
import org.testng.annotations.Test;

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
Expand Down Expand Up @@ -79,6 +83,24 @@ public void testAdditionalPropertiesPutForConfigValues() throws Exception {
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() {
Expand Down
8 changes: 4 additions & 4 deletions samples/server/petstore/php-lumen/lib/app/Http/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,28 +81,28 @@
* 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');
/**
Expand Down