Skip to content

Commit c631d38

Browse files
Merge pull request #283 from nextcloud/bugfix/153/fix-optional-values
fix(openapi): Don't mark unknown default values as required
2 parents 3b0913d + 744ee17 commit c631d38

File tree

5 files changed

+302
-1
lines changed

5 files changed

+302
-1
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/lib/Controller/AdminSettingsController.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,4 +61,24 @@ public function movedToSettingsTag(): DataResponse {
6161
public function movedToSettingsTagUnnamed(): DataResponse {
6262
return new DataResponse();
6363
}
64+
65+
public const ALSO_OPTIONAL = 1;
66+
67+
/**
68+
* OCS Route with attribute
69+
*
70+
* @param int $option1 This is optional with magic number
71+
* @param int $option2 This is optional with constant
72+
* @return DataResponse<Http::STATUS_OK, list<empty>, array{}>
73+
*
74+
* 200: Success
75+
*/
76+
#[ApiRoute(verb: 'POST', url: '/optional-parameters')]
77+
public function optionalParameters(
78+
int $option1 = 0,
79+
int $option2 = self::ALSO_OPTIONAL,
80+
) {
81+
return DataResponse();
82+
}
83+
6484
}

tests/openapi-administration.json

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9259,6 +9259,145 @@
92599259
}
92609260
}
92619261
},
9262+
"/ocs/v2.php/apps/notifications/optional-parameters": {
9263+
"post": {
9264+
"operationId": "admin_settings-optional-parameters",
9265+
"summary": "OCS Route with attribute",
9266+
"description": "This endpoint requires admin access",
9267+
"tags": [
9268+
"admin_settings"
9269+
],
9270+
"security": [
9271+
{
9272+
"bearer_auth": []
9273+
},
9274+
{
9275+
"basic_auth": []
9276+
}
9277+
],
9278+
"requestBody": {
9279+
"required": false,
9280+
"content": {
9281+
"application/json": {
9282+
"schema": {
9283+
"type": "object",
9284+
"properties": {
9285+
"option1": {
9286+
"type": "integer",
9287+
"format": "int64",
9288+
"default": 0,
9289+
"description": "This is optional with magic number"
9290+
},
9291+
"option2": {
9292+
"type": "integer",
9293+
"format": "int64",
9294+
"description": "This is optional with constant"
9295+
}
9296+
}
9297+
}
9298+
}
9299+
}
9300+
},
9301+
"parameters": [
9302+
{
9303+
"name": "OCS-APIRequest",
9304+
"in": "header",
9305+
"description": "Required to be true for the API request to pass",
9306+
"required": true,
9307+
"schema": {
9308+
"type": "boolean",
9309+
"default": true
9310+
}
9311+
}
9312+
],
9313+
"responses": {
9314+
"200": {
9315+
"description": "Success",
9316+
"content": {
9317+
"application/json": {
9318+
"schema": {
9319+
"type": "object",
9320+
"required": [
9321+
"ocs"
9322+
],
9323+
"properties": {
9324+
"ocs": {
9325+
"type": "object",
9326+
"required": [
9327+
"meta",
9328+
"data"
9329+
],
9330+
"properties": {
9331+
"meta": {
9332+
"$ref": "#/components/schemas/OCSMeta"
9333+
},
9334+
"data": {}
9335+
}
9336+
}
9337+
}
9338+
}
9339+
}
9340+
}
9341+
},
9342+
"401": {
9343+
"description": "Current user is not logged in",
9344+
"content": {
9345+
"application/json": {
9346+
"schema": {
9347+
"type": "object",
9348+
"required": [
9349+
"ocs"
9350+
],
9351+
"properties": {
9352+
"ocs": {
9353+
"type": "object",
9354+
"required": [
9355+
"meta",
9356+
"data"
9357+
],
9358+
"properties": {
9359+
"meta": {
9360+
"$ref": "#/components/schemas/OCSMeta"
9361+
},
9362+
"data": {}
9363+
}
9364+
}
9365+
}
9366+
}
9367+
}
9368+
}
9369+
},
9370+
"403": {
9371+
"description": "Logged in account must be an admin",
9372+
"content": {
9373+
"application/json": {
9374+
"schema": {
9375+
"type": "object",
9376+
"required": [
9377+
"ocs"
9378+
],
9379+
"properties": {
9380+
"ocs": {
9381+
"type": "object",
9382+
"required": [
9383+
"meta",
9384+
"data"
9385+
],
9386+
"properties": {
9387+
"meta": {
9388+
"$ref": "#/components/schemas/OCSMeta"
9389+
},
9390+
"data": {}
9391+
}
9392+
}
9393+
}
9394+
}
9395+
}
9396+
}
9397+
}
9398+
}
9399+
}
9400+
},
92629401
"/ocs/v2.php/tests/attribute-ocs/{param}": {
92639402
"get": {
92649403
"operationId": "routing-attributeocs-route",

tests/openapi-full.json

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9459,6 +9459,145 @@
94599459
}
94609460
}
94619461
},
9462+
"/ocs/v2.php/apps/notifications/optional-parameters": {
9463+
"post": {
9464+
"operationId": "admin_settings-optional-parameters",
9465+
"summary": "OCS Route with attribute",
9466+
"description": "This endpoint requires admin access",
9467+
"tags": [
9468+
"admin_settings"
9469+
],
9470+
"security": [
9471+
{
9472+
"bearer_auth": []
9473+
},
9474+
{
9475+
"basic_auth": []
9476+
}
9477+
],
9478+
"requestBody": {
9479+
"required": false,
9480+
"content": {
9481+
"application/json": {
9482+
"schema": {
9483+
"type": "object",
9484+
"properties": {
9485+
"option1": {
9486+
"type": "integer",
9487+
"format": "int64",
9488+
"default": 0,
9489+
"description": "This is optional with magic number"
9490+
},
9491+
"option2": {
9492+
"type": "integer",
9493+
"format": "int64",
9494+
"description": "This is optional with constant"
9495+
}
9496+
}
9497+
}
9498+
}
9499+
}
9500+
},
9501+
"parameters": [
9502+
{
9503+
"name": "OCS-APIRequest",
9504+
"in": "header",
9505+
"description": "Required to be true for the API request to pass",
9506+
"required": true,
9507+
"schema": {
9508+
"type": "boolean",
9509+
"default": true
9510+
}
9511+
}
9512+
],
9513+
"responses": {
9514+
"200": {
9515+
"description": "Success",
9516+
"content": {
9517+
"application/json": {
9518+
"schema": {
9519+
"type": "object",
9520+
"required": [
9521+
"ocs"
9522+
],
9523+
"properties": {
9524+
"ocs": {
9525+
"type": "object",
9526+
"required": [
9527+
"meta",
9528+
"data"
9529+
],
9530+
"properties": {
9531+
"meta": {
9532+
"$ref": "#/components/schemas/OCSMeta"
9533+
},
9534+
"data": {}
9535+
}
9536+
}
9537+
}
9538+
}
9539+
}
9540+
}
9541+
},
9542+
"401": {
9543+
"description": "Current user is not logged in",
9544+
"content": {
9545+
"application/json": {
9546+
"schema": {
9547+
"type": "object",
9548+
"required": [
9549+
"ocs"
9550+
],
9551+
"properties": {
9552+
"ocs": {
9553+
"type": "object",
9554+
"required": [
9555+
"meta",
9556+
"data"
9557+
],
9558+
"properties": {
9559+
"meta": {
9560+
"$ref": "#/components/schemas/OCSMeta"
9561+
},
9562+
"data": {}
9563+
}
9564+
}
9565+
}
9566+
}
9567+
}
9568+
}
9569+
},
9570+
"403": {
9571+
"description": "Logged in account must be an admin",
9572+
"content": {
9573+
"application/json": {
9574+
"schema": {
9575+
"type": "object",
9576+
"required": [
9577+
"ocs"
9578+
],
9579+
"properties": {
9580+
"ocs": {
9581+
"type": "object",
9582+
"required": [
9583+
"meta",
9584+
"data"
9585+
],
9586+
"properties": {
9587+
"meta": {
9588+
"$ref": "#/components/schemas/OCSMeta"
9589+
},
9590+
"data": {}
9591+
}
9592+
}
9593+
}
9594+
}
9595+
}
9596+
}
9597+
}
9598+
}
9599+
}
9600+
},
94629601
"/ocs/v2.php/tests/attribute-ocs/{param}": {
94639602
"get": {
94649603
"operationId": "routing-attributeocs-route",

0 commit comments

Comments
 (0)