Skip to content

Commit 6110d16

Browse files
authored
Merge pull request #70 from DoclerLabs/fix-enum
add support for int enum
2 parents 8211257 + 9f5d28d commit 6110d16

File tree

9 files changed

+75
-38
lines changed

9 files changed

+75
-38
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file.
44
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
55
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
66

7+
## [9.1.0] - 2022.05.05
8+
### Added
9+
- Added support for non string enums (no constant for enum value is generated in this case)
10+
711
## [9.0.0] - 2022.02.24
812
### Changed
913
- API Client Generation triggers a full regeneration of src folder.

Dockerfile

+2-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ COPY --from=composer /usr/bin/composer /usr/bin/composer
1515
COPY composer.json /dependencies
1616
COPY composer.lock /dependencies
1717

18-
RUN composer install
18+
RUN composer install \
19+
&& git config --global --add safe.directory /app
1920

2021
FROM php:7.4-cli-alpine3.13
2122

src/Generator/MutatorAccessorClassGeneratorAbstract.php

+7-5
Original file line numberDiff line numberDiff line change
@@ -105,11 +105,13 @@ protected function generateEnumStatements(Field $field): array
105105
$enumValues = $field->getEnumValues();
106106
if (!empty($enumValues)) {
107107
foreach ($enumValues as $enumValue) {
108-
$constName = SchemaNaming::getEnumConstName($field, $enumValue);
109-
$statements[] = $this->builder->constant(
110-
$constName,
111-
$this->builder->val($enumValue)
112-
);
108+
if (is_string($enumValue)) {
109+
$constName = SchemaNaming::getEnumConstName($field, $enumValue);
110+
$statements[] = $this->builder->constant(
111+
$constName,
112+
$this->builder->val($enumValue)
113+
);
114+
}
113115
}
114116
}
115117

src/Input/Factory/FieldFactory.php

-3
Original file line numberDiff line numberDiff line change
@@ -132,9 +132,6 @@ public function create(
132132
} elseif (!empty($objectProperties)) {
133133
$field->setObjectProperties($objectProperties);
134134
} elseif (isset($schema->enum)) {
135-
if (!FieldType::isSpecificationTypeString($type)) {
136-
throw new InvalidSpecificationException('Only string enum fields are currently supported');
137-
}
138135
$field->setEnumValues($schema->enum);
139136
}
140137

test/suite/functional/Generator/Schema/ItemPhp70.php

+21
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,9 @@ class Item implements SerializableInterface, JsonSerializable
7373
/** @var string|null */
7474
private $optionalEnum;
7575

76+
/** @var int|null */
77+
private $optionalIntEnum;
78+
7679
/** @var DateTimeInterface|null */
7780
private $optionalDate;
7881

@@ -179,6 +182,13 @@ public function setOptionalEnum(string $optionalEnum): self
179182
return $this;
180183
}
181184

185+
public function setOptionalIntEnum(int $optionalIntEnum): self
186+
{
187+
$this->optionalIntEnum = $optionalIntEnum;
188+
189+
return $this;
190+
}
191+
182192
public function setOptionalDate(DateTimeInterface $optionalDate): self
183193
{
184194
$this->optionalDate = $optionalDate;
@@ -447,6 +457,14 @@ public function getOptionalEnum()
447457
return $this->optionalEnum;
448458
}
449459

460+
/**
461+
* @return int|null
462+
*/
463+
public function getOptionalIntEnum()
464+
{
465+
return $this->optionalIntEnum;
466+
}
467+
450468
/**
451469
* @return DateTimeInterface|null
452470
*/
@@ -577,6 +595,9 @@ public function toArray(): array
577595
if ($this->optionalEnum !== null) {
578596
$fields['optionalEnum'] = $this->optionalEnum;
579597
}
598+
if ($this->optionalIntEnum !== null) {
599+
$fields['optionalIntEnum'] = $this->optionalIntEnum;
600+
}
580601
if ($this->optionalDate !== null) {
581602
$fields['optionalDate'] = $this->optionalDate->format(DATE_RFC3339);
582603
}

test/suite/functional/Generator/Schema/ItemPhp72.php

+18
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,9 @@ class Item implements SerializableInterface, JsonSerializable
7373
/** @var string|null */
7474
private $optionalEnum;
7575

