forked from loafle/openapi-generator-original
Rewrite the PHP Laravel generator (#20526)
* remove legacy laravel generator * initial setup of my vision for the laravel generator * update the php laravel samples * update php laravel docs * moved api validation into controller and handle edge cases presented by sample generation * updated samples * added php-laravel to github workflow php8 and removed php7 workflow as it only contained old laravel * preemptive work to support union types as soon as php serde supports them * updated samples * update templates in accordance to samples output * fix pipelines and update samples * correct serde version * fixed phpunit execution and updated samples * added named routes * remove * readd samples --------- Co-authored-by: gijs.blanken@futureof.finance <gijs.blanken@finly.nl> Co-authored-by: William Cheng <wing328hk@gmail.com>
This commit is contained in:
@@ -0,0 +1,84 @@
|
||||
<?php declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* OpenAPI Petstore
|
||||
* This spec is mainly for testing Petstore server and contains fake endpoints, models. Please do not use this for any other purpose. Special characters: \" \\
|
||||
* PHP version 8.1
|
||||
*
|
||||
* The version of the OpenAPI document: 1.0.0
|
||||
*
|
||||
*
|
||||
* NOTE: This class is auto generated by OpenAPI-Generator
|
||||
* https://openapi-generator.tech
|
||||
* Do not edit the class manually.
|
||||
*
|
||||
* Source files are located at:
|
||||
*
|
||||
* > https://github.com/OpenAPITools/openapi-generator/blob/master/modules/openapi-generator/src/main/resources/php-laravel/
|
||||
*/
|
||||
|
||||
|
||||
namespace OpenAPI\Server\Http\Controllers;
|
||||
|
||||
use Crell\Serde\SerdeCommon;
|
||||
use Illuminate\Routing\Controller;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Validator;
|
||||
|
||||
|
||||
use OpenAPI\Server\Api\AnotherFakeApiInterface;
|
||||
|
||||
class AnotherFakeController extends Controller
|
||||
{
|
||||
/**
|
||||
* Constructor
|
||||
*/
|
||||
public function __construct(
|
||||
private readonly AnotherFakeApiInterface $api,
|
||||
private readonly SerdeCommon $serde = new SerdeCommon(),
|
||||
)
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* Operation call123TestSpecialTags
|
||||
*
|
||||
* To test special tags.
|
||||
*
|
||||
*/
|
||||
public function call123TestSpecialTags(Request $request): JsonResponse
|
||||
{
|
||||
$validator = Validator::make(
|
||||
array_merge(
|
||||
[
|
||||
|
||||
],
|
||||
$request->all(),
|
||||
),
|
||||
[
|
||||
],
|
||||
);
|
||||
|
||||
if ($validator->fails()) {
|
||||
return response()->json(['error' => 'Invalid input'], 400);
|
||||
}
|
||||
|
||||
$client = $this->serde->deserialize($request->getContent(), from: 'json', to: \OpenAPI\Server\Model\Client::class);
|
||||
|
||||
try {
|
||||
$apiResult = $this->api->call123TestSpecialTags($client);
|
||||
} catch (\Exception $exception) {
|
||||
// This shouldn't happen
|
||||
return response()->json(['error' => $exception->getMessage()], 500);
|
||||
}
|
||||
|
||||
if ($apiResult instanceof \OpenAPI\Server\Model\Client) {
|
||||
return response()->json($this->serde->serialize($apiResult, format: 'array'), 200);
|
||||
}
|
||||
|
||||
|
||||
// This shouldn't happen
|
||||
return response()->abort(500);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,82 @@
|
||||
<?php declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* OpenAPI Petstore
|
||||
* This spec is mainly for testing Petstore server and contains fake endpoints, models. Please do not use this for any other purpose. Special characters: \" \\
|
||||
* PHP version 8.1
|
||||
*
|
||||
* The version of the OpenAPI document: 1.0.0
|
||||
*
|
||||
*
|
||||
* NOTE: This class is auto generated by OpenAPI-Generator
|
||||
* https://openapi-generator.tech
|
||||
* Do not edit the class manually.
|
||||
*
|
||||
* Source files are located at:
|
||||
*
|
||||
* > https://github.com/OpenAPITools/openapi-generator/blob/master/modules/openapi-generator/src/main/resources/php-laravel/
|
||||
*/
|
||||
|
||||
|
||||
namespace OpenAPI\Server\Http\Controllers;
|
||||
|
||||
use Crell\Serde\SerdeCommon;
|
||||
use Illuminate\Routing\Controller;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Validator;
|
||||
|
||||
|
||||
use OpenAPI\Server\Api\DefaultApiInterface;
|
||||
|
||||
class DefaultController extends Controller
|
||||
{
|
||||
/**
|
||||
* Constructor
|
||||
*/
|
||||
public function __construct(
|
||||
private readonly DefaultApiInterface $api,
|
||||
private readonly SerdeCommon $serde = new SerdeCommon(),
|
||||
)
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* Operation fooGet
|
||||
*
|
||||
* .
|
||||
*
|
||||
*/
|
||||
public function fooGet(Request $request): JsonResponse
|
||||
{
|
||||
$validator = Validator::make(
|
||||
array_merge(
|
||||
[
|
||||
|
||||
],
|
||||
$request->all(),
|
||||
),
|
||||
[
|
||||
],
|
||||
);
|
||||
|
||||
if ($validator->fails()) {
|
||||
return response()->json(['error' => 'Invalid input'], 400);
|
||||
}
|
||||
|
||||
try {
|
||||
$apiResult = $this->api->fooGet();
|
||||
} catch (\Exception $exception) {
|
||||
// This shouldn't happen
|
||||
return response()->json(['error' => $exception->getMessage()], 500);
|
||||
}
|
||||
|
||||
if ($apiResult instanceof \OpenAPI\Server\Model\FooGetDefaultResponse) {
|
||||
return response()->json($this->serde->serialize($apiResult, format: 'array'), 0);
|
||||
}
|
||||
|
||||
|
||||
// This shouldn't happen
|
||||
return response()->abort(500);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,84 @@
|
||||
<?php declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* OpenAPI Petstore
|
||||
* This spec is mainly for testing Petstore server and contains fake endpoints, models. Please do not use this for any other purpose. Special characters: \" \\
|
||||
* PHP version 8.1
|
||||
*
|
||||
* The version of the OpenAPI document: 1.0.0
|
||||
*
|
||||
*
|
||||
* NOTE: This class is auto generated by OpenAPI-Generator
|
||||
* https://openapi-generator.tech
|
||||
* Do not edit the class manually.
|
||||
*
|
||||
* Source files are located at:
|
||||
*
|
||||
* > https://github.com/OpenAPITools/openapi-generator/blob/master/modules/openapi-generator/src/main/resources/php-laravel/
|
||||
*/
|
||||
|
||||
|
||||
namespace OpenAPI\Server\Http\Controllers;
|
||||
|
||||
use Crell\Serde\SerdeCommon;
|
||||
use Illuminate\Routing\Controller;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Validator;
|
||||
|
||||
|
||||
use OpenAPI\Server\Api\FakeClassnameTags123ApiInterface;
|
||||
|
||||
class FakeClassnameTags123Controller extends Controller
|
||||
{
|
||||
/**
|
||||
* Constructor
|
||||
*/
|
||||
public function __construct(
|
||||
private readonly FakeClassnameTags123ApiInterface $api,
|
||||
private readonly SerdeCommon $serde = new SerdeCommon(),
|
||||
)
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* Operation testClassname
|
||||
*
|
||||
* To test class name in snake case.
|
||||
*
|
||||
*/
|
||||
public function testClassname(Request $request): JsonResponse
|
||||
{
|
||||
$validator = Validator::make(
|
||||
array_merge(
|
||||
[
|
||||
|
||||
],
|
||||
$request->all(),
|
||||
),
|
||||
[
|
||||
],
|
||||
);
|
||||
|
||||
if ($validator->fails()) {
|
||||
return response()->json(['error' => 'Invalid input'], 400);
|
||||
}
|
||||
|
||||
$client = $this->serde->deserialize($request->getContent(), from: 'json', to: \OpenAPI\Server\Model\Client::class);
|
||||
|
||||
try {
|
||||
$apiResult = $this->api->testClassname($client);
|
||||
} catch (\Exception $exception) {
|
||||
// This shouldn't happen
|
||||
return response()->json(['error' => $exception->getMessage()], 500);
|
||||
}
|
||||
|
||||
if ($apiResult instanceof \OpenAPI\Server\Model\Client) {
|
||||
return response()->json($this->serde->serialize($apiResult, format: 'array'), 200);
|
||||
}
|
||||
|
||||
|
||||
// This shouldn't happen
|
||||
return response()->abort(500);
|
||||
}
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,506 @@
|
||||
<?php declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* OpenAPI Petstore
|
||||
* This spec is mainly for testing Petstore server and contains fake endpoints, models. Please do not use this for any other purpose. Special characters: \" \\
|
||||
* PHP version 8.1
|
||||
*
|
||||
* The version of the OpenAPI document: 1.0.0
|
||||
*
|
||||
*
|
||||
* NOTE: This class is auto generated by OpenAPI-Generator
|
||||
* https://openapi-generator.tech
|
||||
* Do not edit the class manually.
|
||||
*
|
||||
* Source files are located at:
|
||||
*
|
||||
* > https://github.com/OpenAPITools/openapi-generator/blob/master/modules/openapi-generator/src/main/resources/php-laravel/
|
||||
*/
|
||||
|
||||
|
||||
namespace OpenAPI\Server\Http\Controllers;
|
||||
|
||||
use Crell\Serde\SerdeCommon;
|
||||
use Illuminate\Routing\Controller;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Validator;
|
||||
|
||||
|
||||
use OpenAPI\Server\Api\PetApiInterface;
|
||||
|
||||
class PetController extends Controller
|
||||
{
|
||||
/**
|
||||
* Constructor
|
||||
*/
|
||||
public function __construct(
|
||||
private readonly PetApiInterface $api,
|
||||
private readonly SerdeCommon $serde = new SerdeCommon(),
|
||||
)
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* Operation addPet
|
||||
*
|
||||
* Add a new pet to the store.
|
||||
*
|
||||
*/
|
||||
public function addPet(Request $request): JsonResponse
|
||||
{
|
||||
$validator = Validator::make(
|
||||
array_merge(
|
||||
[
|
||||
|
||||
],
|
||||
$request->all(),
|
||||
),
|
||||
[
|
||||
],
|
||||
);
|
||||
|
||||
if ($validator->fails()) {
|
||||
return response()->json(['error' => 'Invalid input'], 400);
|
||||
}
|
||||
|
||||
$pet = $this->serde->deserialize($request->getContent(), from: 'json', to: \OpenAPI\Server\Model\Pet::class);
|
||||
|
||||
try {
|
||||
$apiResult = $this->api->addPet($pet);
|
||||
} catch (\Exception $exception) {
|
||||
// This shouldn't happen
|
||||
return response()->json(['error' => $exception->getMessage()], 500);
|
||||
}
|
||||
|
||||
if ($apiResult instanceof \OpenAPI\Server\Model\NoContent200) {
|
||||
return response()->json($this->serde->serialize($apiResult, format: 'array'), 200);
|
||||
}
|
||||
|
||||
if ($apiResult instanceof \OpenAPI\Server\Model\NoContent405) {
|
||||
return response()->json($this->serde->serialize($apiResult, format: 'array'), 405);
|
||||
}
|
||||
|
||||
|
||||
// This shouldn't happen
|
||||
return response()->abort(500);
|
||||
}
|
||||
/**
|
||||
* Operation deletePet
|
||||
*
|
||||
* Deletes a pet.
|
||||
*
|
||||
*/
|
||||
public function deletePet(Request $request, int $petId): JsonResponse
|
||||
{
|
||||
$validator = Validator::make(
|
||||
array_merge(
|
||||
[
|
||||
'petId' => $petId,
|
||||
],
|
||||
$request->all(),
|
||||
),
|
||||
[
|
||||
'petId' => [
|
||||
'required',
|
||||
'integer',
|
||||
],
|
||||
'apiKey' => [
|
||||
'string',
|
||||
],
|
||||
],
|
||||
);
|
||||
|
||||
if ($validator->fails()) {
|
||||
return response()->json(['error' => 'Invalid input'], 400);
|
||||
}
|
||||
|
||||
|
||||
$apiKey = $request->string('apiKey')->value();
|
||||
|
||||
try {
|
||||
$apiResult = $this->api->deletePet($petId, $apiKey);
|
||||
} catch (\Exception $exception) {
|
||||
// This shouldn't happen
|
||||
return response()->json(['error' => $exception->getMessage()], 500);
|
||||
}
|
||||
|
||||
if ($apiResult instanceof \OpenAPI\Server\Model\NoContent200) {
|
||||
return response()->json($this->serde->serialize($apiResult, format: 'array'), 200);
|
||||
}
|
||||
|
||||
if ($apiResult instanceof \OpenAPI\Server\Model\NoContent400) {
|
||||
return response()->json($this->serde->serialize($apiResult, format: 'array'), 400);
|
||||
}
|
||||
|
||||
|
||||
// This shouldn't happen
|
||||
return response()->abort(500);
|
||||
}
|
||||
/**
|
||||
* Operation findPetsByStatus
|
||||
*
|
||||
* Finds Pets by status.
|
||||
*
|
||||
*/
|
||||
public function findPetsByStatus(Request $request): JsonResponse
|
||||
{
|
||||
$validator = Validator::make(
|
||||
array_merge(
|
||||
[
|
||||
|
||||
],
|
||||
$request->all(),
|
||||
),
|
||||
[
|
||||
'status' => [
|
||||
'required',
|
||||
'array',
|
||||
],
|
||||
],
|
||||
);
|
||||
|
||||
if ($validator->fails()) {
|
||||
return response()->json(['error' => 'Invalid input'], 400);
|
||||
}
|
||||
|
||||
$status = $request->get('status');
|
||||
|
||||
try {
|
||||
$apiResult = $this->api->findPetsByStatus($status);
|
||||
} catch (\Exception $exception) {
|
||||
// This shouldn't happen
|
||||
return response()->json(['error' => $exception->getMessage()], 500);
|
||||
}
|
||||
|
||||
if (is_array($apiResult)) {
|
||||
$serialized = array_map(fn ($item) => $this->serde->serialize($item, format: 'array'), $apiResult);
|
||||
return response()->json($serialized, 200);
|
||||
}
|
||||
|
||||
if ($apiResult instanceof \OpenAPI\Server\Model\NoContent400) {
|
||||
return response()->json($this->serde->serialize($apiResult, format: 'array'), 400);
|
||||
}
|
||||
|
||||
|
||||
// This shouldn't happen
|
||||
return response()->abort(500);
|
||||
}
|
||||
/**
|
||||
* Operation findPetsByTags
|
||||
*
|
||||
* Finds Pets by tags.
|
||||
*
|
||||
* @deprecated
|
||||
*/
|
||||
public function findPetsByTags(Request $request): JsonResponse
|
||||
{
|
||||
$validator = Validator::make(
|
||||
array_merge(
|
||||
[
|
||||
|
||||
],
|
||||
$request->all(),
|
||||
),
|
||||
[
|
||||
'tags' => [
|
||||
'required',
|
||||
'array',
|
||||
],
|
||||
],
|
||||
);
|
||||
|
||||
if ($validator->fails()) {
|
||||
return response()->json(['error' => 'Invalid input'], 400);
|
||||
}
|
||||
|
||||
$tags = $request->get('tags');
|
||||
|
||||
try {
|
||||
$apiResult = $this->api->findPetsByTags($tags);
|
||||
} catch (\Exception $exception) {
|
||||
// This shouldn't happen
|
||||
return response()->json(['error' => $exception->getMessage()], 500);
|
||||
}
|
||||
|
||||
if (is_array($apiResult)) {
|
||||
$serialized = array_map(fn ($item) => $this->serde->serialize($item, format: 'array'), $apiResult);
|
||||
return response()->json($serialized, 200);
|
||||
}
|
||||
|
||||
if ($apiResult instanceof \OpenAPI\Server\Model\NoContent400) {
|
||||
return response()->json($this->serde->serialize($apiResult, format: 'array'), 400);
|
||||
}
|
||||
|
||||
|
||||
// This shouldn't happen
|
||||
return response()->abort(500);
|
||||
}
|
||||
/**
|
||||
* Operation getPetById
|
||||
*
|
||||
* Find pet by ID.
|
||||
*
|
||||
*/
|
||||
public function getPetById(Request $request, int $petId): JsonResponse
|
||||
{
|
||||
$validator = Validator::make(
|
||||
array_merge(
|
||||
[
|
||||
'petId' => $petId,
|
||||
],
|
||||
$request->all(),
|
||||
),
|
||||
[
|
||||
'petId' => [
|
||||
'required',
|
||||
'integer',
|
||||
],
|
||||
],
|
||||
);
|
||||
|
||||
if ($validator->fails()) {
|
||||
return response()->json(['error' => 'Invalid input'], 400);
|
||||
}
|
||||
|
||||
|
||||
try {
|
||||
$apiResult = $this->api->getPetById($petId);
|
||||
} catch (\Exception $exception) {
|
||||
// This shouldn't happen
|
||||
return response()->json(['error' => $exception->getMessage()], 500);
|
||||
}
|
||||
|
||||
if ($apiResult instanceof \OpenAPI\Server\Model\Pet) {
|
||||
return response()->json($this->serde->serialize($apiResult, format: 'array'), 200);
|
||||
}
|
||||
|
||||
if ($apiResult instanceof \OpenAPI\Server\Model\NoContent400) {
|
||||
return response()->json($this->serde->serialize($apiResult, format: 'array'), 400);
|
||||
}
|
||||
|
||||
if ($apiResult instanceof \OpenAPI\Server\Model\NoContent404) {
|
||||
return response()->json($this->serde->serialize($apiResult, format: 'array'), 404);
|
||||
}
|
||||
|
||||
|
||||
// This shouldn't happen
|
||||
return response()->abort(500);
|
||||
}
|
||||
/**
|
||||
* Operation updatePet
|
||||
*
|
||||
* Update an existing pet.
|
||||
*
|
||||
*/
|
||||
public function updatePet(Request $request): JsonResponse
|
||||
{
|
||||
$validator = Validator::make(
|
||||
array_merge(
|
||||
[
|
||||
|
||||
],
|
||||
$request->all(),
|
||||
),
|
||||
[
|
||||
],
|
||||
);
|
||||
|
||||
if ($validator->fails()) {
|
||||
return response()->json(['error' => 'Invalid input'], 400);
|
||||
}
|
||||
|
||||
$pet = $this->serde->deserialize($request->getContent(), from: 'json', to: \OpenAPI\Server\Model\Pet::class);
|
||||
|
||||
try {
|
||||
$apiResult = $this->api->updatePet($pet);
|
||||
} catch (\Exception $exception) {
|
||||
// This shouldn't happen
|
||||
return response()->json(['error' => $exception->getMessage()], 500);
|
||||
}
|
||||
|
||||
if ($apiResult instanceof \OpenAPI\Server\Model\NoContent200) {
|
||||
return response()->json($this->serde->serialize($apiResult, format: 'array'), 200);
|
||||
}
|
||||
|
||||
if ($apiResult instanceof \OpenAPI\Server\Model\NoContent400) {
|
||||
return response()->json($this->serde->serialize($apiResult, format: 'array'), 400);
|
||||
}
|
||||
|
||||
if ($apiResult instanceof \OpenAPI\Server\Model\NoContent404) {
|
||||
return response()->json($this->serde->serialize($apiResult, format: 'array'), 404);
|
||||
}
|
||||
|
||||
if ($apiResult instanceof \OpenAPI\Server\Model\NoContent405) {
|
||||
return response()->json($this->serde->serialize($apiResult, format: 'array'), 405);
|
||||
}
|
||||
|
||||
|
||||
// This shouldn't happen
|
||||
return response()->abort(500);
|
||||
}
|
||||
/**
|
||||
* Operation updatePetWithForm
|
||||
*
|
||||
* Updates a pet in the store with form data.
|
||||
*
|
||||
*/
|
||||
public function updatePetWithForm(Request $request, int $petId): JsonResponse
|
||||
{
|
||||
$validator = Validator::make(
|
||||
array_merge(
|
||||
[
|
||||
'petId' => $petId,
|
||||
],
|
||||
$request->all(),
|
||||
),
|
||||
[
|
||||
'petId' => [
|
||||
'required',
|
||||
'integer',
|
||||
],
|
||||
'name' => [
|
||||
'string',
|
||||
],
|
||||
'status' => [
|
||||
'string',
|
||||
],
|
||||
],
|
||||
);
|
||||
|
||||
if ($validator->fails()) {
|
||||
return response()->json(['error' => 'Invalid input'], 400);
|
||||
}
|
||||
|
||||
|
||||
$name = $request->string('name')->value();
|
||||
|
||||
$status = $request->string('status')->value();
|
||||
|
||||
try {
|
||||
$apiResult = $this->api->updatePetWithForm($petId, $name, $status);
|
||||
} catch (\Exception $exception) {
|
||||
// This shouldn't happen
|
||||
return response()->json(['error' => $exception->getMessage()], 500);
|
||||
}
|
||||
|
||||
if ($apiResult instanceof \OpenAPI\Server\Model\NoContent200) {
|
||||
return response()->json($this->serde->serialize($apiResult, format: 'array'), 200);
|
||||
}
|
||||
|
||||
if ($apiResult instanceof \OpenAPI\Server\Model\NoContent405) {
|
||||
return response()->json($this->serde->serialize($apiResult, format: 'array'), 405);
|
||||
}
|
||||
|
||||
|
||||
// This shouldn't happen
|
||||
return response()->abort(500);
|
||||
}
|
||||
/**
|
||||
* Operation uploadFile
|
||||
*
|
||||
* uploads an image.
|
||||
*
|
||||
*/
|
||||
public function uploadFile(Request $request, int $petId): JsonResponse
|
||||
{
|
||||
$validator = Validator::make(
|
||||
array_merge(
|
||||
[
|
||||
'petId' => $petId,
|
||||
],
|
||||
$request->all(),
|
||||
),
|
||||
[
|
||||
'petId' => [
|
||||
'required',
|
||||
'integer',
|
||||
],
|
||||
'additionalMetadata' => [
|
||||
'string',
|
||||
],
|
||||
'file' => [
|
||||
'file',
|
||||
],
|
||||
],
|
||||
);
|
||||
|
||||
if ($validator->fails()) {
|
||||
return response()->json(['error' => 'Invalid input'], 400);
|
||||
}
|
||||
|
||||
|
||||
$additionalMetadata = $request->string('additionalMetadata')->value();
|
||||
|
||||
$file = $request->file('file');
|
||||
|
||||
try {
|
||||
$apiResult = $this->api->uploadFile($petId, $additionalMetadata, $file);
|
||||
} catch (\Exception $exception) {
|
||||
// This shouldn't happen
|
||||
return response()->json(['error' => $exception->getMessage()], 500);
|
||||
}
|
||||
|
||||
if ($apiResult instanceof \OpenAPI\Server\Model\ApiResponse) {
|
||||
return response()->json($this->serde->serialize($apiResult, format: 'array'), 200);
|
||||
}
|
||||
|
||||
|
||||
// This shouldn't happen
|
||||
return response()->abort(500);
|
||||
}
|
||||
/**
|
||||
* Operation uploadFileWithRequiredFile
|
||||
*
|
||||
* uploads an image (required).
|
||||
*
|
||||
*/
|
||||
public function uploadFileWithRequiredFile(Request $request, int $petId): JsonResponse
|
||||
{
|
||||
$validator = Validator::make(
|
||||
array_merge(
|
||||
[
|
||||
'petId' => $petId,
|
||||
],
|
||||
$request->all(),
|
||||
),
|
||||
[
|
||||
'petId' => [
|
||||
'required',
|
||||
'integer',
|
||||
],
|
||||
'requiredFile' => [
|
||||
'file',
|
||||
'required',
|
||||
],
|
||||
'additionalMetadata' => [
|
||||
'string',
|
||||
],
|
||||
],
|
||||
);
|
||||
|
||||
if ($validator->fails()) {
|
||||
return response()->json(['error' => 'Invalid input'], 400);
|
||||
}
|
||||
|
||||
|
||||
$requiredFile = $request->file('requiredFile');
|
||||
|
||||
$additionalMetadata = $request->string('additionalMetadata')->value();
|
||||
|
||||
try {
|
||||
$apiResult = $this->api->uploadFileWithRequiredFile($petId, $requiredFile, $additionalMetadata);
|
||||
} catch (\Exception $exception) {
|
||||
// This shouldn't happen
|
||||
return response()->json(['error' => $exception->getMessage()], 500);
|
||||
}
|
||||
|
||||
if ($apiResult instanceof \OpenAPI\Server\Model\ApiResponse) {
|
||||
return response()->json($this->serde->serialize($apiResult, format: 'array'), 200);
|
||||
}
|
||||
|
||||
|
||||
// This shouldn't happen
|
||||
return response()->abort(500);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,227 @@
|
||||
<?php declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* OpenAPI Petstore
|
||||
* This spec is mainly for testing Petstore server and contains fake endpoints, models. Please do not use this for any other purpose. Special characters: \" \\
|
||||
* PHP version 8.1
|
||||
*
|
||||
* The version of the OpenAPI document: 1.0.0
|
||||
*
|
||||
*
|
||||
* NOTE: This class is auto generated by OpenAPI-Generator
|
||||
* https://openapi-generator.tech
|
||||
* Do not edit the class manually.
|
||||
*
|
||||
* Source files are located at:
|
||||
*
|
||||
* > https://github.com/OpenAPITools/openapi-generator/blob/master/modules/openapi-generator/src/main/resources/php-laravel/
|
||||
*/
|
||||
|
||||
|
||||
namespace OpenAPI\Server\Http\Controllers;
|
||||
|
||||
use Crell\Serde\SerdeCommon;
|
||||
use Illuminate\Routing\Controller;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Validator;
|
||||
|
||||
|
||||
use OpenAPI\Server\Api\StoreApiInterface;
|
||||
|
||||
class StoreController extends Controller
|
||||
{
|
||||
/**
|
||||
* Constructor
|
||||
*/
|
||||
public function __construct(
|
||||
private readonly StoreApiInterface $api,
|
||||
private readonly SerdeCommon $serde = new SerdeCommon(),
|
||||
)
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* Operation deleteOrder
|
||||
*
|
||||
* Delete purchase order by ID.
|
||||
*
|
||||
*/
|
||||
public function deleteOrder(Request $request, string $orderId): JsonResponse
|
||||
{
|
||||
$validator = Validator::make(
|
||||
array_merge(
|
||||
[
|
||||
'orderId' => $orderId,
|
||||
],
|
||||
$request->all(),
|
||||
),
|
||||
[
|
||||
'orderId' => [
|
||||
'required',
|
||||
'string',
|
||||
],
|
||||
],
|
||||
);
|
||||
|
||||
if ($validator->fails()) {
|
||||
return response()->json(['error' => 'Invalid input'], 400);
|
||||
}
|
||||
|
||||
|
||||
try {
|
||||
$apiResult = $this->api->deleteOrder($orderId);
|
||||
} catch (\Exception $exception) {
|
||||
// This shouldn't happen
|
||||
return response()->json(['error' => $exception->getMessage()], 500);
|
||||
}
|
||||
|
||||
if ($apiResult instanceof \OpenAPI\Server\Model\NoContent400) {
|
||||
return response()->json($this->serde->serialize($apiResult, format: 'array'), 400);
|
||||
}
|
||||
|
||||
if ($apiResult instanceof \OpenAPI\Server\Model\NoContent404) {
|
||||
return response()->json($this->serde->serialize($apiResult, format: 'array'), 404);
|
||||
}
|
||||
|
||||
|
||||
// This shouldn't happen
|
||||
return response()->abort(500);
|
||||
}
|
||||
/**
|
||||
* Operation getInventory
|
||||
*
|
||||
* Returns pet inventories by status.
|
||||
*
|
||||
*/
|
||||
public function getInventory(Request $request): JsonResponse
|
||||
{
|
||||
$validator = Validator::make(
|
||||
array_merge(
|
||||
[
|
||||
|
||||
],
|
||||
$request->all(),
|
||||
),
|
||||
[
|
||||
],
|
||||
);
|
||||
|
||||
if ($validator->fails()) {
|
||||
return response()->json(['error' => 'Invalid input'], 400);
|
||||
}
|
||||
|
||||
try {
|
||||
$apiResult = $this->api->getInventory();
|
||||
} catch (\Exception $exception) {
|
||||
// This shouldn't happen
|
||||
return response()->json(['error' => $exception->getMessage()], 500);
|
||||
}
|
||||
|
||||
if (is_array($apiResult)) {
|
||||
$serialized = array_map(fn ($item) => $this->serde->serialize($item, format: 'array'), $apiResult);
|
||||
return response()->json($serialized, 200);
|
||||
}
|
||||
|
||||
|
||||
// This shouldn't happen
|
||||
return response()->abort(500);
|
||||
}
|
||||
/**
|
||||
* Operation getOrderById
|
||||
*
|
||||
* Find purchase order by ID.
|
||||
*
|
||||
*/
|
||||
public function getOrderById(Request $request, int $orderId): JsonResponse
|
||||
{
|
||||
$validator = Validator::make(
|
||||
array_merge(
|
||||
[
|
||||
'orderId' => $orderId,
|
||||
],
|
||||
$request->all(),
|
||||
),
|
||||
[
|
||||
'orderId' => [
|
||||
'required',
|
||||
'gte:1',
|
||||
'lte:5',
|
||||
'integer',
|
||||
],
|
||||
],
|
||||
);
|
||||
|
||||
if ($validator->fails()) {
|
||||
return response()->json(['error' => 'Invalid input'], 400);
|
||||
}
|
||||
|
||||
|
||||
try {
|
||||
$apiResult = $this->api->getOrderById($orderId);
|
||||
} catch (\Exception $exception) {
|
||||
// This shouldn't happen
|
||||
return response()->json(['error' => $exception->getMessage()], 500);
|
||||
}
|
||||
|
||||
if ($apiResult instanceof \OpenAPI\Server\Model\Order) {
|
||||
return response()->json($this->serde->serialize($apiResult, format: 'array'), 200);
|
||||
}
|
||||
|
||||
if ($apiResult instanceof \OpenAPI\Server\Model\NoContent400) {
|
||||
return response()->json($this->serde->serialize($apiResult, format: 'array'), 400);
|
||||
}
|
||||
|
||||
if ($apiResult instanceof \OpenAPI\Server\Model\NoContent404) {
|
||||
return response()->json($this->serde->serialize($apiResult, format: 'array'), 404);
|
||||
}
|
||||
|
||||
|
||||
// This shouldn't happen
|
||||
return response()->abort(500);
|
||||
}
|
||||
/**
|
||||
* Operation placeOrder
|
||||
*
|
||||
* Place an order for a pet.
|
||||
*
|
||||
*/
|
||||
public function placeOrder(Request $request): JsonResponse
|
||||
{
|
||||
$validator = Validator::make(
|
||||
array_merge(
|
||||
[
|
||||
|
||||
],
|
||||
$request->all(),
|
||||
),
|
||||
[
|
||||
],
|
||||
);
|
||||
|
||||
if ($validator->fails()) {
|
||||
return response()->json(['error' => 'Invalid input'], 400);
|
||||
}
|
||||
|
||||
$order = $this->serde->deserialize($request->getContent(), from: 'json', to: \OpenAPI\Server\Model\Order::class);
|
||||
|
||||
try {
|
||||
$apiResult = $this->api->placeOrder($order);
|
||||
} catch (\Exception $exception) {
|
||||
// This shouldn't happen
|
||||
return response()->json(['error' => $exception->getMessage()], 500);
|
||||
}
|
||||
|
||||
if ($apiResult instanceof \OpenAPI\Server\Model\Order) {
|
||||
return response()->json($this->serde->serialize($apiResult, format: 'array'), 200);
|
||||
}
|
||||
|
||||
if ($apiResult instanceof \OpenAPI\Server\Model\NoContent400) {
|
||||
return response()->json($this->serde->serialize($apiResult, format: 'array'), 400);
|
||||
}
|
||||
|
||||
|
||||
// This shouldn't happen
|
||||
return response()->abort(500);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,399 @@
|
||||
<?php declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* OpenAPI Petstore
|
||||
* This spec is mainly for testing Petstore server and contains fake endpoints, models. Please do not use this for any other purpose. Special characters: \" \\
|
||||
* PHP version 8.1
|
||||
*
|
||||
* The version of the OpenAPI document: 1.0.0
|
||||
*
|
||||
*
|
||||
* NOTE: This class is auto generated by OpenAPI-Generator
|
||||
* https://openapi-generator.tech
|
||||
* Do not edit the class manually.
|
||||
*
|
||||
* Source files are located at:
|
||||
*
|
||||
* > https://github.com/OpenAPITools/openapi-generator/blob/master/modules/openapi-generator/src/main/resources/php-laravel/
|
||||
*/
|
||||
|
||||
|
||||
namespace OpenAPI\Server\Http\Controllers;
|
||||
|
||||
use Crell\Serde\SerdeCommon;
|
||||
use Illuminate\Routing\Controller;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Validator;
|
||||
|
||||
|
||||
use OpenAPI\Server\Api\UserApiInterface;
|
||||
|
||||
class UserController extends Controller
|
||||
{
|
||||
/**
|
||||
* Constructor
|
||||
*/
|
||||
public function __construct(
|
||||
private readonly UserApiInterface $api,
|
||||
private readonly SerdeCommon $serde = new SerdeCommon(),
|
||||
)
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* Operation createUser
|
||||
*
|
||||
* Create user.
|
||||
*
|
||||
*/
|
||||
public function createUser(Request $request): JsonResponse
|
||||
{
|
||||
$validator = Validator::make(
|
||||
array_merge(
|
||||
[
|
||||
|
||||
],
|
||||
$request->all(),
|
||||
),
|
||||
[
|
||||
],
|
||||
);
|
||||
|
||||
if ($validator->fails()) {
|
||||
return response()->json(['error' => 'Invalid input'], 400);
|
||||
}
|
||||
|
||||
$user = $this->serde->deserialize($request->getContent(), from: 'json', to: \OpenAPI\Server\Model\User::class);
|
||||
|
||||
try {
|
||||
$apiResult = $this->api->createUser($user);
|
||||
} catch (\Exception $exception) {
|
||||
// This shouldn't happen
|
||||
return response()->json(['error' => $exception->getMessage()], 500);
|
||||
}
|
||||
|
||||
if ($apiResult instanceof \OpenAPI\Server\Model\NoContentDefault) {
|
||||
return response()->json($this->serde->serialize($apiResult, format: 'array'), 0);
|
||||
}
|
||||
|
||||
|
||||
// This shouldn't happen
|
||||
return response()->abort(500);
|
||||
}
|
||||
/**
|
||||
* Operation createUsersWithArrayInput
|
||||
*
|
||||
* Creates list of users with given input array.
|
||||
*
|
||||
*/
|
||||
public function createUsersWithArrayInput(Request $request): JsonResponse
|
||||
{
|
||||
$validator = Validator::make(
|
||||
array_merge(
|
||||
[
|
||||
|
||||
],
|
||||
$request->all(),
|
||||
),
|
||||
[
|
||||
],
|
||||
);
|
||||
|
||||
if ($validator->fails()) {
|
||||
return response()->json(['error' => 'Invalid input'], 400);
|
||||
}
|
||||
|
||||
$user = $request->get('user');
|
||||
|
||||
try {
|
||||
$apiResult = $this->api->createUsersWithArrayInput($user);
|
||||
} catch (\Exception $exception) {
|
||||
// This shouldn't happen
|
||||
return response()->json(['error' => $exception->getMessage()], 500);
|
||||
}
|
||||
|
||||
if ($apiResult instanceof \OpenAPI\Server\Model\NoContentDefault) {
|
||||
return response()->json($this->serde->serialize($apiResult, format: 'array'), 0);
|
||||
}
|
||||
|
||||
|
||||
// This shouldn't happen
|
||||
return response()->abort(500);
|
||||
}
|
||||
/**
|
||||
* Operation createUsersWithListInput
|
||||
*
|
||||
* Creates list of users with given input array.
|
||||
*
|
||||
*/
|
||||
public function createUsersWithListInput(Request $request): JsonResponse
|
||||
{
|
||||
$validator = Validator::make(
|
||||
array_merge(
|
||||
[
|
||||
|
||||
],
|
||||
$request->all(),
|
||||
),
|
||||
[
|
||||
],
|
||||
);
|
||||
|
||||
if ($validator->fails()) {
|
||||
return response()->json(['error' => 'Invalid input'], 400);
|
||||
}
|
||||
|
||||
$user = $request->get('user');
|
||||
|
||||
try {
|
||||
$apiResult = $this->api->createUsersWithListInput($user);
|
||||
} catch (\Exception $exception) {
|
||||
// This shouldn't happen
|
||||
return response()->json(['error' => $exception->getMessage()], 500);
|
||||
}
|
||||
|
||||
if ($apiResult instanceof \OpenAPI\Server\Model\NoContentDefault) {
|
||||
return response()->json($this->serde->serialize($apiResult, format: 'array'), 0);
|
||||
}
|
||||
|
||||
|
||||
// This shouldn't happen
|
||||
return response()->abort(500);
|
||||
}
|
||||
/**
|
||||
* Operation deleteUser
|
||||
*
|
||||
* Delete user.
|
||||
*
|
||||
*/
|
||||
public function deleteUser(Request $request, string $username): JsonResponse
|
||||
{
|
||||
$validator = Validator::make(
|
||||
array_merge(
|
||||
[
|
||||
'username' => $username,
|
||||
],
|
||||
$request->all(),
|
||||
),
|
||||
[
|
||||
'username' => [
|
||||
'required',
|
||||
'string',
|
||||
],
|
||||
],
|
||||
);
|
||||
|
||||
if ($validator->fails()) {
|
||||
return response()->json(['error' => 'Invalid input'], 400);
|
||||
}
|
||||
|
||||
|
||||
try {
|
||||
$apiResult = $this->api->deleteUser($username);
|
||||
} catch (\Exception $exception) {
|
||||
// This shouldn't happen
|
||||
return response()->json(['error' => $exception->getMessage()], 500);
|
||||
}
|
||||
|
||||
if ($apiResult instanceof \OpenAPI\Server\Model\NoContent400) {
|
||||
return response()->json($this->serde->serialize($apiResult, format: 'array'), 400);
|
||||
}
|
||||
|
||||
if ($apiResult instanceof \OpenAPI\Server\Model\NoContent404) {
|
||||
return response()->json($this->serde->serialize($apiResult, format: 'array'), 404);
|
||||
}
|
||||
|
||||
|
||||
// This shouldn't happen
|
||||
return response()->abort(500);
|
||||
}
|
||||
/**
|
||||
* Operation getUserByName
|
||||
*
|
||||
* Get user by user name.
|
||||
*
|
||||
*/
|
||||
public function getUserByName(Request $request, string $username): JsonResponse
|
||||
{
|
||||
$validator = Validator::make(
|
||||
array_merge(
|
||||
[
|
||||
'username' => $username,
|
||||
],
|
||||
$request->all(),
|
||||
),
|
||||
[
|
||||
'username' => [
|
||||
'required',
|
||||
'string',
|
||||
],
|
||||
],
|
||||
);
|
||||
|
||||
if ($validator->fails()) {
|
||||
return response()->json(['error' => 'Invalid input'], 400);
|
||||
}
|
||||
|
||||
|
||||
try {
|
||||
$apiResult = $this->api->getUserByName($username);
|
||||
} catch (\Exception $exception) {
|
||||
// This shouldn't happen
|
||||
return response()->json(['error' => $exception->getMessage()], 500);
|
||||
}
|
||||
|
||||
if ($apiResult instanceof \OpenAPI\Server\Model\User) {
|
||||
return response()->json($this->serde->serialize($apiResult, format: 'array'), 200);
|
||||
}
|
||||
|
||||
if ($apiResult instanceof \OpenAPI\Server\Model\NoContent400) {
|
||||
return response()->json($this->serde->serialize($apiResult, format: 'array'), 400);
|
||||
}
|
||||
|
||||
if ($apiResult instanceof \OpenAPI\Server\Model\NoContent404) {
|
||||
return response()->json($this->serde->serialize($apiResult, format: 'array'), 404);
|
||||
}
|
||||
|
||||
|
||||
// This shouldn't happen
|
||||
return response()->abort(500);
|
||||
}
|
||||
/**
|
||||
* Operation loginUser
|
||||
*
|
||||
* Logs user into the system.
|
||||
*
|
||||
*/
|
||||
public function loginUser(Request $request): JsonResponse
|
||||
{
|
||||
$validator = Validator::make(
|
||||
array_merge(
|
||||
[
|
||||
|
||||
],
|
||||
$request->all(),
|
||||
),
|
||||
[
|
||||
'username' => [
|
||||
'required',
|
||||
'string',
|
||||
],
|
||||
'password' => [
|
||||
'required',
|
||||
'string',
|
||||
],
|
||||
],
|
||||
);
|
||||
|
||||
if ($validator->fails()) {
|
||||
return response()->json(['error' => 'Invalid input'], 400);
|
||||
}
|
||||
|
||||
$username = $request->string('username')->value();
|
||||
|
||||
$password = $request->string('password')->value();
|
||||
|
||||
try {
|
||||
$apiResult = $this->api->loginUser($username, $password);
|
||||
} catch (\Exception $exception) {
|
||||
// This shouldn't happen
|
||||
return response()->json(['error' => $exception->getMessage()], 500);
|
||||
}
|
||||
|
||||
if ($apiResult instanceof string) {
|
||||
return response()->json($this->serde->serialize($apiResult, format: 'array'), 200);
|
||||
}
|
||||
|
||||
if ($apiResult instanceof \OpenAPI\Server\Model\NoContent400) {
|
||||
return response()->json($this->serde->serialize($apiResult, format: 'array'), 400);
|
||||
}
|
||||
|
||||
|
||||
// This shouldn't happen
|
||||
return response()->abort(500);
|
||||
}
|
||||
/**
|
||||
* Operation logoutUser
|
||||
*
|
||||
* Logs out current logged in user session.
|
||||
*
|
||||
*/
|
||||
public function logoutUser(Request $request): JsonResponse
|
||||
{
|
||||
$validator = Validator::make(
|
||||
array_merge(
|
||||
[
|
||||
|
||||
],
|
||||
$request->all(),
|
||||
),
|
||||
[
|
||||
],
|
||||
);
|
||||
|
||||
if ($validator->fails()) {
|
||||
return response()->json(['error' => 'Invalid input'], 400);
|
||||
}
|
||||
|
||||
try {
|
||||
$apiResult = $this->api->logoutUser();
|
||||
} catch (\Exception $exception) {
|
||||
// This shouldn't happen
|
||||
return response()->json(['error' => $exception->getMessage()], 500);
|
||||
}
|
||||
|
||||
if ($apiResult instanceof \OpenAPI\Server\Model\NoContentDefault) {
|
||||
return response()->json($this->serde->serialize($apiResult, format: 'array'), 0);
|
||||
}
|
||||
|
||||
|
||||
// This shouldn't happen
|
||||
return response()->abort(500);
|
||||
}
|
||||
/**
|
||||
* Operation updateUser
|
||||
*
|
||||
* Updated user.
|
||||
*
|
||||
*/
|
||||
public function updateUser(Request $request, string $username): JsonResponse
|
||||
{
|
||||
$validator = Validator::make(
|
||||
array_merge(
|
||||
[
|
||||
'username' => $username,
|
||||
],
|
||||
$request->all(),
|
||||
),
|
||||
[
|
||||
],
|
||||
);
|
||||
|
||||
if ($validator->fails()) {
|
||||
return response()->json(['error' => 'Invalid input'], 400);
|
||||
}
|
||||
|
||||
|
||||
$user = $this->serde->deserialize($request->getContent(), from: 'json', to: \OpenAPI\Server\Model\User::class);
|
||||
|
||||
try {
|
||||
$apiResult = $this->api->updateUser($username, $user);
|
||||
} catch (\Exception $exception) {
|
||||
// This shouldn't happen
|
||||
return response()->json(['error' => $exception->getMessage()], 500);
|
||||
}
|
||||
|
||||
if ($apiResult instanceof \OpenAPI\Server\Model\NoContent400) {
|
||||
return response()->json($this->serde->serialize($apiResult, format: 'array'), 400);
|
||||
}
|
||||
|
||||
if ($apiResult instanceof \OpenAPI\Server\Model\NoContent404) {
|
||||
return response()->json($this->serde->serialize($apiResult, format: 'array'), 404);
|
||||
}
|
||||
|
||||
|
||||
// This shouldn't happen
|
||||
return response()->abort(500);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user