Skip to content

Commit d86718f

Browse files
authored
Remove enum type validation from generated client
1 parent 83afa98 commit d86718f

18 files changed

+36
-323
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@ vendor
77
phpstan.neon
88
phpunit.xml
99
.php_cs.cache
10+
/build/logs

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+
## [6.0.0] - 2021-06-22
8+
### Changed
9+
- `enum` type are not anymore validated from generated client side.
10+
711
## [5.7.1] - 2021-05-31
812
### Fixed
913
- `nullable: true` for mandatory $ref was not checking for null before trying to map

example/gen/src/Request/FindPetsByStatusRequest.php

-15
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,10 @@
88

99
namespace OpenApi\PetStoreClient\Request;
1010

11-
use DoclerLabs\ApiClientException\RequestValidationException;
1211
use OpenApi\PetStoreClient\Schema\SerializableInterface;
1312

1413
class FindPetsByStatusRequest implements RequestInterface
1514
{
16-
public const STATUS_AVAILABLE = 'available';
17-
18-
public const STATUS_PENDING = 'pending';
19-
20-
public const STATUS_SOLD = 'sold';
21-
22-
public const ALLOWED_STATUS_LIST = [self::STATUS_AVAILABLE, self::STATUS_PENDING, self::STATUS_SOLD];
23-
2415
private ?string $status = null;
2516

2617
private string $contentType = '';
@@ -30,14 +21,8 @@ public function getContentType(): string
3021
return $this->contentType;
3122
}
3223

