Skip to content
This repository was archived by the owner on Jun 4, 2024. It is now read-only.

Bugfix #18

Closed
wants to merge 7 commits into from
Closed
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
18 changes: 9 additions & 9 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,14 @@ services:
- DB_USER=dbuser
- DB_PASSWORD=dbpass
- IN_DOCKER=docker
- PHP_XDEBUG_ENABLED=1
- XDEBUG_CONFIG="remote_host=host.docker.internal"
- PHP_IDE_CONFIG="serverName=Docker"
depends_on:
- mysql
- postgres
- maria
tty: true
networks:
net: {}
mysql:
image: mysql:5.7
ports:
Expand All @@ -32,8 +33,6 @@ services:
MYSQL_USER: dbuser
MYSQL_PASSWORD: dbpass
MYSQL_DATABASE: testdb
networks:
net: {}
maria:
image: mariadb
ports:
Expand All @@ -47,8 +46,6 @@ services:
MYSQL_PASSWORD: dbpass
MYSQL_DATABASE: testdb
MYSQL_INITDB_SKIP_TZINFO: 1
networks:
net: {}
postgres:
image: postgres:12
ports:
Expand All @@ -61,8 +58,11 @@ services:
POSTGRES_USER: dbuser
POSTGRES_PASSWORD: dbpass
POSTGRES_DB: testdb
networks:
net: {}

networks:
net: {}
default:
driver: bridge
ipam:
config:
- subnet: 172.14.0.0/24

3 changes: 3 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
</testsuite>
</testsuites>
<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<directory suffix=".php">./src/lib</directory>
</whitelist>
<blacklist>
<directory>./vendor</directory>
<directory>./tests</directory>
Expand Down
37 changes: 30 additions & 7 deletions src/lib/AttributeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@
use cebe\yii2openapi\lib\items\DbModel;
use Yii;
use yii\helpers\Inflector;
use yii\helpers\Json;
use yii\helpers\StringHelper;
use function in_array;
use function is_string;
use function str_replace;
use function strpos;
use function substr;
Expand Down Expand Up @@ -130,11 +132,6 @@ protected function resolveProperty($propertyName, SpecObjectInterface $property,
->asHasOne([$foreignPk => $attribute->columnName]);
$this->relations[$propertyName] = $relation;
}
} else {
//TODO: This case for self-table relations, should be covered later
// $attribute->setPhpType(
// TypeResolver::schemaToPhpType($relatedSchema->type, $relatedSchema->format)
// );
}
}

Expand All @@ -144,8 +141,8 @@ protected function resolveProperty($propertyName, SpecObjectInterface $property,
$attribute->setPhpType($phpType)
->setDbType($this->guessDbType($property, ($propertyName === $this->primaryKey)))
->setUnique($property->{CustomSpecAttr::UNIQUE} ?? false)
->setSize($property->maxLength ?? null)
->setDefault($property->default ?? null);
->setSize($property->maxLength ?? null);
$attribute->setDefault($this->guessDefault($property, $attribute));
[$min, $max] = $this->guessMinMax($property);
$attribute->setLimits($min, $max, $property->minLength ?? null);
if (isset($property->enum) && is_array($property->enum)) {
Expand Down Expand Up @@ -222,4 +219,30 @@ protected function guessDbType(Schema $property, bool $isPk, bool $isReference =
}
return TypeResolver::schemaToDbType($property, $isPk);
}

protected function guessDefault(Schema $property, Attribute $attribute)
{
if (!isset($property->default)) {
return null;
}

if ($attribute->phpType === 'array' && in_array($property->default, ['{}', '[]'])) {
return [];
}
if (is_string($property->default)
&& $attribute->phpType === 'array'
&& StringHelper::startsWith($attribute->dbType, 'json')) {
try {
return Json::decode($property->default);
} catch (\Throwable $e) {
return [];
}
}

if ($attribute->phpType === 'integer' && $property->default !== null) {
return (int) $property->default;
}

return $property->default;
}
}
Loading