Skip to content

Commit

Permalink
test: multiline description
Browse files Browse the repository at this point in the history
  • Loading branch information
scarf005 committed Jul 2, 2023
1 parent 8967cc9 commit ffb878b
Showing 1 changed file with 89 additions and 35 deletions.
124 changes: 89 additions & 35 deletions lib/tests/description-in-zod.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,46 +2,49 @@ import type { OpenAPIObject } from "openapi3-ts";
import { expect, test } from "vitest";
import { generateZodClientFromOpenAPI } from "../src";

test("description-in-zod", async () => {
const openApiDoc: OpenAPIObject = {
openapi: "3.0.0",
info: {
version: "1.0.0",
title: "Numerical enums",
},
paths: {
"/sample": {
get: {
parameters: [
{
in: "query",
name: "foo",
schema: {
type: "integer",
enum: [1, -2, 3],
},
description: "foo description",
},
{
in: "query",
name: "bar",
schema: {
type: "number",
enum: [1.2, 34, -56.789],
},
description: "bar description",
const openApiDoc: OpenAPIObject = {
openapi: "3.0.0",
info: {
version: "1.0.0",
title: "Numerical enums",
},
paths: {
"/sample": {
get: {
parameters: [
{
in: "query",
name: "foo",
schema: {
type: "integer",
enum: [1, -2, 3],
},
],
responses: {
"200": {
description: "resoponse",
description: "foo description",
},
{
in: "query",
name: "bar",
schema: {
type: "number",
enum: [1.2, 34, -56.789],
},
description: `multi
line
bar
description`,
},
],
responses: {
"200": {
description: "resoponse",
},
},
},
},
};
},
};

test("description-in-zod", async () => {
const output = await generateZodClientFromOpenAPI({
disableWriteToFile: true,
openApiDoc,
Expand All @@ -62,15 +65,15 @@ test("description-in-zod", async () => {
type: "Query",
schema: z
.union([z.literal(1), z.literal(-2), z.literal(3)])
.describe("foo description")
.describe(\`foo description\`)
.optional(),
},
{
name: "bar",
type: "Query",
schema: z
.union([z.literal(1.2), z.literal(34), z.literal(-56.789)])
.describe("bar description")
.describe(\`multi line bar description\`)
.optional(),
},
],
Expand All @@ -86,3 +89,54 @@ test("description-in-zod", async () => {
"
`);
});

test("description-in-zod-multiline", async () => {
const output = await generateZodClientFromOpenAPI({
disableWriteToFile: true,
openApiDoc,
options: { withDescription: "multiline" },
});
expect(output).toMatchInlineSnapshot(`
"import { makeApi, Zodios, type ZodiosOptions } from "@zodios/core";
import { z } from "zod";
const endpoints = makeApi([
{
method: "get",
path: "/sample",
requestFormat: "json",
parameters: [
{
name: "foo",
type: "Query",
schema: z
.union([z.literal(1), z.literal(-2), z.literal(3)])
.describe(\`foo description\`)
.optional(),
},
{
name: "bar",
type: "Query",
schema: z
.union([z.literal(1.2), z.literal(34), z.literal(-56.789)])
.describe(
\`multi
line
bar
description\`
)
.optional(),
},
],
response: z.void(),
},
]);
export const api = new Zodios(endpoints);
export function createApiClient(baseUrl: string, options?: ZodiosOptions) {
return new Zodios(baseUrl, endpoints, options);
}
"
`)
})

0 comments on commit ffb878b

Please sign in to comment.