Skip to content

Commit a9d6161

Browse files
committed
1.43.0
1 parent 2bcf1ab commit a9d6161

File tree

4 files changed

+31
-19
lines changed

4 files changed

+31
-19
lines changed

swagger_parser/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
## 1.43.0
2+
- Allow standard dart_mappable serializer method naming convention
3+
- Fix default values for Enums in forms
4+
- Fix errors with corrector
5+
16
## 1.42.0
27
- Support for streaming & SSE
38
- Fix preserve property names and required modifiers in properties blocks

swagger_parser/lib/src/parser/corrector/open_api_corrector.dart

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

swagger_parser/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: swagger_parser
22
description: Package that generates REST clients and data classes from OpenApi definition file
3-
version: 1.42.0
3+
version: 1.43.0
44
repository: https://github.com/Carapacik/swagger_parser
55
topics:
66
- swagger

swagger_parser/test/corrector/corrector_performance_test.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ String _generateLargeSchema({
2525
..writeln(' content:')
2626
..writeln(' application/json:')
2727
..writeln(' schema:')
28-
..writeln(" \$ref: '#/components/schemas/Schema0'")
28+
..writeln(r" $ref: '#/components/schemas/Schema0'")
2929
..writeln('components:')
3030
..writeln(' schemas:');
3131

@@ -52,7 +52,7 @@ void main() {
5252
'corrector with replacement rules completes in reasonable time '
5353
'for large schemas',
5454
() {
55-
final schemaCount = 200;
55+
const schemaCount = 200;
5656
final schema = _generateLargeSchema(
5757
schemaCount: schemaCount,
5858
propertiesPerSchema: 5,

0 commit comments

Comments
 (0)