Skip to content

Commit 48020e7

Browse files
committed
fixed bad swagger string patterns
1 parent 1c82ccf commit 48020e7

1 file changed

Lines changed: 34 additions & 29 deletions

File tree

app/helpers/Swagger/ParameterConstraints.php

Lines changed: 34 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -4,36 +4,41 @@
44

55
class ParameterConstraints
66
{
7-
private array $constraints;
8-
9-
/**
10-
* Constructs a container for swagger constraints.
11-
* Constructor parameter names match swagger keywords, see
12-
* https://swagger.io/docs/specification/v3_0/data-models/keywords/.
13-
* @param ?string $pattern String regex pattern.
14-
* @param ?int $minLength String min length.
15-
* @param ?int $maxLength String max length.
16-
*/
17-
public function __construct(?string $pattern = null, ?int $minLength = null, ?int $maxLength = null)
18-
{
19-
$this->constraints["pattern"] = $pattern;
20-
$this->constraints["minLength"] = $minLength;
21-
$this->constraints["maxLength"] = $maxLength;
22-
}
7+
private array $constraints;
238

24-
/**
25-
* Adds constraints to a ParenthesesBuilder for swagger doc construction.
26-
* @param \App\Helpers\Swagger\ParenthesesBuilder $container The container for keywords and values.
27-
*/
28-
public function addConstraints(ParenthesesBuilder $container)
29-
{
30-
foreach ($this->constraints as $keyword=>$value) {
31-
// skip null values
32-
if ($value === null) {
33-
continue;
34-
}
9+
/**
10+
* Constructs a container for swagger constraints.
11+
* Constructor parameter names match swagger keywords, see
12+
* https://swagger.io/docs/specification/v3_0/data-models/keywords/.
13+
* @param ?string $pattern String regex pattern.
14+
* @param ?int $minLength String min length.
15+
* @param ?int $maxLength String max length.
16+
*/
17+
public function __construct(?string $pattern = null, ?int $minLength = null, ?int $maxLength = null)
18+
{
19+
# swagger patterns must not contain the bounding '/.../' slashes
20+
if ($pattern != null && strlen($pattern) >= 2) {
21+
$pattern = substr($pattern, 1, -1);
22+
}
3523

36-
$container->addKeyValue($keyword, $value);
24+
$this->constraints["pattern"] = $pattern;
25+
$this->constraints["minLength"] = $minLength;
26+
$this->constraints["maxLength"] = $maxLength;
27+
}
28+
29+
/**
30+
* Adds constraints to a ParenthesesBuilder for swagger doc construction.
31+
* @param \App\Helpers\Swagger\ParenthesesBuilder $container The container for keywords and values.
32+
*/
33+
public function addConstraints(ParenthesesBuilder $container)
34+
{
35+
foreach ($this->constraints as $keyword => $value) {
36+
// skip null values
37+
if ($value === null) {
38+
continue;
39+
}
40+
41+
$container->addKeyValue($keyword, $value);
42+
}
3743
}
38-
}
3944
}

0 commit comments

Comments
 (0)