@@ -33,26 +33,33 @@ public async Task<IActionResult> Index(
3333 [ FromQuery ] bool fireRules = false ,
3434 [ FromQuery ] int depth = 1 )
3535 {
36- _logger . LogInformation ( $ "DotCmsUVEController.Index called with path: ' { catchAll } '" ) ;
36+ _logger . LogInformation ( "DotCmsUVEController.Index called with path: {Path}" , catchAll ) ;
3737
3838 try
3939 {
40+ // Validate the path parameter
41+ if ( string . IsNullOrWhiteSpace ( catchAll ) )
42+ {
43+ catchAll = "/" ; // Default to root path if not provided
44+ }
45+
4046 PageMode pageMode ;
4147 if ( ! Enum . TryParse ( mode , true , out pageMode ) )
4248 {
4349 pageMode = PageMode . LIVE_MODE ; // Default to LIVE_MODE if not provided
4450 }
4551
46- // Log the query parameters
47- _logger . LogInformation ( $ "Query parameters: siteId={ siteId } , mode={ mode } , language_id={ language_id } , " +
48- $ "persona={ personaId } , fireRules={ fireRules } , depth={ depth } ") ;
52+ // Log the query parameters using structured logging
53+ _logger . LogInformation ( "Query parameters: siteId={SiteId}, mode={Mode}, language_id={LanguageId}, " +
54+ "persona={PersonaId}, fireRules={FireRules}, depth={Depth}" ,
55+ siteId , pageMode , language_id , personaId , fireRules , depth ) ;
4956
5057 // Create a PageQueryParams object to pass to the service
5158 var queryParams = new PageQueryParams
5259 {
5360 Path = catchAll ,
5461 Site = siteId ,
55- PageMode = mode ,
62+ PageMode = pageMode . ToString ( ) , // Use the parsed pageMode instead of the raw string
5663 Language = language_id ,
5764 Persona = personaId ,
5865 FireRules = fireRules ,
@@ -67,16 +74,18 @@ public async Task<IActionResult> Index(
6774 }
6875 catch ( Exception ex )
6976 {
70- _logger . LogError ( ex , "Error processing request" ) ;
71- return StatusCode ( 500 , ex . Message ) ;
77+ _logger . LogError ( ex , "Error processing request for path: {Path}" , catchAll ) ;
78+
79+ // Return a generic error message to avoid exposing internal details
80+ return StatusCode ( 500 , "An error occurred while processing your request." ) ;
7281 }
7382 }
7483
7584 [ HttpGet ]
7685 [ Route ( "contentlet-example/{contentletId}" ) ]
7786 public IActionResult ContentletExample ( string contentletId )
7887 {
79- _logger . LogInformation ( $ "ContentletExample called with contentletId: ' { contentletId } '" ) ;
88+ _logger . LogInformation ( "ContentletExample called with contentletId: {ContentletId}" , contentletId ) ;
8089
8190 try
8291 {
@@ -90,16 +99,24 @@ public IActionResult ContentletExample(string contentletId)
9099 } ;
91100
92101 // Add sample properties that a Banner might have
93- using ( var jsonDoc = System . Text . Json . JsonDocument . Parse ( @"{
102+ var jsonString = @"{
94103 ""image"": ""/path/to/banner-image.jpg"",
95104 ""link"": ""https://www.dotcms.com"",
96105 ""altText"": ""DotCMS Banner"",
97106 ""description"": ""<p>This is an example banner created programmatically.</p>""
98- }" ) )
107+ }" ;
108+
109+ // Parse the JSON string and store the elements without cloning
110+ // This avoids the resource leak from Clone() method
111+ using ( var jsonDoc = System . Text . Json . JsonDocument . Parse ( jsonString ) )
99112 {
100- foreach ( var property in jsonDoc . RootElement . EnumerateObject ( ) )
113+ var json = jsonDoc . RootElement . GetRawText ( ) ;
114+ using ( var newDoc = System . Text . Json . JsonDocument . Parse ( json ) )
101115 {
102- contentlet . AdditionalProperties [ property . Name ] = property . Value . Clone ( ) ;
116+ foreach ( var property in newDoc . RootElement . EnumerateObject ( ) )
117+ {
118+ contentlet . AdditionalProperties [ property . Name ] = property . Value ;
119+ }
103120 }
104121 }
105122
@@ -108,8 +125,10 @@ public IActionResult ContentletExample(string contentletId)
108125 }
109126 catch ( Exception ex )
110127 {
111- _logger . LogError ( ex , "Error processing contentlet example request" ) ;
112- return StatusCode ( 500 , ex . Message ) ;
128+ _logger . LogError ( ex , "Error processing contentlet example request for ID: {ContentletId}" , contentletId ) ;
129+
130+ // Return a generic error message to avoid exposing internal details
131+ return StatusCode ( 500 , "An error occurred while processing the contentlet example." ) ;
113132 }
114133 }
115134 }
0 commit comments