* Generate a derived class for a discriminated union variant.
(
className: string,
baseClassName: string,
discriminatorProperty: string,
discriminatorValue: string,
schema: JSONSchema7,
knownTypes: Map<string, string>,
nestedClasses: Map<string, string>,
enumOutput: string[],
propertyResolver: PropertyTypeResolver,
experimental = false,
options: DiscriminatedUnionGenerationOptions = {}
)
| 860 | * Generate a derived class for a discriminated union variant. |
| 861 | */ |
| 862 | function generateDerivedClass( |
| 863 | className: string, |
| 864 | baseClassName: string, |
| 865 | discriminatorProperty: string, |
| 866 | discriminatorValue: string, |
| 867 | schema: JSONSchema7, |
| 868 | knownTypes: Map<string, string>, |
| 869 | nestedClasses: Map<string, string>, |
| 870 | enumOutput: string[], |
| 871 | propertyResolver: PropertyTypeResolver, |
| 872 | experimental = false, |
| 873 | options: DiscriminatedUnionGenerationOptions = {} |
| 874 | ): string { |
| 875 | const lines: string[] = []; |
| 876 | const required = new Set(schema.required || []); |
| 877 | |
| 878 | lines.push(...xmlDocCommentWithFallback(schema.description, `The <c>${escapeXml(discriminatorValue)}</c> variant of <see cref="${baseClassName}"/>.`, "")); |
| 879 | if (experimental || isSchemaExperimental(schema)) pushExperimentalAttribute(lines); |
| 880 | if (isSchemaDeprecated(schema)) pushObsoleteAttributes(lines); |
| 881 | lines.push(`public ${options.sealLeafTypes ? "sealed " : ""}partial class ${className} : ${baseClassName}`); |
| 882 | lines.push(`{`); |
| 883 | lines.push(` /// <inheritdoc />`); |
| 884 | lines.push(` [JsonIgnore]`); |
| 885 | lines.push(` public override string ${toPascalCase(discriminatorProperty)} => "${discriminatorValue}";`); |
| 886 | lines.push(""); |
| 887 | |
| 888 | if (schema.properties) { |
| 889 | for (const [propName, propSchema] of Object.entries(schema.properties).sort(([a], [b]) => a.localeCompare(b))) { |
| 890 | if (typeof propSchema !== "object") continue; |
| 891 | if (propName === discriminatorProperty) continue; |
| 892 | |
| 893 | const isReq = required.has(propName); |
| 894 | const prop = propSchema as JSONSchema7; |
| 895 | const csharpName = toCSharpPropertyName(propName, prop); |
| 896 | const csharpType = propertyResolver(prop, className, csharpName, isReq, knownTypes, nestedClasses, enumOutput); |
| 897 | |
| 898 | lines.push(...xmlDocPropertyComment(prop.description, propName, " ")); |
| 899 | lines.push(...emitDataAnnotations(prop, " ", csharpType)); |
| 900 | if (isSchemaDeprecated(prop)) pushObsoleteAttributes(lines, " "); |
| 901 | if (isSchemaExperimental(prop)) pushExperimentalAttribute(lines, " "); |
| 902 | if (isMillisecondsDurationProperty(propName, prop)) lines.push(` [JsonConverter(typeof(MillisecondsTimeSpanConverter))]`); |
| 903 | if (!isReq) lines.push(` [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]`); |
| 904 | const propVisibility = pushCSharpInternalAttribute(lines, prop); |
| 905 | lines.push(` [JsonPropertyName("${propName}")]`); |
| 906 | const reqMod = isReq && !csharpType.endsWith("?") ? "required " : ""; |
| 907 | lines.push(` ${propVisibility} ${reqMod}${csharpType} ${csharpName} { get; set; }`, ""); |
| 908 | } |
| 909 | } |
| 910 | |
| 911 | if (lines[lines.length - 1] === "") lines.pop(); |
| 912 | lines.push(`}`); |
| 913 | return lines.join("\n"); |
| 914 | } |
| 915 | |
| 916 | interface JsonUnionVariant { |
| 917 | typeName: string; |
no test coverage detected
searching dependent graphs…