Skip to content

Commit

Permalink
Merge pull request #36 from php-openapi/35-resolve-todo-re-check-opti…
Browse files Browse the repository at this point in the history
…ons-route-in-fractal-action

Resolve TODO: re-check options route in fractal action #35
  • Loading branch information
cebe authored Dec 11, 2024
2 parents ccd2f28 + d649920 commit ca23577
Show file tree
Hide file tree
Showing 20 changed files with 650 additions and 22 deletions.
2 changes: 0 additions & 2 deletions src/lib/ColumnToCode.php
Original file line number Diff line number Diff line change
Expand Up @@ -400,8 +400,6 @@ private function getIsBuiltinType($type, $dbType)
private function resolveEnumType():void
{
if (ApiGenerator::isPostgres()) {
// $rawTableName = $this->dbSchema->getRawTableName($this->tableAlias);
// $this->rawParts['type'] = '"enum_'.$rawTableName.'_' . $this->column->name.'"';
$this->rawParts['type'] = '"'.$this->column->dbType.'"';
return;
}
Expand Down
12 changes: 2 additions & 10 deletions src/lib/items/FractalAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
*/
final class FractalAction extends BaseObject
{
use OptionsRoutesTrait;

/**@var string* */
public $id;

Expand Down Expand Up @@ -102,16 +104,6 @@ public function getRoute():string
return $this->controllerId.'/'.$this->id;
}

public function getOptionsRoute():string
{
//@TODO: re-check
if ($this->prefix && !empty($this->prefixSettings)) {
$prefix = $this->prefixSettings['module'] ?? $this->prefix;
return trim($prefix, '/').'/'.$this->controllerId.'/options';
}
return $this->controllerId.'/options';
}

public function getBaseModelName():string
{
return $this->modelFqn ? StringHelper::basename($this->modelFqn) : '';
Expand Down
62 changes: 62 additions & 0 deletions src/lib/items/OptionsRoutesTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

/**
* @copyright Copyright (c) 2018 Carsten Brandt <[email protected]> and contributors
* @license https://github.com/cebe/yii2-openapi/blob/master/LICENSE
*/

namespace cebe\yii2openapi\lib\items;

trait OptionsRoutesTrait
{
public function getOptionsRoute():string
{
if ($this->prefix && !empty($this->prefixSettings)) {
if (isset($this->prefixSettings['module'])) {
$prefix = $this->prefixSettings['module'];
return static::finalOptionsRoute($prefix, $this->controllerId);
} elseif (isset($this->prefixSettings['namespace']) && str_contains($this->prefixSettings['namespace'], '\modules\\')) { # if `module` not present then check in namespace and then in path
$prefix = static::computeModule('\\', $this->prefixSettings['namespace']);
if ($prefix) {
return static::finalOptionsRoute($prefix, $this->controllerId);
}
} elseif (isset($this->prefixSettings['path']) && str_contains($this->prefixSettings['path'], '/modules/')) {
$prefix = static::computeModule('/', $this->prefixSettings['path']);
if ($prefix) {
return static::finalOptionsRoute($prefix, $this->controllerId);
}
}
}
return $this->controllerId.'/options';
}

/**
* @param string $separator
* @param string $entity path or namespace
* @return void
*/
public static function computeModule(string $separator, string $entity): ?string
{
$parts = explode($separator . 'modules' . $separator, $entity); # /app/modules/forum/controllers => /forum/controllers
if (empty($parts[1])) {
return null;
}
if (str_contains($parts[1], 'controller')) {
$result = explode($separator . 'controller', $parts[1]); // compute everything in between "modules" and "controllers" e.g. api/v1
$result = array_map(function ($val) {
return str_replace('\\', '/', $val);
}, $result);
} else {
$result = explode($separator, $parts[1]); # forum/controllers => forum
}
if (empty($result[0])) {
return null;
}
return $result[0];
}

public static function finalOptionsRoute(string $prefix, string $controllerId): string
{
return trim($prefix, '/') . '/' . $controllerId . '/options';
}
}
12 changes: 2 additions & 10 deletions src/lib/items/RestAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
*/
final class RestAction extends BaseObject
{
use OptionsRoutesTrait;

/**@var string* */
public $id;

Expand Down Expand Up @@ -96,16 +98,6 @@ public function getRoute():string
return $this->controllerId . '/' . $this->id;
}

public function getOptionsRoute():string
{
//@TODO: re-check
if ($this->prefix && !empty($this->prefixSettings)) {
$prefix = $this->prefixSettings['module'] ?? $this->prefix;
return trim($prefix, '/').'/'.$this->controllerId.'/options';
}
return $this->controllerId.'/options';
}

public function getBaseModelName():string
{
return $this->modelFqn ? StringHelper::basename($this->modelFqn) : '';
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

return [
'openApiPath' => '@specs/issue_fix/35_resolve_todo_re_check_options_route_in_fractal_action/index.yaml',
'generateUrls' => true,
'generateModels' => false,
'excludeModels' => [
'Error',
],
'generateControllers' => true,
'generateMigrations' => false,
'useJsonApi' => false,
'urlPrefixes' => [
'animals' => '',
'/info' => ['module' => 'petinfo', 'namespace' => '\app\modules\petinfo\controllers'],
'/forum' => ['namespace' => '\app\modules\forum\controllers'], # namespace contains "\modules\"
'/forum2' => ['path' => '@app/modules/forum2/controllers', 'namespace' => '\app\forum2\controllers'], # path contains "/modules/"
'/api/v1' => ['path' => '@app/modules/some/controllers', 'namespace' => '\app\api\v1\controllers'], # namespace contains "\modules\"; module will be "api/v1"
'/api/v2' => ['path' => '@app/modules/api/v2/controllers', 'namespace' => '\app\some\controllers'], # namespace contains "\modules\"; module will be "api/v2"
]
];
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
openapi: "3.0.0"
info:
version: 1.0.0
title: Swagger Petstore
license:
name: MIT
servers:
- url: http://petstore.swagger.io/v1
paths:
/api/v1/pets:
get:
summary: List all pets
operationId: listPets
tags:
- pets
parameters:
- name: limit
in: query
description: How many items to return at one time (max 100)
required: false
schema:
type: integer
format: int32
responses:
'200':
description: A paged array of pets
headers:
x-next:
description: A link to the next page of responses
schema:
type: string
content:
application/json:
schema:
$ref: "#/components/schemas/Pets"
default:
description: unexpected error
content:
application/json:
schema:
$ref: "#/components/schemas/Error"
post:
summary: Create a pet
operationId: createPets
tags:
- pets
responses:
'201':
description: Null response
default:
description: unexpected error
content:
application/json:
schema:
$ref: "#/components/schemas/Error"
/animals/pets/{id}:
parameters:
- name: id
in: path
required: true
description: The id of the pet to update
schema:
type: string
get:
summary: Info for a specific pet
operationId: showPetById
tags:
- pets
responses:
'200':
description: Expected response to a valid request
content:
application/json:
schema:
$ref: "#/components/schemas/Pet"
default:
description: unexpected error
content:
application/json:
schema:
$ref: "#/components/schemas/Error"
patch:
summary: update a specific pet
operationId: updatePetById
tags:
- pets
responses:
'200':
description: The updated pet
content:
application/json:
schema:
$ref: "#/components/schemas/Pet"
delete:
summary: delete a specific pet
operationId: deletePetById
tags:
- pets
responses:
'204':
description: successfully deleted pet
/petComments:
get:
description: list all pet comments
responses:
'200':
description: list of comments
/info/pet-details:
get:
description: list all pet details
responses:
'200':
description: list of details
/forum/pet2-details:
get:
description: list all pet details
responses:
'200':
description: list of details
/forum2/pet3-details:
get:
description: list all pet details
responses:
'200':
description: list of details
/api/v2/comments:
get:
description: list all pet details
responses:
'200':
description: list of details

components:
schemas:
Pet:
description: A Pet
required:
- id
- name
properties:
id:
type: integer
format: int64
readOnly: True
name:
type: string
store:
$ref: '#/components/schemas/Store'
tag:
type: string
x-faker: "$faker->randomElement(['one', 'two', 'three', 'four'])"
Store:
description: A store's description
required:
- id
- name
properties:
id:
type: integer
format: int64
readOnly: True
name:
type: string
Pets:
type: array
items:
$ref: "#/components/schemas/Pet"
Error:
required:
- code
- message
properties:
code:
type: integer
format: int32
message:
type: string
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php
/**
* OpenAPI UrlRules
*
* This file is auto generated.
*/
return [
'GET api/v1/pets' => 'api/v1/pet/list',
'POST api/v1/pets' => 'api/v1/pet/create',
'GET animals/pets/<id:[\w-]+>' => 'pet/view',
'DELETE animals/pets/<id:[\w-]+>' => 'pet/delete',
'PATCH animals/pets/<id:[\w-]+>' => 'pet/update',
'GET petComments' => 'pet-comment/list',
'GET info/pet-details' => 'petinfo/pet-detail/list',
'GET forum/pet2-details' => 'forum/pet2-detail/list',
'GET forum2/pet3-details' => 'forum2/pet3-detail/list',
'GET api/v2/comments' => 'api/v2/comment/list',
'api/v1/pets' => 'some/pet/options',
'animals/pets/<id:[\w-]+>' => 'pet/options',
'petComments' => 'pet-comment/options',
'info/pet-details' => 'petinfo/pet-detail/options',
'forum/pet2-details' => 'forum/pet2-detail/options',
'forum2/pet3-details' => 'forum2/pet3-detail/options',
'api/v2/comments' => 'api/v2/comment/options',
];
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace app\controllers;

class PetCommentController extends \app\controllers\base\PetCommentController
{

public function checkAccess($action, $model = null, $params = [])
{
//TODO implement checkAccess
}

public function actionList()
{
//TODO implement actionList
}


}

Loading

0 comments on commit ca23577

Please sign in to comment.