fork of hey-api/openapi-ts because I need some additional things

Simplify discriminator logic to use schema's own value only

Removed getAllDiscriminatorValues and union logic. Now each schema gets only its own discriminator value from the mapping, matching the requested output format.

Co-authored-by: mrlubos <12529395+mrlubos@users.noreply.github.com>

+10 -112
+1 -1
packages/openapi-ts-tests/main/test/__snapshots__/3.0.x/discriminator-allof-nested/types.gen.ts
··· 10 10 }; 11 11 12 12 export type CarDto = VehicleDto & { 13 - $type: 'Car' | 'Volvo'; 13 + $type: 'Car'; 14 14 } & { 15 15 modelName: string; 16 16 };
+1 -1
packages/openapi-ts-tests/main/test/__snapshots__/3.1.x/discriminator-allof-nested/types.gen.ts
··· 10 10 }; 11 11 12 12 export type CarDto = VehicleDto & { 13 - $type: 'Car' | 'Volvo'; 13 + $type: 'Car'; 14 14 } & { 15 15 modelName: string; 16 16 };
+4 -55
packages/openapi-ts/src/openApi/3.0.x/parser/schema.ts
··· 79 79 return discriminators; 80 80 }; 81 81 82 - /** 83 - * Gets all discriminator values for a schema and its children in the inheritance hierarchy. 84 - * For intermediate schemas (those that are extended by others), returns a union of all values. 85 - */ 86 - const getAllDiscriminatorValues = ({ 87 - context, 88 - discriminator, 89 - schemaRef, 90 - }: { 91 - context: Context; 92 - discriminator: NonNullable<SchemaObject['discriminator']>; 93 - schemaRef: string; 94 - }): string[] => { 95 - const values: string[] = []; 96 - 97 - // Check each entry in the discriminator mapping 98 - for (const [value, mappedSchemaRef] of Object.entries( 99 - discriminator.mapping || {}, 100 - )) { 101 - if (mappedSchemaRef === schemaRef) { 102 - // This is the current schema's own value 103 - values.push(value); 104 - continue; 105 - } 106 - 107 - // Check if the mapped schema extends the current schema 108 - const mappedSchema = context.resolveRef<SchemaObject>(mappedSchemaRef); 109 - if (mappedSchema.allOf) { 110 - for (const item of mappedSchema.allOf) { 111 - if ('$ref' in item && item.$ref === schemaRef) { 112 - // This schema extends the current schema, add its value 113 - values.push(value); 114 - break; 115 - } 116 - } 117 - } 118 - } 119 - 120 - return values; 121 - }; 122 - 123 82 const parseSchemaJsDoc = ({ 124 83 irSchema, 125 84 schema, ··· 460 419 ); 461 420 462 421 if (values.length > 0) { 463 - // For schemas that are extended by others, we need to include all child discriminator values 464 - // to create a union type (e.g., CarDto should have $type: 'Car' | 'Volvo') 465 - const allValues = getAllDiscriminatorValues({ 466 - context, 467 - discriminator, 468 - schemaRef: state.$ref, 469 - }); 470 - 471 - // Use allValues if we found children, otherwise use the original values 472 - const finalValues = allValues.length > 0 ? allValues : values; 473 - 474 - const valueSchemas: ReadonlyArray<IR.SchemaObject> = 475 - finalValues.map((value) => ({ 422 + const valueSchemas: ReadonlyArray<IR.SchemaObject> = values.map( 423 + (value) => ({ 476 424 const: value, 477 425 type: 'string', 478 - })); 426 + }), 427 + ); 479 428 const irDiscriminatorSchema: IR.SchemaObject = { 480 429 properties: { 481 430 [discriminator.propertyName]:
+4 -55
packages/openapi-ts/src/openApi/3.1.x/parser/schema.ts
··· 83 83 return discriminators; 84 84 }; 85 85 86 - /** 87 - * Gets all discriminator values for a schema and its children in the inheritance hierarchy. 88 - * For intermediate schemas (those that are extended by others), returns a union of all values. 89 - */ 90 - const getAllDiscriminatorValues = ({ 91 - context, 92 - discriminator, 93 - schemaRef, 94 - }: { 95 - context: Context; 96 - discriminator: NonNullable<SchemaObject['discriminator']>; 97 - schemaRef: string; 98 - }): string[] => { 99 - const values: string[] = []; 100 - 101 - // Check each entry in the discriminator mapping 102 - for (const [value, mappedSchemaRef] of Object.entries( 103 - discriminator.mapping || {}, 104 - )) { 105 - if (mappedSchemaRef === schemaRef) { 106 - // This is the current schema's own value 107 - values.push(value); 108 - continue; 109 - } 110 - 111 - // Check if the mapped schema extends the current schema 112 - const mappedSchema = context.resolveRef<SchemaObject>(mappedSchemaRef); 113 - if (mappedSchema.allOf) { 114 - for (const item of mappedSchema.allOf) { 115 - if (item.$ref && item.$ref === schemaRef) { 116 - // This schema extends the current schema, add its value 117 - values.push(value); 118 - break; 119 - } 120 - } 121 - } 122 - } 123 - 124 - return values; 125 - }; 126 - 127 86 const parseSchemaJsDoc = ({ 128 87 irSchema, 129 88 schema, ··· 542 501 ); 543 502 544 503 if (values.length > 0) { 545 - // For schemas that are extended by others, we need to include all child discriminator values 546 - // to create a union type (e.g., CarDto should have $type: 'Car' | 'Volvo') 547 - const allValues = getAllDiscriminatorValues({ 548 - context, 549 - discriminator, 550 - schemaRef: state.$ref, 551 - }); 552 - 553 - // Use allValues if we found children, otherwise use the original values 554 - const finalValues = allValues.length > 0 ? allValues : values; 555 - 556 - const valueSchemas: ReadonlyArray<IR.SchemaObject> = 557 - finalValues.map((value) => ({ 504 + const valueSchemas: ReadonlyArray<IR.SchemaObject> = values.map( 505 + (value) => ({ 558 506 const: value, 559 507 type: 'string', 560 - })); 508 + }), 509 + ); 561 510 const irDiscriminatorSchema: IR.SchemaObject = { 562 511 properties: { 563 512 [discriminator.propertyName]: