Skip to content
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

[Slim] Upgrade API server integration tests to use Fake Petstore spec #354

Merged
merged 11 commits into from
Jun 23, 2018
Merged
Show file tree
Hide file tree
Changes from 6 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
2 changes: 1 addition & 1 deletion bin/php-slim-petstore-server.sh
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,6 @@ fi

# if you've executed sbt assembly previously it will use that instead.
export JAVA_OPTS="${JAVA_OPTS} -XX:MaxPermSize=256M -Xmx1024M -DloggerPath=conf/log4j.properties"
ags="generate -t modules/openapi-generator/src/main/resources/slim -i modules/openapi-generator/src/test/resources/2_0/petstore.yaml -g php-slim -o samples/server/petstore/php-slim $@"
ags="generate -t modules/openapi-generator/src/main/resources/slim -i modules/openapi-generator/src/test/resources/2_0/petstore-with-fake-endpoints-models-for-testing.yaml -g php-slim -o samples/server/petstore/php-slim $@"

java $JAVA_OPTS -jar $executable $ags
2 changes: 1 addition & 1 deletion bin/windows/php-slim-petstore-server.bat
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ If Not Exist %executable% (
)

REM set JAVA_OPTS=%JAVA_OPTS% -Xmx1024M
set ags=generate -i modules\openapi-generator\src\test\resources\2_0\petstore.yaml -g php-slim -o samples\server\petstore\php-slim
set ags=generate -i modules\openapi-generator\src\test\resources\2_0\petstore-with-fake-endpoints-models-for-testing.yaml -g php-slim -o samples\server\petstore\php-slim

java %JAVA_OPTS% -jar %executable% %ags%
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,31 @@
package org.openapitools.codegen.languages;

import org.apache.commons.lang3.StringUtils;
import org.openapitools.codegen.CodegenOperation;
import org.openapitools.codegen.CodegenConfig;
import org.openapitools.codegen.CodegenConstants;
import org.openapitools.codegen.CodegenType;
import org.openapitools.codegen.DefaultCodegen;
import org.openapitools.codegen.SupportingFile;
import org.openapitools.codegen.utils.ModelUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import io.swagger.v3.oas.models.media.*;

import java.io.File;
import java.util.Arrays;
import java.util.Map;
import java.util.List;
import java.util.HashMap;
import java.util.HashSet;
import java.util.regex.Matcher;
import java.util.Comparator;
import java.util.Collections;

public class PhpSlimServerCodegen extends DefaultCodegen implements CodegenConfig {
private static final Logger LOGGER = LoggerFactory.getLogger(PhpSlimServerCodegen.class);

protected String invokerPackage;
protected String srcBasePath = "lib";
protected String groupId = "org.openapitools";
Expand Down Expand Up @@ -232,9 +241,36 @@ public String toParamName(String name) {

@Override
public String toModelName(String name) {
// remove [
name = name.replaceAll("\\]", "");

// Note: backslash ("\\") is allowed for e.g. "\\DateTime"
name = name.replaceAll("[^\\w\\\\]+", "_"); // FIXME: a parameter should not be assigned. Also declare the methods parameters as 'final'.

// remove dollar sign
name = name.replaceAll("$", "");

// model name cannot use reserved keyword
if (isReservedWord(name)) {
escapeReservedWord(name); // e.g. return => _return
LOGGER.warn(name + " (reserved word) cannot be used as model name. Renamed to " + camelize("model_" + name));
name = "model_" + name; // e.g. return => ModelReturn (after camelize)
}

// model name starts with number
if (name.matches("^\\d.*")) {
LOGGER.warn(name + " (model name starts with number) cannot be used as model name. Renamed to " + camelize("model_" + name));
name = "model_" + name; // e.g. 200Response => Model200Response (after camelize)
}

// add prefix and/or suffic only if name does not start wth \ (e.g. \DateTime)
if (!name.matches("^\\\\.*")) {
if (!StringUtils.isEmpty(modelNamePrefix)) {
name = modelNamePrefix + "_" + name;
}

if (!StringUtils.isEmpty(modelNameSuffix)) {
name = name + "_" + modelNameSuffix;
}
}

// camelize the model name
Expand Down Expand Up @@ -292,4 +328,44 @@ public String escapeUnsafeCharacters(String input) {
return input.replace("*/", "");
}

@Override
public Map<String, Object> postProcessOperations(Map<String, Object> objs) {
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", "*_/_*");
}
}
}
}
return objs;
}

