Skip to content

Commit

Permalink
Merge pull request #70 from DoclerLabs/fix-enum
Browse files Browse the repository at this point in the history
add support for int enum
  • Loading branch information
vsouz4 authored May 5, 2022
2 parents 8211257 + 9f5d28d commit 6110d16
Show file tree
Hide file tree
Showing 9 changed files with 75 additions and 38 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## [9.1.0] - 2022.05.05
### Added
- Added support for non string enums (no constant for enum value is generated in this case)

## [9.0.0] - 2022.02.24
### Changed
- API Client Generation triggers a full regeneration of src folder.
Expand Down
3 changes: 2 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ COPY --from=composer /usr/bin/composer /usr/bin/composer
COPY composer.json /dependencies
COPY composer.lock /dependencies

RUN composer install
RUN composer install \
&& git config --global --add safe.directory /app

FROM php:7.4-cli-alpine3.13

Expand Down
12 changes: 7 additions & 5 deletions src/Generator/MutatorAccessorClassGeneratorAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,11 +105,13 @@ protected function generateEnumStatements(Field $field): array
$enumValues = $field->getEnumValues();
if (!empty($enumValues)) {
foreach ($enumValues as $enumValue) {
$constName = SchemaNaming::getEnumConstName($field, $enumValue);
$statements[] = $this->builder->constant(
$constName,
$this->builder->val($enumValue)
);
if (is_string($enumValue)) {
$constName = SchemaNaming::getEnumConstName($field, $enumValue);
$statements[] = $this->builder->constant(
$constName,
$this->builder->val($enumValue)
);
}
}
}

Expand Down
3 changes: 0 additions & 3 deletions src/Input/Factory/FieldFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,9 +132,6 @@ public function create(
} elseif (!empty($objectProperties)) {
$field->setObjectProperties($objectProperties);
} elseif (isset($schema->enum)) {
if (!FieldType::isSpecificationTypeString($type)) {
throw new InvalidSpecificationException('Only string enum fields are currently supported');
}
$field->setEnumValues($schema->enum);
}

Expand Down
21 changes: 21 additions & 0 deletions test/suite/functional/Generator/Schema/ItemPhp70.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ class Item implements SerializableInterface, JsonSerializable
/** @var string|null */
private $optionalEnum;

/** @var int|null */
private $optionalIntEnum;

/** @var DateTimeInterface|null */
private $optionalDate;

Expand Down Expand Up @@ -179,6 +182,13 @@ public function setOptionalEnum(string $optionalEnum): self
return $this;
}

public function setOptionalIntEnum(int $optionalIntEnum): self
{
$this->optionalIntEnum = $optionalIntEnum;

return $this;
}

public function setOptionalDate(DateTimeInterface $optionalDate): self
{
$this->optionalDate = $optionalDate;
Expand Down Expand Up @@ -447,6 +457,14 @@ public function getOptionalEnum()
return $this->optionalEnum;
}

/**
* @return int|null
*/
public function getOptionalIntEnum()
{
return $this->optionalIntEnum;
}

/**
* @return DateTimeInterface|null
*/
Expand Down Expand Up @@ -577,6 +595,9 @@ public function toArray(): array
if ($this->optionalEnum !== null) {
$fields['optionalEnum'] = $this->optionalEnum;
}
if ($this->optionalIntEnum !== null) {
$fields['optionalIntEnum'] = $this->optionalIntEnum;
}
if ($this->optionalDate !== null) {
$fields['optionalDate'] = $this->optionalDate->format(DATE_RFC3339);
}
Expand Down
18 changes: 18 additions & 0 deletions test/suite/functional/Generator/Schema/ItemPhp72.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ class Item implements SerializableInterface, JsonSerializable
/** @var string|null */
private $optionalEnum;

/** @var int|null */
private $optionalIntEnum;

/** @var DateTimeInterface|null */
private $optionalDate;

Expand Down Expand Up @@ -172,6 +175,13 @@ public function setOptionalEnum(string $optionalEnum): self
return $this;
}

public function setOptionalIntEnum(int $optionalIntEnum): self
{
$this->optionalIntEnum = $optionalIntEnum;

return $this;
}

public function setOptionalDate(DateTimeInterface $optionalDate): self
{
$this->optionalDate = $optionalDate;
Expand Down Expand Up @@ -422,6 +432,11 @@ public function getOptionalEnum(): ?string
return $this->optionalEnum;
}

public function getOptionalIntEnum(): ?int
{
return $this->optionalIntEnum;
}

public function getOptionalDate(): ?DateTimeInterface
{
return $this->optionalDate;
Expand Down Expand Up @@ -522,6 +537,9 @@ public function toArray(): array
if ($this->optionalEnum !== null) {
$fields['optionalEnum'] = $this->optionalEnum;
}
if ($this->optionalIntEnum !== null) {
$fields['optionalIntEnum'] = $this->optionalIntEnum;
}
if ($this->optionalDate !== null) {
$fields['optionalDate'] = $this->optionalDate->format(DATE_RFC3339);
}
Expand Down
17 changes: 17 additions & 0 deletions test/suite/functional/Generator/Schema/ItemPhp74.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ class Item implements SerializableInterface, JsonSerializable

private ?string $optionalEnum = null;

private ?int $optionalIntEnum = null;

private ?DateTimeInterface $optionalDate = null;

private ?float $optionalFloat = null;
Expand Down Expand Up @@ -144,6 +146,13 @@ public function setOptionalEnum(string $optionalEnum): self
return $this;
}

public function setOptionalIntEnum(int $optionalIntEnum): self
{
$this->optionalIntEnum = $optionalIntEnum;

return $this;
}

public function setOptionalDate(DateTimeInterface $optionalDate): self
{
$this->optionalDate = $optionalDate;
Expand Down Expand Up @@ -394,6 +403,11 @@ public function getOptionalEnum(): ?string
return $this->optionalEnum;
}

public function getOptionalIntEnum(): ?int
{
return $this->optionalIntEnum;
}

public function getOptionalDate(): ?DateTimeInterface
{
return $this->optionalDate;
Expand Down Expand Up @@ -494,6 +508,9 @@ public function toArray(): array
if ($this->optionalEnum !== null) {
$fields['optionalEnum'] = $this->optionalEnum;
}
if ($this->optionalIntEnum !== null) {
$fields['optionalIntEnum'] = $this->optionalIntEnum;
}
if ($this->optionalDate !== null) {
$fields['optionalDate'] = $this->optionalDate->format(DATE_RFC3339);
}
Expand Down
6 changes: 6 additions & 0 deletions test/suite/functional/Generator/Schema/item.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,12 @@ components:
enum:
- 'one option'
- 'another option'
optionalIntEnum:
type: integer
enum:
- 0
- 1
- 2
optionalDate:
type: string
format: 'date-time'
Expand Down
29 changes: 0 additions & 29 deletions test/suite/functional/Input/ParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -264,35 +264,6 @@ public function invalidSpecificationProvider()
],
],
],
'Only string enum supported' => [
[
'openapi' => '3.0.0',
'info' => [
'title' => 'Sample API',
'version' => '1.0.0',
],
'paths' => [
'/users' => [
'get' => [
'operationId' => 'getUsers',
'responses' => [
'200' => [
'description' => 'OK',
'content' => [
'application/json' => [
'schema' => [
'type' => 'integer',
'enum' => [4, 5, 6],
],
],
],
],
],
],
],
],
],
],
'Invalid field name' => [
[
'openapi' => '3.0.0',
Expand Down

0 comments on commit 6110d16

Please sign in to comment.