Skip to content

Commit

Permalink
Merge pull request #28 from onmoon/default-value-support
Browse files Browse the repository at this point in the history
Default value support
  • Loading branch information
DimanKuskov authored Feb 13, 2020
2 parents c88090c + c832b5f commit dcf83a2
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 7 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ operation must have an unique `operationId`.

Currently, there are also the following limitations:
- `number` without `format` is treated as float
- Default values are not supported for `date` and `date-time` string formats
- Only scalar types are allowed in path parameters
- Partial match pattern are ignored in path parameter patterns, only `^...$` patterns are used
- If pattern is specified in path parameter then type- and format-generated requirements are ignored
Expand Down
2 changes: 1 addition & 1 deletion src/CodeGenerator/ApiServerCodeGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public function generate() : void
foreach ($this->loader->list() as $specificationName => $specification) {
$parsedSpecification = $this->loader->load($specificationName);

$apiName = $specification->getNameSpace();
$apiName = $this->namingStrategy->stringToNamespace($specification->getNameSpace());
$specMediaType = $specification->getMediaType();
$apiNamespace = $this->namingStrategy->buildNamespace($this->rootNamespace, self::APIS_NAMESPACE, $apiName);
$apiPath = $this->namingStrategy->buildPath($this->rootPath, self::APIS_NAMESPACE, $apiName);
Expand Down
48 changes: 42 additions & 6 deletions src/CodeGenerator/Dto/PhpParserDtoFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
use PhpParser\PrettyPrinter\Standard;
use function array_map;
use function array_merge;
use function class_exists;
use function count;
use function implode;
use function in_array;
Expand Down Expand Up @@ -89,11 +90,19 @@ public function generateParamDto(
$type = null;
$iterableType = null;
$required = $parameter->required;
$default = null;

if (! $parameter->schema instanceof Schema) {
continue;
}

/** @var string|int|float|bool|null $schemaDefaultValue */
$schemaDefaultValue = $parameter->schema->default;

$defaultValue = $schemaDefaultValue !== null && class_exists(
$this->typeResolver->getPhpType($this->typeResolver->findScalarType($parameter->schema))
) ? null : $schemaDefaultValue;

if (Type::isScalar($parameter->schema->type)) {
$typeId = $this->typeResolver->findScalarType($parameter->schema);
$type = $this->typeResolver->getPhpType($typeId);
Expand Down Expand Up @@ -138,6 +147,7 @@ public function generateParamDto(
$parameter->name,
$type,
! $required,
$defaultValue,
$iterableType,
$parameter->description
)
Expand All @@ -147,6 +157,7 @@ public function generateParamDto(
$parameter->name,
$type,
! $required,
$defaultValue,
$iterableType
)
);
Expand Down Expand Up @@ -229,6 +240,7 @@ public function generateDtoClassGraph(

$type = null;
$iterableType = null;
$defaultValue = null;
/**
* @psalm-suppress RedundantConditionGivenDocblockType
*/
Expand All @@ -238,6 +250,13 @@ public function generateDtoClassGraph(
throw new Exception('Cannot work with References');
}

/** @var string|int|float|bool|null $schemaDefaultValue */
$schemaDefaultValue = $property->default;

$defaultValue = $schemaDefaultValue !== null && class_exists(
$this->typeResolver->getPhpType($this->typeResolver->findScalarType($property))
) ? null : $schemaDefaultValue;

if (Type::isScalar($property->type)) {
$typeId = $this->typeResolver->findScalarType($property);
$type = $this->typeResolver->getPhpType($typeId);
Expand Down Expand Up @@ -307,6 +326,7 @@ public function generateDtoClassGraph(
$propertyName,
$type,
! $required,
$defaultValue,
$iterableType,
$property->description
)
Expand All @@ -328,7 +348,13 @@ public function generateDtoClassGraph(
}
}

$getterBuilders[] = $this->getGetterDefinition($propertyName, $type, ! $required, $iterableType);
$getterBuilders[] = $this->getGetterDefinition(
$propertyName,
$type,
! $required,
$defaultValue,
$iterableType
);
}

if ($constructorRequired) {
Expand Down Expand Up @@ -469,18 +495,24 @@ private function getConstructorDocBlock(array $lines) : string
return implode(PHP_EOL, ['/**', ...$lines, ' */']);
}

/**
* @param string|int|float|bool|null $defaultValue
*/
private function getPropertyDefinition(
string $name,
string $type,
bool $nullable = false,
$defaultValue = null,
?string $iterableType = null,
?string $description = null
) : Property {
$property = $this->factory
->property($name)
->makePrivate();

if ($nullable) {
if ($defaultValue !== null) {
$property->setDefault($defaultValue);
} elseif ($nullable) {
$property->setDefault(null);
}

Expand All @@ -490,10 +522,10 @@ private function getPropertyDefinition(
$docCommentLines[] = sprintf(' %s', $description);
}

$nullableDocblock = $nullable ? '|null' : '';
$nullableDocblock = $nullable && $defaultValue === null ? '|null' : '';

if (version_compare($this->languageLevel, '7.4.0') >= 0) {
$property->setType(($nullable ? '?' : '') . ($iterableType ? 'array' : $type));
$property->setType(($nullable && $defaultValue === null ? '?' : '') . ($iterableType ? 'array' : $type));
}

if (count($docCommentLines) > 0) {
Expand Down Expand Up @@ -542,24 +574,28 @@ private function getAssignmentDefinition(string $name) : Assign
return new Assign(new Variable('this->' . $name), new Variable($name));
}

/**
* @param string|int|float|bool|null $defaultValue
*/
private function getGetterDefinition(
string $name,
string $type,
bool $nullable = false,
$defaultValue = null,
?string $iterableType = null
) : Method {
$method = $this->factory
->method('get' . ucfirst($name))
->makePublic()
->setReturnType(($nullable ? '?' : '') . ($iterableType ? 'array' : $type))
->setReturnType(($nullable && $defaultValue === null ? '?' : '') . ($iterableType ? 'array' : $type))
->addStmt(new Return_(new Variable('this->' . $name)));

if ($iterableType !== null) {
$method->setDocComment(
sprintf(
'/** @return %s[]%s */',
$iterableType,
$nullable ? '|null' : ''
$nullable && $defaultValue === null ? '|null' : ''
)
);
}
Expand Down

0 comments on commit dcf83a2

Please sign in to comment.