Skip to content

Commit 7dc3bdd

Browse files
committed
bugfix: required properties are now listed correctly
1 parent 97728d7 commit 7dc3bdd

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

app/helpers/Swagger/AnnotationData.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,17 @@ class AnnotationData
1313

1414
public string $className;
1515
public string $methodName;
16+
/**
17+
* @var AnnotationParameterData[]
18+
*/
1619
public array $pathParams;
20+
/**
21+
* @var AnnotationParameterData[]
22+
*/
1723
public array $queryParams;
24+
/**
25+
* @var AnnotationParameterData[]
26+
*/
1827
public array $bodyParams;
1928
public ?string $endpointDescription;
2029

@@ -68,9 +77,26 @@ private function getBodyAnnotation(): string | null
6877
// only json is supported due to the media type
6978
$head = '@OA\RequestBody(@OA\MediaType(mediaType="application/json",@OA\Schema';
7079
$body = new ParenthesesBuilder();
80+
// list of all required properties
81+
$required = [];
7182

7283
foreach ($this->bodyParams as $bodyParam) {
7384
$body->addValue($bodyParam->toPropertyAnnotation());
85+
if ($bodyParam->required) {
86+
$required[] = $bodyParam->name;
87+
}
88+
}
89+
90+
// add a list of required properties
91+
if (count($required) > 0) {
92+
// stringify the list (it has to be in '{"name1","name1",...}' format)
93+
$requiredString = "{";
94+
foreach ($required as $name) {
95+
$requiredString .= "\"$name\",";
96+
}
97+
$requiredString .= "}";
98+
99+
$body->addValue("required=" . $requiredString);
74100
}
75101

76102
return $head . $body->toString() . "))";

app/helpers/Swagger/AnnotationParameterData.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,6 @@ public function toPropertyAnnotation(): string
128128
$body->addKeyValue("property", $this->name);
129129
$body->addKeyValue("type", $this->swaggerType);
130130
$body->addKeyValue("nullable", $this->nullable);
131-
$body->addKeyValue("required", $this->required);
132131

133132
if ($this->description !== null) {
134133
$body->addKeyValue("description", $this->description);

0 commit comments

Comments
 (0)