Repository: RobinTail/express-zod-api
Pull request #82 (2021-07-23 21:07:40)

Schema description generation fix.


Patch:
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 9fb91e62..6e2db0cb 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -6,6 +6,7 @@
 
 - First stable release of the v2.
 - All dependencies are up to date.
+- Minor changes of response descriptions in OpenAPI / Swagger documentation generator.
 
 ### v2.0.0-beta4
 
diff --git a/example/example.swagger.yaml b/example/example.swagger.yaml
index 80630adc..16c0160d 100644
--- a/example/example.swagger.yaml
+++ b/example/example.swagger.yaml
@@ -7,7 +7,7 @@ paths:
     get:
       responses:
         "200":
-          description: Successful response
+          description: GET /v1/getUser Successful response
           content:
             application/json:
               schema:
@@ -30,9 +30,8 @@ paths:
                 required:
                   - status
                   - data
-                description: GET /v1/getUser Successful response
         "400":
-          description: Error response
+          description: GET /v1/getUser Error response
           content:
             application/json:
               schema:
@@ -52,7 +51,6 @@ paths:
                 required:
                   - status
                   - error
-                description: GET /v1/getUser Error response
       description: example user retrieval endpoint
       parameters:
         - name: id
@@ -65,7 +63,7 @@ paths:
     post:
       responses:
         "200":
-          description: Successful response
+          description: POST /v1/setUser Successful response
           content:
             application/json:
               schema:
@@ -88,9 +86,8 @@ paths:
                 required:
                   - status
                   - data
-                description: POST /v1/setUser Successful response
         "400":
-          description: Error response
+          description: POST /v1/setUser Error response
           content:
             application/json:
               schema:
@@ -110,7 +107,6 @@ paths:
                 required:
                   - status
                   - error
-                description: POST /v1/setUser Error response
       description: example user update endpoint
       requestBody:
         content:
@@ -133,19 +129,17 @@ paths:
     get:
       responses:
         "200":
-          description: Successful response
+          description: GET /v1/avatar Successful response
           content:
             image/svg+xml:
               schema:
                 type: string
-                description: GET /v1/avatar Successful response
         "400":
-          description: Error response
+          description: GET /v1/avatar Error response
           content:
             text/plain:
               schema:
                 type: string
-                description: GET /v1/avatar Error response
       parameters:
         - name: userId
           in: query
@@ -157,19 +151,17 @@ paths:
     get:
       responses:
         "200":
