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

Resolve TODO: re-check options route in fractal action #35 #36

Merged
merged 9 commits into from
Dec 11, 2024
2 changes: 0 additions & 2 deletions src/lib/ColumnToCode.php
Original file line number Diff line number Diff line change
Expand Up @@ -407,8 +407,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',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why do auto generated rules contain module paths? I'd expect only controller/action here

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is due to urlPrefixes

public $urlPrefixes = [];

It is configured here in

];
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