Skip to content

Commit

Permalink
merge from other
Browse files Browse the repository at this point in the history
  • Loading branch information
RohinBhargava committed Dec 20, 2024
2 parents 2644c44 + bfa9801 commit 70bad66
Show file tree
Hide file tree
Showing 9 changed files with 622 additions and 422 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,6 @@ export class OperationObjectConverterNode extends BaseOpenApiV3_1ConverterNode<
queryParameters: convertOperationObjectProperties(this.queryParameters),
requestHeaders: convertOperationObjectProperties(this.requestHeaders),
responseHeaders: responses?.[0]?.headers,
// TODO: revisit fdr shape to suport multiple requests
requests: this.requests?.convert(),
responses: responses?.map((response) => response.response),
errors,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,10 +135,11 @@ describe("RequestMediaTypeObjectConverterNode", () => {
accessPath: [],
pathId: "test",
},
"application/xml"
"application/xml",
"testpath",
200
);

expect(converter.contentType).toBeUndefined();
expect(mockContext.errors.warning).toHaveBeenCalled();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -4,150 +4,158 @@ import { SchemaConverterNode } from "../../../schemas/SchemaConverter.node";
import { ResponseMediaTypeObjectConverterNode } from "../../response/ResponseMediaTypeObjectConverter.node";