-          description: Successful response
+          description: GET /v1/stream Successful response
           content:
             image/*:
               schema:
                 type: string
-                description: GET /v1/stream Successful response
         "400":
-          description: Error response
+          description: GET /v1/stream Error response
           content:
             text/plain:
               schema:
                 type: string
-                description: GET /v1/stream Error response
       parameters:
         - name: userId
           in: query
diff --git a/src/open-api.ts b/src/open-api.ts
index ed521a5d..269520c8 100644
--- a/src/open-api.ts
+++ b/src/open-api.ts
@@ -158,26 +158,20 @@ export class OpenAPI extends OpenApiBuilder {
       const operation: OperationObject = {
         responses: {
           '200': {
-            description: successfulResponseDescription,
+            description: `${method.toUpperCase()} ${fullPath} ${successfulResponseDescription}`,
             content: endpoint.getPositiveMimeTypes().reduce((carry, mimeType) => ({
               ...carry,
               [mimeType]: {
-                schema: {
-                  ...describeSchema(endpoint.getPositiveResponseSchema(), true),
-                  description: `${method.toUpperCase()} ${fullPath} ${successfulResponseDescription}`
-                }
+                schema: describeSchema(endpoint.getPositiveResponseSchema(), true)
               }
             }), {} as ContentObject),
           },
           '400': {
-            description: errorResponseDescription,
+            description: `${method.toUpperCase()} ${fullPath} ${errorResponseDescription}`,
             content: endpoint.getNegativeMimeTypes().reduce((carry, mimeType) => ({
               ...carry,
               [mimeType]: {
-                schema: {
-                  ...describeSchema(endpoint.getNegativeResponseSchema(), true),
-                  description: `${method.toUpperCase()} ${fullPath} ${errorResponseDescription}`
-                }
+                schema: describeSchema(endpoint.getNegativeResponseSchema(), true)
               }
             }), {} as ContentObject),
           }


Test patch:
diff --git a/tests/unit/__snapshots__/open-api.spec.ts.snap b/tests/unit/__snapshots__/open-api.spec.ts.snap
index 8a4a7e27..9e101eff 100644
--- a/tests/unit/__snapshots__/open-api.spec.ts.snap
+++ b/tests/unit/__snapshots__/open-api.spec.ts.snap
@@ -10,7 +10,7 @@ paths:
     get:
       responses:
         \\"200\\":
-          description: Successful response
+          description: GET /v1/getSomething Successful response
           content:
             application/json:
               schema:
@@ -35,9 +35,8 @@ paths:
                 required:
                   - status
                   - data
-                description: GET /v1/getSomething Successful response
         \\"400\\":
-          description: Error response
+          description: GET /v1/getSomething Error response
           content:
             application/json:
               schema:
@@ -57,7 +56,6 @@ paths:
                 required:
                   - status
                   - error
-                description: GET /v1/getSomething Error response
       parameters:
         - name: array
           in: query
@@ -99,7 +97,7 @@ paths:
     post:
       responses:
         \\"200\\":
-          description: Successful response
+          description: POST /v1/getSomething Successful response
           content:
             application/json:
               schema:
@@ -131,9 +129,8 @@ paths:
                 required:
                   - status
                   - data
-                description: POST /v1/getSomething Successful response
         \\"400\\":
-          description: Error response
+          description: POST /v1/getSomething Error response
           content:
             application/json:
               schema:
@@ -153,7 +150,6 @@ paths:
                 required:
                   - status
                   - error
-                description: POST /v1/getSomething Error response
       requestBody:
         content:
           application/json:
@@ -203,7 +199,7 @@ paths:
     get:
       responses:
         \\"200\\":
-          description: Successful response
+          description: GET /v1/getSomething Successful response
           content:
             application/json:
               schema:
@@ -224,9 +220,8 @@ paths:
                 required:
                   - status
                   - data
-                description: GET /v1/getSomething Successful response
         \\"400\\":
-          description: Error response
+          description: GET /v1/getSomething Error response
           content:
             application/json:
               schema:
@@ -246,7 +241,6 @@ paths:
                 required:
                   - status
                   - error
-                description: GET /v1/getSomething Error response
       parameters:
         - name: optional
           in: query
@@ -280,7 +274,7 @@ paths:
     get:
       responses:
         \\"200\\":
-          description: Successful response
+          description: GET /v1/getUser Successful response
           content:
             application/json:
               schema:
@@ -303,9 +297,8 @@ paths:
                 required:
                   - status
                   - data
-                description: GET /v1/getUser Successful response
         \\"400\\":
-          description: Error response
+          description: GET /v1/getUser Error response
           content:
             application/json:
               schema:
@@ -325,7 +318,6 @@ paths:
                 required:
                   - status
                   - error
-                description: GET /v1/getUser Error response
       description: example user retrieval endpoint
       parameters:
         - name: id
@@ -338,7 +330,7 @@ paths:
     post:
       responses:
         \\"200\\":
-          description: Successful response
+          description: POST /v1/setUser Successful response
           content:
             application/json:
               schema:
@@ -361,9 +353,8 @@ paths:
                 required:
                   - status
                   - data
-                description: POST /v1/setUser Successful response
         \\"400\\":
-          description: Error response
+          description: POST /v1/setUser Error response
           content:
             application/json:
               schema:
@@ -383,7 +374,6 @@ paths:
                 required:
                   - status
                   - error
-                description: POST /v1/setUser Error response
       description: example user update endpoint
       requestBody:
         content:
@@ -406,19 +396,17 @@ paths:
     get:
       responses:
         \\"200\\":
-          description: Successful response
+          description: GET /v1/avatar Successful response
           content:
             image/svg+xml:
               schema:
                 type: string
-                description: GET /v1/avatar Successful response
         \\"400\\":
-          description: Error response
+          description: GET /v1/avatar Error response
           content:
             text/plain:
               schema:
                 type: string
-                description: GET /v1/avatar Error response
       parameters:
         - name: userId
           in: query
@@ -430,19 +418,17 @@ paths:
     get:
       responses:
         \\"200\\":
-          description: Successful response
+          description: GET /v1/stream Successful response
           content:
             image/*:
               schema:
                 type: string
-                description: GET /v1/stream Successful response
         \\"400\\":
-          description: Error response
+          description: GET /v1/stream Error response
           content:
             text/plain:
               schema:
                 type: string
-                description: GET /v1/stream Error response
       parameters:
         - name: userId
           in: query
@@ -476,7 +462,7 @@ paths:
     post:
       responses:
         \\"200\\":
-          description: Successful response
+          description: POST /v1/getSomething Successful response
           content:
             application/json:
               schema:
@@ -498,9 +484,8 @@ paths:
                 required:
                   - status
                   - data
-                description: POST /v1/getSomething Successful response
         \\"400\\":
-          description: Error response
+          description: POST /v1/getSomething Error response
           content:
             application/json:
               schema:
@@ -520,7 +505,6 @@ paths:
                 required:
                   - status
                   - error
-                description: POST /v1/getSomething Error response
       requestBody:
         content:
           application/json:
@@ -576,7 +560,7 @@ paths:
     post:
       responses:
         \\"200\\":
-          description: Successful response
+          description: POST /v1/getSomething Successful response
           content:
             application/json:
               schema:
@@ -598,9 +582,8 @@ paths:
                 required:
                   - status
                   - data
-                description: POST /v1/getSomething Successful response
         \\"400\\":
-          description: Error response
+          description: POST /v1/getSomething Error response
           content:
             application/json:
               schema:
@@ -620,7 +603,6 @@ paths:
                 required:
                   - status
                   - error
-                description: POST /v1/getSomething Error response
       requestBody:
         content:
           application/json:
@@ -666,7 +648,7 @@ paths:
     post:
       responses:
         \\"200\\":
-          description: Successful response
+          description: POST /v1/getSomething Successful response
           content:
             application/json:
               schema:
@@ -689,9 +671,8 @@ paths:
                 required:
                   - status
                   - data
-                description: POST /v1/getSomething Successful response
         \\"400\\":
-          description: Error response
+          description: POST /v1/getSomething Error response
           content:
             application/json:
               schema:
@@ -711,7 +692,6 @@ paths:
                 required:
                   - status
                   - error
-                description: POST /v1/getSomething Error response
       requestBody:
         content:
           application/json:
@@ -752,7 +732,7 @@ paths:
     post:
       responses:
         \\"200\\":
-          description: Successful response
+          description: POST /v1/getSomething Successful response
           content:
             application/json:
               schema:
@@ -772,9 +752,8 @@ paths:
                 required:
                   - status
                   - data
-                description: POST /v1/getSomething Successful response
         \\"400\\":
-          description: Error response
+          description: POST /v1/getSomething Error response
           content:
             application/json:
               schema:
@@ -794,7 +773,6 @@ paths:
                 required:
                   - status
                   - error
-                description: POST /v1/getSomething Error response
       requestBody:
         content:
           application/json: