From f58a521a39349460fe754f09dc9f4f10e19a15d1 Mon Sep 17 00:00:00 2001 From: Daniel Jakob Date: Fri, 5 Nov 2021 08:00:21 +0100 Subject: [PATCH] Don't make assumptions on how versioning should work Api-Method versioning should completly get handled in userspace. The library itself should be version-agnostic --- README.md | 4 ++-- dist/request.json | 11 +++++------ src/Contract/MethodProviderInterface.php | 4 +--- src/Dispatch/MethodDispatcher.php | 7 +------ tests/Dispatch/MethodDispatcherTest.php | 8 +++----- 5 files changed, 12 insertions(+), 22 deletions(-) diff --git a/README.md b/README.md index d43157f..3b1fe31 100644 --- a/README.md +++ b/README.md @@ -12,7 +12,7 @@ to the json schema validator and work with the data right away. Every method needs a corresponding schema-file which describes, how the request data should like alike. You can find a simple example in the `example/schema`-folder. -Every request has also to follow a basic schema (see `dist/request.json`) which contains informations about the method and the methods version. +Every request has also to follow a basic schema (see `dist/request.json`) which contains informations about the method. ## Requirements @@ -99,5 +99,5 @@ Just cd to the example-folder and fire up the the php internal webserver `php -S Now you can send `POST`-Requests to the api like this curl-request. ```shell script -curl -X POST -d '{"method": "beerlist", "version": null, "parameter": {"test1": "foobar", "test2": 666}}' "http://localhost:8888" +curl -X POST -d '{"method": "beerlist", "parameter": {"test1": "foobar", "test2": 666}}' "http://localhost:8888" ``` diff --git a/dist/request.json b/dist/request.json index 1ea7d2f..46ea236 100644 --- a/dist/request.json +++ b/dist/request.json @@ -8,16 +8,15 @@ "type": "object", "properties": { "method": { - "type": "string" + "type": "string", + "description": "The method name" }, "parameter": { - "type": "object" - }, - "version": { - "type": ["integer", "null"] + "type": "object", + "description": "The methods parameter" } } } }, - "required": ["method", "parameter", "version"] + "required": ["method", "parameter"] } diff --git a/src/Contract/MethodProviderInterface.php b/src/Contract/MethodProviderInterface.php index 37b9070..f357815 100644 --- a/src/Contract/MethodProviderInterface.php +++ b/src/Contract/MethodProviderInterface.php @@ -3,9 +3,7 @@ namespace Usox\JsonSchemaApi\Contract; /** - * A method provider should perform a lookup an api method by its name. - * If api versions are used, the version number gets appended to the method name. - * e.g. someNamespace.someMethod, someNamespace.someMethod.1 + * Lookup a method name and return the api method handler (e.g. by using a dict * * If the method name does not exist, lookup is expected to return null. */ diff --git a/src/Dispatch/MethodDispatcher.php b/src/Dispatch/MethodDispatcher.php index 1bf374e..8353888 100644 --- a/src/Dispatch/MethodDispatcher.php +++ b/src/Dispatch/MethodDispatcher.php @@ -42,13 +42,8 @@ public function dispatch( ServerRequestInterface $request, stdClass $input ): array { - // Get the method and version from the request + // Get the method from the request and perform lookup $methodName = $input->method; - $version = $input->version; - - if ($version !== null) { - $methodName = sprintf('%s.%d', $methodName, $version); - } $handler = $this->methodProvider->lookup($methodName); if (!$handler instanceof ApiMethodInterface) { diff --git a/tests/Dispatch/MethodDispatcherTest.php b/tests/Dispatch/MethodDispatcherTest.php index 545433c..8ccbd96 100644 --- a/tests/Dispatch/MethodDispatcherTest.php +++ b/tests/Dispatch/MethodDispatcherTest.php @@ -48,9 +48,8 @@ public function testDispatchThrowsExceptionIfMethodDoesNotExist(): void $this->expectExceptionCode(StatusCode::BAD_REQUEST); $method = 'some-method'; - $version = null; - $input = ['method' => $method, 'version' => $version]; + $input = ['method' => $method]; $this->methodProvider->shouldReceive('lookup') ->with($method) @@ -66,13 +65,12 @@ public function testDispatchThrowsExceptionIfMethodDoesNotExist(): void public function testDispatchReturnsHandler(): void { $method = 'some-method'; - $version = 666; $result = ['some-result']; $parameter = (object) ['some-parameter']; $schemaContent = (object) ['some' => 'schema-content']; $schemaFilePath = 'some-path'; - $input = (object) ['method' => $method, 'version' => $version, 'parameter' => $parameter]; + $input = (object) ['method' => $method, 'parameter' => $parameter]; $request = Mockery::mock(ServerRequestInterface::class); $handler = Mockery::mock(ApiMethodInterface::class); @@ -88,7 +86,7 @@ public function testDispatchReturnsHandler(): void ->andReturn($schemaContent); $this->methodProvider->shouldReceive('lookup') - ->with(sprintf('%s.%d', $method, $version)) + ->with($method) ->once() ->andReturn($handler);