From f9b84197e52e89de4bcf7b4bb1f77f6f38ffe7e0 Mon Sep 17 00:00:00 2001 From: provokateurin Date: Wed, 17 Jan 2024 12:13:56 +0100 Subject: [PATCH] fix(OpenApiType): Deduplicate enums Signed-off-by: provokateurin --- src/OpenApiType.php | 6 + tests/appinfo/routes.php | 10 +- tests/lib/Controller/SettingsController.php | 72 +++ tests/openapi.json | 507 +++++++++++++++++++- 4 files changed, 591 insertions(+), 4 deletions(-) diff --git a/src/OpenApiType.php b/src/OpenApiType.php index 15a6635..492848e 100644 --- a/src/OpenApiType.php +++ b/src/OpenApiType.php @@ -289,6 +289,12 @@ private static function mergeEnums(array $types): array { } } + foreach (array_map(static fn (OpenApiType $type) => $type->type, $nonEnums) as $type) { + if (array_key_exists($type, $enums)) { + unset($enums[$type]); + } + } + return array_merge($nonEnums, array_map(fn (string $type) => new OpenApiType(type: $type, enum: $enums[$type]), array_keys($enums))); } diff --git a/tests/appinfo/routes.php b/tests/appinfo/routes.php index c0ef2c2..e17e0e6 100644 --- a/tests/appinfo/routes.php +++ b/tests/appinfo/routes.php @@ -45,8 +45,14 @@ ['name' => 'Settings#listOfIntStringAndOneBool', 'url' => '/api/{apiVersion}/mixed-list-one', 'verb' => 'POST', 'requirements' => ['apiVersion' => '(v2)']], ['name' => 'Settings#listOfIntStringAndAllBools', 'url' => '/api/{apiVersion}/mixed-list-all', 'verb' => 'POST', 'requirements' => ['apiVersion' => '(v2)']], ['name' => 'Settings#booleanParameterRequired', 'url' => '/api/{apiVersion}/boolean', 'verb' => 'POST', 'requirements' => ['apiVersion' => '(v2)']], - ['name' => 'Settings#booleanParameterDefaultFalse', 'url' => '/api/{apiVersion}/boolean-false', 'verb' => 'POST', 'requirements' => ['apiVersion' => '(v2)']], - ['name' => 'Settings#booleanParameterDefaultTrue', 'url' => '/api/{apiVersion}/boolean-true', 'verb' => 'POST', 'requirements' => ['apiVersion' => '(v2)']], + ['name' => 'Settings#booleanParameterDefaultFalse', 'url' => '/api/{apiVersion}/boolean-default-false', 'verb' => 'POST', 'requirements' => ['apiVersion' => '(v2)']], + ['name' => 'Settings#booleanParameterDefaultTrue', 'url' => '/api/{apiVersion}/boolean-default-true', 'verb' => 'POST', 'requirements' => ['apiVersion' => '(v2)']], + ['name' => 'Settings#booleanTrueParameter', 'url' => '/api/{apiVersion}/boolean-true', 'verb' => 'POST', 'requirements' => ['apiVersion' => '(v2)']], + ['name' => 'Settings#booleanFalseParameter', 'url' => '/api/{apiVersion}/boolean-false', 'verb' => 'POST', 'requirements' => ['apiVersion' => '(v2)']], + ['name' => 'Settings#booleanTrueFalseParameter', 'url' => '/api/{apiVersion}/boolean-true-false', 'verb' => 'POST', 'requirements' => ['apiVersion' => '(v2)']], + ['name' => 'Settings#trueFalseParameter', 'url' => '/api/{apiVersion}/true-false', 'verb' => 'POST', 'requirements' => ['apiVersion' => '(v2)']], + ['name' => 'Settings#stringValueParameter', 'url' => '/api/{apiVersion}/string-value', 'verb' => 'POST', 'requirements' => ['apiVersion' => '(v2)']], + ['name' => 'Settings#intValueParameter', 'url' => '/api/{apiVersion}/int-value', 'verb' => 'POST', 'requirements' => ['apiVersion' => '(v2)']], ['name' => 'Settings#numericParameter', 'url' => '/api/{apiVersion}/numeric', 'verb' => 'POST', 'requirements' => ['apiVersion' => '(v2)']], ], ]; diff --git a/tests/lib/Controller/SettingsController.php b/tests/lib/Controller/SettingsController.php index 4efbf2f..71f9878 100644 --- a/tests/lib/Controller/SettingsController.php +++ b/tests/lib/Controller/SettingsController.php @@ -270,6 +270,78 @@ public function booleanParameterDefaultTrue(bool $yesOrNo = true): DataResponse return new DataResponse(); } + /** + * A route with boolean or true + * + * @param bool|true $yesOrNo boolean or true + * @return DataResponse, array{}> + * + * 200: Admin settings updated + */ + public function booleanTrueParameter(bool $yesOrNo): DataResponse { + return new DataResponse(); + } + + /** + * A route with boolean or false + * + * @param bool|false $yesOrNo boolean or false + * @return DataResponse, array{}> + * + * 200: Admin settings updated + */ + public function booleanFalseParameter(bool $yesOrNo): DataResponse { + return new DataResponse(); + } + + /** + * A route with boolean or true or false + * + * @param bool|true|false $yesOrNo boolean or true or false + * @return DataResponse, array{}> + * + * 200: Admin settings updated + */ + public function booleanTrueFalseParameter(bool $yesOrNo): DataResponse { + return new DataResponse(); + } + + /** + * A route with true or false + * + * @param true|false $yesOrNo true or false + * @return DataResponse, array{}> + * + * 200: Admin settings updated + */ + public function trueFalseParameter(bool $yesOrNo): DataResponse { + return new DataResponse(); + } + + /** + * A route with string or 'test' + * + * @param string|'test' $value string or 'test' + * @return DataResponse, array{}> + * + * 200: Admin settings updated + */ + public function stringValueParameter(string $value): DataResponse { + return new DataResponse(); + } + + /** + * A route with int or 0 + * + * @param int|0 $value int or 0 + * @return DataResponse, array{}> + * + * 200: Admin settings updated + */ + public function intValueParameter(int $value): DataResponse { + return new DataResponse(); + } + /** * A route with numeric * diff --git a/tests/openapi.json b/tests/openapi.json index 2d70af8..501197f 100644 --- a/tests/openapi.json +++ b/tests/openapi.json @@ -1563,7 +1563,7 @@ } } }, - "/ocs/v2.php/apps/notifications/api/{apiVersion}/boolean-false": { + "/ocs/v2.php/apps/notifications/api/{apiVersion}/boolean-default-false": { "post": { "operationId": "settings-boolean-parameter-default-false", "summary": "A route with boolean defaulting to false", @@ -1648,7 +1648,7 @@ } } }, - "/ocs/v2.php/apps/notifications/api/{apiVersion}/boolean-true": { + "/ocs/v2.php/apps/notifications/api/{apiVersion}/boolean-default-true": { "post": { "operationId": "settings-boolean-parameter-default-true", "summary": "A route with boolean defaulting to true", @@ -1733,6 +1733,509 @@ } } }, + "/ocs/v2.php/apps/notifications/api/{apiVersion}/boolean-true": { + "post": { + "operationId": "settings-boolean-true-parameter", + "summary": "A route with boolean or true", + "description": "This endpoint requires admin access", + "tags": [ + "settings" + ], + "security": [ + { + "bearer_auth": [] + }, + { + "basic_auth": [] + } + ], + "parameters": [ + { + "name": "yesOrNo", + "in": "query", + "description": "boolean or true", + "required": true, + "schema": { + "type": "integer", + "enum": [ + 0, + 1 + ] + } + }, + { + "name": "apiVersion", + "in": "path", + "required": true, + "schema": { + "type": "string", + "enum": [ + "v2" + ], + "default": "v2" + } + }, + { + "name": "OCS-APIRequest", + "in": "header", + "description": "Required to be true for the API request to pass", + "required": true, + "schema": { + "type": "boolean", + "default": true + } + } + ], + "responses": { + "200": { + "description": "Admin settings updated", + "content": { + "application/json": { + "schema": { + "type": "object", + "required": [ + "ocs" + ], + "properties": { + "ocs": { + "type": "object", + "required": [ + "meta", + "data" + ], + "properties": { + "meta": { + "$ref": "#/components/schemas/OCSMeta" + }, + "data": {} + } + } + } + } + } + } + } + } + } + }, + "/ocs/v2.php/apps/notifications/api/{apiVersion}/boolean-false": { + "post": { + "operationId": "settings-boolean-false-parameter", + "summary": "A route with boolean or false", + "description": "This endpoint requires admin access", + "tags": [ + "settings" + ], + "security": [ + { + "bearer_auth": [] + }, + { + "basic_auth": [] + } + ], + "parameters": [ + { + "name": "yesOrNo", + "in": "query", + "description": "boolean or false", + "required": true, + "schema": { + "type": "integer", + "enum": [ + 0, + 1 + ] + } + }, + { + "name": "apiVersion", + "in": "path", + "required": true, + "schema": { + "type": "string", + "enum": [ + "v2" + ], + "default": "v2" + } + }, + { + "name": "OCS-APIRequest", + "in": "header", + "description": "Required to be true for the API request to pass", + "required": true, + "schema": { + "type": "boolean", + "default": true + } + } + ], + "responses": { + "200": { + "description": "Admin settings updated", + "content": { + "application/json": { + "schema": { + "type": "object", + "required": [ + "ocs" + ], + "properties": { + "ocs": { + "type": "object", + "required": [ + "meta", + "data" + ], + "properties": { + "meta": { + "$ref": "#/components/schemas/OCSMeta" + }, + "data": {} + } + } + } + } + } + } + } + } + } + }, + "/ocs/v2.php/apps/notifications/api/{apiVersion}/boolean-true-false": { + "post": { + "operationId": "settings-boolean-true-false-parameter", + "summary": "A route with boolean or true or false", + "description": "This endpoint requires admin access", + "tags": [ + "settings" + ], + "security": [ + { + "bearer_auth": [] + }, + { + "basic_auth": [] + } + ], + "parameters": [ + { + "name": "yesOrNo", + "in": "query", + "description": "boolean or true or false", + "required": true, + "schema": { + "type": "integer", + "enum": [ + 0, + 1 + ] + } + }, + { + "name": "apiVersion", + "in": "path", + "required": true, + "schema": { + "type": "string", + "enum": [ + "v2" + ], + "default": "v2" + } + }, + { + "name": "OCS-APIRequest", + "in": "header", + "description": "Required to be true for the API request to pass", + "required": true, + "schema": { + "type": "boolean", + "default": true + } + } + ], + "responses": { + "200": { + "description": "Admin settings updated", + "content": { + "application/json": { + "schema": { + "type": "object", + "required": [ + "ocs" + ], + "properties": { + "ocs": { + "type": "object", + "required": [ + "meta", + "data" + ], + "properties": { + "meta": { + "$ref": "#/components/schemas/OCSMeta" + }, + "data": {} + } + } + } + } + } + } + } + } + } + }, + "/ocs/v2.php/apps/notifications/api/{apiVersion}/true-false": { + "post": { + "operationId": "settings-true-false-parameter", + "summary": "A route with true or false", + "description": "This endpoint requires admin access", + "tags": [ + "settings" + ], + "security": [ + { + "bearer_auth": [] + }, + { + "basic_auth": [] + } + ], + "parameters": [ + { + "name": "yesOrNo", + "in": "query", + "description": "true or false", + "required": true, + "schema": { + "type": "integer", + "enum": [ + 0, + 1 + ] + } + }, + { + "name": "apiVersion", + "in": "path", + "required": true, + "schema": { + "type": "string", + "enum": [ + "v2" + ], + "default": "v2" + } + }, + { + "name": "OCS-APIRequest", + "in": "header", + "description": "Required to be true for the API request to pass", + "required": true, + "schema": { + "type": "boolean", + "default": true + } + } + ], + "responses": { + "200": { + "description": "Admin settings updated", + "content": { + "application/json": { + "schema": { + "type": "object", + "required": [ + "ocs" + ], + "properties": { + "ocs": { + "type": "object", + "required": [ + "meta", + "data" + ], + "properties": { + "meta": { + "$ref": "#/components/schemas/OCSMeta" + }, + "data": {} + } + } + } + } + } + } + } + } + } + }, + "/ocs/v2.php/apps/notifications/api/{apiVersion}/string-value": { + "post": { + "operationId": "settings-string-value-parameter", + "summary": "A route with string or 'test'", + "description": "This endpoint requires admin access", + "tags": [ + "settings" + ], + "security": [ + { + "bearer_auth": [] + }, + { + "basic_auth": [] + } + ], + "parameters": [ + { + "name": "value", + "in": "query", + "description": "string or 'test'", + "required": true, + "schema": { + "type": "string" + } + }, + { + "name": "apiVersion", + "in": "path", + "required": true, + "schema": { + "type": "string", + "enum": [ + "v2" + ], + "default": "v2" + } + }, + { + "name": "OCS-APIRequest", + "in": "header", + "description": "Required to be true for the API request to pass", + "required": true, + "schema": { + "type": "boolean", + "default": true + } + } + ], + "responses": { + "200": { + "description": "Admin settings updated", + "content": { + "application/json": { + "schema": { + "type": "object", + "required": [ + "ocs" + ], + "properties": { + "ocs": { + "type": "object", + "required": [ + "meta", + "data" + ], + "properties": { + "meta": { + "$ref": "#/components/schemas/OCSMeta" + }, + "data": {} + } + } + } + } + } + } + } + } + } + }, + "/ocs/v2.php/apps/notifications/api/{apiVersion}/int-value": { + "post": { + "operationId": "settings-int-value-parameter", + "summary": "A route with int or 0", + "description": "This endpoint requires admin access", + "tags": [ + "settings" + ], + "security": [ + { + "bearer_auth": [] + }, + { + "basic_auth": [] + } + ], + "parameters": [ + { + "name": "value", + "in": "query", + "description": "int or 0", + "required": true, + "schema": { + "type": "integer", + "format": "int64" + } + }, + { + "name": "apiVersion", + "in": "path", + "required": true, + "schema": { + "type": "string", + "enum": [ + "v2" + ], + "default": "v2" + } + }, + { + "name": "OCS-APIRequest", + "in": "header", + "description": "Required to be true for the API request to pass", + "required": true, + "schema": { + "type": "boolean", + "default": true + } + } + ], + "responses": { + "200": { + "description": "Admin settings updated", + "content": { + "application/json": { + "schema": { + "type": "object", + "required": [ + "ocs" + ], + "properties": { + "ocs": { + "type": "object", + "required": [ + "meta", + "data" + ], + "properties": { + "meta": { + "$ref": "#/components/schemas/OCSMeta" + }, + "data": {} + } + } + } + } + } + } + } + } + } + }, "/ocs/v2.php/apps/notifications/api/{apiVersion}/numeric": { "post": { "operationId": "settings-numeric-parameter",