diff --git a/src/generator/default/transformer.php b/src/generator/default/transformer.php index 00462b21..6847e514 100644 --- a/src/generator/default/transformer.php +++ b/src/generator/default/transformer.php @@ -43,6 +43,15 @@ public function includegetCamelName()?>(dbModel-> return $this->collection($relation, $transformer, 'makeResourceKey($relation->getClassKey())?>'); } - + +many2Many as $relation):?> + + public function includegetCamelName()?>(dbModel->getClassName()?> $model) + { + $relation = $model->name)?>; + $transformer = new getRelatedClassName())?>Transformer(); + return $this->collection($relation, $transformer, 'makeResourceKey($relation->relatedSchemaName)?>'); + } + + } diff --git a/src/lib/items/Transformer.php b/src/lib/items/Transformer.php index ab8b5d6b..9d2b0a3d 100644 --- a/src/lib/items/Transformer.php +++ b/src/lib/items/Transformer.php @@ -11,12 +11,13 @@ use yii\helpers\Inflector; /** - * @property-read string $name - * @property-read array $defaultRelations - * @property-read string $modelFQN - * @property-read string $fQN - * @property-read array|\cebe\yii2openapi\lib\items\AttributeRelation[] $relations - * @property-read array $availableRelations + * @property-read string $name + * @property-read array $defaultRelations + * @property-read string $modelFQN + * @property-read string $fQN + * @property-read array|\cebe\yii2openapi\lib\items\AttributeRelation[] $relations + * @property-read \cebe\yii2openapi\lib\items\ManyToManyRelation[]|array $many2Many + * @property-read array $availableRelations */ class Transformer extends BaseObject { @@ -77,6 +78,14 @@ public function getRelations(): array return $this->dbModel->relations; } + /** + * @return array|\cebe\yii2openapi\lib\items\ManyToManyRelation[] + */ + public function getMany2Many(): array + { + return $this->dbModel->many2many; + } + public function getDefaultRelations(): array { return []; @@ -84,11 +93,12 @@ public function getDefaultRelations(): array public function getAvailableRelations(): array { - return \array_keys($this->dbModel->relations); + return array_merge(array_keys($this->dbModel->relations), array_keys($this->dbModel->many2many)); } public function makeResourceKey(string $value): string { - return $this->singularResourceKey ? Inflector::singularize($value): Inflector::pluralize($value); + $value = $this->singularResourceKey ? Inflector::singularize($value): Inflector::pluralize($value); + return strtolower(Inflector::camel2id($value, '_')); } } diff --git a/tests/specs/many2many.yaml b/tests/specs/many2many.yaml index d48be3f1..1cf439d8 100644 --- a/tests/specs/many2many.yaml +++ b/tests/specs/many2many.yaml @@ -7,13 +7,13 @@ info: servers: - url: http://blog.dummy.io/v2 paths: - /some: + /posts: get: summary: dummy path operationId: showDummy responses: '200': - description: Some response + $ref: '#/components/responses/PostListResponse' components: schemas: Post: @@ -86,3 +86,38 @@ components: $ref: '#/components/schemas/Photo' post: $ref: '#/components/schemas/Post' + _PostResource: + type: object + properties: + id: + type: integer + example: 1 + type: + type: string + attributes: + $ref: '#/components/schemas/Post' + links: + type: object + relationships: + type: object + responses: + PostResponse: + description: Single post info + content: + application/vnd.api+json: + schema: + type: object + properties: + data: + $ref: '#/components/schemas/_PostResource' + PostListResponse: + description: Posts List + content: + application/vnd.api+json: + schema: + type: object + properties: + data: + type: array + items: + $ref: '#/components/schemas/_PostResource'