Skip to content

Commit 5b8016d

Browse files
fix: fix bug #640
1 parent 45673fa commit 5b8016d

File tree

7 files changed

+1688
-2431
lines changed

7 files changed

+1688
-2431
lines changed

.changeset/soft-icons-lay.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'openapi-ts-request': patch
3+
---
4+
5+
fix: fix bug #640

src/generator/serviceGenarator.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ import type {
7777
import { type MergeOption } from './type';
7878
import {
7979
capitalizeFirstLetter,
80+
escapeStringForJs,
8081
genDefaultFunctionName,
8182
getAxiosResponseType,
8283
getBasePrefix,
@@ -1156,6 +1157,7 @@ export default class ServiceGenerator {
11561157
});
11571158

11581159
env.addFilter('capitalizeFirst', capitalizeFirstLetter);
1160+
env.addFilter('escapeJs', escapeStringForJs);
11591161
const destPath = join(this.config.serversPath, fileName);
11601162
const destCode = nunjucks.renderString(template, {
11611163
disableTypeCheck: false,

src/generator/util.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -594,6 +594,20 @@ export function capitalizeFirstLetter(str: string) {
594594
return str.replace(/^[a-z]/, (match) => match.toUpperCase());
595595
}
596596

597+
/**
598+
* 转义字符串,用于 JavaScript 字符串字面量
599+
* 将换行符、单引号等特殊字符转义为转义序列
600+
*/
601+
export function escapeStringForJs(str: string): string {
602+
if (!str) return str;
603+
return str
604+
.replace(/\\/g, '\\\\') // 先转义反斜杠
605+
.replace(/'/g, "\\'") // 转义单引号
606+
.replace(/\n/g, '\\n') // 转义换行符
607+
.replace(/\r/g, '\\r') // 转义回车符
608+
.replace(/\t/g, '\\t'); // 转义制表符
609+
}
610+
597611
// 解析 description 中的枚举翻译
598612
export const parseDescriptionEnum = (
599613
description: string

templates/displayTypeLabel.njk

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import * as {{ namespace }} from './{{ interfaceFileName }}';
2323
{%- if p["$ref"] and not p.name %}
2424
display{{ p.type | safe }}(field as keyof {{ namespace }}.{{ p.type }})
2525
{%- else %}
26-
{{ p.name }}: '{{ p.desc if p.desc else p.name }}',
26+
{{ p.name }}: '{{ (p.desc if p.desc else p.name) | escapeJs }}',
2727
{%- endif %}
2828
{%- endfor %}
2929
{%- if prop.length > 1 %}

test/__snapshots__/common/测试描述信息支持换行.snap

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,24 @@ export function displayMajorEnum(field: API.Major) {
1616
}
1717
/* eslint-disable */
1818
// @ts-ignore
19+
import * as API from './types';
20+
21+
export function displayStudent(field: keyof API.Student) {
22+
return {
23+
name: 'name',
24+
age: 'age',
25+
gender: 'gender',
26+
major:
27+
'\n * Major enum \n * COMPUTER SCIENCE: Computer Science\n * MATHEMATICS: Mathematics\n * PHYSICS: Physics\n * LITERATURE: Literature\n',
28+
}[field];
29+
}
30+
/* eslint-disable */
31+
// @ts-ignore
1932
export * from './types';
2033
export * from './displayEnumLabel';
2134

35+
export * from './displayTypeLabel';
36+
2237
export * from './student';
2338
/* eslint-disable */
2439
// @ts-ignore

0 commit comments

Comments
 (0)