Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions jest/tstype.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,5 @@ type Type_11 = { name: Label };
type Type_12 = AAA[];

type Type_13 = {};

type Type_14 = { id: string; name: string; children: AAA[] }[];
24 changes: 0 additions & 24 deletions src/__tests__/__snapshots__/generic.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -202,14 +202,6 @@ Object {
"value",
],
"type": "object",
"typeParams": Object {
"0": Object {
"default": Object {
"type": "string",
},
"name": "T",
},
},
},
},
"properties": Object {
Expand Down Expand Up @@ -243,21 +235,5 @@ Object {
"value",
],
"type": "object",
"typeParams": Object {
"0": Object {
"default": Object {
"$realRef": "Generic_8",
"$ref": "#Generic_8<string>",
"items": Object {
"extraParams": Array [
Object {
"type": "string",
},
],
},
},
"name": "T",
},
},
}
`;
42 changes: 42 additions & 0 deletions src/__tests__/__snapshots__/tstype.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,48 @@ Object {
}
`;

exports[`type类型_数组_2 1`] = `
Object {
"definitions": Object {
"AAA": Object {
"additionalProperties": false,
"properties": Object {
"other1": Object {
"type": "string",
},
},
"required": Array [
"other1",
],
"type": "object",
},
},
"items": Object {
"properties": Object {
"children": Object {
"items": Object {
"$ref": "#/definitions/AAA",
},
"type": "array",
},
"id": Object {
"type": "string",
},
"name": Object {
"type": "string",
},
},
"required": Array [
"id",
"name",
"children",
],
"type": "object",
},
"type": "array",
}
`;

exports[`type类型_空对象_1 1`] = `
Object {
"type": "object",
Expand Down
5 changes: 5 additions & 0 deletions src/__tests__/tstype.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,8 @@ test('type类型_数组_1', () => {
test('type类型_空对象_1', () => {
expect(getSchema('Type_13')).toMatchSnapshot();
});

test('type类型_数组_2', () => {
expect(getSchema('Type_14')).toMatchSnapshot();
});

24 changes: 20 additions & 4 deletions src/get-jsonschema-from-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ export default class genTypeSchema extends typescriptToFileDatas {
if (!result) return;
const definitions_ = _.get(result, 'definitions') || {};
delete result.definitions;
delete result.typeParams;
const definitions = { [$refKey]: result, ...definitions_ };
typeJson.definitions = { ...typeJson.definitions, ...definitions };
};
Expand Down Expand Up @@ -240,7 +241,7 @@ export default class genTypeSchema extends typescriptToFileDatas {
const item = result.properties[key] || {};
if (item.$ref) {
const name = item.$ref.replace('#', '');
const ref = fileJson[name];
let ref = fileJson[name];
if (ref && ref.$realRef && ref.$ref && ref.items && ref.items.extraParams) {
item.$ref = ref.$ref;
item.$realRef = ref.$realRef;
Expand Down Expand Up @@ -358,6 +359,13 @@ export default class genTypeSchema extends typescriptToFileDatas {
const commonArrayHandle = (typeJson: AnyOption) => {
if (_.get(typeJson, 'items') && _.get(typeJson, 'items.$ref')) {
commonRefHandle(typeJson.items);
} else if (_.get(typeJson, 'items') && _.get(typeJson, 'items.properties')) {
const properties = typeJson.items.properties || {};
for (const key in properties) {
if (properties[key].type === 'array') {
commonArrayHandle(properties[key]);
}
}
} else {
const itemArr =
_.get(typeJson, 'allOf') ||
Expand Down Expand Up @@ -534,7 +542,12 @@ export default class genTypeSchema extends typescriptToFileDatas {
res && Object.assign(item, res);
} else if (typeof item === 'object') {
delete item.items;
attrCommonHandle(item);
let handle = true;
const $refKey = item.$ref.replace(/#(\/definitions\/)?/, '');
if (typeJson.definitions && Object.keys(typeJson.definitions[$refKey] || {}).length) {
handle = false;
}
attrCommonHandle(item, handle);
}
};

Expand Down Expand Up @@ -567,7 +580,7 @@ export default class genTypeSchema extends typescriptToFileDatas {
const { definitions, $ref } = typeJson_;
if ($ref && definitions) {
const key = $ref.replace(/#\/definitions\//, '');
const res = definitions[key];
const res = definitions[key] || { definitions: {} };
delete typeJson_.definitions[key];
res.definitions = { ...res.definitions, ...typeJson_.definitions };
typeJson = res;
Expand Down Expand Up @@ -595,7 +608,10 @@ export default class genTypeSchema extends typescriptToFileDatas {

// 处理默认值
if (typeJson.typeParams) {
handleGenericDefaultType(typeJson.properties, typeJson.typeParams);
const defaultNames = handleGenericDefaultType(typeJson.properties, typeJson.typeParams);
if (defaultNames.length) {
delete typeJson.typeParams;
}
}

// 对象类型
Expand Down