diff --git a/composer.json b/composer.json index d88d933..2323caf 100644 --- a/composer.json +++ b/composer.json @@ -24,7 +24,7 @@ "php": "^8.4", "laravel/framework": "^13.0", "nikic/php-parser": "^5.8", - "php-skir/runtime": "^0.2@dev", + "php-skir/runtime": "^0.2.1@dev", "symfony/process": "^7.4|^8.0" }, "require-dev": { diff --git a/docs/routing.md b/docs/routing.md index 9cac660..10c9bfe 100644 --- a/docs/routing.md +++ b/docs/routing.md @@ -338,7 +338,7 @@ Calling `studio()` explicitly enables Studio for that endpoint, regardless of `s The Studio app reads the endpoint's methods from a GET request to `/api/skir?list`. The answer follows the upstream Skir services: every method with its name, number, and the type descriptors of its request and response. The list is available only while Studio is enabled for the endpoint; otherwise it answers `404`. -The PHP runtime types carry no record names, so the names in the list are derived. A method's request and response are named `Request` and `Response`, and a nested struct or enum is named after the field or variant that holds it. Structs and enums with the same shape share one definition. These names only label the Studio forms; they do not affect the wire format. +Records are listed under the name the schema gives them, with their doc comments, when the generated types carry that information: regenerate with a generator built on `@php-skir/generator-core` 0.3 or later. Types without it get derived names: a method's request and response are named `Request` and `Response`, a nested struct or enum is named after the field or variant that holds it, and structs and enums with the same shape share one definition. Names only label the Studio forms; they do not affect the wire format. Studio sends its calls from the browser as `{"method": ..., "request": ...}` POST requests, with the `Authorization` header you enter in the app. Outer route middleware applies to the Studio page, the method list, and every call. diff --git a/src/Studio/MethodList.php b/src/Studio/MethodList.php index 223791c..078d80b 100644 --- a/src/Studio/MethodList.php +++ b/src/Studio/MethodList.php @@ -4,6 +4,7 @@ namespace Skir\Server\Studio; +use Skir\Runtime\MethodDescriptor; use Skir\Server\RegisteredProcedure; use Skir\Server\SkirServer; @@ -16,7 +17,8 @@ final class MethodList * method: string, * number: int, * request: array, - * response: array + * response: array, + * doc?: string * }> * } */ @@ -24,20 +26,38 @@ public function toArray(SkirServer $server): array { return [ 'methods' => array_values(array_map( - fn (RegisteredProcedure $procedure): array => [ - 'method' => $procedure->descriptor->name, - 'number' => $procedure->descriptor->number, - 'request' => (new TypeDescriptor)->describe( - $procedure->descriptor->requestType, - "{$procedure->descriptor->name}Request", - ), - 'response' => (new TypeDescriptor)->describe( - $procedure->descriptor->responseType, - "{$procedure->descriptor->name}Response", - ), - ], + fn (RegisteredProcedure $procedure): array => $this->method($procedure->descriptor), $server->procedures(), )), ]; } + + /** + * @return array{ + * method: string, + * number: int, + * request: array, + * response: array, + * doc?: string + * } + */ + private function method(MethodDescriptor $descriptor): array + { + $method = [ + 'method' => $descriptor->name, + 'number' => $descriptor->number, + 'request' => (new TypeDescriptor)->describe($descriptor->requestType, "{$descriptor->name}Request"), + 'response' => (new TypeDescriptor)->describe($descriptor->responseType, "{$descriptor->name}Response"), + ]; + + if ($descriptor->doc === null) { + return $method; + } + + if ($descriptor->doc === '') { + return $method; + } + + return [...$method, 'doc' => $descriptor->doc]; + } } diff --git a/src/Studio/TypeDescriptor.php b/src/Studio/TypeDescriptor.php index a76a448..e52dc12 100644 --- a/src/Studio/TypeDescriptor.php +++ b/src/Studio/TypeDescriptor.php @@ -14,8 +14,9 @@ * Describes a runtime type in the JSON format of Skir's type descriptors: a type signature plus the * definition of every struct and enum it refers to. * - * Runtime types carry no record names, so names are derived: the root is named by the caller, and a nested - * record is named after the field or variant that holds it. Records with the same shape share one definition. + * A record is identified by the id its generator gave it. Types built without one get a derived name: the + * root is named by the caller, a nested record after the field or variant that holds it, and records with + * the same shape share one definition. */ final class TypeDescriptor { @@ -69,6 +70,10 @@ private function signature(Type $type, string $name): array private function recordId(Type $type, string $name): string { + if ($type->recordId !== null) { + return $this->namedRecordId($type, $type->recordId); + } + $shape = serialize($type); if (isset($this->recordIdsByShape[$shape])) { @@ -87,6 +92,20 @@ private function recordId(Type $type, string $name): string return $recordId; } + private function namedRecordId(Type $type, string $recordId): string + { + if (isset($this->records[$recordId])) { + return $recordId; + } + + $this->records[$recordId] = []; + $this->records[$recordId] = $type->kind === TypeKind::Struct + ? $this->structDefinition($type, $recordId) + : $this->enumDefinition($type, $recordId); + + return $recordId; + } + private function uniqueRecordId(string $name): string { $studlyName = Str::studly($name); @@ -117,11 +136,11 @@ private function structDefinition(Type $type, string $recordId): array $fields[] = $this->fieldDefinition($field); } - $definition = [ + $definition = $this->withDoc([ 'kind' => 'struct', 'id' => $recordId, 'fields' => $fields, - ]; + ], $type->doc); if ($removedNumbers !== []) { $definition['removed_numbers'] = $removedNumbers; @@ -135,11 +154,11 @@ private function fieldDefinition(Field $field): array { $name = (string) $field->name; - return [ + return $this->withDoc([ 'name' => $name, 'number' => $field->number, 'type' => $this->signature($field->type ?? Type::bool(), $name), - ]; + ], $field->doc); } /** @return array */ @@ -156,20 +175,20 @@ private function enumDefinition(Type $type, string $recordId): array $variants[] = $this->variantDefinition($variant); } - return [ + return $this->withDoc([ 'kind' => 'enum', 'id' => $recordId, 'variants' => $variants, - ]; + ], $type->doc); } /** @return array */ private function variantDefinition(Variant $variant): array { - $definition = [ + $definition = $this->withDoc([ 'name' => $variant->name, 'number' => $variant->number, - ]; + ], $variant->doc); if ($variant->payloadType !== null) { $definition['type'] = $this->signature($variant->payloadType, $variant->name); @@ -178,6 +197,23 @@ private function variantDefinition(Variant $variant): array return $definition; } + /** + * @param array $definition + * @return array + */ + private function withDoc(array $definition, ?string $doc): array + { + if ($doc === null) { + return $definition; + } + + if ($doc === '') { + return $definition; + } + + return [...$definition, 'doc' => $doc]; + } + private function itemType(Type $type): Type { return $type->itemType ?? Type::bool(); diff --git a/tests/Feature/StudioMethodListTest.php b/tests/Feature/StudioMethodListTest.php index 771cd0f..d51619a 100644 --- a/tests/Feature/StudioMethodListTest.php +++ b/tests/Feature/StudioMethodListTest.php @@ -152,6 +152,66 @@ public function records_with_the_same_shape_share_one_definition_and_clashing_na ); } + #[Test] + public function it_uses_the_schema_names_and_doc_comments_when_types_carry_them(): void + { + $status = Type::enum( + [Variant::constant('pending', 1, doc: 'Not answered yet.'), Variant::constant('accepted', 2)], + recordId: 'invitations/invitations.skir:Invitation.Status', + doc: 'Where an invitation stands.', + ); + $invitation = Type::struct( + [ + Field::value('status', 0, $status, doc: 'Changes when the invitee answers.'), + Field::value('previous_status', 1, $status), + ], + recordId: 'invitations/invitations.skir:Invitation', + doc: 'One invitation.', + ); + + $server = new SkirServer(new ProcedureRegistry); + $server->addMethod( + new MethodDescriptor('GetInvitation', 7003, Type::string(), $invitation, doc: 'Looks up an invitation.'), + fn (string $request): array => [], + ); + + $method = (new MethodList)->toArray($server)['methods'][0]; + + $this->assertSame('Looks up an invitation.', $method['doc']); + $this->assertSame([ + 'type' => ['kind' => 'record', 'value' => 'invitations/invitations.skir:Invitation'], + 'records' => [ + [ + 'kind' => 'struct', + 'id' => 'invitations/invitations.skir:Invitation', + 'fields' => [ + [ + 'name' => 'status', + 'number' => 0, + 'type' => ['kind' => 'record', 'value' => 'invitations/invitations.skir:Invitation.Status'], + 'doc' => 'Changes when the invitee answers.', + ], + [ + 'name' => 'previous_status', + 'number' => 1, + 'type' => ['kind' => 'record', 'value' => 'invitations/invitations.skir:Invitation.Status'], + ], + ], + 'doc' => 'One invitation.', + ], + [ + 'kind' => 'enum', + 'id' => 'invitations/invitations.skir:Invitation.Status', + 'variants' => [ + ['name' => 'pending', 'number' => 1, 'doc' => 'Not answered yet.'], + ['name' => 'accepted', 'number' => 2], + ], + 'doc' => 'Where an invitation stands.', + ], + ], + ], $method['response']); + } + private function inviteDescriptor(): MethodDescriptor { return new MethodDescriptor(