Skip to content

Commit 0d04c85

Browse files
authored
Merge pull request #139 from aoisupersix/add-updateannotatedfile-option
Add updateAnnotatedFile property
2 parents 2eab4ab + 47bb0fc commit 0d04c85

12 files changed

+83
-15
lines changed

openapi-generator-annotations/lib/src/openapi_generator_annotations_base.dart

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,10 @@ class Openapi {
111111
/// Include in depth logging output from run commands.
112112
final bool debugLogging;
113113

114+
/// If set to true, the annotated file will be added or updated comment lines as of the last run date on the top of the file.
115+
/// Defaults to true
116+
final bool updateAnnotatedFile;
117+
114118
const Openapi({
115119
this.additionalProperties,
116120
this.skipSpecValidation = false,
@@ -129,6 +133,7 @@ class Openapi {
129133
this.cachePath,
130134
this.projectPubspecPath,
131135
this.debugLogging = false,
136+
this.updateAnnotatedFile = true,
132137
});
133138
}
134139

openapi-generator-annotations/test/openapi_generator_annotations_test.dart

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,13 @@ void main() {
5151
debugLogging: true);
5252
expect(api.debugLogging, isTrue);
5353
});
54+
test('Sets updateAnnotatedFile', () {
55+
final api = Openapi(
56+
inputSpec: InputSpec.json(),
57+
generatorName: Generator.dart,
58+
updateAnnotatedFile: false);
59+
expect(api.updateAnnotatedFile, isFalse);
60+
});
5461
group('InputSpec', () {
5562
group('local spec', () {
5663
test('provides default yaml path', () {

openapi-generator/lib/src/models/generator_arguments.dart

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,11 @@ class GeneratorArguments {
4747
/// Default: false
4848
final bool skipValidation;
4949

50+
/// Write the last run placeholder to the annotated file.
51+
///
52+
/// Default: true
53+
final bool updateAnnotatedFile;
54+
5055
/// Provides an OAS spec file.
5156
///
5257
/// When the [useNextGen] flag is set this should be the spec file configuration
@@ -104,6 +109,8 @@ class GeneratorArguments {
104109
annotations.readPropertyOrDefault('runSourceGenOnOutput', true),
105110
shouldFetchDependencies =
106111
annotations.readPropertyOrDefault('fetchDependencies', true),
112+
updateAnnotatedFile =
113+
annotations.readPropertyOrDefault('updateAnnotatedFile', true),
107114
outputDirectory = annotations.readPropertyOrNull('outputDirectory'),
108115
cachePath =
109116
annotations.readPropertyOrDefault('cachePath', defaultCachedPath),

openapi-generator/lib/src/openapi_generator_runner.dart

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -220,24 +220,35 @@ class OpenapiGenerator extends GeneratorForAnnotation<annots.Openapi> {
220220
),
221221
),
222222
);
223-
await updateAnnotatedFile(annotatedPath: annotatedPath).then(
224-
(_) => logOutputMessage(
225-
log: log,
226-
communication: OutputMessage(
227-
message: 'Successfully updated annotated file.',
228-
level: Level.CONFIG,
223+
if (args.updateAnnotatedFile) {
224+
await updateAnnotatedFile(annotatedPath: annotatedPath).then(
225+
(_) => logOutputMessage(
226+
log: log,
227+
communication: OutputMessage(
228+
message: 'Successfully updated annotated file.',
229+
level: Level.CONFIG,
230+
),
229231
),
230-
),
231-
onError: (e, st) => logOutputMessage(
232+
onError: (e, st) => logOutputMessage(
233+
log: log,
234+
communication: OutputMessage(
235+
message: 'Failed to update annotated class file.',
236+
level: Level.WARNING,
237+
additionalContext: e,
238+
stackTrace: st,
239+
),
240+
),
241+
);
242+
} else {
243+
logOutputMessage(
232244
log: log,
233245
communication: OutputMessage(
234-
message: 'Failed to update annotated class file.',
246+
message:
247+
'Skipped updating annotated file step because flag was set.',
235248
level: Level.WARNING,
236-
additionalContext: e,
237-
stackTrace: st,
238249
),
239-
),
240-
);
250+
);
251+
}
241252
}
242253
return '';
243254
}

openapi-generator/test/builder_test.dart

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,8 +132,8 @@ class TestClassConfig extends OpenapiGeneratorConfig {}
132132
'https://raw.githubusercontent.com/Nexushunter/tagmine-api/main/openapi.yaml';
133133
final basePath = '${testSpecPath}output-nextgen/';
134134
final f = File('${basePath}cache.json');
135-
tearDown(() {
136-
final b = File(basePath);
135+
tearDownAll(() {
136+
final b = Directory(basePath);
137137
if (b.existsSync()) b.deleteSync(recursive: true);
138138
});
139139

@@ -263,6 +263,27 @@ class TestClassConfig extends OpenapiGeneratorConfig {}
263263
copy.deleteSync();
264264
expect(hasOutput, isTrue);
265265
});
266+
test('skip updating annotated file', () async {
267+
final annotatedFile = File(
268+
'.${Platform.pathSeparator}test${Platform.pathSeparator}specs${Platform.pathSeparator}output-nextgen${Platform.pathSeparator}annotated_file.dart');
269+
final annotetedFileContent = '\n';
270+
await annotatedFile.writeAsString(annotetedFileContent, flush: true);
271+
272+
generatedOutput = await generate('''
273+
@Openapi(
274+
inputSpecFile: '$specPath',
275+
inputSpec: RemoteSpec(path: '$specPath'),
276+
useNextGen: true,
277+
cachePath: '${f.path}',
278+
updateAnnotatedFile: false,
279+
)
280+
''', path: annotatedFile.path);
281+
expect(
282+
generatedOutput,
283+
contains(
284+
'Skipped updating annotated file step because flag was set.'));
285+
expect(annotatedFile.readAsStringSync(), equals(annotetedFileContent));
286+
});
266287
group('source gen', () {
267288
group('uses Flutter', () {
268289
group('with wrapper', () {

openapi-generator/test/generator_arguments_test.dart

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ void main() {
1919
test('shouldFetchDependencies',
2020
() => expect(args.shouldFetchDependencies, isTrue));
2121
test('skipValidation', () => expect(args.skipValidation, isFalse));
22+
test('updateAnnotatedFile',
23+
() => expect(args.updateAnnotatedFile, isTrue));
2224
test(
2325
'pubspecPath',
2426
() => expect(
@@ -168,6 +170,7 @@ void main() {
168170
expect(args.isRemote, isFalse);
169171
expect(args.generatorName, 'dart-dio');
170172
expect(args.shouldGenerateSources, isTrue);
173+
expect(args.updateAnnotatedFile, isTrue);
171174
expect(args.additionalProperties?.useEnumExtension, isTrue);
172175
expect(args.additionalProperties?.pubAuthor, 'test author');
173176
expect(await args.jarArgs, [
@@ -218,6 +221,7 @@ void main() {
218221
expect(args.isRemote, isFalse);
219222
expect(args.generatorName, 'dart-dio');
220223
expect(args.shouldGenerateSources, isTrue);
224+
expect(args.updateAnnotatedFile, isTrue);
221225
expect(args.additionalProperties?.useEnumExtension, isTrue);
222226
expect((args.additionalProperties as DioProperties?)?.nullableFields,
223227
isTrue);
@@ -269,6 +273,7 @@ void main() {
269273
expect(args.isRemote, isFalse);
270274
expect(args.generatorName, 'dart-dio');
271275
expect(args.shouldGenerateSources, isTrue);
276+
expect(args.updateAnnotatedFile, isTrue);
272277
expect(args.additionalProperties?.useEnumExtension, isTrue);
273278
expect(
274279
(args.additionalProperties as DioAltProperties?)?.nullSafe, isTrue);
@@ -329,6 +334,7 @@ void main() {
329334
'https://petstore3.swagger.io/api/v3/openapi.json');
330335
expect(args.generatorName, 'dart-dio');
331336
expect(args.shouldGenerateSources, isTrue);
337+
expect(args.updateAnnotatedFile, isTrue);
332338
expect(args.additionalProperties?.useEnumExtension, isTrue);
333339
expect(
334340
(args.additionalProperties as DioAltProperties?)?.nullSafe, isTrue);

openapi-generator/test/specs/dio_alt_properties_test_config.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,6 @@ import 'package:openapi_generator_annotations/openapi_generator_annotations.dart
2222
nullSafeArrayDefault: true),
2323
inlineSchemaNameMappings: {'200resp': 'OkResp'},
2424
projectPubspecPath: './test/specs/dart_pubspec.test.yaml',
25+
updateAnnotatedFile: true,
2526
)
2627
class DioAltPropertiesTestConfig {}

openapi-generator/test/specs/dio_properties_test_config.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,6 @@ import 'package:openapi_generator_annotations/openapi_generator_annotations.dart
2121
nullableFields: true),
2222
inlineSchemaNameMappings: {'200resp': 'OkResp'},
2323
projectPubspecPath: './test/specs/dart_pubspec.test.yaml',
24+
updateAnnotatedFile: true,
2425
)
2526
class DioPropertiesTestConfig {}

openapi-generator/test/specs/input_remote_properties_test_config.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,6 @@ import 'package:openapi_generator_annotations/openapi_generator_annotations.dart
2323
nullSafeArrayDefault: true),
2424
inlineSchemaNameMappings: {'200resp': 'OkResp'},
2525
projectPubspecPath: './test/specs/dart_pubspec.test.yaml',
26+
updateAnnotatedFile: true,
2627
)
2728
class DioAltPropertiesTestConfig {}

openapi-generator/test/specs/test_config.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,6 @@ import 'package:openapi_generator_annotations/openapi_generator_annotations.dart
2222
legacyDiscriminatorBehavior: true),
2323
inlineSchemaNameMappings: {'200resp': 'OkResp'},
2424
projectPubspecPath: './test/specs/dart_pubspec.test.yaml',
25+
updateAnnotatedFile: true,
2526
)
2627
class TestClassConfig {}

0 commit comments

Comments
 (0)