Treat class types with additional properties as maps

This commit is contained in:
Mark Probst 2018-02-14 10:20:29 -08:00
Родитель 8a457b7c91
Коммит e5b6e3c541
3 изменённых файлов: 41 добавлений и 6 удалений

Просмотреть файл

@ -232,20 +232,32 @@ export function addTypesInSchema(
} else {
required = checkStringArray(schema.required);
}
let classType: TypeRef | undefined = undefined;
if (schema.properties !== undefined) {
return makeClass(path, typeAttributes, checkStringMap(schema.properties), required);
} else if (schema.additionalProperties !== undefined) {
classType = makeClass(path, typeAttributes, checkStringMap(schema.properties), required);
}
let mapType: TypeRef | undefined = undefined;
if (schema.additionalProperties !== undefined) {
const additional = schema.additionalProperties;
if (additional === true) {
return typeBuilder.getMapType(typeBuilder.getPrimitiveType("any"));
} else if (additional === false) {
return makeClass(path, typeAttributes, {}, required);
if (classType === undefined) {
classType = makeClass(path, typeAttributes, {}, required);
}
} else {
return makeMap(path, typeAttributes, checkStringMap(additional));
mapType = makeMap(path, typeAttributes, checkStringMap(additional));
}
} else {
return typeBuilder.getMapType(typeBuilder.getPrimitiveType("any"));
}
if (classType !== undefined && mapType !== undefined) {
return unifyTypes(Set([classType.deref()[0], mapType.deref()[0]]), typeAttributes, typeBuilder, true, true, conflateNumbers);
}
if (classType !== undefined) return classType;
if (mapType !== undefined) return mapType;
return typeBuilder.getMapType(typeBuilder.getPrimitiveType("any"));
case "array":
if (schema.items !== undefined) {
path = path.push({ kind: PathElementKind.Items });

Просмотреть файл

@ -0,0 +1,6 @@
{
"map": {
"foo": 3.1415,
"bar": true
}
}

Просмотреть файл

@ -0,0 +1,17 @@
{
"type": "object",
"additionalProperties": false,
"properties": {
"map": {
"type": "object",
"properties": {
"foo": {
"type": "number"
}
},
"additionalProperties": {
"type": "boolean"
}
}
}
}