@@ -11,13 +11,16 @@ import {
1111} from '@asyncapi/modelina' ;
1212import { GenericCodegenContext , PayloadRenderType } from '../../types' ;
1313import { AsyncAPIDocumentInterface } from '@asyncapi/parser' ;
14- import { processAsyncAPIPayloads , ProcessedPayloadSchemaData } from '../../inputs/asyncapi/generators/payloads' ;
14+ import {
15+ processAsyncAPIPayloads ,
16+ ProcessedPayloadSchemaData
17+ } from '../../inputs/asyncapi/generators/payloads' ;
1518import { processOpenAPIPayloads } from '../../inputs/openapi/generators/payloads' ;
1619import { z } from 'zod' ;
1720import { defaultCodegenTypescriptModelinaOptions } from './utils' ;
1821import { Logger } from '../../../LoggingInterface' ;
1922import { TypeScriptRenderer } from '@asyncapi/modelina/lib/types/generators/typescript/TypeScriptRenderer' ;
20- import { OpenAPIV2 , OpenAPIV3 , OpenAPIV3_1 } from 'openapi-types' ;
23+ import { OpenAPIV2 , OpenAPIV3 , OpenAPIV3_1 } from 'openapi-types' ;
2124
2225export const zodTypeScriptPayloadGenerator = z . object ( {
2326 id : z . string ( ) . optional ( ) . default ( 'payloads-typescript' ) ,
@@ -87,8 +90,14 @@ export type TypeScriptPayloadRenderType =
8790
8891// Interface for processed payloads data (input-agnostic)
8992export interface ProcessedPayloadData {
90- channelModels : Record < string , { messageModel : OutputModel ; messageType : string } > ;
91- operationModels : Record < string , { messageModel : OutputModel ; messageType : string } > ;
93+ channelModels : Record <
94+ string ,
95+ { messageModel : OutputModel ; messageType : string }
96+ > ;
97+ operationModels : Record <
98+ string ,
99+ { messageModel : OutputModel ; messageType : string }
100+ > ;
92101 otherModels : Array < { messageModel : OutputModel ; messageType : string } > ;
93102}
94103
@@ -303,14 +312,14 @@ export function safeStringify(value: any): string {
303312 let depth = 0 ;
304313 const maxDepth = 255 ;
305314 const maxRepetitions = 5 ; // Allow up to 5 repetitions of the same object
306-
315+
307316 // eslint-disable-next-line sonarjs/cognitive-complexity
308317 function stringify ( val : any , currentPath : any [ ] = [ ] ) : any {
309318 // Check depth limit
310319 if ( depth > maxDepth ) {
311320 return true ;
312321 }
313-
322+
314323 switch ( typeof val ) {
315324 case 'function' :
316325 return true ;
@@ -322,38 +331,47 @@ export function safeStringify(value: any): string {
322331 if ( val === null ) {
323332 return null ;
324333 }
325-
334+
326335 // Check for immediate circular reference (direct self-reference)
327- if ( currentPath . length > 0 && currentPath [ currentPath . length - 1 ] === val ) {
336+ if (
337+ currentPath . length > 0 &&
338+ currentPath [ currentPath . length - 1 ] === val
339+ ) {
328340 return true ;
329341 }
330-
342+
331343 // Count how many times this object appears in the current path
332- const repetitionCount = currentPath . filter ( obj => obj === val ) . length ;
333-
344+ const repetitionCount = currentPath . filter ( ( obj ) => obj === val ) . length ;
345+
334346 // If we've seen this object too many times in the current path, cut it off
335347 if ( repetitionCount >= maxRepetitions ) {
336348 return true ;
337349 }
338-
350+
339351 depth ++ ;
340352 const newPath = [ ...currentPath , val ] ;
341-
353+
342354 let result : any ;
343-
355+
344356 if ( Array . isArray ( val ) ) {
345- result = val . map ( item => stringify ( item , newPath ) ) ;
357+ result = val . map ( ( item ) => stringify ( item , newPath ) ) ;
346358 } else {
347359 result = { } ;
348360 for ( const [ key , value ] of Object . entries ( val ) ) {
349361 // Skip extension properties
350- if ( key . startsWith ( 'x-modelina' ) || key . startsWith ( 'x-the-codegen-project' ) || key . startsWith ( 'x-parser-' ) || key . startsWith ( 'x-modelgen-' ) || key . startsWith ( 'discriminator' ) ) {
362+ if (
363+ key . startsWith ( 'x-modelina' ) ||
364+ key . startsWith ( 'x-the-codegen-project' ) ||
365+ key . startsWith ( 'x-parser-' ) ||
366+ key . startsWith ( 'x-modelgen-' ) ||
367+ key . startsWith ( 'discriminator' )
368+ ) {
351369 continue ;
352370 }
353371 result [ key ] = stringify ( value , newPath ) ;
354372 }
355373 }
356-
374+
357375 depth -- ;
358376 return result ;
359377 }
@@ -363,7 +381,7 @@ export function safeStringify(value: any): string {
363381 return true ;
364382 }
365383 }
366-
384+
367385 return JSON . stringify ( stringify ( value ) ) ;
368386}
369387
@@ -372,7 +390,7 @@ export async function generateTypescriptPayloadsCore(
372390 processedData : ProcessedPayloadData ,
373391 generator : TypeScriptPayloadGeneratorInternal
374392) : Promise < TypeScriptPayloadRenderType > {
375- // The models are already generated by the input processors,
393+ // The models are already generated by the input processors,
376394 // so we just need to return them in the expected format
377395 return {
378396 channelModels : processedData . channelModels ,
@@ -480,12 +498,21 @@ ${renderUnionUnmarshalByStatusCode(model)}`;
480498 useJavascriptReservedKeywords : generator . useForJavaScript
481499 } ) ;
482500
483- const channelModels : Record < string , { messageModel : OutputModel ; messageType : string } > = { } ;
484- const operationModels : Record < string , { messageModel : OutputModel ; messageType : string } > = { } ;
485- const otherModels : Array < { messageModel : OutputModel ; messageType : string } > = [ ] ;
501+ const channelModels : Record <
502+ string ,
503+ { messageModel : OutputModel ; messageType : string }
504+ > = { } ;
505+ const operationModels : Record <
506+ string ,
507+ { messageModel : OutputModel ; messageType : string }
508+ > = { } ;
509+ const otherModels : Array < { messageModel : OutputModel ; messageType : string } > =
510+ [ ] ;
486511
487512 // Generate models for channel payloads
488- for ( const [ channelId , schemaData ] of Object . entries ( processedSchemaData . channelPayloads ) ) {
513+ for ( const [ channelId , schemaData ] of Object . entries (
514+ processedSchemaData . channelPayloads
515+ ) ) {
489516 if ( schemaData ) {
490517 const models = await modelinaGenerator . generateToFiles (
491518 schemaData . schema ,
@@ -508,7 +535,9 @@ ${renderUnionUnmarshalByStatusCode(model)}`;
508535 }
509536
510537 // Generate models for operation payloads
511- for ( const [ operationId , schemaData ] of Object . entries ( processedSchemaData . operationPayloads ) ) {
538+ for ( const [ operationId , schemaData ] of Object . entries (
539+ processedSchemaData . operationPayloads
540+ ) ) {
512541 if ( schemaData ) {
513542 const models = await modelinaGenerator . generateToFiles (
514543 schemaData . schema ,
@@ -573,15 +602,15 @@ export async function generateTypescriptPayload(
573602 if ( ! asyncapiDocument ) {
574603 throw new Error ( 'Expected AsyncAPI input, was not given' ) ;
575604 }
576-
605+
577606 processedSchemaData = await processAsyncAPIPayloads ( asyncapiDocument ) ;
578607 break ;
579608 }
580609 case 'openapi' : {
581610 if ( ! openapiDocument ) {
582611 throw new Error ( 'Expected OpenAPI input, was not given' ) ;
583612 }
584-
613+
585614 processedSchemaData = processOpenAPIPayloads ( openapiDocument ) ;
586615 break ;
587616 }
@@ -590,5 +619,8 @@ export async function generateTypescriptPayload(
590619 }
591620
592621 // Generate final result using processed schema data
593- return generateTypescriptPayloadsCoreFromSchemas ( processedSchemaData , generator ) ;
622+ return generateTypescriptPayloadsCoreFromSchemas (
623+ processedSchemaData ,
624+ generator
625+ ) ;
594626}
0 commit comments