(
typeName: string,
discriminatorProp: string,
mapping: Map<string, JSONSchema7>,
ctx: PyCodegenCtx,
description?: string,
experimental = false
)
| 2301 | } |
| 2302 | |
| 2303 | function emitPyFlatDiscriminatedUnion( |
| 2304 | typeName: string, |
| 2305 | discriminatorProp: string, |
| 2306 | mapping: Map<string, JSONSchema7>, |
| 2307 | ctx: PyCodegenCtx, |
| 2308 | description?: string, |
| 2309 | experimental = false |
| 2310 | ): void { |
| 2311 | if (ctx.generatedNames.has(typeName)) { |
| 2312 | return; |
| 2313 | } |
| 2314 | ctx.generatedNames.add(typeName); |
| 2315 | |
| 2316 | const allProps = new Map<string, { schema: JSONSchema7; requiredInAll: boolean }>(); |
| 2317 | for (const [, variant] of mapping) { |
| 2318 | const required = new Set(variant.required || []); |
| 2319 | for (const [propName, propSchema] of Object.entries(variant.properties || {})) { |
| 2320 | if (typeof propSchema !== "object") { |
| 2321 | continue; |
| 2322 | } |
| 2323 | if (!allProps.has(propName)) { |
| 2324 | allProps.set(propName, { |
| 2325 | schema: propSchema as JSONSchema7, |
| 2326 | requiredInAll: required.has(propName), |
| 2327 | }); |
| 2328 | } else if (!required.has(propName)) { |
| 2329 | allProps.get(propName)!.requiredInAll = false; |
| 2330 | } |
| 2331 | } |
| 2332 | } |
| 2333 | |
| 2334 | const variantCount = mapping.size; |
| 2335 | for (const [propName, info] of allProps) { |
| 2336 | let presentCount = 0; |
| 2337 | for (const [, variant] of mapping) { |
| 2338 | if (variant.properties && propName in variant.properties) { |
| 2339 | presentCount++; |
| 2340 | } |
| 2341 | } |
| 2342 | if (presentCount < variantCount) { |
| 2343 | info.requiredInAll = false; |
| 2344 | } |
| 2345 | } |
| 2346 | |
| 2347 | const discriminatorEnumName = getOrCreatePyEnum( |
| 2348 | typeName + toPascalCase(discriminatorProp), |
| 2349 | [...mapping.keys()], |
| 2350 | ctx, |
| 2351 | description ? `${description} discriminator` : `${typeName} discriminator`, |
| 2352 | undefined, |
| 2353 | false, |
| 2354 | experimental |
| 2355 | ); |
| 2356 | |
| 2357 | const fieldEntries: Array<[string, JSONSchema7, boolean]> = [ |
| 2358 | [ |
| 2359 | discriminatorProp, |
| 2360 | { |
no test coverage detected
searching dependent graphs…