76+
/** @var int|null */
77+
private $optionalIntEnum;
78+
7679
/** @var DateTimeInterface|null */
7780
private $optionalDate;
7881

@@ -172,6 +175,13 @@ public function setOptionalEnum(string $optionalEnum): self
172175
return $this;
173176
}
174177

178+
public function setOptionalIntEnum(int $optionalIntEnum): self
179+
{
180+
$this->optionalIntEnum = $optionalIntEnum;
181+
182+
return $this;
183+
}
184+
175185
public function setOptionalDate(DateTimeInterface $optionalDate): self
176186
{
177187
$this->optionalDate = $optionalDate;
@@ -422,6 +432,11 @@ public function getOptionalEnum(): ?string
422432
return $this->optionalEnum;
423433
}
424434

435+
public function getOptionalIntEnum(): ?int
436+
{
437+
return $this->optionalIntEnum;
438+
}
439+
425440
public function getOptionalDate(): ?DateTimeInterface
426441
{
427442
return $this->optionalDate;
@@ -522,6 +537,9 @@ public function toArray(): array
522537
if ($this->optionalEnum !== null) {
523538
$fields['optionalEnum'] = $this->optionalEnum;
524539
}
540+
if ($this->optionalIntEnum !== null) {
541+
$fields['optionalIntEnum'] = $this->optionalIntEnum;
542+
}
525543
if ($this->optionalDate !== null) {
526544
$fields['optionalDate'] = $this->optionalDate->format(DATE_RFC3339);
527545
}

test/suite/functional/Generator/Schema/ItemPhp74.php

+17
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ class Item implements SerializableInterface, JsonSerializable
5858

5959
private ?string $optionalEnum = null;
6060

61+
private ?int $optionalIntEnum = null;
62+
6163
private ?DateTimeInterface $optionalDate = null;
6264

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

149+
public function setOptionalIntEnum(int $optionalIntEnum): self
150+
{
151+
$this->optionalIntEnum = $optionalIntEnum;
152+
153+
return $this;
154+
}
155+
147156
public function setOptionalDate(DateTimeInterface $optionalDate): self
148157
{
149158
$this->optionalDate = $optionalDate;
@@ -394,6 +403,11 @@ public function getOptionalEnum(): ?string
394403
return $this->optionalEnum;
395404
}
396405

406+
public function getOptionalIntEnum(): ?int
407+
{
408+
return $this->optionalIntEnum;
409+
}
410+
397411
public function getOptionalDate(): ?DateTimeInterface
398412
{
399413
return $this->optionalDate;
@@ -494,6 +508,9 @@ public function toArray(): array
494508
if ($this->optionalEnum !== null) {
495509
$fields['optionalEnum'] = $this->optionalEnum;
496510
}
511+
if ($this->optionalIntEnum !== null) {
512+
$fields['optionalIntEnum'] = $this->optionalIntEnum;
513+
}
497514
if ($this->optionalDate !== null) {
498515
$fields['optionalDate'] = $this->optionalDate->format(DATE_RFC3339);
499516
}

test/suite/functional/Generator/Schema/item.yaml

+6
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,12 @@ components:
8282
enum:
8383
- 'one option'
8484
- 'another option'
85+
optionalIntEnum:
86+
type: integer
87+
enum:
88+
- 0
89+
- 1
90+
- 2
8591
optionalDate:
8692
type: string
8793
format: 'date-time'

test/suite/functional/Input/ParserTest.php

-29
Original file line numberDiff line numberDiff line change
@@ -264,35 +264,6 @@ public function invalidSpecificationProvider()
264264
],
265265
],
266266
],
267-
'Only string enum supported' => [
268-
[
269-
'openapi' => '3.0.0',
270-
'info' => [
271-
'title' => 'Sample API',
272-
'version' => '1.0.0',
273-
],
274-
'paths' => [
275-
'/users' => [
276-
'get' => [
277-
'operationId' => 'getUsers',
278-
'responses' => [
279-
'200' => [
280-
'description' => 'OK',
281-
'content' => [
282-
'application/json' => [
283-
'schema' => [
284-
'type' => 'integer',
285-
'enum' => [4, 5, 6],
286-
],
287-
],
288-
],
289-
],
290-
],
291-
],
292-
],
293-
],
294-
],
295-
],
296267
'Invalid field name' => [
297268
[
298269
'openapi' => '3.0.0',

0 commit comments

Comments
 (0)