Describe the bug
Backticks aren't escaped when the --with-description option is specified and the description spans multiple lines.
Minimal reproduction
Input
{
"openapi": "3.0.0",
"info": {},
"paths": {
"/pet": {
"get": {
"responses": {
"200": {
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Pet"
}
}
}
}
}
}
}
},
"components": {
"schemas": {
"Pet": {
"type": "object",
"properties": {
"name": {
"type": "string",
"description": "The name of the `pet`\n"
}
}
}
}
}
}
Run openapi-zod-client ./test.json -o ./test.ts --with-description
Output
An error occurs because the backticks are not escaped.
import { makeApi, Zodios, type ZodiosOptions } from "@zodios/core";
import { z } from "zod";
const Pet = z.object({ name: z.string().describe(`The name of the `pet`
`) }).partial().passthrough(); // here
export const schemas = {
Pet,
};
const endpoints = makeApi([
{
method: "get",
path: "/pet",
alias: "getPet",
requestFormat: "json",
response: z.object({ name: z.string().describe(`The name of the `pet`
`) }).partial().passthrough(), // here
},
]);
export const api = new Zodios(endpoints);
export function createApiClient(baseUrl: string, options?: ZodiosOptions) {
return new Zodios(baseUrl, endpoints, options);
}
Expected behavior
Backticks included in the description should be escaped.
import { makeApi, Zodios, type ZodiosOptions } from "@zodios/core";
import { z } from "zod";
const Pet = z.object({ name: z.string().describe(`The name of the \`pet\`
`) }).partial().passthrough(); // here
export const schemas = {
Pet,
};
const endpoints = makeApi([
{
method: "get",
path: "/pet",
alias: "getPet",
requestFormat: "json",
response: z.object({ name: z.string().describe(`The name of the \`pet\`
`) }).partial().passthrough(), // here
},
]);
export const api = new Zodios(endpoints);
export function createApiClient(baseUrl: string, options?: ZodiosOptions) {
return new Zodios(baseUrl, endpoints, options);
}
Additional context
This issue appears to be caused by not escaping the content of schema.description in the following code.
|
if (["\n", "\r", "\r\n"].some((c) => String.prototype.includes.call(schema.description, c))) { |
|
chains.push(`describe(\`${schema.description}\`)`); |
|
} else { |
Describe the bug
Backticks aren't escaped when the
--with-descriptionoption is specified and the description spans multiple lines.Minimal reproduction
Input
{ "openapi": "3.0.0", "info": {}, "paths": { "/pet": { "get": { "responses": { "200": { "content": { "application/json": { "schema": { "$ref": "#/components/schemas/Pet" } } } } } } } }, "components": { "schemas": { "Pet": { "type": "object", "properties": { "name": { "type": "string", "description": "The name of the `pet`\n" } } } } } }Run
openapi-zod-client ./test.json -o ./test.ts --with-descriptionOutput
An error occurs because the backticks are not escaped.
Expected behavior
Backticks included in the description should be escaped.
Additional context
This issue appears to be caused by not escaping the content of
schema.descriptionin the following code.openapi-zod-client/lib/src/openApiToZod.ts
Lines 315 to 317 in 76d86f6