From bf358d3a39f250b72bf8b1ff6d589240e9edd1ee Mon Sep 17 00:00:00 2001 From: Rohin Bhargava Date: Wed, 27 Nov 2024 16:09:02 -0700 Subject: [PATCH] update tests --- .../ResponsesObjectConverter.node.test.ts | 222 ++++++++---------- .../__test__/SchemaConverter.node.test.ts | 59 +++++ 2 files changed, 163 insertions(+), 118 deletions(-) diff --git a/packages/parsers/src/openapi/3.1/paths/__test__/response/ResponsesObjectConverter.node.test.ts b/packages/parsers/src/openapi/3.1/paths/__test__/response/ResponsesObjectConverter.node.test.ts index 1107d65120..7da0c03b86 100644 --- a/packages/parsers/src/openapi/3.1/paths/__test__/response/ResponsesObjectConverter.node.test.ts +++ b/packages/parsers/src/openapi/3.1/paths/__test__/response/ResponsesObjectConverter.node.test.ts @@ -1,156 +1,142 @@ import { OpenAPIV3_1 } from "openapi-types"; import { createMockContext } from "../../../../../__test__/createMockContext.util"; -import { ReferenceConverterNode } from "../../../schemas/ReferenceConverter.node"; -import { RequestMediaTypeObjectConverterNode } from "../../request/RequestMediaTypeObjectConverter.node"; +import { ResponsesObjectConverterNode } from "../../response/ResponsesObjectConverter.node"; -describe("RequestMediaTypeObjectConverterNode", () => { +describe("ResponsesObjectConverterNode", () => { const mockContext = createMockContext(); - it("should handle application/json content type", () => { - const input: OpenAPIV3_1.MediaTypeObject = { - schema: { - type: "object", - properties: { - name: { type: "string" }, + it("should handle success responses", () => { + const input: OpenAPIV3_1.ResponsesObject = { + "200": { + description: "Success response", + content: { + "application/json": { + schema: { + type: "object", + properties: { + data: { type: "string" }, + }, + }, + }, }, }, }; - const converter = new RequestMediaTypeObjectConverterNode( - { - input, - context: mockContext, - accessPath: [], - pathId: "test", - }, - "application/json", - ); + const converter = new ResponsesObjectConverterNode({ + input, + context: mockContext, + accessPath: [], + pathId: "test", + }); - expect(converter.contentType).toBe("application/json"); - expect(converter.schema).toBeDefined(); + const result = converter.convert(); + expect(result?.responses).toBeDefined(); + expect(result?.responses[0]?.response.statusCode).toBe(200); + expect(result?.responses[0]?.response.description).toBe("Success response"); }); - it("should handle application/octet-stream content type", () => { - const input: OpenAPIV3_1.MediaTypeObject = { - schema: { - type: "object", - required: ["data"], + it("should handle error responses", () => { + const input: OpenAPIV3_1.ResponsesObject = { + "400": { + description: "Bad request", + content: { + "application/json": { + schema: { + type: "object", + properties: { + error: { type: "string" }, + }, + }, + }, + }, }, }; - const converter = new RequestMediaTypeObjectConverterNode( - { - input, - context: mockContext, - accessPath: [], - pathId: "test", - }, - "application/octet-stream", - ); + const converter = new ResponsesObjectConverterNode({ + input, + context: mockContext, + accessPath: [], + pathId: "test", + }); - expect(converter.contentType).toBe("application/octet-stream"); - expect(converter.isOptional).toBe(false); + const result = converter.convert(); + expect(result?.errors).toBeDefined(); + expect(result?.errors[0]?.statusCode).toBe(400); + expect(result?.errors[0]?.description).toBe("Bad request"); }); - it("should handle multipart/form-data content type", () => { - const input: OpenAPIV3_1.MediaTypeObject = { - schema: { - type: "object", - properties: { - file: { - type: "string", - format: "binary", + it("should handle responses with headers", () => { + const input: OpenAPIV3_1.ResponsesObject = { + "200": { + description: "Success with headers", + headers: { + "X-Rate-Limit": { + schema: { + type: "integer", + }, + }, + }, + content: { + "application/json": { + schema: { + type: "object", + }, }, }, - required: ["file"], }, }; - const converter = new RequestMediaTypeObjectConverterNode( - { - input, - context: mockContext, - accessPath: [], - pathId: "test", - }, - "multipart/form-data", - ); + const converter = new ResponsesObjectConverterNode({ + input, + context: mockContext, + accessPath: [], + pathId: "test", + }); - expect(converter.contentType).toBe("multipart/form-data"); - expect(converter.fields).toBeDefined(); - expect(converter.requiredFields).toContain("file"); + const result = converter.convert(); + expect(result?.responses[0]?.headers).toBeDefined(); }); - it("should handle reference objects", () => { - const input: OpenAPIV3_1.MediaTypeObject = { - schema: { - $ref: "#/components/schemas/Request", - }, - }; - - mockContext.document.components ??= {}; - mockContext.document.components.schemas = { - Request: { - type: "object", - properties: { - data: { type: "string" }, + it("should handle default responses", () => { + const input: OpenAPIV3_1.ResponsesObject = { + default: { + description: "Default response", + content: { + "application/json": { + schema: { + type: "object", + }, + }, }, }, + "200": { + description: "Success", + }, }; - const converter = new RequestMediaTypeObjectConverterNode( - { - input, - context: mockContext, - accessPath: [], - pathId: "test", - }, - "application/json", - ); + const converter = new ResponsesObjectConverterNode({ + input, + context: mockContext, + accessPath: [], + pathId: "test", + }); - expect(converter.schema).toBeDefined(); - expect(converter.schema instanceof ReferenceConverterNode).toBe(true); + const result = converter.convert(); + expect(result?.responses).toBeDefined(); }); - it("should warn when schema is missing", () => { - const input: OpenAPIV3_1.MediaTypeObject = {}; + it("should handle empty responses", () => { + const input: OpenAPIV3_1.ResponsesObject = {}; - new RequestMediaTypeObjectConverterNode( - { - input, - context: mockContext, - accessPath: [], - pathId: "test", - }, - "application/json", - ); - - expect(mockContext.errors.warning).toHaveBeenCalledWith({ - message: expect.stringContaining("Expected media type schema"), - path: ["test"], + const converter = new ResponsesObjectConverterNode({ + input, + context: mockContext, + accessPath: [], + pathId: "test", }); - }); - it("should warn for unsupported content types", () => { - const input: OpenAPIV3_1.MediaTypeObject = { - schema: { - type: "object", - }, - }; - - new RequestMediaTypeObjectConverterNode( - { - input, - context: mockContext, - accessPath: [], - pathId: "test", - }, - "text/plain", - ); - - expect(mockContext.errors.warning).toHaveBeenCalledWith({ - message: expect.stringContaining("Expected request body"), - path: ["test"], - }); + const result = converter.convert(); + expect(result?.responses).toEqual([]); + expect(result?.errors).toEqual([]); }); }); diff --git a/packages/parsers/src/openapi/3.1/schemas/__test__/SchemaConverter.node.test.ts b/packages/parsers/src/openapi/3.1/schemas/__test__/SchemaConverter.node.test.ts index fbf4a71d61..07d753087b 100644 --- a/packages/parsers/src/openapi/3.1/schemas/__test__/SchemaConverter.node.test.ts +++ b/packages/parsers/src/openapi/3.1/schemas/__test__/SchemaConverter.node.test.ts @@ -93,6 +93,65 @@ describe("SchemaConverterNode", () => { expect(node.typeShapeNode).toBeDefined(); }); + it("should handle array type", () => { + const input: OpenAPIV3_1.SchemaObject = { + type: "array", + items: { + type: "string", + }, + }; + const node = new SchemaConverterNode({ + input, + context: mockContext, + accessPath: [], + pathId: "test", + }); + expect(node.typeShapeNode).toBeDefined(); + }); + + it("should handle enum type", () => { + const input: OpenAPIV3_1.SchemaObject = { + type: "string", + enum: ["RED", "GREEN", "BLUE"], + }; + const node = new SchemaConverterNode({ + input, + context: mockContext, + accessPath: [], + pathId: "test", + }); + expect(node.typeShapeNode).toBeDefined(); + }); + + it("should handle oneOf type", () => { + const input: OpenAPIV3_1.SchemaObject = { + oneOf: [{ type: "string" }, { type: "number" }], + }; + const node = new SchemaConverterNode({ + input, + context: mockContext, + accessPath: [], + pathId: "test", + }); + expect(node.typeShapeNode).toBeDefined(); + }); + + it("should handle allOf type", () => { + const input: OpenAPIV3_1.SchemaObject = { + allOf: [ + { type: "object", properties: { name: { type: "string" } } }, + { type: "object", properties: { age: { type: "number" } } }, + ], + }; + const node = new SchemaConverterNode({ + input, + context: mockContext, + accessPath: [], + pathId: "test", + }); + expect(node.typeShapeNode).toBeDefined(); + }); + it("should set description", () => { const input: OpenAPIV3_1.SchemaObject = { type: "string",