From 3815cb3a64e65f0a9ebdfe78efb7d4b4e86a1628 Mon Sep 17 00:00:00 2001 From: Sohel Ahmed Mesaniya Date: Wed, 20 Nov 2024 21:04:57 +0530 Subject: [PATCH] Implement for property level extension --- src/lib/AttributeResolver.php | 3 ++- src/lib/items/Attribute.php | 25 +++++++++++++------ src/lib/migrations/MysqlMigrationBuilder.php | 14 ++++++++--- .../index.yml | 17 ++++++++----- 4 files changed, 42 insertions(+), 17 deletions(-) diff --git a/src/lib/AttributeResolver.php b/src/lib/AttributeResolver.php index fd9beae1..a43aa8ba 100644 --- a/src/lib/AttributeResolver.php +++ b/src/lib/AttributeResolver.php @@ -117,7 +117,7 @@ public function resolve(): DbModel //For valid primary keys for junction tables 'junctionCols' => $this->isJunctionSchema ? $this->junctions->junctionCols($this->schemaName) : [], 'isNotDb' => $this->componentSchema->isNonDb(), - 'descriptionIsComment' => !empty(($this->componentSchema->{CustomSpecAttr::DESC_IS_COMMENT})) ? $this->componentSchema->{CustomSpecAttr::DESC_IS_COMMENT} : false, + 'descriptionIsComment' => !empty(($this->componentSchema->getSchema()->{CustomSpecAttr::DESC_IS_COMMENT})) ], ]); } @@ -226,6 +226,7 @@ protected function resolveProperty( ->setDefault($property->guessDefault()) ->setXDbType($property->getAttr(CustomSpecAttr::DB_TYPE)) ->setXDbDefaultExpression($property->getAttr(CustomSpecAttr::DB_DEFAULT_EXPRESSION)) + ->setXDescriptionIsComment($property->getAttr(CustomSpecAttr::DESC_IS_COMMENT)) ->setNullable($nullableValue) ->setIsPrimary($property->isPrimaryKey()) ->setForeignKeyColumnName($property->fkColName) diff --git a/src/lib/items/Attribute.php b/src/lib/items/Attribute.php index ffbbcba2..c079d06b 100644 --- a/src/lib/items/Attribute.php +++ b/src/lib/items/Attribute.php @@ -61,13 +61,6 @@ class Attribute extends BaseObject */ public $dbType = 'string'; - /** - * Custom db type - * string | null | false - * if `false` then this attribute is virtual - */ - public $xDbType; - /** * nullable * bool | null @@ -128,6 +121,18 @@ class Attribute extends BaseObject **/ public $isVirtual = false; + /** + * Custom db type + * string | null | false + * if `false` then this attribute is virtual + */ + public $xDbType; + + /** + * @see \cebe\yii2openapi\lib\CustomSpecAttr::DESC_IS_COMMENT + */ + public ?bool $xDescriptionIsComment = false; + public function __construct(string $propertyName, array $config = []) { $this->propertyName = $propertyName; @@ -397,4 +402,10 @@ public function handleDecimal(ColumnSchema $columnSchema): void $columnSchema->dbType = $decimalAttributes['dbType']; } } + + public function setXDescriptionIsComment($xDescriptionIsComment): Attribute + { + $this->xDescriptionIsComment = $xDescriptionIsComment; + return $this; + } } diff --git a/src/lib/migrations/MysqlMigrationBuilder.php b/src/lib/migrations/MysqlMigrationBuilder.php index 50947555..a860ac6d 100644 --- a/src/lib/migrations/MysqlMigrationBuilder.php +++ b/src/lib/migrations/MysqlMigrationBuilder.php @@ -64,9 +64,17 @@ protected function compareColumns(ColumnSchema $current, ColumnSchema $desired): , 'dbType', 'phpType' , 'precision', 'scale', 'unsigned'#, 'comment' ]; - if (!empty($this->config->getOpenApi()->{CustomSpecAttr::DESC_IS_COMMENT})) { - $properties[] = 'comment'; - } elseif ($this->model->descriptionIsComment) { // TODO + $comment = false; + if ($this->model->attributes[$desired->name]->xDescriptionIsComment) { + $comment = true; + } + if ($this->model->descriptionIsComment) { + $comment = true; + } + if ($this->config && !empty($this->config->getOpenApi()->{CustomSpecAttr::DESC_IS_COMMENT})) { + $comment = true; + } + if ($comment) { $properties[] = 'comment'; } foreach ($properties as $attr) { diff --git a/tests/specs/issue_fix/60_description_of_a_property_in_spec_must_correspond_to_db_table_column_comment/index.yml b/tests/specs/issue_fix/60_description_of_a_property_in_spec_must_correspond_to_db_table_column_comment/index.yml index aad9014f..d9272c32 100644 --- a/tests/specs/issue_fix/60_description_of_a_property_in_spec_must_correspond_to_db_table_column_comment/index.yml +++ b/tests/specs/issue_fix/60_description_of_a_property_in_spec_must_correspond_to_db_table_column_comment/index.yml @@ -1,5 +1,5 @@ openapi: 3.0.3 -x-description-is-comment: true +#x-description-is-comment: true info: title: 'Description of a property in spec must correspond to DB TABLE COLUMN COMMENT #60' version: 1.0.0 @@ -8,43 +8,48 @@ components: schemas: Fruit: type: object + # x-description-is-comment: true properties: id: type: integer name: type: string description: desc with ' quote + x-description-is-comment: true description: type: number x-db-type: double precision description: desc ' 2 + x-description-is-comment: true Animal: type: object + # x-description-is-comment: true properties: id: type: integer name: type: integer - # description: desc - # description: - # type: string - # x-db-type: varchar - # description: desc 2 + x-description-is-comment: true g: type: string description: desc for g + x-description-is-comment: true g2: type: string description: changed comment on g2 col + x-description-is-comment: true g3: type: string description: the comment on g3 col remains same + x-description-is-comment: true g4: type: integer description: data type changes but comment remains same + x-description-is-comment: true new_col: type: string description: new col added + x-description-is-comment: true paths: '/':