|
1 | 1 | import { describe, expect, it } from "@jest/globals";
|
| 2 | +import { type ZodType, z } from "zod"; |
2 | 3 | import { bundlePaths, createRouteRecord } from "./route";
|
3 | 4 | import type { OperationObject } from "@omer-x/openapi-types/operation";
|
4 |
| -import type { ZodType } from "zod"; |
5 | 5 |
|
6 | 6 | describe("createRouteRecord", () => {
|
7 | 7 | it("should create a route record with the correct method, path, and apiData", () => {
|
@@ -103,4 +103,129 @@ describe("bundlePaths", () => {
|
103 | 103 | },
|
104 | 104 | });
|
105 | 105 | });
|
| 106 | + |
| 107 | + it("should create references for the responses that are identical to pre-defined schemas", () => { |
| 108 | + const source = [ |
| 109 | + { |
| 110 | + method: "get", |
| 111 | + path: "/user", |
| 112 | + apiData: { |
| 113 | + summary: "Get user", |
| 114 | + responses: { |
| 115 | + 200: { |
| 116 | + description: "Successful response", |
| 117 | + content: { |
| 118 | + "application/json": { |
| 119 | + schema: { |
| 120 | + type: "object", |
| 121 | + properties: { |
| 122 | + name: { |
| 123 | + type: "string", |
| 124 | + }, |
| 125 | + email: { |
| 126 | + type: "string", |
| 127 | + format: "email", |
| 128 | + }, |
| 129 | + }, |
| 130 | + required: ["name", "email"], |
| 131 | + additionalProperties: false, |
| 132 | + }, |
| 133 | + }, |
| 134 | + }, |
| 135 | + }, |
| 136 | + }, |
| 137 | + } as OperationObject, |
| 138 | + }, |
| 139 | + ]; |
| 140 | + const storedSchemas: Record<string, ZodType> = { |
| 141 | + UserDTO: z.object({ |
| 142 | + name: z.string(), |
| 143 | + email: z.string().email(), |
| 144 | + }), |
| 145 | + }; |
| 146 | + |
| 147 | + const result = bundlePaths(source, storedSchemas); |
| 148 | + |
| 149 | + expect(result).toStrictEqual({ |
| 150 | + "/user": { |
| 151 | + get: { |
| 152 | + parameters: undefined, |
| 153 | + requestBody: undefined, |
| 154 | + responses: { |
| 155 | + 200: { |
| 156 | + content: { |
| 157 | + "application/json": { |
| 158 | + schema: { |
| 159 | + $ref: "#/components/schemas/UserDTO", |
| 160 | + }, |
| 161 | + }, |
| 162 | + }, |
| 163 | + description: "Successful response", |
| 164 | + }, |
| 165 | + }, |
| 166 | + summary: "Get user", |
| 167 | + }, |
| 168 | + }, |
| 169 | + }); |
| 170 | + }); |
| 171 | + |
| 172 | + it("should handle the cases where schema is undefined or referenced already", () => { |
| 173 | + const source = [ |
| 174 | + { |
| 175 | + method: "get", |
| 176 | + path: "/user", |
| 177 | + apiData: { |
| 178 | + summary: "Get user", |
| 179 | + parameters: [ |
| 180 | + { |
| 181 | + $ref: "#/components/parameters/QueryLimit", |
| 182 | + }, |
| 183 | + ], |
| 184 | + responses: { |
| 185 | + 200: { |
| 186 | + description: "Successful response", |
| 187 | + content: { |
| 188 | + "application/json": { |
| 189 | + schema: undefined, |
| 190 | + }, |
| 191 | + }, |
| 192 | + }, |
| 193 | + 404: { |
| 194 | + $ref: "#/components/responses/NotFound", |
| 195 | + }, |
| 196 | + }, |
| 197 | + } as OperationObject, |
| 198 | + }, |
| 199 | + ]; |
| 200 | + const storedSchemas: Record<string, ZodType> = {}; |
| 201 | + |
| 202 | + const result = bundlePaths(source, storedSchemas); |
| 203 | + |
| 204 | + expect(result).toStrictEqual({ |
| 205 | + "/user": { |
| 206 | + get: { |
| 207 | + parameters: [ |
| 208 | + { |
| 209 | + $ref: "#/components/parameters/QueryLimit", |
| 210 | + }, |
| 211 | + ], |
| 212 | + requestBody: undefined, |
| 213 | + responses: { |
| 214 | + 200: { |
| 215 | + content: { |
| 216 | + "application/json": { |
| 217 | + schema: undefined, |
| 218 | + }, |
| 219 | + }, |
| 220 | + description: "Successful response", |
| 221 | + }, |
| 222 | + 404: { |
| 223 | + $ref: "#/components/responses/NotFound", |
| 224 | + }, |
| 225 | + }, |
| 226 | + summary: "Get user", |
| 227 | + }, |
| 228 | + }, |
| 229 | + }); |
| 230 | + }); |
106 | 231 | });
|
0 commit comments