修复 additionalProperties 的问题

This commit is contained in:
2betop 2020-09-16 13:20:10 +08:00
parent 238f055ec6
commit fcc8134824
4 changed files with 118 additions and 10 deletions

View File

@ -32,15 +32,29 @@ export class IntersectionTypeFormatter extends BaseIntersectionTypeFormatter {
}
});
const mainDefinition = rest.reduce(
getAllOfDefinitionReducer(this.childFormatter),
{
type: 'object',
additionalProperties: true // 这里只能是 true 了
} as Definition
);
const patternProperties: any = {};
patternProperties[
`^(${Object.keys(mainDefinition.properties).join('|')})$`
] = {};
return {
allOf: references
.map((type: any) => this.childFormatter.getDefinition(type))
.concat(
rest.reduce(getAllOfDefinitionReducer(this.childFormatter), {
type: 'object',
additionalProperties: true // 这里只能是 true 了
} as Definition)
)
.map((type: any) => {
const definition = this.childFormatter.getDefinition(type)!;
return {
...definition,
patternProperties: patternProperties
};
})
.concat(mainDefinition)
.filter((item: any) => item)
} as any;
}

View File

@ -14,7 +14,9 @@ import {
UnknownNodeError,
UnknownTypeError,
IntersectionNodeParser,
SubNodeParser
SubNodeParser,
Schema,
uniqueArray
} from 'ts-json-schema-generator';
import {IntersectionTypeFormatter as MyIntersectionTypeFormatter} from './TypeFormatter/IntersectionTypeFormatter';
import {IntersectionNodeParser as MyIntersectionNodeParser} from './NodeParser/IntersectionNodeParser';
@ -37,11 +39,103 @@ async function main() {
hackIt(generator);
const schema = generator.createSchema(config.type);
fixSchema(schema);
const outputFile = path.join(outDir, 'schema.json');
mkdirp(path.dirname(outputFile));
fs.writeFileSync(outputFile, JSON.stringify(schema, null, 2));
}
function fixSchema(schema: Schema) {
const keys = Object.keys(schema.definitions!);
const list: Array<{
patternProperties: any;
referenceKey: string;
definationKey: string;
}> = [];
keys.forEach(definationKey => {
const definition: any = schema.definitions![definationKey];
if (
Array.isArray(definition.allOf) &&
definition.allOf.length &&
definition.allOf[0].patternProperties &&
/^#\/definitions\/(.*?)$/.test(definition.allOf[0].$ref || '')
) {
const referenceKey = RegExp.$1;
if (schema.definitions![referenceKey]) {
list.push({
patternProperties: definition.allOf[0].patternProperties,
referenceKey,
definationKey
});
definition.allOf[0].$ref =
'#/definitions/' + referenceKey + definationKey;
}
}
});
copyAnyOf(schema, list);
}
function copyAnyOf(
schema: Schema,
list: Array<{
patternProperties: any;
referenceKey: string;
definationKey: string;
}>
) {
list.forEach(({referenceKey, definationKey, patternProperties}) => {
const definition: any = schema.definitions![referenceKey];
if (Array.isArray(definition.anyOf)) {
const anyOf = definition.anyOf.map((item: any) => {
if (!/^#\/definitions\/(.*?)$/.test(item.$ref || '')) {
return item;
}
const baseKey = RegExp.$1;
if (!baseKey || !schema.definitions![baseKey]) {
return item;
}
const baseDefinition: any = schema.definitions![baseKey]!;
if (!baseDefinition) {
return item;
}
if (Array.isArray(baseDefinition.anyOf)) {
// todo
return item;
} else if (!baseDefinition.properties) {
return item;
}
const keys = Object.keys(patternProperties)[0];
const extenedPatternProperties = {
[`^(${uniqueArray(
Object.keys(baseDefinition.properties).concat(
keys.substring(2, keys.length - 2).split('|')
)
).join('|')})$`]: {}
};
return {
...item,
additionalProperties: false,
patternProperties: extenedPatternProperties
};
});
schema.definitions![`${referenceKey}${definationKey}`] = {
anyOf: anyOf
};
}
});
}
function hackIt(generator: any) {
const circularReferenceTypeFormatter = generator.typeFormatter;
const typeFormatters =

View File

@ -13,7 +13,7 @@ import {SchemaClassName} from '../../Schema';
export type GroupSubControl = FormControlSchema & {
/**
* , group
*
*/
columnClassName?: SchemaClassName;

View File

@ -122,7 +122,7 @@ export interface ColumnArray extends Array<ColumnNode> {}
*/
export interface GridSchema extends BaseSchema {
/**
* each展示类型
* Grid
*/
type: 'grid';
columns: Array<GridColumn>;