forked from loafle/openapi-generator-original
[php-symfony] Fix problem with clients, that put charset in content type header. (#6078)
* Fix problem with clients, that put charset in content type header. With this fix header "Content-Type: application/json; charset=utf-8" working same as "Content-Type: application/json" for parse input data * Fix code style, add $consumes length check. * Add isContentTypeAllowed static method and tests * Fix old tests Right now serializer doesn't support anything beside json and xml. Call tests with application/json instead of form data. Co-authored-by: Yuriy Belenko <yura-bely@mail.ru>
This commit is contained in:
@@ -55,6 +55,7 @@ public class PhpSymfonyServerCodegen extends AbstractPhpCodegen implements Codeg
|
|||||||
protected String controllerDirName = "Controller";
|
protected String controllerDirName = "Controller";
|
||||||
protected String serviceDirName = "Service";
|
protected String serviceDirName = "Service";
|
||||||
protected String controllerPackage;
|
protected String controllerPackage;
|
||||||
|
protected String controllerTestsPackage;
|
||||||
protected String servicePackage;
|
protected String servicePackage;
|
||||||
protected Boolean phpLegacySupport = Boolean.TRUE;
|
protected Boolean phpLegacySupport = Boolean.TRUE;
|
||||||
|
|
||||||
@@ -301,6 +302,7 @@ public class PhpSymfonyServerCodegen extends AbstractPhpCodegen implements Codeg
|
|||||||
additionalProperties.put("servicePackage", servicePackage);
|
additionalProperties.put("servicePackage", servicePackage);
|
||||||
additionalProperties.put("apiTestsPackage", apiTestsPackage);
|
additionalProperties.put("apiTestsPackage", apiTestsPackage);
|
||||||
additionalProperties.put("modelTestsPackage", modelTestsPackage);
|
additionalProperties.put("modelTestsPackage", modelTestsPackage);
|
||||||
|
additionalProperties.put("controllerTestsPackage", controllerTestsPackage);
|
||||||
|
|
||||||
// make Symonfy-specific properties available
|
// make Symonfy-specific properties available
|
||||||
additionalProperties.put("bundleName", bundleName);
|
additionalProperties.put("bundleName", bundleName);
|
||||||
@@ -311,11 +313,13 @@ public class PhpSymfonyServerCodegen extends AbstractPhpCodegen implements Codeg
|
|||||||
// make api and model src path available in mustache template
|
// make api and model src path available in mustache template
|
||||||
additionalProperties.put("apiSrcPath", "." + "/" + toSrcPath(apiPackage, srcBasePath));
|
additionalProperties.put("apiSrcPath", "." + "/" + toSrcPath(apiPackage, srcBasePath));
|
||||||
additionalProperties.put("modelSrcPath", "." + "/" + toSrcPath(modelPackage, srcBasePath));
|
additionalProperties.put("modelSrcPath", "." + "/" + toSrcPath(modelPackage, srcBasePath));
|
||||||
|
additionalProperties.put("controllerSrcPath", "." + "/" + toSrcPath(controllerPackage, srcBasePath));
|
||||||
additionalProperties.put("testsSrcPath", "." + "/" + toSrcPath(testsPackage, srcBasePath));
|
additionalProperties.put("testsSrcPath", "." + "/" + toSrcPath(testsPackage, srcBasePath));
|
||||||
additionalProperties.put("apiTestsSrcPath", "." + "/" + toSrcPath(apiTestsPackage, srcBasePath));
|
additionalProperties.put("apiTestsSrcPath", "." + "/" + toSrcPath(apiTestsPackage, srcBasePath));
|
||||||
additionalProperties.put("modelTestsSrcPath", "." + "/" + toSrcPath(modelTestsPackage, srcBasePath));
|
additionalProperties.put("modelTestsSrcPath", "." + "/" + toSrcPath(modelTestsPackage, srcBasePath));
|
||||||
additionalProperties.put("apiTestPath", "." + "/" + testsDirName + "/" + apiDirName);
|
additionalProperties.put("apiTestPath", "." + "/" + testsDirName + "/" + apiDirName);
|
||||||
additionalProperties.put("modelTestPath", "." + "/" + testsDirName + "/" + modelDirName);
|
additionalProperties.put("modelTestPath", "." + "/" + testsDirName + "/" + modelDirName);
|
||||||
|
additionalProperties.put("controllerTestPath", "." + "/" + testsDirName + "/" + controllerDirName);
|
||||||
|
|
||||||
// make api and model doc path available in mustache template
|
// make api and model doc path available in mustache template
|
||||||
additionalProperties.put("apiDocPath", apiDocPath);
|
additionalProperties.put("apiDocPath", apiDocPath);
|
||||||
@@ -346,6 +350,7 @@ public class PhpSymfonyServerCodegen extends AbstractPhpCodegen implements Codeg
|
|||||||
supportingFiles.add(new SupportingFile("testing/phpunit.xml.mustache", "", "phpunit.xml.dist"));
|
supportingFiles.add(new SupportingFile("testing/phpunit.xml.mustache", "", "phpunit.xml.dist"));
|
||||||
supportingFiles.add(new SupportingFile("testing/pom.xml", "", "pom.xml"));
|
supportingFiles.add(new SupportingFile("testing/pom.xml", "", "pom.xml"));
|
||||||
supportingFiles.add(new SupportingFile("testing/AppKernel.php", toSrcPath(testsPackage, srcBasePath), "AppKernel.php"));
|
supportingFiles.add(new SupportingFile("testing/AppKernel.php", toSrcPath(testsPackage, srcBasePath), "AppKernel.php"));
|
||||||
|
supportingFiles.add(new SupportingFile("testing/ControllerTest.mustache", toSrcPath(controllerTestsPackage, srcBasePath), "ControllerTest.php"));
|
||||||
supportingFiles.add(new SupportingFile("testing/test_config.yml", toSrcPath(testsPackage, srcBasePath), "test_config.yml"));
|
supportingFiles.add(new SupportingFile("testing/test_config.yml", toSrcPath(testsPackage, srcBasePath), "test_config.yml"));
|
||||||
|
|
||||||
supportingFiles.add(new SupportingFile("routing.mustache", configDir, "routing.yml"));
|
supportingFiles.add(new SupportingFile("routing.mustache", configDir, "routing.yml"));
|
||||||
@@ -540,6 +545,7 @@ public class PhpSymfonyServerCodegen extends AbstractPhpCodegen implements Codeg
|
|||||||
apiTestsPackage = testsPackage + "\\" + apiDirName;
|
apiTestsPackage = testsPackage + "\\" + apiDirName;
|
||||||
modelTestsPackage = testsPackage + "\\" + modelDirName;
|
modelTestsPackage = testsPackage + "\\" + modelDirName;
|
||||||
controllerPackage = invokerPackage + "\\" + controllerDirName;
|
controllerPackage = invokerPackage + "\\" + controllerDirName;
|
||||||
|
controllerTestsPackage = testsPackage + "\\" + controllerDirName;
|
||||||
servicePackage = invokerPackage + "\\" + serviceDirName;
|
servicePackage = invokerPackage + "\\" + serviceDirName;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -20,6 +20,7 @@
|
|||||||
namespace {{controllerPackage}};
|
namespace {{controllerPackage}};
|
||||||
|
|
||||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||||
|
use Symfony\Component\HttpFoundation\Request;
|
||||||
use Symfony\Component\HttpFoundation\Response;
|
use Symfony\Component\HttpFoundation\Response;
|
||||||
use Symfony\Component\HttpKernel\Exception\HttpException;
|
use Symfony\Component\HttpKernel\Exception\HttpException;
|
||||||
use {{servicePackage}}\SerializerInterface;
|
use {{servicePackage}}\SerializerInterface;
|
||||||
@@ -176,4 +177,40 @@ class Controller extends AbstractController
|
|||||||
// If we reach this point, we don't have a common ground between server and client
|
// If we reach this point, we don't have a common ground between server and client
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Checks whether Content-Type request header presented in supported formats.
|
||||||
|
*
|
||||||
|
* @param Request $request Request instance.
|
||||||
|
* @param array $consumes Array of supported content types.
|
||||||
|
*
|
||||||
|
* @return bool Returns true if Content-Type supported otherwise false.
|
||||||
|
*/
|
||||||
|
public static function isContentTypeAllowed(Request $request, array $consumes = [])
|
||||||
|
{
|
||||||
|
if (!empty($consumes) && $consumes[0] !== '*/*') {
|
||||||
|
$currentFormat = $request->getContentType();
|
||||||
|
foreach ($consumes as $mimeType) {
|
||||||
|
// canonize mime type
|
||||||
|
if (is_string($mimeType) && false !== $pos = strpos($mimeType, ';')) {
|
||||||
|
$mimeType = trim(substr($mimeType, 0, $pos));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!$format = $request->getFormat($mimeType)) {
|
||||||
|
// add custom format to request
|
||||||
|
$format = $mimeType;
|
||||||
|
$request->setFormat($format, $format);
|
||||||
|
$currentFormat = $request->getContentType();
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($format === $currentFormat) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -60,8 +60,7 @@ class {{controllerName}} extends Controller
|
|||||||
{{#bodyParams}}
|
{{#bodyParams}}
|
||||||
// Make sure that the client is providing something that we can consume
|
// Make sure that the client is providing something that we can consume
|
||||||
$consumes = [{{#consumes}}'{{{mediaType}}}'{{#hasMore}}, {{/hasMore}}{{/consumes}}];
|
$consumes = [{{#consumes}}'{{{mediaType}}}'{{#hasMore}}, {{/hasMore}}{{/consumes}}];
|
||||||
$inputFormat = $request->headers->has('Content-Type')?$request->headers->get('Content-Type'):$consumes[0];
|
if (!static::isContentTypeAllowed($request, $consumes)) {
|
||||||
if (!in_array($inputFormat, $consumes)) {
|
|
||||||
// We can't consume the content that the client is sending us
|
// We can't consume the content that the client is sending us
|
||||||
return new Response('', 415);
|
return new Response('', 415);
|
||||||
}
|
}
|
||||||
@@ -146,6 +145,7 @@ class {{controllerName}} extends Controller
|
|||||||
{{#allParams}}
|
{{#allParams}}
|
||||||
{{^isFile}}
|
{{^isFile}}
|
||||||
{{#isBodyParam}}
|
{{#isBodyParam}}
|
||||||
|
$inputFormat = $request->getMimeType($request->getContentType());
|
||||||
${{paramName}} = $this->deserialize(${{paramName}}, '{{#isContainer}}{{#items}}array<{{dataType}}>{{/items}}{{/isContainer}}{{^isContainer}}{{dataType}}{{/isContainer}}', $inputFormat);
|
${{paramName}} = $this->deserialize(${{paramName}}, '{{#isContainer}}{{#items}}array<{{dataType}}>{{/items}}{{/isContainer}}{{^isContainer}}{{dataType}}{{/isContainer}}', $inputFormat);
|
||||||
{{/isBodyParam}}
|
{{/isBodyParam}}
|
||||||
{{^isBodyParam}}
|
{{^isBodyParam}}
|
||||||
|
|||||||
113
modules/openapi-generator/src/main/resources/php-symfony/testing/ControllerTest.mustache
vendored
Normal file
113
modules/openapi-generator/src/main/resources/php-symfony/testing/ControllerTest.mustache
vendored
Normal file
@@ -0,0 +1,113 @@
|
|||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* ControllerTest
|
||||||
|
* PHP version 5
|
||||||
|
*
|
||||||
|
* @category Class
|
||||||
|
* @package {{controllerTestsPackage}}
|
||||||
|
* @author openapi-generator contributors
|
||||||
|
* @link https://github.com/openapitools/openapi-generator
|
||||||
|
*/
|
||||||
|
|
||||||
|
{{>partial_header}}
|
||||||
|
/**
|
||||||
|
* NOTE: This class is auto generated by the openapi generator program.
|
||||||
|
* https://github.com/openapitools/openapi-generator
|
||||||
|
* Please update the test case below to test the endpoint.
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace {{controllerTestsPackage}};
|
||||||
|
|
||||||
|
use {{controllerPackage}}\Controller;
|
||||||
|
use PHPUnit\Framework\TestCase;
|
||||||
|
use Symfony\Component\HttpFoundation\Request;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* ControllerTest Class Doc Comment
|
||||||
|
*
|
||||||
|
* @category Class
|
||||||
|
* @package {{controllerTestsPackage}}
|
||||||
|
* @author openapi-generator contributors
|
||||||
|
* @link https://github.com/openapitools/openapi-generator
|
||||||
|
* @coversDefaultClass \{{controllerPackage}}\Controller
|
||||||
|
*/
|
||||||
|
class ControllerTest extends TestCase
|
||||||
|
{
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Tests isContentTypeAllowed static method.
|
||||||
|
*
|
||||||
|
* @param string $contentType
|
||||||
|
* @param array $consumes
|
||||||
|
* @param bool $expectedReturn
|
||||||
|
*
|
||||||
|
* @covers ::isContentTypeAllowed
|
||||||
|
* @dataProvider provideArgumentsForIsContentTypeAllowed
|
||||||
|
*/
|
||||||
|
public function testIsContentTypeAllowed($contentType, array $consumes, $expectedReturn)
|
||||||
|
{
|
||||||
|
$request = new Request();
|
||||||
|
$request->headers->set('CONTENT_TYPE', $contentType, true);// last one argument overrides header
|
||||||
|
$this->assertSame(
|
||||||
|
$expectedReturn,
|
||||||
|
Controller::isContentTypeAllowed($request, $consumes),
|
||||||
|
sprintf(
|
||||||
|
'Failed assertion that "Content-Type: %s" %s by [%s] consumes array.',
|
||||||
|
$contentType,
|
||||||
|
($expectedReturn) ? 'is allowed' : 'is forbidden',
|
||||||
|
implode(', ', $consumes)
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function provideArgumentsForIsContentTypeAllowed()
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'usual JSON content type' => [
|
||||||
|
'application/json',
|
||||||
|
['application/json'],
|
||||||
|
true,
|
||||||
|
],
|
||||||
|
'extended content type from PR #6078' => [
|
||||||
|
'application/json; charset=utf-8',
|
||||||
|
['application/json'],
|
||||||
|
true,
|
||||||
|
],
|
||||||
|
'more than one content types' => [
|
||||||
|
'application/json',
|
||||||
|
['application/xml', 'application/json; charset=utf-8'],
|
||||||
|
true,
|
||||||
|
],
|
||||||
|
'empty consumes array' => [
|
||||||
|
'application/json',
|
||||||
|
[],
|
||||||
|
true,
|
||||||
|
],
|
||||||
|
'empty consumes and content type' => [
|
||||||
|
null,
|
||||||
|
[],
|
||||||
|
true,
|
||||||
|
],
|
||||||
|
'consumes everything' => [
|
||||||
|
'application/json',
|
||||||
|
['*/*'],
|
||||||
|
true,
|
||||||
|
],
|
||||||
|
'fancy custom content type' => [
|
||||||
|
'foobar/foobaz',
|
||||||
|
['application/xml', 'foobar/foobaz; charset=utf-8'],
|
||||||
|
true,
|
||||||
|
],
|
||||||
|
'empty content type' => [
|
||||||
|
null,
|
||||||
|
['application/xml', 'application/json; charset=utf-8'],
|
||||||
|
false,
|
||||||
|
],
|
||||||
|
'content type out of consumes' => [
|
||||||
|
'text/html',
|
||||||
|
['application/xml', 'application/json; charset=utf-8'],
|
||||||
|
false,
|
||||||
|
],
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -99,7 +99,7 @@ use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
|
|||||||
$path = str_replace($pattern, $data, $path);
|
$path = str_replace($pattern, $data, $path);
|
||||||
{{/pathParams}}
|
{{/pathParams}}
|
||||||
|
|
||||||
$crawler = $client->request('{{httpMethod}}', $path);
|
$crawler = $client->request('{{httpMethod}}', $path{{#hasBodyParam}}, [], [], ['CONTENT_TYPE' => 'application/json']{{/hasBodyParam}});
|
||||||
}
|
}
|
||||||
{{/operation}}
|
{{/operation}}
|
||||||
|
|
||||||
|
|||||||
@@ -9,12 +9,14 @@
|
|||||||
<testsuite>
|
<testsuite>
|
||||||
<directory>{{apiTestPath}}</directory>
|
<directory>{{apiTestPath}}</directory>
|
||||||
<directory>{{modelTestPath}}</directory>
|
<directory>{{modelTestPath}}</directory>
|
||||||
|
<directory>{{controllerTestPath}}</directory>
|
||||||
</testsuite>
|
</testsuite>
|
||||||
</testsuites>
|
</testsuites>
|
||||||
<filter>
|
<filter>
|
||||||
<whitelist processUncoveredFilesFromWhitelist="true">
|
<whitelist processUncoveredFilesFromWhitelist="true">
|
||||||
<directory suffix=".php">{{apiSrcPath}}</directory>
|
<directory suffix=".php">{{apiSrcPath}}</directory>
|
||||||
<directory suffix=".php">{{modelSrcPath}}</directory>
|
<directory suffix=".php">{{modelSrcPath}}</directory>
|
||||||
|
<directory suffix=".php">{{controllerSrcPath}}</directory>
|
||||||
</whitelist>
|
</whitelist>
|
||||||
</filter>
|
</filter>
|
||||||
<php>
|
<php>
|
||||||
|
|||||||
@@ -30,6 +30,7 @@
|
|||||||
namespace OpenAPI\Server\Controller;
|
namespace OpenAPI\Server\Controller;
|
||||||
|
|
||||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||||
|
use Symfony\Component\HttpFoundation\Request;
|
||||||
use Symfony\Component\HttpFoundation\Response;
|
use Symfony\Component\HttpFoundation\Response;
|
||||||
use Symfony\Component\HttpKernel\Exception\HttpException;
|
use Symfony\Component\HttpKernel\Exception\HttpException;
|
||||||
use OpenAPI\Server\Service\SerializerInterface;
|
use OpenAPI\Server\Service\SerializerInterface;
|
||||||
@@ -186,4 +187,40 @@ class Controller extends AbstractController
|
|||||||
// If we reach this point, we don't have a common ground between server and client
|
// If we reach this point, we don't have a common ground between server and client
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Checks whether Content-Type request header presented in supported formats.
|
||||||
|
*
|
||||||
|
* @param Request $request Request instance.
|
||||||
|
* @param array $consumes Array of supported content types.
|
||||||
|
*
|
||||||
|
* @return bool Returns true if Content-Type supported otherwise false.
|
||||||
|
*/
|
||||||
|
public static function isContentTypeAllowed(Request $request, array $consumes = [])
|
||||||
|
{
|
||||||
|
if (!empty($consumes) && $consumes[0] !== '*/*') {
|
||||||
|
$currentFormat = $request->getContentType();
|
||||||
|
foreach ($consumes as $mimeType) {
|
||||||
|
// canonize mime type
|
||||||
|
if (is_string($mimeType) && false !== $pos = strpos($mimeType, ';')) {
|
||||||
|
$mimeType = trim(substr($mimeType, 0, $pos));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!$format = $request->getFormat($mimeType)) {
|
||||||
|
// add custom format to request
|
||||||
|
$format = $mimeType;
|
||||||
|
$request->setFormat($format, $format);
|
||||||
|
$currentFormat = $request->getContentType();
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($format === $currentFormat) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -62,8 +62,7 @@ class PetController extends Controller
|
|||||||
{
|
{
|
||||||
// Make sure that the client is providing something that we can consume
|
// Make sure that the client is providing something that we can consume
|
||||||
$consumes = ['application/json', 'application/xml'];
|
$consumes = ['application/json', 'application/xml'];
|
||||||
$inputFormat = $request->headers->has('Content-Type')?$request->headers->get('Content-Type'):$consumes[0];
|
if (!static::isContentTypeAllowed($request, $consumes)) {
|
||||||
if (!in_array($inputFormat, $consumes)) {
|
|
||||||
// We can't consume the content that the client is sending us
|
// We can't consume the content that the client is sending us
|
||||||
return new Response('', 415);
|
return new Response('', 415);
|
||||||
}
|
}
|
||||||
@@ -80,6 +79,7 @@ class PetController extends Controller
|
|||||||
|
|
||||||
// Deserialize the input values that needs it
|
// Deserialize the input values that needs it
|
||||||
try {
|
try {
|
||||||
|
$inputFormat = $request->getMimeType($request->getContentType());
|
||||||
$body = $this->deserialize($body, 'OpenAPI\Server\Model\Pet', $inputFormat);
|
$body = $this->deserialize($body, 'OpenAPI\Server\Model\Pet', $inputFormat);
|
||||||
} catch (SerializerRuntimeException $exception) {
|
} catch (SerializerRuntimeException $exception) {
|
||||||
return $this->createBadRequestResponse($exception->getMessage());
|
return $this->createBadRequestResponse($exception->getMessage());
|
||||||
@@ -491,8 +491,7 @@ class PetController extends Controller
|
|||||||
{
|
{
|
||||||
// Make sure that the client is providing something that we can consume
|
// Make sure that the client is providing something that we can consume
|
||||||
$consumes = ['application/json', 'application/xml'];
|
$consumes = ['application/json', 'application/xml'];
|
||||||
$inputFormat = $request->headers->has('Content-Type')?$request->headers->get('Content-Type'):$consumes[0];
|
if (!static::isContentTypeAllowed($request, $consumes)) {
|
||||||
if (!in_array($inputFormat, $consumes)) {
|
|
||||||
// We can't consume the content that the client is sending us
|
// We can't consume the content that the client is sending us
|
||||||
return new Response('', 415);
|
return new Response('', 415);
|
||||||
}
|
}
|
||||||
@@ -509,6 +508,7 @@ class PetController extends Controller
|
|||||||
|
|
||||||
// Deserialize the input values that needs it
|
// Deserialize the input values that needs it
|
||||||
try {
|
try {
|
||||||
|
$inputFormat = $request->getMimeType($request->getContentType());
|
||||||
$body = $this->deserialize($body, 'OpenAPI\Server\Model\Pet', $inputFormat);
|
$body = $this->deserialize($body, 'OpenAPI\Server\Model\Pet', $inputFormat);
|
||||||
} catch (SerializerRuntimeException $exception) {
|
} catch (SerializerRuntimeException $exception) {
|
||||||
return $this->createBadRequestResponse($exception->getMessage());
|
return $this->createBadRequestResponse($exception->getMessage());
|
||||||
|
|||||||
@@ -284,8 +284,7 @@ class StoreController extends Controller
|
|||||||
{
|
{
|
||||||
// Make sure that the client is providing something that we can consume
|
// Make sure that the client is providing something that we can consume
|
||||||
$consumes = [];
|
$consumes = [];
|
||||||
$inputFormat = $request->headers->has('Content-Type')?$request->headers->get('Content-Type'):$consumes[0];
|
if (!static::isContentTypeAllowed($request, $consumes)) {
|
||||||
if (!in_array($inputFormat, $consumes)) {
|
|
||||||
// We can't consume the content that the client is sending us
|
// We can't consume the content that the client is sending us
|
||||||
return new Response('', 415);
|
return new Response('', 415);
|
||||||
}
|
}
|
||||||
@@ -308,6 +307,7 @@ class StoreController extends Controller
|
|||||||
|
|
||||||
// Deserialize the input values that needs it
|
// Deserialize the input values that needs it
|
||||||
try {
|
try {
|
||||||
|
$inputFormat = $request->getMimeType($request->getContentType());
|
||||||
$body = $this->deserialize($body, 'OpenAPI\Server\Model\Order', $inputFormat);
|
$body = $this->deserialize($body, 'OpenAPI\Server\Model\Order', $inputFormat);
|
||||||
} catch (SerializerRuntimeException $exception) {
|
} catch (SerializerRuntimeException $exception) {
|
||||||
return $this->createBadRequestResponse($exception->getMessage());
|
return $this->createBadRequestResponse($exception->getMessage());
|
||||||
|
|||||||
@@ -61,8 +61,7 @@ class UserController extends Controller
|
|||||||
{
|
{
|
||||||
// Make sure that the client is providing something that we can consume
|
// Make sure that the client is providing something that we can consume
|
||||||
$consumes = [];
|
$consumes = [];
|
||||||
$inputFormat = $request->headers->has('Content-Type')?$request->headers->get('Content-Type'):$consumes[0];
|
if (!static::isContentTypeAllowed($request, $consumes)) {
|
||||||
if (!in_array($inputFormat, $consumes)) {
|
|
||||||
// We can't consume the content that the client is sending us
|
// We can't consume the content that the client is sending us
|
||||||
return new Response('', 415);
|
return new Response('', 415);
|
||||||
}
|
}
|
||||||
@@ -76,6 +75,7 @@ class UserController extends Controller
|
|||||||
|
|
||||||
// Deserialize the input values that needs it
|
// Deserialize the input values that needs it
|
||||||
try {
|
try {
|
||||||
|
$inputFormat = $request->getMimeType($request->getContentType());
|
||||||
$body = $this->deserialize($body, 'OpenAPI\Server\Model\User', $inputFormat);
|
$body = $this->deserialize($body, 'OpenAPI\Server\Model\User', $inputFormat);
|
||||||
} catch (SerializerRuntimeException $exception) {
|
} catch (SerializerRuntimeException $exception) {
|
||||||
return $this->createBadRequestResponse($exception->getMessage());
|
return $this->createBadRequestResponse($exception->getMessage());
|
||||||
@@ -138,8 +138,7 @@ class UserController extends Controller
|
|||||||
{
|
{
|
||||||
// Make sure that the client is providing something that we can consume
|
// Make sure that the client is providing something that we can consume
|
||||||
$consumes = [];
|
$consumes = [];
|
||||||
$inputFormat = $request->headers->has('Content-Type')?$request->headers->get('Content-Type'):$consumes[0];
|
if (!static::isContentTypeAllowed($request, $consumes)) {
|
||||||
if (!in_array($inputFormat, $consumes)) {
|
|
||||||
// We can't consume the content that the client is sending us
|
// We can't consume the content that the client is sending us
|
||||||
return new Response('', 415);
|
return new Response('', 415);
|
||||||
}
|
}
|
||||||
@@ -153,6 +152,7 @@ class UserController extends Controller
|
|||||||
|
|
||||||
// Deserialize the input values that needs it
|
// Deserialize the input values that needs it
|
||||||
try {
|
try {
|
||||||
|
$inputFormat = $request->getMimeType($request->getContentType());
|
||||||
$body = $this->deserialize($body, 'array<OpenAPI\Server\Model\User>', $inputFormat);
|
$body = $this->deserialize($body, 'array<OpenAPI\Server\Model\User>', $inputFormat);
|
||||||
} catch (SerializerRuntimeException $exception) {
|
} catch (SerializerRuntimeException $exception) {
|
||||||
return $this->createBadRequestResponse($exception->getMessage());
|
return $this->createBadRequestResponse($exception->getMessage());
|
||||||
@@ -217,8 +217,7 @@ class UserController extends Controller
|
|||||||
{
|
{
|
||||||
// Make sure that the client is providing something that we can consume
|
// Make sure that the client is providing something that we can consume
|
||||||
$consumes = [];
|
$consumes = [];
|
||||||
$inputFormat = $request->headers->has('Content-Type')?$request->headers->get('Content-Type'):$consumes[0];
|
if (!static::isContentTypeAllowed($request, $consumes)) {
|
||||||
if (!in_array($inputFormat, $consumes)) {
|
|
||||||
// We can't consume the content that the client is sending us
|
// We can't consume the content that the client is sending us
|
||||||
return new Response('', 415);
|
return new Response('', 415);
|
||||||
}
|
}
|
||||||
@@ -232,6 +231,7 @@ class UserController extends Controller
|
|||||||
|
|
||||||
// Deserialize the input values that needs it
|
// Deserialize the input values that needs it
|
||||||
try {
|
try {
|
||||||
|
$inputFormat = $request->getMimeType($request->getContentType());
|
||||||
$body = $this->deserialize($body, 'array<OpenAPI\Server\Model\User>', $inputFormat);
|
$body = $this->deserialize($body, 'array<OpenAPI\Server\Model\User>', $inputFormat);
|
||||||
} catch (SerializerRuntimeException $exception) {
|
} catch (SerializerRuntimeException $exception) {
|
||||||
return $this->createBadRequestResponse($exception->getMessage());
|
return $this->createBadRequestResponse($exception->getMessage());
|
||||||
@@ -592,8 +592,7 @@ class UserController extends Controller
|
|||||||
{
|
{
|
||||||
// Make sure that the client is providing something that we can consume
|
// Make sure that the client is providing something that we can consume
|
||||||
$consumes = [];
|
$consumes = [];
|
||||||
$inputFormat = $request->headers->has('Content-Type')?$request->headers->get('Content-Type'):$consumes[0];
|
if (!static::isContentTypeAllowed($request, $consumes)) {
|
||||||
if (!in_array($inputFormat, $consumes)) {
|
|
||||||
// We can't consume the content that the client is sending us
|
// We can't consume the content that the client is sending us
|
||||||
return new Response('', 415);
|
return new Response('', 415);
|
||||||
}
|
}
|
||||||
@@ -608,6 +607,7 @@ class UserController extends Controller
|
|||||||
// Deserialize the input values that needs it
|
// Deserialize the input values that needs it
|
||||||
try {
|
try {
|
||||||
$username = $this->deserialize($username, 'string', 'string');
|
$username = $this->deserialize($username, 'string', 'string');
|
||||||
|
$inputFormat = $request->getMimeType($request->getContentType());
|
||||||
$body = $this->deserialize($body, 'OpenAPI\Server\Model\User', $inputFormat);
|
$body = $this->deserialize($body, 'OpenAPI\Server\Model\User', $inputFormat);
|
||||||
} catch (SerializerRuntimeException $exception) {
|
} catch (SerializerRuntimeException $exception) {
|
||||||
return $this->createBadRequestResponse($exception->getMessage());
|
return $this->createBadRequestResponse($exception->getMessage());
|
||||||
|
|||||||
@@ -85,7 +85,7 @@ class PetApiInterfaceTest extends WebTestCase
|
|||||||
|
|
||||||
$path = '/pet';
|
$path = '/pet';
|
||||||
|
|
||||||
$crawler = $client->request('POST', $path);
|
$crawler = $client->request('POST', $path, [], [], ['CONTENT_TYPE' => 'application/json']);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -166,7 +166,7 @@ class PetApiInterfaceTest extends WebTestCase
|
|||||||
|
|
||||||
$path = '/pet';
|
$path = '/pet';
|
||||||
|
|
||||||
$crawler = $client->request('PUT', $path);
|
$crawler = $client->request('PUT', $path, [], [], ['CONTENT_TYPE' => 'application/json']);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -136,7 +136,7 @@ class StoreApiInterfaceTest extends WebTestCase
|
|||||||
|
|
||||||
$path = '/store/order';
|
$path = '/store/order';
|
||||||
|
|
||||||
$crawler = $client->request('POST', $path);
|
$crawler = $client->request('POST', $path, [], [], ['CONTENT_TYPE' => 'application/json']);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function genTestData($regexp)
|
protected function genTestData($regexp)
|
||||||
|
|||||||
@@ -85,7 +85,7 @@ class UserApiInterfaceTest extends WebTestCase
|
|||||||
|
|
||||||
$path = '/user';
|
$path = '/user';
|
||||||
|
|
||||||
$crawler = $client->request('POST', $path);
|
$crawler = $client->request('POST', $path, [], [], ['CONTENT_TYPE' => 'application/json']);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -100,7 +100,7 @@ class UserApiInterfaceTest extends WebTestCase
|
|||||||
|
|
||||||
$path = '/user/createWithArray';
|
$path = '/user/createWithArray';
|
||||||
|
|
||||||
$crawler = $client->request('POST', $path);
|
$crawler = $client->request('POST', $path, [], [], ['CONTENT_TYPE' => 'application/json']);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -115,7 +115,7 @@ class UserApiInterfaceTest extends WebTestCase
|
|||||||
|
|
||||||
$path = '/user/createWithList';
|
$path = '/user/createWithList';
|
||||||
|
|
||||||
$crawler = $client->request('POST', $path);
|
$crawler = $client->request('POST', $path, [], [], ['CONTENT_TYPE' => 'application/json']);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -199,7 +199,7 @@ class UserApiInterfaceTest extends WebTestCase
|
|||||||
$data = $this->genTestData('[a-z0-9]+');
|
$data = $this->genTestData('[a-z0-9]+');
|
||||||
$path = str_replace($pattern, $data, $path);
|
$path = str_replace($pattern, $data, $path);
|
||||||
|
|
||||||
$crawler = $client->request('PUT', $path);
|
$crawler = $client->request('PUT', $path, [], [], ['CONTENT_TYPE' => 'application/json']);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function genTestData($regexp)
|
protected function genTestData($regexp)
|
||||||
|
|||||||
@@ -0,0 +1,123 @@
|
|||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* ControllerTest
|
||||||
|
* PHP version 5
|
||||||
|
*
|
||||||
|
* @category Class
|
||||||
|
* @package OpenAPI\Server\Tests\Controller
|
||||||
|
* @author openapi-generator contributors
|
||||||
|
* @link https://github.com/openapitools/openapi-generator
|
||||||
|
*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* OpenAPI Petstore
|
||||||
|
*
|
||||||
|
* This is a sample server Petstore server. For this sample, you can use the api key `special-key` to test the authorization filters.
|
||||||
|
*
|
||||||
|
* The version of the OpenAPI document: 1.0.0
|
||||||
|
*
|
||||||
|
* Generated by: https://github.com/openapitools/openapi-generator.git
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* NOTE: This class is auto generated by the openapi generator program.
|
||||||
|
* https://github.com/openapitools/openapi-generator
|
||||||
|
* Please update the test case below to test the endpoint.
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace OpenAPI\Server\Tests\Controller;
|
||||||
|
|
||||||
|
use OpenAPI\Server\Controller\Controller;
|
||||||
|
use PHPUnit\Framework\TestCase;
|
||||||
|
use Symfony\Component\HttpFoundation\Request;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* ControllerTest Class Doc Comment
|
||||||
|
*
|
||||||
|
* @category Class
|
||||||
|
* @package OpenAPI\Server\Tests\Controller
|
||||||
|
* @author openapi-generator contributors
|
||||||
|
* @link https://github.com/openapitools/openapi-generator
|
||||||
|
* @coversDefaultClass \OpenAPI\Server\Controller\Controller
|
||||||
|
*/
|
||||||
|
class ControllerTest extends TestCase
|
||||||
|
{
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Tests isContentTypeAllowed static method.
|
||||||
|
*
|
||||||
|
* @param string $contentType
|
||||||
|
* @param array $consumes
|
||||||
|
* @param bool $expectedReturn
|
||||||
|
*
|
||||||
|
* @covers ::isContentTypeAllowed
|
||||||
|
* @dataProvider provideArgumentsForIsContentTypeAllowed
|
||||||
|
*/
|
||||||
|
public function testIsContentTypeAllowed($contentType, array $consumes, $expectedReturn)
|
||||||
|
{
|
||||||
|
$request = new Request();
|
||||||
|
$request->headers->set('CONTENT_TYPE', $contentType, true);// last one argument overrides header
|
||||||
|
$this->assertSame(
|
||||||
|
$expectedReturn,
|
||||||
|
Controller::isContentTypeAllowed($request, $consumes),
|
||||||
|
sprintf(
|
||||||
|
'Failed assertion that "Content-Type: %s" %s by [%s] consumes array.',
|
||||||
|
$contentType,
|
||||||
|
($expectedReturn) ? 'is allowed' : 'is forbidden',
|
||||||
|
implode(', ', $consumes)
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function provideArgumentsForIsContentTypeAllowed()
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'usual JSON content type' => [
|
||||||
|
'application/json',
|
||||||
|
['application/json'],
|
||||||
|
true,
|
||||||
|
],
|
||||||
|
'extended content type from PR #6078' => [
|
||||||
|
'application/json; charset=utf-8',
|
||||||
|
['application/json'],
|
||||||
|
true,
|
||||||
|
],
|
||||||
|
'more than one content types' => [
|
||||||
|
'application/json',
|
||||||
|
['application/xml', 'application/json; charset=utf-8'],
|
||||||
|
true,
|
||||||
|
],
|
||||||
|
'empty consumes array' => [
|
||||||
|
'application/json',
|
||||||
|
[],
|
||||||
|
true,
|
||||||
|
],
|
||||||
|
'empty consumes and content type' => [
|
||||||
|
null,
|
||||||
|
[],
|
||||||
|
true,
|
||||||
|
],
|
||||||
|
'consumes everything' => [
|
||||||
|
'application/json',
|
||||||
|
['*/*'],
|
||||||
|
true,
|
||||||
|
],
|
||||||
|
'fancy custom content type' => [
|
||||||
|
'foobar/foobaz',
|
||||||
|
['application/xml', 'foobar/foobaz; charset=utf-8'],
|
||||||
|
true,
|
||||||
|
],
|
||||||
|
'empty content type' => [
|
||||||
|
null,
|
||||||
|
['application/xml', 'application/json; charset=utf-8'],
|
||||||
|
false,
|
||||||
|
],
|
||||||
|
'content type out of consumes' => [
|
||||||
|
'text/html',
|
||||||
|
['application/xml', 'application/json; charset=utf-8'],
|
||||||
|
false,
|
||||||
|
],
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -9,12 +9,14 @@
|
|||||||
<testsuite>
|
<testsuite>
|
||||||
<directory>./Tests/Api</directory>
|
<directory>./Tests/Api</directory>
|
||||||
<directory>./Tests/Model</directory>
|
<directory>./Tests/Model</directory>
|
||||||
|
<directory>./Tests/Controller</directory>
|
||||||
</testsuite>
|
</testsuite>
|
||||||
</testsuites>
|
</testsuites>
|
||||||
<filter>
|
<filter>
|
||||||
<whitelist processUncoveredFilesFromWhitelist="true">
|
<whitelist processUncoveredFilesFromWhitelist="true">
|
||||||
<directory suffix=".php">././Api</directory>
|
<directory suffix=".php">././Api</directory>
|
||||||
<directory suffix=".php">././Model</directory>
|
<directory suffix=".php">././Model</directory>
|
||||||
|
<directory suffix=".php">././Controller</directory>
|
||||||
</whitelist>
|
</whitelist>
|
||||||
</filter>
|
</filter>
|
||||||
<php>
|
<php>
|
||||||
|
|||||||
Reference in New Issue
Block a user