Skip to content

Commit 744ee17

Browse files
committed
fix(openapi): Don't mark unknown default values as required
Signed-off-by: Joas Schilling <[email protected]>
1 parent d03a6cd commit 744ee17

File tree

4 files changed

+6
-9
lines changed

4 files changed

+6
-9
lines changed

src/ControllerMethod.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -545,6 +545,8 @@ public static function parse(string $context,
545545
$type->defaultValue = Helpers::exprToValue($context, $methodParameter->default);
546546
$type->hasDefaultValue = true;
547547
} catch (UnsupportedExprException $e) {
548+
$type->hasDefaultValue = true;
549+
$type->hasUnknownDefaultValue = true;
548550
Logger::debug($context, $e);
549551
}
550552
}

src/OpenApiType.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ public function __construct(
4242
public ?string $format = null,
4343
public bool $nullable = false,
4444
public bool $hasDefaultValue = false,
45+
public bool $hasUnknownDefaultValue = false,
4546
public bool $deprecated = false,
4647
public mixed $defaultValue = null,
4748
public ?OpenApiType $items = null,
@@ -92,7 +93,7 @@ public function toArray(bool $isParameter = false): array|stdClass {
9293
if ($this->deprecated) {
9394
$values['deprecated'] = true;
9495
}
95-
if ($this->hasDefaultValue) {
96+
if ($this->hasDefaultValue && !$this->hasUnknownDefaultValue) {
9697
$values['default'] = $this->type === 'object' && is_array($this->defaultValue) && count($this->defaultValue) === 0 ? new stdClass() : $this->defaultValue;
9798
}
9899
if ($this->enum !== null) {

tests/openapi-administration.json

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9276,14 +9276,11 @@
92769276
}
92779277
],
92789278
"requestBody": {
9279-
"required": true,
9279+
"required": false,
92809280
"content": {
92819281
"application/json": {
92829282
"schema": {
92839283
"type": "object",
9284-
"required": [
9285-
"option2"
9286-
],
92879284
"properties": {
92889285
"option1": {
92899286
"type": "integer",

tests/openapi-full.json

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9476,14 +9476,11 @@
94769476
}
94779477
],
94789478
"requestBody": {
9479-
"required": true,
9479+
"required": false,
94809480
"content": {
94819481
"application/json": {
94829482
"schema": {
94839483
"type": "object",
9484-
"required": [
9485-
"option2"
9486-
],
94879484
"properties": {
94889485
"option1": {
94899486
"type": "integer",

0 commit comments

Comments
 (0)