@Override
public Map<String, Object> postProcessSupportingFileData(Map<String, Object> objs) {
Map<String, Object> apiInfo = (Map<String, Object>) objs.get("apiInfo");
List<HashMap<String, Object>> apiList = (List<HashMap<String, Object>>) apiInfo.get("apis");
for (HashMap<String, Object> api : apiList) {
HashMap<String, Object> operations = (HashMap<String, Object>) api.get("operations");
List<CodegenOperation> operationList = (List<CodegenOperation>) operations.get("operation");

// Sort operations to avoid static routes shadowing
// ref: https://github.com/nikic/FastRoute/blob/master/src/DataGenerator/RegexBasedAbstract.php#L92-L101
Collections.sort(operationList, new Comparator<CodegenOperation>() {
@Override
public int compare(CodegenOperation one, CodegenOperation another) {
if (one.getHasPathParams() && !another.getHasPathParams()) return 1;
if (!one.getHasPathParams() && another.getHasPathParams()) return -1;
return 0;
}
});
}
return objs;
}

}
41 changes: 33 additions & 8 deletions modules/openapi-generator/src/main/resources/slim/index.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,39 @@ $app = new Slim\App();
{{#hasProduces}} * Output-Formats: [{{#produces}}{{{mediaType}}}{{#hasMore}}, {{/hasMore}}{{/produces}}]{{/hasProduces}}
*/
$app->{{httpMethod}}('{{{basePathWithoutHost}}}{{{path}}}', function($request, $response, $args) {
{{#hasHeaderParams}}$headers = $request->getHeaders();{{/hasHeaderParams}}
{{#hasQueryParams}}$queryParams = $request->getQueryParams();
{{#queryParams}}${{paramName}} = $queryParams['{{paramName}}'];{{newline}} {{/queryParams}}{{/hasQueryParams}}
{{#hasFormParams}}{{#formParams}}${{paramName}} = $args['{{paramName}}'];{{newline}} {{/formParams}}{{/hasFormParams}}
{{#hasBodyParam}}$body = $request->getParsedBody();{{/hasBodyParam}}
$response->write('How about implementing {{nickname}} as a {{httpMethod}} method ?');
return $response;
});
{{#hasHeaderParams}}
$headers = $request->getHeaders();
{{#headerParams}}
${{paramName}} = $request->hasHeader('{{baseName}}') ? $headers['{{baseName}}'] : null;
{{/headerParams}}
{{/hasHeaderParams}}
{{#hasPathParams}}
{{#pathParams}}
${{paramName}} = $args['{{baseName}}'];
{{/pathParams}}
{{/hasPathParams}}
{{#hasQueryParams}}
$queryParams = $request->getQueryParams();
{{#queryParams}}
${{paramName}} = $request->getQueryParam('{{baseName}}');
{{/queryParams}}
{{/hasQueryParams}}
{{#hasFormParams}}
{{#formParams}}
{{#notFile}}
${{paramName}} = $request->getParsedBodyParam('{{baseName}}');
{{/notFile}}
{{#isFile}}
${{paramName}} = $request->getUploadedFiles()['{{baseName}}'];
{{/isFile}}
{{/formParams}}
{{/hasFormParams}}
{{#hasBodyParam}}
$body = $request->getParsedBody();
{{/hasBodyParam}}
$response->write('How about implementing {{nickname}} as a {{httpMethod}} method ?');
return $response;
});

{{/operation}}{{/operations}}{{/apis}}{{/apiInfo}}

Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
3.0.0-SNAPSHOT
3.0.3-SNAPSHOT
11 changes: 4 additions & 7 deletions samples/server/petstore-security-test/slim/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,10 @@

*/
$app->PUT('/fake', function($request, $response, $args) {



$body = $request->getParsedBody();
$response->write('How about implementing testCodeInject */ &#39; &quot; &#x3D;end \r\n \n \r as a PUT method ?');
return $response;
});
$body = $request->getParsedBody();
$response->write('How about implementing testCodeInject */ &#39; &quot; &#x3D;end \r\n \n \r as a PUT method ?');
return $response;
});



Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php
/*
* ModelReturn
*/
namespace \Models;

/*
* ModelReturn
*/
class ModelReturn {
/* @var int $return property description &#39; \&quot; &#x3D;end -- \\r\\n \\n \\r */
private $return;
}
Loading