Skip to content

Commit

Permalink
Merge branches 'master' and '63-just-column-name-rename' of github.co…
Browse files Browse the repository at this point in the history
…m:php-openapi/yii2-openapi into 63-just-column-name-rename
  • Loading branch information
SOHELAHMED7 committed Dec 12, 2024
2 parents 36c576e + ca23577 commit 1ce997f
Show file tree
Hide file tree
Showing 22 changed files with 655 additions and 41 deletions.
17 changes: 4 additions & 13 deletions src/lib/ColumnToCode.php
Original file line number Diff line number Diff line change
Expand Up @@ -220,25 +220,18 @@ public function getCode(bool $quoted = false):string
array_unshift($parts, '$this');
return implode('->', array_filter(array_map('trim', $parts)));
}
if ($this->rawParts['default'] === null) {
$default = '';
} elseif (ApiGenerator::isPostgres() && $this->isEnum()) {
$default =
$this->rawParts['default'] !== null ? ' DEFAULT ' . trim($this->rawParts['default']) : '';
} else {
$default = $this->rawParts['default'] !== null ? ' DEFAULT ' . trim($this->rawParts['default']) : '';

if (ApiGenerator::isPostgres() && $this->alterByXDbType) {
return $quoted ? VarDumper::export($this->rawParts['type']) : $this->rawParts['type'];
}

$default = $this->rawParts['default'] !== null ? ' DEFAULT ' . trim($this->rawParts['default']) : '';
$code = $this->rawParts['type'] . ' ' . $this->rawParts['nullable'] . $default;

if ((ApiGenerator::isMysql() || ApiGenerator::isMariaDb())) {
$code .= $this->rawParts['position'] ? ' ' . $this->rawParts['position'] : '';
$code .= $this->rawParts['comment'] ? ' '.$this->rawParts['comment'] : '';
}

if (ApiGenerator::isPostgres() && $this->alterByXDbType) {
return $quoted ? VarDumper::export($this->rawParts['type']) : $this->rawParts['type'];
}
return $quoted ? VarDumper::export($code) : $code;
}

Expand Down Expand Up @@ -407,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
8 changes: 2 additions & 6 deletions src/lib/migrations/MysqlMigrationBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,6 @@ protected function buildColumnChanges(ColumnSchema $current, ColumnSchema $desir
foreach ($changed as $attr) {
$newColumn->$attr = $desired->$attr;
}
if (static::isEnum($newColumn)) {
$newColumn->dbType = 'enum'; // TODO this is concretely not correct
}
$this->migration->addUpCode($this->recordBuilder->alterColumn($this->model->getTableAlias(), $newColumn, $positionDesired))
->addDownCode($this->recordBuilder->alterColumn($this->model->getTableAlias(), $current, $positionCurrent));
}
Expand Down Expand Up @@ -131,13 +128,12 @@ protected function findTableIndexes():array

public static function getColumnSchemaBuilderClass(): string
{
if (ApiGenerator::isMysql()) {
return \yii\db\mysql\ColumnSchemaBuilder::class;
} elseif (ApiGenerator::isMariaDb()) {
if (ApiGenerator::isMariaDb()) {
return \SamIT\Yii2\MariaDb\ColumnSchemaBuilder::class;
} else {
throw new \Exception('Unknown database');
}
return \yii\db\mysql\ColumnSchemaBuilder::class;
}

public function modifyCurrent(ColumnSchema $current): void
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
Loading

0 comments on commit 1ce997f

Please sign in to comment.