Skip to content

Commit 91bb707

Browse files
fix: fix apifox includes encode character
1 parent dd423d9 commit 91bb707

File tree

6 files changed

+1714
-21
lines changed

6 files changed

+1714
-21
lines changed

.changeset/mighty-bats-smash.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 apifox $ref includes encode character

src/generator/serviceGenarator.ts

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ import {
7272
getDefaultFileTag,
7373
getDefaultType,
7474
getFinalFileName,
75+
getLastRefName,
7576
handleDuplicateTypeNames,
7677
isArraySchemaObject,
7778
isBinaryArraySchemaObject,
@@ -811,8 +812,7 @@ export default class ServiceGenerator {
811812
DEFAULT_SCHEMA) as SchemaObject;
812813

813814
if (isReferenceObject(schema)) {
814-
const refPaths = schema.$ref.split('/');
815-
const refName = refPaths[refPaths.length - 1];
815+
const refName = getLastRefName(schema.$ref);
816816
const childrenSchema = components.schemas[refName];
817817

818818
if (isNonArraySchemaObject(childrenSchema) && this.config.dataFields) {
@@ -852,15 +852,13 @@ export default class ServiceGenerator {
852852
const isDirectObject =
853853
((p.schema as SchemaObject)?.type === 'object' ||
854854
(p as unknown as SchemaObject).type) === 'object';
855-
const refList = (
855+
const refName = getLastRefName(
856856
(p.schema as ReferenceObject)?.$ref ||
857-
(p as unknown as ReferenceObject).$ref ||
858-
''
859-
).split('/');
860-
const ref = refList[refList.length - 1];
857+
(p as unknown as ReferenceObject).$ref
858+
);
861859
const deRefObj =
862860
entries(this.openAPIData.components?.schemas).find(
863-
([k]) => k === ref
861+
([k]) => k === refName
864862
) || [];
865863
const isRefObject =
866864
(deRefObj[1] as SchemaObject)?.type === 'object' &&
@@ -937,10 +935,10 @@ export default class ServiceGenerator {
937935

938936
private resolveArray(schemaObject: ArraySchemaObject) {
939937
if (isReferenceObject(schemaObject.items)) {
940-
const refPaths = schemaObject.items.$ref.split('/');
938+
const refName = getLastRefName(schemaObject.items.$ref);
941939

942940
return {
943-
type: `${refPaths[refPaths.length - 1]}[]`,
941+
type: `${refName}[]`,
944942
};
945943
}
946944

@@ -1041,7 +1039,7 @@ export default class ServiceGenerator {
10411039

10421040
if (refPaths[0] === '#') {
10431041
const schema =
1044-
this.openAPIData.components?.schemas?.[refPaths[refPaths.length - 1]];
1042+
this.openAPIData.components?.schemas?.[getLastRefName(refObject.$ref)];
10451043

10461044
if (!schema) {
10471045
throw new Error(`[GenSDK] Data Error! Notfoud: ${refObject.$ref}`);

src/generator/util.ts

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -105,9 +105,15 @@ function getRefName(refObject: ReferenceObject | string) {
105105
return refObject;
106106
}
107107

108-
const refPaths = refObject.$ref.split('/');
108+
return resolveTypeName(getLastRefName(refObject.$ref));
109+
}
110+
111+
export function getLastRefName(refPath: string = '') {
112+
const refPaths = refPath.split('/');
109113

110-
return resolveTypeName(refPaths[refPaths.length - 1]);
114+
return refPaths.length > 0
115+
? decodeURIComponent(refPaths[refPaths.length - 1])
116+
: '';
111117
}
112118

113119
export function getDefaultType(
@@ -211,8 +217,7 @@ export function getDefaultType(
211217
const allofList = schemaObject.allOf.map((item) => {
212218
if (isReferenceObject(item)) {
213219
// 不使用 getRefName 函数处理,无法通过 schemas[schemaKey] 获取到schema
214-
const refPaths = item.$ref.split('/');
215-
const schemaKey = refPaths[refPaths.length - 1];
220+
const schemaKey = getLastRefName(item.$ref);
216221

217222
if ((schemas?.[schemaKey] as SchemaObject)?.enum) {
218223
return `I${getDefaultType(item, namespace)}`;
@@ -407,10 +412,7 @@ export function markAllowSchema(
407412
const refs = schemaStr?.match(/#\/components\/schemas\/([A-Za-z0-9._-]+)/g);
408413

409414
forEach(refs, (ref) => {
410-
const refPaths = ref.split('/');
411-
const schema = schemas?.[
412-
refPaths[refPaths.length - 1]
413-
] as ICustomSchemaObject;
415+
const schema = schemas?.[getLastRefName(ref)] as ICustomSchemaObject;
414416

415417
if (schema && !schema.isAllowed) {
416418
schema.isAllowed = true;

0 commit comments

Comments
 (0)