describe("ResponseMediaTypeObjectConverterNode", () => {
const mockContext = createMockContext();

it("should handle application/json content type", () => {
const input: OpenAPIV3_1.MediaTypeObject = {
schema: {
type: "object",
properties: {
name: { type: "string" },
},
},
};

const converter = new ResponseMediaTypeObjectConverterNode(
{
input,
context: mockContext,
accessPath: [],
pathId: "test",
},
"application/json",
undefined,
);

expect(converter.contentType).toBe("application/json");
expect(converter.schema).toBeDefined();
expect(converter.streamingFormat).toBeUndefined();
});

it("should handle application/octet-stream content type", () => {
const input: OpenAPIV3_1.MediaTypeObject = {
schema: {
type: "string",
contentMediaType: "image/png",
},
};

const converter = new ResponseMediaTypeObjectConverterNode(
{
input,
context: mockContext,
accessPath: [],
pathId: "test",
},
"application/octet-stream",
undefined,
);

expect(converter.contentType).toBe("application/octet-stream");
expect(converter.contentSubtype).toBe("image/png");
});

it("should handle streaming JSON responses", () => {
const input: OpenAPIV3_1.MediaTypeObject = {
schema: {
type: "object",
properties: {
data: { type: "string" },
},
},
};

const converter = new ResponseMediaTypeObjectConverterNode(
{
input,
context: mockContext,
accessPath: [],
pathId: "test",
},
"application/json",
"json",
);

expect(converter.contentType).toBe("application/json");
expect(converter.streamingFormat).toBe("json");
expect(converter.schema).toBeDefined();
});

it("should handle SSE streaming responses", () => {
const input: OpenAPIV3_1.MediaTypeObject = {};

const converter = new ResponseMediaTypeObjectConverterNode(
{
input,
context: mockContext,
accessPath: [],
pathId: "test",
},
"text/event-stream",
"sse",
);

const result = converter.convert();
expect(result).toEqual({ type: "streamingText" });
});

it("should error when JSON response is missing schema", () => {
const input: OpenAPIV3_1.MediaTypeObject = {};

new ResponseMediaTypeObjectConverterNode(
{
input,
context: mockContext,
accessPath: [],
pathId: "test",
},
"application/json",
undefined,
);

expect(mockContext.errors.error).toHaveBeenCalledWith({
message: "Expected schema for JSON response body. Received null",
path: ["test"],
});
});

it("should handle reference objects", () => {
const input: OpenAPIV3_1.MediaTypeObject = {
schema: {
$ref: "#/components/schemas/Response",
},
};

mockContext.document.components ??= {};
mockContext.document.components.schemas = {
Response: {
type: "object",
properties: {
data: { type: "string" },
},
},
};

const converter = new ResponseMediaTypeObjectConverterNode(
{
input,
context: mockContext,
accessPath: [],
pathId: "test",
},
"application/json",
undefined,
);

expect(converter.schema).toBeDefined();
expect(converter.schema instanceof SchemaConverterNode).toBe(true);
const mockContext = createMockContext();

it("should handle application/json content type", () => {
const input: OpenAPIV3_1.MediaTypeObject = {
schema: {
type: "object",
properties: {
name: { type: "string" },
},
},
};

const converter = new ResponseMediaTypeObjectConverterNode(
{
input,
context: mockContext,
accessPath: [],
pathId: "test",
},
"application/json",
undefined
);

expect(converter.contentType).toBe("application/json");
expect(converter.schema).toBeDefined();
expect(converter.streamingFormat).toBeUndefined();
});

it("should handle application/octet-stream content type", () => {
const input: OpenAPIV3_1.MediaTypeObject = {
schema: {
type: "string",
contentMediaType: "image/png",
},
};

const converter = new ResponseMediaTypeObjectConverterNode(
{
input,
context: mockContext,
accessPath: [],
pathId: "test",
},
"application/octet-stream",
undefined
);

expect(converter.contentType).toBe("application/octet-stream");
expect(converter.contentSubtype).toBe("image/png");
});

it("should handle streaming JSON responses", () => {
const input: OpenAPIV3_1.MediaTypeObject = {
schema: {
type: "object",
properties: {
data: { type: "string" },
},
},
};

const converter = new ResponseMediaTypeObjectConverterNode(
{
input,
context: mockContext,
accessPath: [],
pathId: "test",
},
"application/json",
"json",
"testpath",
200,
undefined
);

expect(converter.contentType).toBe("application/json");
expect(converter.schema).toBeDefined();
});

it("should handle SSE streaming responses", () => {
const input: OpenAPIV3_1.MediaTypeObject = {};

const converter = new ResponseMediaTypeObjectConverterNode(
{
input,
context: mockContext,
accessPath: [],
pathId: "test",
},
"text/event-stream",
"sse",
"testpath",
200,
undefined
);

const result = converter.convert();
expect(result[0]).toEqual({ type: "streamingText" });
});

it("should error when JSON response is missing schema", () => {
const input: OpenAPIV3_1.MediaTypeObject = {};

new ResponseMediaTypeObjectConverterNode(
{
input,
context: mockContext,
accessPath: [],
pathId: "test",
},
"application/json",
undefined,
"testpath",
200,
undefined
);

expect(mockContext.errors.error).toHaveBeenCalledWith({
message: "Expected schema for JSON response body. Received null",
path: ["test"],
});
});

it("should handle reference objects", () => {
const input: OpenAPIV3_1.MediaTypeObject = {
schema: {
$ref: "#/components/schemas/Response",
},
};

mockContext.document.components ??= {};
mockContext.document.components.schemas = {
Response: {
type: "object",
properties: {
data: { type: "string" },
},
},
};

const converter = new ResponseMediaTypeObjectConverterNode(
{
input,
context: mockContext,
accessPath: [],
pathId: "test",
},
"application/json",
undefined
);

expect(converter.schema).toBeDefined();
expect(converter.schema instanceof SchemaConverterNode).toBe(true);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -63,18 +63,24 @@ export class RequestBodyObjectConverterNode extends BaseOpenApiV3_1ConverterNode

convert(): FernRegistry.api.latest.HttpRequest[] {
return Object.entries(this.requestBodiesByContentType ?? {})
.map(([contentType, mediaTypeObject]) => {
const body = mediaTypeObject.convert();
.flatMap(([contentType, mediaTypeObject]) => {
const bodies = mediaTypeObject.convert();

if (body == null) {
if (bodies == null) {
return undefined;
}

return {
description: this.description,
contentType,
body,
};
return bodies.map((body) => {
if (body == null) {
return undefined;
}

return {
description: this.description,
contentType,
body,
};
});
})
.filter(isNonNullish);
}
Expand Down
Loading

0 comments on commit 70bad66

Please sign in to comment.