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

[php-symfony] Allow Symfony 7 / Remove EOL PHP / Bearer Fix #18357

Merged
merged 1 commit into from
Apr 13, 2024
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 @@ -92,7 +92,7 @@ class Controller extends AbstractController
$json = $this->exceptionToArray($exception);
$json['statusCode'] = $statusCode;
return new Response(json_encode($json, 15, 512), $statusCode, $headers);
return new Response(json_encode($json, 15), $statusCode, $headers);
}

/**
Expand Down Expand Up @@ -219,7 +219,7 @@ class Controller extends AbstractController
public static function isContentTypeAllowed(Request $request, array $consumes = []): bool
{
if (!empty($consumes) && $consumes[0] !== '*/*') {
$currentFormat = $request->getContentType();
$currentFormat = $request->getContentTypeFormat();
foreach ($consumes as $mimeType) {
// canonize mime type
if (is_string($mimeType) && false !== $pos = strpos($mimeType, ';')) {
Expand All @@ -230,7 +230,7 @@ class Controller extends AbstractController
// add custom format to request
$format = $mimeType;
$request->setFormat($format, $format);
$currentFormat = $request->getContentType();
$currentFormat = $request->getContentTypeFormat();
}

if ($format === $currentFormat) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,10 @@ class {{controllerName}} extends Controller
// HTTP basic authentication required
$security{{name}} = $request->headers->get('authorization');
{{/isBasicBasic}}
{{#isBasicBearer}}
// HTTP bearer authentication required
$security{{name}} = $request->headers->get('authorization');
{{/isBasicBearer}}
{{#isOAuth}}
// Oauth required
$security{{name}} = $request->headers->get('authorization');
Expand Down Expand Up @@ -148,7 +152,7 @@ class {{controllerName}} extends Controller
{{#allParams}}
{{^isFile}}
{{#isBodyParam}}
$inputFormat = $request->getMimeType($request->getContentType());
$inputFormat = $request->getMimeType($request->getContentTypeFormat());
${{paramName}} = $this->deserialize(${{paramName}}, '{{#isContainer}}{{#items}}array<{{dataType}}>{{/items}}{{/isContainer}}{{^isContainer}}{{#isEnumRef}}\{{modelPackage}}\{{dataType}}{{/isEnumRef}}{{^isEnumRef}}{{dataType}}{{/isEnumRef}}{{/isContainer}}', $inputFormat);
{{/isBodyParam}}
{{^isBodyParam}}
Expand Down Expand Up @@ -178,17 +182,12 @@ class {{controllerName}} extends Controller

{{#returnType}}$result = {{/returnType}}$handler->{{operationId}}({{#allParams}}${{paramName}}, {{/allParams}}$responseCode, $responseHeaders);

// Find default response message
$message = '{{#responses}}{{#isDefault}}{{message}}{{/isDefault}}{{/responses}}';

// Find a more specific message, if available
switch ($responseCode) {
$message = match($responseCode) {
{{#responses}}
case {{code}}:
$message = '{{message}}';
break;
{{code}} => '{{message}}',
{{/responses}}
}
default => '{{#responses}}{{#isDefault}}{{message}}{{/isDefault}}{{/responses}}',
};

return new Response(
{{#returnType}}$result !== null ?$this->serialize($result, $responseFormat):''{{/returnType}}{{^returnType}}''{{/returnType}},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,19 @@
}
],
"require": {
"php": ">=7.4.0|>=8.0.2",
"php": ">=8.1",
"ext-curl": "*",
"ext-json": "*",
"ext-mbstring": "*",
"symfony/validator": "^5.0|^6.0",
"jms/serializer-bundle": "^4.0",
"symfony/framework-bundle": "^5.0|^6.0"
"symfony/validator": "^6.4|^7.0",
"jms/serializer-bundle": "^5.4",
"symfony/framework-bundle": "^6.4|^7.0"
},
"require-dev": {
"phpunit/phpunit": "^9.5",
"friendsofphp/php-cs-fixer": "^2.16.3",
"symfony/browser-kit": "^5.0|^6.0",
"symfony/yaml": "^5.0|^6.0",
"phpunit/phpunit": "^10.5|^11.0",
"friendsofphp/php-cs-fixer": "*",
"symfony/browser-kit": "^6.4|7.0",
"symfony/yaml": "^6.4|^7.0",
"hoa/regex": "~1.0"
},
"autoload": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,11 @@ class JmsSerializer implements SerializerInterface

private function convertFormat(string $format): ?string
{
switch ($format) {
case 'application/json':
return 'json';
case 'application/xml':
return 'xml';
}

return null;
return match($format) {
'application/json' => 'json',
'application/xml' => 'xml',
default => null,
};
}

private function deserializeString($data, string $type)
Expand Down Expand Up @@ -132,22 +129,13 @@ class JmsSerializer implements SerializerInterface
}

// Parse the string using the correct separator
switch ($format) {
case 'csv':
$data = explode(',', $data);
break;
case 'ssv':
$data = explode(' ', $data);
break;
case 'tsv':
$data = explode("\t", $data);
break;
case 'pipes':
$data = explode('|', $data);
break;
default;
$data = [];
}
$data = match($format) {
'csv' => explode(',', $data),
'ssv' => explode(' ', $data),
'tsv' => explode("\t", $data),
'pipes' => explode('|', $data),
default => [],
};

// Deserialize each of the array elements
foreach ($data as $key => $item) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class ControllerTest extends TestCase
);
}

public function dataProviderIsContentTypeAllowed(): array
public static function dataProviderIsContentTypeAllowed(): array
{
return [
'usual JSON content type' => [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public function createErrorResponse(HttpException $exception): Response
$json = $this->exceptionToArray($exception);
$json['statusCode'] = $statusCode;

return new Response(json_encode($json, 15, 512), $statusCode, $headers);
return new Response(json_encode($json, 15), $statusCode, $headers);
}

/**
Expand Down Expand Up @@ -229,7 +229,7 @@ protected function getOutputFormat(string $accept, array $produced): ?string
public static function isContentTypeAllowed(Request $request, array $consumes = []): bool
{
if (!empty($consumes) && $consumes[0] !== '*/*') {
$currentFormat = $request->getContentType();
$currentFormat = $request->getContentTypeFormat();
foreach ($consumes as $mimeType) {
// canonize mime type
if (is_string($mimeType) && false !== $pos = strpos($mimeType, ';')) {
Expand All @@ -240,7 +240,7 @@ public static function isContentTypeAllowed(Request $request, array $consumes =
// add custom format to request
$format = $mimeType;
$request->setFormat($format, $format);
$currentFormat = $request->getContentType();
$currentFormat = $request->getContentTypeFormat();
}

if ($format === $currentFormat) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public function addPetAction(Request $request)

// Deserialize the input values that needs it
try {
$inputFormat = $request->getMimeType($request->getContentType());
$inputFormat = $request->getMimeType($request->getContentTypeFormat());
$pet = $this->deserialize($pet, 'OpenAPI\Server\Model\Pet', $inputFormat);
} catch (SerializerRuntimeException $exception) {
return $this->createBadRequestResponse($exception->getMessage());
Expand Down Expand Up @@ -117,18 +117,11 @@ public function addPetAction(Request $request)

$result = $handler->addPet($pet, $responseCode, $responseHeaders);

// Find default response message
$message = '';

// Find a more specific message, if available
switch ($responseCode) {
case 200:
$message = 'successful operation';
break;
case 405:
$message = 'Invalid input';
break;
}
$message = match($responseCode) {
200 => 'successful operation',
405 => 'Invalid input',
default => '',
};

return new Response(
$result !== null ?$this->serialize($result, $responseFormat):'',
Expand Down Expand Up @@ -202,15 +195,10 @@ public function deletePetAction(Request $request, $petId)

$handler->deletePet($petId, $apiKey, $responseCode, $responseHeaders);

// Find default response message
$message = '';

// Find a more specific message, if available
switch ($responseCode) {
case 400:
$message = 'Invalid pet value';
break;
}
$message = match($responseCode) {
400 => 'Invalid pet value',
default => '',
};

return new Response(
'',
Expand Down Expand Up @@ -291,18 +279,11 @@ public function findPetsByStatusAction(Request $request)

$result = $handler->findPetsByStatus($status, $responseCode, $responseHeaders);

// Find default response message
$message = '';

// Find a more specific message, if available
switch ($responseCode) {
case 200:
$message = 'successful operation';
break;
case 400:
$message = 'Invalid status value';
break;
}
$message = match($responseCode) {
200 => 'successful operation',
400 => 'Invalid status value',
default => '',
};

return new Response(
$result !== null ?$this->serialize($result, $responseFormat):'',
Expand Down Expand Up @@ -382,18 +363,11 @@ public function findPetsByTagsAction(Request $request)

$result = $handler->findPetsByTags($tags, $responseCode, $responseHeaders);

// Find default response message
$message = '';

// Find a more specific message, if available
switch ($responseCode) {
case 200:
$message = 'successful operation';
break;
case 400:
$message = 'Invalid tag value';
break;
}
$message = match($responseCode) {
200 => 'successful operation',
400 => 'Invalid tag value',
default => '',
};

return new Response(
$result !== null ?$this->serialize($result, $responseFormat):'',
Expand Down Expand Up @@ -468,21 +442,12 @@ public function getPetByIdAction(Request $request, $petId)

$result = $handler->getPetById($petId, $responseCode, $responseHeaders);

// Find default response message
$message = '';

// Find a more specific message, if available
switch ($responseCode) {
case 200:
$message = 'successful operation';
break;
case 400:
$message = 'Invalid ID supplied';
break;
case 404:
$message = 'Pet not found';
break;
}
$message = match($responseCode) {
200 => 'successful operation',
400 => 'Invalid ID supplied',
404 => 'Pet not found',
default => '',
};

return new Response(
$result !== null ?$this->serialize($result, $responseFormat):'',
Expand Down Expand Up @@ -538,7 +503,7 @@ public function updatePetAction(Request $request)

// Deserialize the input values that needs it
try {
$inputFormat = $request->getMimeType($request->getContentType());
$inputFormat = $request->getMimeType($request->getContentTypeFormat());
$pet = $this->deserialize($pet, 'OpenAPI\Server\Model\Pet', $inputFormat);
} catch (SerializerRuntimeException $exception) {
return $this->createBadRequestResponse($exception->getMessage());
Expand Down Expand Up @@ -567,24 +532,13 @@ public function updatePetAction(Request $request)

$result = $handler->updatePet($pet, $responseCode, $responseHeaders);

// Find default response message
$message = '';

// Find a more specific message, if available
switch ($responseCode) {
case 200:
$message = 'successful operation';
break;
case 400:
$message = 'Invalid ID supplied';
break;
case 404:
$message = 'Pet not found';
break;
case 405:
$message = 'Validation exception';
break;
}
$message = match($responseCode) {
200 => 'successful operation',
400 => 'Invalid ID supplied',
404 => 'Pet not found',
405 => 'Validation exception',
default => '',
};

return new Response(
$result !== null ?$this->serialize($result, $responseFormat):'',
Expand Down Expand Up @@ -666,15 +620,10 @@ public function updatePetWithFormAction(Request $request, $petId)

$handler->updatePetWithForm($petId, $name, $status, $responseCode, $responseHeaders);

// Find default response message
$message = '';

// Find a more specific message, if available
switch ($responseCode) {
case 405:
$message = 'Invalid input';
break;
}
$message = match($responseCode) {
405 => 'Invalid input',
default => '',
};

return new Response(
'',
Expand Down Expand Up @@ -763,15 +712,10 @@ public function uploadFileAction(Request $request, $petId)

$result = $handler->uploadFile($petId, $additionalMetadata, $file, $responseCode, $responseHeaders);

// Find default response message
$message = '';

// Find a more specific message, if available
switch ($responseCode) {
case 200:
$message = 'successful operation';
break;
}
$message = match($responseCode) {
200 => 'successful operation',
default => '',
};

return new Response(
$result !== null ?$this->serialize($result, $responseFormat):'',
Expand Down
Loading
Loading