From e7bfa947a9a9610509bd52f0cec08a5689495015 Mon Sep 17 00:00:00 2001 From: aoisupersix Date: Sun, 17 Mar 2024 19:05:01 +0900 Subject: [PATCH 1/4] feat: updateAnnotatedFile property to OpenApi annotation --- .../lib/src/openapi_generator_annotations_base.dart | 5 +++++ .../test/openapi_generator_annotations_test.dart | 7 +++++++ 2 files changed, 12 insertions(+) diff --git a/openapi-generator-annotations/lib/src/openapi_generator_annotations_base.dart b/openapi-generator-annotations/lib/src/openapi_generator_annotations_base.dart index d7c73ac..6811707 100644 --- a/openapi-generator-annotations/lib/src/openapi_generator_annotations_base.dart +++ b/openapi-generator-annotations/lib/src/openapi_generator_annotations_base.dart @@ -111,6 +111,10 @@ class Openapi { /// Include in depth logging output from run commands. final bool debugLogging; + /// 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. + /// Defaults to true + final bool updateAnnotatedFile; + const Openapi({ this.additionalProperties, this.skipSpecValidation = false, @@ -129,6 +133,7 @@ class Openapi { this.cachePath, this.projectPubspecPath, this.debugLogging = false, + this.updateAnnotatedFile = true, }); } diff --git a/openapi-generator-annotations/test/openapi_generator_annotations_test.dart b/openapi-generator-annotations/test/openapi_generator_annotations_test.dart index 3bf3ea1..97fdf21 100644 --- a/openapi-generator-annotations/test/openapi_generator_annotations_test.dart +++ b/openapi-generator-annotations/test/openapi_generator_annotations_test.dart @@ -51,6 +51,13 @@ void main() { debugLogging: true); expect(api.debugLogging, isTrue); }); + test('Sets updateAnnotatedFile', () { + final api = Openapi( + inputSpec: InputSpec.json(), + generatorName: Generator.dart, + updateAnnotatedFile: false); + expect(api.updateAnnotatedFile, isFalse); + }); group('InputSpec', () { group('local spec', () { test('provides default yaml path', () { From c108469787da68794dce7ca595387539e009eb50 Mon Sep 17 00:00:00 2001 From: aoisupersix Date: Sun, 17 Mar 2024 19:15:37 +0900 Subject: [PATCH 2/4] feat: annotated file updating made optional by argument --- .../lib/src/models/generator_arguments.dart | 7 ++++ .../lib/src/openapi_generator_runner.dart | 37 ++++++++++++------- 2 files changed, 31 insertions(+), 13 deletions(-) diff --git a/openapi-generator/lib/src/models/generator_arguments.dart b/openapi-generator/lib/src/models/generator_arguments.dart index 2cbcbba..edb0b52 100644 --- a/openapi-generator/lib/src/models/generator_arguments.dart +++ b/openapi-generator/lib/src/models/generator_arguments.dart @@ -47,6 +47,11 @@ class GeneratorArguments { /// Default: false final bool skipValidation; + /// Write the last run placeholder to the annotated file. + /// + /// Default: true + final bool updateAnnotatedFile; + /// Provides an OAS spec file. /// /// When the [useNextGen] flag is set this should be the spec file configuration @@ -104,6 +109,8 @@ class GeneratorArguments { annotations.readPropertyOrDefault('runSourceGenOnOutput', true), shouldFetchDependencies = annotations.readPropertyOrDefault('fetchDependencies', true), + updateAnnotatedFile = + annotations.readPropertyOrDefault('updateAnnotatedFile', true), outputDirectory = annotations.readPropertyOrNull('outputDirectory'), cachePath = annotations.readPropertyOrDefault('cachePath', defaultCachedPath), diff --git a/openapi-generator/lib/src/openapi_generator_runner.dart b/openapi-generator/lib/src/openapi_generator_runner.dart index c92a140..baf0671 100755 --- a/openapi-generator/lib/src/openapi_generator_runner.dart +++ b/openapi-generator/lib/src/openapi_generator_runner.dart @@ -220,24 +220,35 @@ class OpenapiGenerator extends GeneratorForAnnotation { ), ), ); - await updateAnnotatedFile(annotatedPath: annotatedPath).then( - (_) => logOutputMessage( - log: log, - communication: OutputMessage( - message: 'Successfully updated annotated file.', - level: Level.CONFIG, + if (args.updateAnnotatedFile) { + await updateAnnotatedFile(annotatedPath: annotatedPath).then( + (_) => logOutputMessage( + log: log, + communication: OutputMessage( + message: 'Successfully updated annotated file.', + level: Level.CONFIG, + ), ), - ), - onError: (e, st) => logOutputMessage( + onError: (e, st) => logOutputMessage( + log: log, + communication: OutputMessage( + message: 'Failed to update annotated class file.', + level: Level.WARNING, + additionalContext: e, + stackTrace: st, + ), + ), + ); + } else { + logOutputMessage( log: log, communication: OutputMessage( - message: 'Failed to update annotated class file.', + message: + 'Skipped updating annotated file step because flag was set.', level: Level.WARNING, - additionalContext: e, - stackTrace: st, ), - ), - ); + ); + } } return ''; } From b02f09b4d55ef820a74f70d34c1c994bd96adbdd Mon Sep 17 00:00:00 2001 From: aoisupersix Date: Sun, 17 Mar 2024 19:28:32 +0900 Subject: [PATCH 3/4] test: add/modify tests for updateAnnotatedFile property --- openapi-generator/test/builder_test.dart | 21 +++++++++++++++++++ .../test/generator_arguments_test.dart | 6 ++++++ .../specs/dio_alt_properties_test_config.dart | 1 + .../specs/dio_properties_test_config.dart | 1 + .../input_remote_properties_test_config.dart | 1 + openapi-generator/test/specs/test_config.dart | 1 + .../test/test_annotations/test_configs.dart | 6 ++++++ .../test/test_annotations/test_generator.dart | 1 + 8 files changed, 38 insertions(+) diff --git a/openapi-generator/test/builder_test.dart b/openapi-generator/test/builder_test.dart index e95efc5..9a25bb3 100644 --- a/openapi-generator/test/builder_test.dart +++ b/openapi-generator/test/builder_test.dart @@ -263,6 +263,27 @@ class TestClassConfig extends OpenapiGeneratorConfig {} copy.deleteSync(); expect(hasOutput, isTrue); }); + test('skip updating annotated file', () async { + final annotatedFile = File( + '.${Platform.pathSeparator}test${Platform.pathSeparator}specs${Platform.pathSeparator}output-nextgen${Platform.pathSeparator}annotated_file.dart'); + final annotetedFileContent = '\n'; + await annotatedFile.writeAsString(annotetedFileContent, flush: true); + + generatedOutput = await generate(''' +@Openapi( + inputSpecFile: '$specPath', + inputSpec: RemoteSpec(path: '$specPath'), + useNextGen: true, + cachePath: '${f.path}', + updateAnnotatedFile: false, +) + ''', path: annotatedFile.path); + expect( + generatedOutput, + contains( + 'Skipped updating annotated file step because flag was set.')); + expect(annotatedFile.readAsStringSync(), equals(annotetedFileContent)); + }); group('source gen', () { group('uses Flutter', () { group('with wrapper', () { diff --git a/openapi-generator/test/generator_arguments_test.dart b/openapi-generator/test/generator_arguments_test.dart index 3f15acb..747cb8c 100644 --- a/openapi-generator/test/generator_arguments_test.dart +++ b/openapi-generator/test/generator_arguments_test.dart @@ -19,6 +19,8 @@ void main() { test('shouldFetchDependencies', () => expect(args.shouldFetchDependencies, isTrue)); test('skipValidation', () => expect(args.skipValidation, isFalse)); + test('updateAnnotatedFile', + () => expect(args.updateAnnotatedFile, isTrue)); test( 'pubspecPath', () => expect( @@ -168,6 +170,7 @@ void main() { expect(args.isRemote, isFalse); expect(args.generatorName, 'dart-dio'); expect(args.shouldGenerateSources, isTrue); + expect(args.updateAnnotatedFile, isTrue); expect(args.additionalProperties?.useEnumExtension, isTrue); expect(args.additionalProperties?.pubAuthor, 'test author'); expect(await args.jarArgs, [ @@ -218,6 +221,7 @@ void main() { expect(args.isRemote, isFalse); expect(args.generatorName, 'dart-dio'); expect(args.shouldGenerateSources, isTrue); + expect(args.updateAnnotatedFile, isTrue); expect(args.additionalProperties?.useEnumExtension, isTrue); expect((args.additionalProperties as DioProperties?)?.nullableFields, isTrue); @@ -269,6 +273,7 @@ void main() { expect(args.isRemote, isFalse); expect(args.generatorName, 'dart-dio'); expect(args.shouldGenerateSources, isTrue); + expect(args.updateAnnotatedFile, isTrue); expect(args.additionalProperties?.useEnumExtension, isTrue); expect( (args.additionalProperties as DioAltProperties?)?.nullSafe, isTrue); @@ -329,6 +334,7 @@ void main() { 'https://petstore3.swagger.io/api/v3/openapi.json'); expect(args.generatorName, 'dart-dio'); expect(args.shouldGenerateSources, isTrue); + expect(args.updateAnnotatedFile, isTrue); expect(args.additionalProperties?.useEnumExtension, isTrue); expect( (args.additionalProperties as DioAltProperties?)?.nullSafe, isTrue); diff --git a/openapi-generator/test/specs/dio_alt_properties_test_config.dart b/openapi-generator/test/specs/dio_alt_properties_test_config.dart index 91f9571..3f2e015 100644 --- a/openapi-generator/test/specs/dio_alt_properties_test_config.dart +++ b/openapi-generator/test/specs/dio_alt_properties_test_config.dart @@ -22,5 +22,6 @@ import 'package:openapi_generator_annotations/openapi_generator_annotations.dart nullSafeArrayDefault: true), inlineSchemaNameMappings: {'200resp': 'OkResp'}, projectPubspecPath: './test/specs/dart_pubspec.test.yaml', + updateAnnotatedFile: true, ) class DioAltPropertiesTestConfig {} diff --git a/openapi-generator/test/specs/dio_properties_test_config.dart b/openapi-generator/test/specs/dio_properties_test_config.dart index ce4327b..082c1f3 100644 --- a/openapi-generator/test/specs/dio_properties_test_config.dart +++ b/openapi-generator/test/specs/dio_properties_test_config.dart @@ -21,5 +21,6 @@ import 'package:openapi_generator_annotations/openapi_generator_annotations.dart nullableFields: true), inlineSchemaNameMappings: {'200resp': 'OkResp'}, projectPubspecPath: './test/specs/dart_pubspec.test.yaml', + updateAnnotatedFile: true, ) class DioPropertiesTestConfig {} diff --git a/openapi-generator/test/specs/input_remote_properties_test_config.dart b/openapi-generator/test/specs/input_remote_properties_test_config.dart index 2c875f2..fdf9e89 100644 --- a/openapi-generator/test/specs/input_remote_properties_test_config.dart +++ b/openapi-generator/test/specs/input_remote_properties_test_config.dart @@ -23,5 +23,6 @@ import 'package:openapi_generator_annotations/openapi_generator_annotations.dart nullSafeArrayDefault: true), inlineSchemaNameMappings: {'200resp': 'OkResp'}, projectPubspecPath: './test/specs/dart_pubspec.test.yaml', + updateAnnotatedFile: true, ) class DioAltPropertiesTestConfig {} diff --git a/openapi-generator/test/specs/test_config.dart b/openapi-generator/test/specs/test_config.dart index 902d5ee..8087ada 100644 --- a/openapi-generator/test/specs/test_config.dart +++ b/openapi-generator/test/specs/test_config.dart @@ -22,5 +22,6 @@ import 'package:openapi_generator_annotations/openapi_generator_annotations.dart legacyDiscriminatorBehavior: true), inlineSchemaNameMappings: {'200resp': 'OkResp'}, projectPubspecPath: './test/specs/dart_pubspec.test.yaml', + updateAnnotatedFile: true, ) class TestClassConfig {} diff --git a/openapi-generator/test/test_annotations/test_configs.dart b/openapi-generator/test/test_annotations/test_configs.dart index 8778825..212fd5b 100644 --- a/openapi-generator/test/test_annotations/test_configs.dart +++ b/openapi-generator/test/test_annotations/test_configs.dart @@ -13,6 +13,8 @@ const inputSpec = ''; const runSourceGenOnOutput = true; const skipSpecValidation = false; + +const updateAnnotatedFile = true; ''') @Openapi(inputSpec: InputSpec(path: ''), generatorName: Generator.dio) class TestClassDefault {} @@ -29,6 +31,8 @@ const inputSpec = ''; const runSourceGenOnOutput = true; const skipSpecValidation = false; + +const updateAnnotatedFile = true; ''') @Openapi( inputSpec: InputSpec(path: ''), @@ -49,6 +53,8 @@ const inputSpec = ''; const runSourceGenOnOutput = true; const skipSpecValidation = false; + +const updateAnnotatedFile = true; ''') @Openapi( inputSpec: InputSpec(path: ''), diff --git a/openapi-generator/test/test_annotations/test_generator.dart b/openapi-generator/test/test_annotations/test_generator.dart index 643c923..3688f11 100644 --- a/openapi-generator/test/test_annotations/test_generator.dart +++ b/openapi-generator/test/test_annotations/test_generator.dart @@ -48,6 +48,7 @@ class TestGenerator extends src_gen.GeneratorForAnnotation { SupportedFields(name: 'runSourceGenOnOutput', type: bool), SupportedFields(name: 'cachePath', type: String), SupportedFields(name: 'projectPubspecPath', type: String), + SupportedFields(name: 'updateAnnotatedFile', type: bool), ]..sort((a, b) => a.name.compareTo(b.name)); for (final field in fields) { final v = annotation.read(field.name); From 47bb0fc3ee3152d4e17e91fa31e9dce2578395c9 Mon Sep 17 00:00:00 2001 From: aoisupersix Date: Sun, 17 Mar 2024 19:35:05 +0900 Subject: [PATCH 4/4] test: fix test spec directory is not being deleted --- openapi-generator/test/builder_test.dart | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/openapi-generator/test/builder_test.dart b/openapi-generator/test/builder_test.dart index 9a25bb3..991b987 100644 --- a/openapi-generator/test/builder_test.dart +++ b/openapi-generator/test/builder_test.dart @@ -132,8 +132,8 @@ class TestClassConfig extends OpenapiGeneratorConfig {} 'https://raw.githubusercontent.com/Nexushunter/tagmine-api/main/openapi.yaml'; final basePath = '${testSpecPath}output-nextgen/'; final f = File('${basePath}cache.json'); - tearDown(() { - final b = File(basePath); + tearDownAll(() { + final b = Directory(basePath); if (b.existsSync()) b.deleteSync(recursive: true); });