@@ -60,6 +60,8 @@ class OpenApiCorrector {
6060 Map <String , dynamic > models,
6161 Map <String , String > typeCorrections,
6262 ) {
63+ var correctedContent = fileContent;
64+
6365 // BUGFIX: Protect properties blocks and API path definitions by detecting
6466 // their range and replacing with placeholders
6567 final blocks =
@@ -77,7 +79,7 @@ class OpenApiCorrector {
7779 multiLine: true ,
7880 );
7981
80- for (final match in propertiesPattern.allMatches (fileContent )) {
82+ for (final match in propertiesPattern.allMatches (correctedContent )) {
8183 final indent = match[1 ]! ;
8284 final indentLength = indent.length;
8385 final matchStart = match.start;
@@ -86,7 +88,7 @@ class OpenApiCorrector {
8688 // Find the block end by detecting the next key with same or shallower
8789 // indentation
8890 var blockEnd = matchEnd;
89- final lines = fileContent .substring (matchEnd).split ('\n ' );
91+ final lines = correctedContent .substring (matchEnd).split ('\n ' );
9092
9193 for (var i = 1 ; i < lines.length; i++ ) {
9294 final line = lines[i];
@@ -109,7 +111,7 @@ class OpenApiCorrector {
109111 }
110112
111113 // Get the entire properties block
112- final originalBlock = fileContent .substring (matchStart, blockEnd);
114+ final originalBlock = correctedContent .substring (matchStart, blockEnd);
113115
114116 // Generate placeholder
115117 final placeholder = '${indent }___PROPERTIES_BLOCK_${blocks .length }___' ;
@@ -123,13 +125,13 @@ class OpenApiCorrector {
123125 }
124126
125127 // Detect and protect API path definitions
126- for (final match in pathPattern.allMatches (fileContent )) {
128+ for (final match in pathPattern.allMatches (correctedContent )) {
127129 final indent = match[1 ]! ;
128130 final matchStart = match.start;
129131 final matchEnd = match.end;
130132
131133 // API path definitions are single lines
132- final originalPath = fileContent .substring (matchStart, matchEnd);
134+ final originalPath = correctedContent .substring (matchStart, matchEnd);
133135
134136 // Generate placeholder
135137 final placeholder = '${indent }___PATH_DEFINITION_${blocks .length }___' ;
@@ -177,13 +179,14 @@ class OpenApiCorrector {
177179 < String , ({int start, int end, String placeholder, String original})> {};
178180
179181 for (final block in validBlocks) {
180- placeholderBuf.write (fileContent.substring (lastEnd, block.start));
181- placeholderBuf.write (block.placeholder);
182+ placeholderBuf
183+ ..write (correctedContent.substring (lastEnd, block.start))
184+ ..write (block.placeholder);
182185 placeholderToBlock[block.placeholder] = block;
183186 lastEnd = block.end;
184187 }
185- placeholderBuf.write (fileContent .substring (lastEnd));
186- fileContent = placeholderBuf.toString ();
188+ placeholderBuf.write (correctedContent .substring (lastEnd));
189+ correctedContent = placeholderBuf.toString ();
187190
188191 // Apply replacement rules to all class names and format to PascalCase
189192 // (properties blocks are already replaced with placeholders)
@@ -203,7 +206,7 @@ class OpenApiCorrector {
203206 // placeholders)
204207 final replacementPattern = RegExp ('[ "\' /]$escapedType [ "\' :]' );
205208
206- fileContent = fileContent .replaceAllMapped (
209+ correctedContent = correctedContent .replaceAllMapped (
207210 replacementPattern,
208211 (match) => match[0 ]! .replaceAll (type, correctType),
209212 );
@@ -222,11 +225,13 @@ class OpenApiCorrector {
222225
223226 for (final block in validBlocks) {
224227 final placeholder = block.placeholder;
225- final idx = fileContent.indexOf (placeholder, searchStart);
226- if (idx == - 1 ) continue ;
228+ final idx = correctedContent.indexOf (placeholder, searchStart);
229+ if (idx == - 1 ) {
230+ continue ;
231+ }
227232
228233 // Write everything before the placeholder
229- resultBuf.write (fileContent .substring (searchStart, idx));
234+ resultBuf.write (correctedContent .substring (searchStart, idx));
230235
231236 var restoredBlock = block.original;
232237
@@ -254,15 +259,17 @@ class OpenApiCorrector {
254259 }
255260
256261 // Write remaining content after the last placeholder
257- resultBuf.write (fileContent .substring (searchStart));
262+ resultBuf.write (correctedContent .substring (searchStart));
258263
259264 return resultBuf.toString ();
260265 }
261266
262267 /// Build a single regex that matches any $ref or discriminator mapping value
263268 /// containing any of the types that need correction.
264269 RegExp ? _buildCombinedRefPattern (Map <String , String > typeCorrections) {
265- if (typeCorrections.isEmpty) return null ;
270+ if (typeCorrections.isEmpty) {
271+ return null ;
272+ }
266273
267274 // Escape each type for use in regex, sort by length descending so longer
268275 // names match first (prevents partial matches)
@@ -279,7 +286,7 @@ class OpenApiCorrector {
279286 // Match $ref or discriminator mapping values that reference any of the
280287 // types. Captures the schema name in group 1.
281288 return RegExp (
282- '(?:\\\$ ref:|\\ w+):\\ s*[\' "]#/[^\' "]*/(${ alternation } )[\' "]' ,
289+ '(?:\\\$ ref:|\\ w+):\\ s*[\' "]#/[^\' "]*/($alternation )[\' "]' ,
283290 );
284291 }
285292}
0 commit comments