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

Support changing default table schema property name #555

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion docs/attributes.md
Original file line number Diff line number Diff line change
Expand Up @@ -539,6 +539,7 @@ Class level attribute for modifying OpenAPI Schema.
| [visibility](#visibility) | int `1` | No | Determines the visibility of the schema, see OpenApiSchema class constants |
| title | ?string `null` | Yes | Overwrites the default title |
| description | ?string `null` | Yes | Overwrites the default description (if any) |
| name | ?string `null` | Yes | The name of the OpenAPI property [defaults to the CakePHP table alias]. |

#### Visibility

Expand All @@ -554,7 +555,7 @@ You can use the constants below when defining `visibility`:
Example:

```php
#[OpenApiSchema(visbility: OpenApiSchema::VISIBLE_ALWAYS, title: 'Always visible schema')]
#[OpenApiSchema(visbility: OpenApiSchema::VISIBLE_ALWAYS, title: 'Always visible schema', name: 'RenamedCakeTableAlias')]
class Actor extends Entity{}
```

Expand Down
13 changes: 8 additions & 5 deletions src/Lib/Attribute/OpenApiSchema.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,16 @@ class OpenApiSchema
public const VISIBLE_NEVER = 4;

/**
* @param int $visibility See class constants for options.
* @param string|null $title The title of the schema
* @param string|null $description The description of the schema
* @param int $visibility See class constants for options [default: VISIBLE_DEFAULT].
* @param string|null $title The title of the schema [default: null].
* @param string|null $description The description of the schema [default: null].
* @param string|null $name The name of the OpenAPI property [defaults to the CakePHP table alias].
*/
public function __construct(
public readonly int $visibility = 1,
public readonly int $visibility = self::VISIBLE_DEFAULT,
public readonly ?string $title = null,
public readonly ?string $description = null
public readonly ?string $description = null,
public readonly ?string $name = null
) {
if ($this->visibility < 1 || $this->visibility > 4) {
throw new InvalidArgumentException(
Expand All @@ -56,6 +58,7 @@ public function __construct(
public function createSchema(): Schema
{
return (new Schema($this->title))
->setName($this->name)
->setVisibility($this->visibility)
->setDescription($this->description);
}
Expand Down
12 changes: 7 additions & 5 deletions src/Lib/Schema/SchemaFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,10 @@ public function create(ModelDecorator $modelDecorator, int $propertyType = 6): ?
return null;
}

$name = $openApiSchema instanceof OpenApiSchema ? $openApiSchema->name : null;

$schema = $this
->createSchema($modelDecorator->getModel(), $propertyType)
->createSchema($modelDecorator->getModel(), $propertyType, $name)
->setVisibility($openApiSchema->visibility ?? OpenApiSchema::VISIBLE_DEFAULT)
->setDescription($openApiSchema->description ?? '');

Expand All @@ -84,17 +86,17 @@ public function create(ModelDecorator $modelDecorator, int $propertyType = 6): ?
* @return \SwaggerBake\Lib\OpenApi\Schema
* @throws \ReflectionException
*/
public function createAlways(ModelDecorator $modelDecorator, int $propertyType = 6): Schema
public function createAlways(ModelDecorator $modelDecorator, int $propertyType = 6, ?string $name = null): Schema
{
return $this->createSchema($modelDecorator->getModel(), $propertyType);
return $this->createSchema($modelDecorator->getModel(), $propertyType, $name);
}

/**
* @param \MixerApi\Core\Model\Model $model Model instance
* @param int $propertyType see public constants for options
* @return \SwaggerBake\Lib\OpenApi\Schema
*/
private function createSchema(Model $model, int $propertyType = 6): Schema
private function createSchema(Model $model, int $propertyType = 6, ?string $name = null): Schema
{
$this->validator = $this->getValidator($model);

Expand All @@ -103,7 +105,7 @@ private function createSchema(Model $model, int $propertyType = 6): Schema
$properties = $this->getProperties($model, $propertyType, $docBlock);

$schema = (new Schema())
->setName((new ReflectionClass($model->getEntity()))->getShortName())
->setName($name ?? (new ReflectionClass($model->getEntity()))->getShortName())
->setType('object')
->setProperties($properties);

Expand Down
Loading