Explorar el Código

Set operationId on endpoints in OpenAPI spec

shalvah hace 2 años
padre
commit
69aeec6fe3
Se han modificado 2 ficheros con 15 adiciones y 0 borrados
  1. 9 0
      src/Writing/OpenAPISpecWriter.php
  2. 6 0
      tests/Fixtures/openapi.yaml

+ 9 - 0
src/Writing/OpenAPISpecWriter.php

@@ -84,6 +84,7 @@ class OpenAPISpecWriter
             $operations = $endpoints->mapWithKeys(function (OutputEndpointData $endpoint) use ($groupedEndpoints) {
                 $spec = [
                     'summary' => $endpoint->metadata->title,
+                    'operationId' => $this->operationId($endpoint),
                     'description' => $endpoint->metadata->description,
                     'parameters' => $this->generateEndpointParametersSpec($endpoint),
                     'responses' => $this->generateEndpointResponsesSpec($endpoint),
@@ -535,4 +536,12 @@ class OpenAPISpecWriter
             ];
         }
     }
+
+    function operationId(OutputEndpointData $endpoint): string
+    {
+        if ($endpoint->metadata->title) return preg_replace('/[^\w+]/', '', Str::camel($endpoint->metadata->title));
+
+        $parts = preg_split('/[^\w+]/', $endpoint->uri, -1, PREG_SPLIT_NO_EMPTY);
+        return Str::lower($endpoint->httpMethods[0]) . join('', array_map(fn ($part) => ucfirst($part), $parts));
+    }
 }

+ 6 - 0
tests/Fixtures/openapi.yaml

@@ -11,6 +11,7 @@ paths:
         post:
             summary: 'Endpoint with body form data parameters.'
             description: ''
+            operationId: endpointWithBodyFormDataParameters
             parameters:
                 -
                     in: header
@@ -52,6 +53,7 @@ paths:
         get:
             summary: ''
             description: ''
+            operationId: getApiWithResponseTag
             parameters:
                 -
                     in: header
@@ -88,6 +90,7 @@ paths:
         get:
             summary: ''
             description: ''
+            operationId: getApiWithQueryParameters
             parameters:
                 -
                     in: query
@@ -161,6 +164,7 @@ paths:
         get:
             summary: ''
             description: ''
+            operationId: getApiWithAuthTag
             parameters:
                 -
                     in: header
@@ -183,6 +187,7 @@ paths:
         get:
             summary: ''
             description: ''
+            operationId: getApiEchoesUrlParametersParamParam2Param3Param4
             parameters:
                 -
                     in: query
@@ -267,6 +272,7 @@ paths:
         post:
             summary: 'Endpoint with body parameters as array.'
             description: ''
+            operationId: endpointWithBodyParametersAsArray
             parameters:
                 - in: header
                   name: Custom-Header