(
baseClassName: string,
discriminatorInfo: DiscriminatorInfo,
knownTypes: Map<string, string>,
nestedClasses: Map<string, string>,
enumOutput: string[],
description?: string,
propertyResolver?: PropertyTypeResolver,
experimental = false,
options: DiscriminatedUnionGenerationOptions = {}
)
| 739 | } |
| 740 | |
| 741 | function generateFlattenedBooleanDiscriminatedClass( |
| 742 | baseClassName: string, |
| 743 | discriminatorInfo: DiscriminatorInfo, |
| 744 | knownTypes: Map<string, string>, |
| 745 | nestedClasses: Map<string, string>, |
| 746 | enumOutput: string[], |
| 747 | description?: string, |
| 748 | propertyResolver?: PropertyTypeResolver, |
| 749 | experimental = false, |
| 750 | options: DiscriminatedUnionGenerationOptions = {} |
| 751 | ): string { |
| 752 | const resolver = propertyResolver ?? resolveSessionPropertyType; |
| 753 | const renamedBase = applyTypeRename(baseClassName); |
| 754 | const lines: string[] = []; |
| 755 | const flattenedProperties = new Map<string, { schema: JSONSchema7; requiredCount: number; variantCount: number }>(); |
| 756 | const variants = Array.from(discriminatorInfo.mapping.values()).map((variant) => variant.schema); |
| 757 | |
| 758 | for (const variant of variants) { |
| 759 | const required = new Set(variant.required || []); |
| 760 | for (const [propName, propSchema] of Object.entries(variant.properties || {})) { |
| 761 | if (typeof propSchema !== "object" || propName === discriminatorInfo.property) continue; |
| 762 | |
| 763 | const existing = flattenedProperties.get(propName); |
| 764 | if (existing) { |
| 765 | existing.variantCount++; |
| 766 | if (required.has(propName)) existing.requiredCount++; |
| 767 | continue; |
| 768 | } |
| 769 | |
| 770 | flattenedProperties.set(propName, { |
| 771 | schema: propSchema as JSONSchema7, |
| 772 | requiredCount: required.has(propName) ? 1 : 0, |
| 773 | variantCount: 1, |
| 774 | }); |
| 775 | } |
| 776 | } |
| 777 | |
| 778 | lines.push(...xmlDocCommentWithFallback(description, `Data type discriminated by <c>${escapeXml(discriminatorInfo.property)}</c>.`, "")); |
| 779 | if (experimental) pushExperimentalAttribute(lines); |
| 780 | lines.push(`public ${options.sealLeafTypes ? "sealed " : ""}partial class ${renamedBase}`); |
| 781 | lines.push(`{`); |
| 782 | lines.push(` /// <summary>The boolean discriminator.</summary>`); |
| 783 | lines.push(` [JsonPropertyName("${discriminatorInfo.property}")]`); |
| 784 | lines.push(` public bool ${toPascalCase(discriminatorInfo.property)} { get; set; }`); |
| 785 | |
| 786 | const propertyEntries = Array.from(flattenedProperties.entries()).sort(([a], [b]) => a.localeCompare(b)); |
| 787 | for (const [propName, info] of propertyEntries) { |
| 788 | const isReq = info.variantCount === variants.length && info.requiredCount === variants.length; |
| 789 | const csharpName = toCSharpPropertyName(propName, info.schema); |
| 790 | const csharpType = resolver(info.schema, renamedBase, csharpName, isReq, knownTypes, nestedClasses, enumOutput); |
| 791 | |
| 792 | lines.push(""); |
| 793 | lines.push(...xmlDocPropertyComment(info.schema.description, propName, " ")); |
| 794 | lines.push(...emitDataAnnotations(info.schema, " ", csharpType)); |
| 795 | if (isSchemaDeprecated(info.schema)) pushObsoleteAttributes(lines, " "); |
| 796 | if (isSchemaExperimental(info.schema)) pushExperimentalAttribute(lines, " "); |
| 797 | if (isMillisecondsDurationProperty(propName, info.schema)) lines.push(` [JsonConverter(typeof(MillisecondsTimeSpanConverter))]`); |
| 798 | if (!isReq) lines.push(` [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]`); |
no test coverage detected
searching dependent graphs…