mirror of
https://gitee.com/baidu/amis.git
synced 2024-11-29 10:29:19 +08:00
修复 additionalProperties 的问题
This commit is contained in:
parent
238f055ec6
commit
fcc8134824
@ -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;
|
||||
}
|
||||
|
@ -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 =
|
||||
|
@ -13,7 +13,7 @@ import {SchemaClassName} from '../../Schema';
|
||||
|
||||
export type GroupSubControl = FormControlSchema & {
|
||||
/**
|
||||
* 列类名, 在某些容器里面有用比如 group
|
||||
* 列类名
|
||||
*/
|
||||
columnClassName?: SchemaClassName;
|
||||
|
||||
|
@ -122,7 +122,7 @@ export interface ColumnArray extends Array<ColumnNode> {}
|
||||
*/
|
||||
export interface GridSchema extends BaseSchema {
|
||||
/**
|
||||
* 指定为each展示类型
|
||||
* 指定为 Grid 格子布局渲染器。
|
||||
*/
|
||||
type: 'grid';
|
||||
columns: Array<GridColumn>;
|
||||
|
Loading…
Reference in New Issue
Block a user