33-
/**
34-
* @throws RequestValidationException
35-
*/
3624
public function setStatus(string $status): self
3725
{
38-
if (! \in_array($status, self::ALLOWED_STATUS_LIST, true)) {
39-
throw new RequestValidationException(\sprintf('Invalid %s value. Given: `%s` Allowed: %s', 'status', $status, \json_encode(self::ALLOWED_STATUS_LIST)));
40-
}
4126
$this->status = $status;
4227

4328
return $this;

example/gen/src/Schema/Order.php

-15
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,10 @@
99
namespace OpenApi\PetStoreClient\Schema;
1010

1111
use DateTimeInterface;
12-
use DoclerLabs\ApiClientException\RequestValidationException;
1312
use JsonSerializable;
1413

1514
class Order implements SerializableInterface, JsonSerializable
1615
{
17-
public const STATUS_PLACED = 'placed';
18-
19-
public const STATUS_APPROVED = 'approved';
20-
21-
public const STATUS_DELIVERED = 'delivered';
22-
23-
public const ALLOWED_STATUS_LIST = [self::STATUS_PLACED, self::STATUS_APPROVED, self::STATUS_DELIVERED];
24-
2516
private ?int $id = null;
2617

2718
private ?int $petId = null;
@@ -62,14 +53,8 @@ public function setShipDate(DateTimeInterface $shipDate): self
6253
return $this;
6354
}
6455

65-
/**
66-
* @throws RequestValidationException
67-
*/
6856
public function setStatus(string $status): self
6957
{
70-
if (! \in_array($status, self::ALLOWED_STATUS_LIST, true)) {
71-
throw new RequestValidationException(\sprintf('Invalid %s value. Given: `%s` Allowed: %s', 'status', $status, \json_encode(self::ALLOWED_STATUS_LIST)));
72-
}
7358
$this->status = $status;
7459

7560
return $this;

example/gen/src/Schema/Pet.php

-15
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,10 @@
88

99
namespace OpenApi\PetStoreClient\Schema;
1010

11-
use DoclerLabs\ApiClientException\RequestValidationException;
1211
use JsonSerializable;
1312

1413
class Pet implements SerializableInterface, JsonSerializable
1514
{
16-
public const STATUS_AVAILABLE = 'available';
17-
18-
public const STATUS_PENDING = 'pending';
19-
20-
public const STATUS_SOLD = 'sold';
21-
22-
public const ALLOWED_STATUS_LIST = [self::STATUS_AVAILABLE, self::STATUS_PENDING, self::STATUS_SOLD];
23-
2415
private ?int $id = null;
2516

2617
private string $name;
@@ -63,14 +54,8 @@ public function setTags(TagCollection $tags): self
6354
return $this;
6455
}
6556

66-
/**
67-
* @throws RequestValidationException
68-
*/
6957
public function setStatus(string $status): self
7058
{
71-
if (! \in_array($status, self::ALLOWED_STATUS_LIST, true)) {
72-
throw new RequestValidationException(\sprintf('Invalid %s value. Given: `%s` Allowed: %s', 'status', $status, \json_encode(self::ALLOWED_STATUS_LIST)));
73-
}
7459
$this->status = $status;
7560

7661
return $this;

example/test-example.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
$client = $factory->create($client);
2121

2222
$request = new FindPetsByStatusRequest();
23-
$request->setStatus(FindPetsByStatusRequest::STATUS_SOLD);
23+
$request->setStatus('sold');
2424
$result = $client->findPetsByStatus($request);
2525
if ($result === null || $result->count() === 0) {
2626
sprintf('findPetsByStatus failed, result: %s', json_encode($result, JSON_THROW_ON_ERROR)) || exit(1);

src/Entity/Field.php

+3-14
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
<?php declare(strict_types=1);
1+
<?php
2+
3+
declare(strict_types=1);
24

35
namespace DoclerLabs\ApiClientGenerator\Entity;
46

@@ -21,7 +23,6 @@ class Field
2123
private bool $nullable;
2224
private ?Field $arrayItem = null;
2325
private array $objectProperties = [];
24-
private array $enumValues = [];
2526
private string $format = '';
2627
/** @phpstan-ignore-next-line cannot use strict type before PHP8 with "mixed" pseudo type */
2728
private $default;
@@ -73,18 +74,6 @@ public function setObjectProperties(array $objectProperties): self
7374
return $this;
7475
}
7576

76-
public function getEnumValues(): ?array
77-
{
78-
return $this->enumValues;
79-
}
80-
81-
public function setEnumValues(array $enumValues): self
82-
{
83-
$this->enumValues = $enumValues;
84-
85-
return $this;
86-
}
87-
8877
public function getFormat(): ?string
8978
{
9079
return $this->format;

src/Generator/MutatorAccessorClassGeneratorAbstract.php

+9-70
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
<?php declare(strict_types=1);
1+
<?php
2+
3+
declare(strict_types=1);
24

35
namespace DoclerLabs\ApiClientGenerator\Generator;
46

@@ -8,9 +10,7 @@
810
use DoclerLabs\ApiClientGenerator\Entity\Constraint\ConstraintInterface;
911
use DoclerLabs\ApiClientGenerator\Entity\Field;
1012
use DoclerLabs\ApiClientGenerator\Input\Specification;
11-
use DoclerLabs\ApiClientGenerator\Naming\SchemaNaming;
1213
use DoclerLabs\ApiClientGenerator\Output\Php\PhpFileCollection;
13-
use PhpParser\Node\Stmt;
1414
use PhpParser\Node\Stmt\ClassMethod;
1515
use PhpParser\Node\Stmt\Property;
1616

@@ -22,10 +22,11 @@ abstract public function generate(Specification $specification, PhpFileCollectio
2222

2323
protected function generateValidationStmts(Field $field): array
2424
{
25-
return array_filter([
26-
$this->generateEnumValidation($field),
27-
...$this->generateConstraints($field)
28-
]);
25+
return array_filter(
26+
[
27+
...$this->generateConstraints($field),
28+
]
29+
);
2930
}
3031

3132
protected function generateSet(Field $field): ClassMethod
@@ -97,68 +98,6 @@ protected function getGetMethodName(Field $field): string
9798
return sprintf('get%s', ucfirst($field->getPhpVariableName()));
9899
}
99100

100-
protected function generateEnumStatements(Field $field): array
101-
{
102-
$statements = [];
103-
$enumValues = $field->getEnumValues();
104-
if (!empty($enumValues)) {
105-
$enumConstCalls = [];
106-
foreach ($enumValues as $enumValue) {
107-
$constName = SchemaNaming::getEnumConstName($field, $enumValue);
108-
$statements[] = $this->builder->constant(
109-
$constName,
110-
$this->builder->val($enumValue)
111-
);
112-
113-
$enumConstCalls[] = $this->builder->classConstFetch('self', $constName);
114-
}
115-
$statements[] = $this->builder->constant(
116-
SchemaNaming::getAllowedEnumConstName($field),
117-
$this->builder->array($enumConstCalls)
118-
);
119-
}
120-
121-
return $statements;
122-
}
123-
124-
protected function generateEnumValidation(Field $root): ?Stmt
125-
{
126-
$enumValues = $root->getEnumValues();
127-
if (empty($enumValues)) {
128-
return null;
129-
}
130-
131-
$this
132-
->addImport(RequestValidationException::class);
133-
134-
$propertyVar = $this->builder->var($root->getPhpVariableName());
135-
$allowedConstFetch = $this->builder->classConstFetch(
136-
'self',
137-
SchemaNaming::getAllowedEnumConstName($root)
138-
);
139-
140-
$inArrayArgs = [
141-
$propertyVar,
142-
$allowedConstFetch,
143-
$this->builder->val(true),
144-
];
145-
$ifCondition = $this->builder->not($this->builder->funcCall('in_array', $inArrayArgs));
146-
147-
$exceptionMessage = $this->builder->funcCall(
148-
'sprintf',
149-
[
150-
'Invalid %s value. Given: `%s` Allowed: %s',
151-
$root->getName(),
152-
$propertyVar,
153-
$this->builder->funcCall('json_encode', [$allowedConstFetch]),
154-
]
155-
);
156-
157-
$ifStmt = $this->builder->throw('RequestValidationException', $exceptionMessage);
158-
159-
return $this->builder->if($ifCondition, [$ifStmt]);
160-
}
161-
162101
protected function generateConstraints(Field $root): array
163102
{
164103
$stmts = [];
@@ -182,7 +121,7 @@ protected function generateConstraints(Field $root): array
182121
$stmts[] = $this->builder->if(
183122
$constraint->getIfCondition($propertyVar, $this->builder),
184123
[
185-
$this->builder->throw('RequestValidationException', $exceptionMessage)
124+
$this->builder->throw('RequestValidationException', $exceptionMessage),
186125
]
187126
);
188127
}

src/Generator/RequestGenerator.php

-15
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ protected function generateRequest(PhpFileCollection $fileRegistry, Operation $o
3636
$classBuilder = $this->builder
3737
->class($className)
3838
->implement('RequestInterface')
39-
->addStmts($this->generateEnums($request))
4039
->addStmts($this->generateProperties($request))
4140
->addStmt($this->generateConstructor($request))
4241
->addStmt($this->generateGetContentType())
@@ -48,20 +47,6 @@ protected function generateRequest(PhpFileCollection $fileRegistry, Operation $o
4847
$this->registerFile($fileRegistry, $classBuilder, self::SUBDIRECTORY, self::NAMESPACE_SUBPATH);
4948
}
5049

51-
protected function generateEnums(Request $request): array
52-
{
53-
$statements = [];
54-
foreach ($request->getFields() as $fields) {
55-
foreach ($fields as $field) {
56-
foreach ($this->generateEnumStatements($field) as $statement) {
57-
$statements[] = $statement;
58-
}
59-
}
60-
}
61-
62-
return $statements;
63-
}
64-
6550
protected function generateProperties(Request $request): array
6651
{
6752
$statements = [];

src/Generator/SchemaGenerator.php

+3-14
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
<?php declare(strict_types=1);
1+
<?php
2+
3+
declare(strict_types=1);
24

35
namespace DoclerLabs\ApiClientGenerator\Generator;
46

@@ -34,7 +36,6 @@ protected function generateSchema(Field $field, PhpFileCollection $fileRegistry)
3436
$classBuilder = $this->builder
3537
->class($className)
3638
->implement('SerializableInterface', 'JsonSerializable')
37-
->addStmts($this->generateEnumConsts($field))
3839
->addStmts($this->generateProperties($field))
3940
->addStmt($this->generateConstructor($field))
4041
->addStmts($this->generateSetMethods($field))
@@ -45,18 +46,6 @@ protected function generateSchema(Field $field, PhpFileCollection $fileRegistry)
4546
$this->registerFile($fileRegistry, $classBuilder, self::SUBDIRECTORY, self::NAMESPACE_SUBPATH);
4647
}
4748

48-
protected function generateEnumConsts(Field $root): array
49-
{
50-
$statements = [];
51-
foreach ($root->getObjectProperties() as $propertyField) {
52-
foreach ($this->generateEnumStatements($propertyField) as $statement) {
53-
$statements[] = $statement;
54-
}
55-
}
56-
57-
return $statements;
58-
}
59-
6049
protected function generateProperties(Field $root): array
6150
{
6251
$statements = [];

src/Input/Factory/FieldFactory.php

+3-6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
<?php declare(strict_types=1);
1+
<?php
2+
3+
declare(strict_types=1);
24

35
namespace DoclerLabs\ApiClientGenerator\Input\Factory;
46

@@ -116,11 +118,6 @@ public function create(
116118
$field->setArrayItem($arrayItem);
117119
} elseif (!empty($objectProperties)) {
118120
$field->setObjectProperties($objectProperties);
119-
} elseif (isset($schema->enum)) {
120-
if (!FieldType::isSpecificationTypeString($type)) {
121-
throw new InvalidSpecificationException('Only string enum fields are currently supported');
122-
}
123-
$field->setEnumValues($schema->enum);
124121
}
125122

126123
if (isset($schema->format)) {

0 commit comments

Comments
 (0)