Skip to content

Commit 7224486

Browse files
dgadelhaastahmer
authored andcommitted
fix: multiline description
1 parent 8d84b02 commit 7224486

File tree

3 files changed

+38
-2
lines changed

3 files changed

+38
-2
lines changed

lib/src/getZodiosEndpointDefinitionList.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ export const getZodiosEndpointDefinitionList = (doc: OpenAPIObject, options?: Te
249249
}
250250

251251
if (options?.withDescription && paramSchema) {
252-
(paramSchema as SchemaObject).description = (paramItem.description ?? "")?.replace("\n", "");
252+
(paramSchema as SchemaObject).description = (paramItem.description ?? "").trim();
253253
}
254254

255255
// resolve ref if needed, and fallback to default (unknown) value if needed

lib/src/openApiToZod.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,11 @@ export const getZodChain = ({ schema, meta, options }: ZodChainArgs) => {
302302
.otherwise(() => void 0);
303303

304304
if (typeof schema.description === "string" && schema.description !== "" && options?.withDescription) {
305-
chains.push(`describe("${schema.description}")`);
305+
if (["\n", "\r", "\r\n"].some((c) => String.prototype.includes.call(schema.description, c))) {
306+
chains.push(`describe(\`${schema.description}\`)`);
307+
} else {
308+
chains.push(`describe("${schema.description}")`);
309+
}
306310
}
307311

308312
const output = chains

lib/tests/description-in-zod.test.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,23 @@ test("description-in-zod", async () => {
3131
},
3232
description: "bar description",
3333
},
34+
{
35+
in: "query",
36+
name: "baz",
37+
schema: {
38+
type: "number",
39+
enum: [1.3, 34.1, -57.89],
40+
},
41+
description: "baz\nmultiline\ndescription",
42+
},
43+
{
44+
in: "query",
45+
name: "qux",
46+
schema: {
47+
type: "string",
48+
},
49+
description: " ", // spaces only description
50+
},
3451
],
3552
responses: {
3653
"200": {
@@ -73,6 +90,21 @@ test("description-in-zod", async () => {
7390
.describe("bar description")
7491
.optional(),
7592
},
93+
{
94+
name: "baz",
95+
type: "Query",
96+
schema: z
97+
.union([z.literal(1.3), z.literal(34.1), z.literal(-57.89)])
98+
.describe(
99+
\`baz\nmultiline\ndescription\`
100+
)
101+
.optional(),
102+
},
103+
{
104+
name: "qux",
105+
type: "Query",
106+
schema: z.string().optional(),
107+
},
76108
],
77109
response: z.void(),
78110
},

0 commit comments

Comments
 (0)