Merge pull request #978 from quicktype/fix-description-propagation
Attach property descriptions directly to the object type. Fixes #972
This commit is contained in:
Коммит
7b0958064e
|
@ -7,7 +7,7 @@ import {
|
|||
mapMergeWithInto
|
||||
} from "collection-utils";
|
||||
|
||||
import { TypeAttributeKind, combineTypeAttributes, emptyTypeAttributes } from "./TypeAttributes";
|
||||
import { TypeAttributeKind, emptyTypeAttributes } from "./TypeAttributes";
|
||||
import { JSONSchemaType, Ref, JSONSchemaAttributes } from "./input/JSONSchemaInput";
|
||||
import { JSONSchema } from "./input/JSONSchemaStore";
|
||||
|
||||
|
@ -56,6 +56,11 @@ class PropertyDescriptionsTypeAttributeKind extends TypeAttributeKind<Map<string
|
|||
makeInferred(_: Map<string, ReadonlySet<string>>): undefined {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
stringify(propertyDescriptions: Map<string, ReadonlySet<string>>): string | undefined {
|
||||
if (propertyDescriptions.size === 0) return undefined;
|
||||
return `prop descs: ${propertyDescriptions.size}`;
|
||||
}
|
||||
}
|
||||
|
||||
export const propertyDescriptionsTypeAttributeKind: TypeAttributeKind<
|
||||
|
@ -92,5 +97,5 @@ export function descriptionAttributeProducer(
|
|||
}
|
||||
}
|
||||
|
||||
return { forType: combineTypeAttributes("union", description, propertyDescription) };
|
||||
return { forType: description, forObject: propertyDescription };
|
||||
}
|
||||
|
|
|
@ -467,7 +467,12 @@ export type JSONSchemaType = keyof typeof schemaTypeDict;
|
|||
|
||||
const schemaTypes = Object.getOwnPropertyNames(schemaTypeDict) as JSONSchemaType[];
|
||||
|
||||
export type JSONSchemaAttributes = { forType?: TypeAttributes; forUnion?: TypeAttributes; forCases?: TypeAttributes[] };
|
||||
export type JSONSchemaAttributes = {
|
||||
forType?: TypeAttributes;
|
||||
forUnion?: TypeAttributes;
|
||||
forObject?: TypeAttributes;
|
||||
forCases?: TypeAttributes[];
|
||||
};
|
||||
export type JSONSchemaAttributeProducer = (
|
||||
schema: JSONSchema,
|
||||
canonicalRef: Ref,
|
||||
|
@ -669,15 +674,28 @@ async function addTypesInSchema(
|
|||
}
|
||||
|
||||
const includedTypes = setFilter(schemaTypes, isTypeIncluded);
|
||||
let producedAttributesForNoCases: JSONSchemaAttributes[] | undefined = undefined;
|
||||
|
||||
function forEachProducedAttribute(
|
||||
cases: JSONSchema[] | undefined,
|
||||
f: (attributes: JSONSchemaAttributes) => void
|
||||
): void {
|
||||
for (const producer of attributeProducers) {
|
||||
const newAttributes = producer(schema, loc.canonicalRef, includedTypes, cases);
|
||||
if (newAttributes === undefined) continue;
|
||||
f(newAttributes);
|
||||
let attributes: JSONSchemaAttributes[];
|
||||
if (cases === undefined && producedAttributesForNoCases !== undefined) {
|
||||
attributes = producedAttributesForNoCases;
|
||||
} else {
|
||||
attributes = [];
|
||||
for (const producer of attributeProducers) {
|
||||
const newAttributes = producer(schema, loc.canonicalRef, includedTypes, cases);
|
||||
if (newAttributes === undefined) continue;
|
||||
attributes.push(newAttributes);
|
||||
}
|
||||
if (cases === undefined) {
|
||||
producedAttributesForNoCases = attributes;
|
||||
}
|
||||
}
|
||||
for (const a of attributes) {
|
||||
f(a);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -763,7 +781,14 @@ async function addTypesInSchema(
|
|||
|
||||
const additionalProperties = schema.additionalProperties;
|
||||
|
||||
return await makeObject(loc, inferredAttributes, properties, required, additionalProperties);
|
||||
let objectAttributes = inferredAttributes;
|
||||
|
||||
forEachProducedAttribute(undefined, ({ forObject }) => {
|
||||
if (forObject === undefined) return;
|
||||
objectAttributes = combineTypeAttributes("union", objectAttributes, forObject);
|
||||
});
|
||||
|
||||
return await makeObject(loc, objectAttributes, properties, required, additionalProperties);
|
||||
}
|
||||
|
||||
async function makeTypesFromCases(cases: any, kind: string): Promise<TypeRef[]> {
|
||||
|
|
|
@ -1 +1 @@
|
|||
{ "union": 123, "enum": "foo" }
|
||||
{ "union": 123, "enum": "foo", "object-or-string": "abc" }
|
||||
|
|
|
@ -1 +1 @@
|
|||
{ "union": "bla", "enum": "bar" }
|
||||
{ "union": "bla", "enum": "bar", "object-or-string": { "prop": 123 } }
|
||||
|
|
|
@ -17,8 +17,18 @@
|
|||
"bar": {
|
||||
"type": "boolean",
|
||||
"description": "A pretty boolean"
|
||||
},
|
||||
"object-or-string": {
|
||||
"type": ["object", "string"],
|
||||
"properties": {
|
||||
"prop": {
|
||||
"type": "number",
|
||||
"description": "This must not get lost"
|
||||
}
|
||||
},
|
||||
"required": ["prop"]
|
||||
}
|
||||
},
|
||||
"required": ["union", "enum"],
|
||||
"required": ["union", "enum", "object-or-string"],
|
||||
"additionalProperties": false
|
||||
}
|
||||
|
|
Загрузка…
Ссылка в новой задаче