Skip to content

Commit 3815cb3

Browse files
committed
Implement for property level extension
1 parent 53416d2 commit 3815cb3

File tree

4 files changed

+42
-17
lines changed

4 files changed

+42
-17
lines changed

src/lib/AttributeResolver.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ public function resolve(): DbModel
117117
//For valid primary keys for junction tables
118118
'junctionCols' => $this->isJunctionSchema ? $this->junctions->junctionCols($this->schemaName) : [],
119119
'isNotDb' => $this->componentSchema->isNonDb(),
120-
'descriptionIsComment' => !empty(($this->componentSchema->{CustomSpecAttr::DESC_IS_COMMENT})) ? $this->componentSchema->{CustomSpecAttr::DESC_IS_COMMENT} : false,
120+
'descriptionIsComment' => !empty(($this->componentSchema->getSchema()->{CustomSpecAttr::DESC_IS_COMMENT}))
121121
],
122122
]);
123123
}
@@ -226,6 +226,7 @@ protected function resolveProperty(
226226
->setDefault($property->guessDefault())
227227
->setXDbType($property->getAttr(CustomSpecAttr::DB_TYPE))
228228
->setXDbDefaultExpression($property->getAttr(CustomSpecAttr::DB_DEFAULT_EXPRESSION))
229+
->setXDescriptionIsComment($property->getAttr(CustomSpecAttr::DESC_IS_COMMENT))
229230
->setNullable($nullableValue)
230231
->setIsPrimary($property->isPrimaryKey())
231232
->setForeignKeyColumnName($property->fkColName)

src/lib/items/Attribute.php

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -61,13 +61,6 @@ class Attribute extends BaseObject
6161
*/
6262
public $dbType = 'string';
6363

64-
/**
65-
* Custom db type
66-
* string | null | false
67-
* if `false` then this attribute is virtual
68-
*/
69-
public $xDbType;
70-
7164
/**
7265
* nullable
7366
* bool | null
@@ -128,6 +121,18 @@ class Attribute extends BaseObject
128121
**/
129122
public $isVirtual = false;
130123

124+
/**
125+
* Custom db type
126+
* string | null | false
127+
* if `false` then this attribute is virtual
128+
*/
129+
public $xDbType;
130+
131+
/**
132+
* @see \cebe\yii2openapi\lib\CustomSpecAttr::DESC_IS_COMMENT
133+
*/
134+
public ?bool $xDescriptionIsComment = false;
135+
131136
public function __construct(string $propertyName, array $config = [])
132137
{
133138
$this->propertyName = $propertyName;
@@ -397,4 +402,10 @@ public function handleDecimal(ColumnSchema $columnSchema): void
397402
$columnSchema->dbType = $decimalAttributes['dbType'];
398403
}
399404
}
405+
406+
public function setXDescriptionIsComment($xDescriptionIsComment): Attribute
407+
{
408+
$this->xDescriptionIsComment = $xDescriptionIsComment;
409+
return $this;
410+
}
400411
}

src/lib/migrations/MysqlMigrationBuilder.php

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,17 @@ protected function compareColumns(ColumnSchema $current, ColumnSchema $desired):
6464
, 'dbType', 'phpType'
6565
, 'precision', 'scale', 'unsigned'#, 'comment'
6666
];
67-
if (!empty($this->config->getOpenApi()->{CustomSpecAttr::DESC_IS_COMMENT})) {
68-
$properties[] = 'comment';
69-
} elseif ($this->model->descriptionIsComment) { // TODO
67+
$comment = false;
68+
if ($this->model->attributes[$desired->name]->xDescriptionIsComment) {
69+
$comment = true;
70+
}
71+
if ($this->model->descriptionIsComment) {
72+
$comment = true;
73+
}
74+
if ($this->config && !empty($this->config->getOpenApi()->{CustomSpecAttr::DESC_IS_COMMENT})) {
75+
$comment = true;
76+
}
77+
if ($comment) {
7078
$properties[] = 'comment';
7179
}
7280
foreach ($properties as $attr) {

tests/specs/issue_fix/60_description_of_a_property_in_spec_must_correspond_to_db_table_column_comment/index.yml

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
openapi: 3.0.3
2-
x-description-is-comment: true
2+
#x-description-is-comment: true
33
info:
44
title: 'Description of a property in spec must correspond to DB TABLE COLUMN COMMENT #60'
55
version: 1.0.0
@@ -8,43 +8,48 @@ components:
88
schemas:
99
Fruit:
1010
type: object
11+
# x-description-is-comment: true
1112
properties:
1213
id:
1314
type: integer
1415
name:
1516
type: string
1617
description: desc with ' quote
18+
x-description-is-comment: true
1719
description:
1820
type: number
1921
x-db-type: double precision
2022
description: desc ' 2
23+
x-description-is-comment: true
2124
Animal:
2225
type: object
26+
# x-description-is-comment: true
2327
properties:
2428
id:
2529
type: integer
2630
name:
2731
type: integer
28-
# description: desc
29-
# description:
30-
# type: string
31-
# x-db-type: varchar
32-
# description: desc 2
32+
x-description-is-comment: true
3333
g:
3434
type: string
3535
description: desc for g
36+
x-description-is-comment: true
3637
g2:
3738
type: string
3839
description: changed comment on g2 col
40+
x-description-is-comment: true
3941
g3:
4042
type: string
4143
description: the comment on g3 col remains same
44+
x-description-is-comment: true
4245
g4:
4346
type: integer
4447
description: data type changes but comment remains same
48+
x-description-is-comment: true
4549
new_col:
4650
type: string
4751
description: new col added
52+
x-description-is-comment: true
4853

4954
paths:
5055
'/':

0 commit comments

Comments
 (0)