10000 Backticks aren't escaped when the `--with-description` option is specified and the description spans multiple lines. · Issue #318 · astahmer/openapi-zod-client · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content
Backticks aren't escaped when the --with-description option is specified and the description spans multiple lines. #318
Open
@mola1129

Description

@mola1129

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 {

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0