diff --git a/swagger_parser/CHANGELOG.md b/swagger_parser/CHANGELOG.md index 49e807e6..4406118a 100644 --- a/swagger_parser/CHANGELOG.md +++ b/swagger_parser/CHANGELOG.md @@ -1,3 +1,6 @@ +## 1.12.0 +- Add new config parameter `export_file` + ## 1.11.3 - Fix: annotating client methods with the first specified content type header in OpenAPI V2 schemas if the specified one is not the default. diff --git a/swagger_parser/README.md b/swagger_parser/README.md index 446f7072..9f2f775d 100644 --- a/swagger_parser/README.md +++ b/swagger_parser/README.md @@ -52,20 +52,19 @@ swagger_parser: # Sets the OpenApi schema path directory for api definition. schema_path: schemas/openapi.json - - # Sets the url of the OpenApi schema + # Sets the url of the OpenApi schema. schema_url: https://petstore.swagger.io/v2/swagger.json # Required. Sets output directory for generated files (Clients and DTOs). output_directory: lib/api # Optional. Sets the programming language. - # Current available languages are: dart, kotlin + # Current available languages are: dart, kotlin. language: dart # Optional. If 'schema_path' and 'schema_url' are specified, what will be used. # Current available options are: path, url. - prefer_schema_source: path + prefer_schema_source: url # Optional (dart only). Set 'true' to generate data classes using freezed package. freezed: false @@ -74,16 +73,19 @@ swagger_parser: # with interface and all clients instances. root_client: true - # Optional (dart only). Set root client name + # Optional (dart only). Set root client name. root_client_name: RestClient - # Optional. Set default content-type for all requests + # Optional. Set default content-type for all requests. default_content_type: "application/json" # Optional. Set API name for folder and export file # If not specified, the file name is used. name: null + # Optional (dart only). Set 'true' to generate export file. + export_file: true + # Optional. Set to 'true' to put the all api in its folder. put_in_folder: false diff --git a/swagger_parser/example/swagger_parser.yaml b/swagger_parser/example/swagger_parser.yaml index d45d01d1..0379c7d1 100644 --- a/swagger_parser/example/swagger_parser.yaml +++ b/swagger_parser/example/swagger_parser.yaml @@ -3,8 +3,7 @@ swagger_parser: # Sets the OpenApi schema path directory for api definition. # schema_path: schemas/openapi.json - - # Sets the url of the OpenApi schema + # Sets the url of the OpenApi schema. # schema_url: https://petstore.swagger.io/v2/swagger.json # Required. Sets output directory for generated files (Clients and DTOs). @@ -28,13 +27,16 @@ swagger_parser: # Optional (dart only). Set root client name root_client_name: RestClient - # Optional. Set default content-type for all requests + # Optional. Set default content-type for all requests. default_content_type: "application/json" - # Optional. Set API name for folder and export file (coming soon). + # Optional. Set API name for folder and export file. # If not specified, the file name is used. name: null + # Optional (dart only). Set 'true' to generate export file. + export_file: true + # Optional. Set to 'true' to put the all api in its folder. put_in_folder: false diff --git a/swagger_parser/lib/src/config/yaml_config.dart b/swagger_parser/lib/src/config/yaml_config.dart index b03e52da..727d38bf 100644 --- a/swagger_parser/lib/src/config/yaml_config.dart +++ b/swagger_parser/lib/src/config/yaml_config.dart @@ -29,6 +29,7 @@ final class YamlConfig { this.freezed, this.rootClient, this.rootClientName, + this.exportFile, this.clientPostfix, this.putClientsInFolder, this.squashClients, @@ -157,6 +158,13 @@ final class YamlConfig { ); } + final exportFile = yamlConfig['export_file']; + if (exportFile is! bool?) { + throw const ConfigException( + "Config parameter 'export_file' must be bool.", + ); + } + final rawClientPostfix = yamlConfig['client_postfix']?.toString().trim(); final clientPostfix = @@ -273,6 +281,7 @@ final class YamlConfig { freezed: freezed ?? rootConfig?.freezed, rootClient: rootClient ?? rootConfig?.rootClient, rootClientName: rootClientName ?? rootConfig?.rootClientName, + exportFile: exportFile ?? rootConfig?.exportFile, clientPostfix: clientPostfix ?? rootConfig?.clientPostfix, putInFolder: putInFolder ?? rootConfig?.putInFolder, putClientsInFolder: putClientsInFolder ?? rootConfig?.putClientsInFolder, @@ -373,6 +382,7 @@ final class YamlConfig { final String? clientPostfix; final bool? rootClient; final String? rootClientName; + final bool? exportFile; final bool? squashClients; final bool? pathMethodName; final bool? putClientsInFolder; diff --git a/swagger_parser/lib/src/generator/fill_controller.dart b/swagger_parser/lib/src/generator/fill_controller.dart index dc2dbb7c..0801c72d 100644 --- a/swagger_parser/lib/src/generator/fill_controller.dart +++ b/swagger_parser/lib/src/generator/fill_controller.dart @@ -13,6 +13,7 @@ final class FillController { ProgrammingLanguage programmingLanguage = ProgrammingLanguage.dart, String clientPostfix = 'Client', String rootClientName = 'RestClient', + String exportFileName = 'export', bool putClientsInFolder = false, bool freezed = false, bool enumsToJson = false, @@ -20,9 +21,10 @@ final class FillController { bool markFilesAsGenerated = false, String defaultContentType = 'application/json', }) : _openApiInfo = openApiInfo, - _clientPostfix = clientPostfix, _programmingLanguage = programmingLanguage, + _clientPostfix = clientPostfix, _rootClientName = rootClientName, + _exportFileName = exportFileName, _putClientsInFolder = putClientsInFolder, _freezed = freezed, _enumsToJson = enumsToJson, @@ -32,8 +34,9 @@ final class FillController { final OpenApiInfo _openApiInfo; final ProgrammingLanguage _programmingLanguage; - final String _rootClientName; final String _clientPostfix; + final String _rootClientName; + final String _exportFileName; final bool _freezed; final bool _putClientsInFolder; final bool _enumsToJson; @@ -89,4 +92,19 @@ final class FillController { ), ); } + + GeneratedFile fillExportFile({ + required List restClients, + required List dataClasses, + GeneratedFile? rootClient, + }) => + GeneratedFile( + name: '$_exportFileName.${_programmingLanguage.fileExtension}', + contents: _programmingLanguage.exportFileContent( + restClients: restClients, + dataClasses: dataClasses, + rootClient: rootClient, + markFileAsGenerated: _markFilesAsGenerated, + ), + ); } diff --git a/swagger_parser/lib/src/generator/generator.dart b/swagger_parser/lib/src/generator/generator.dart index 838eeeda..2e02e428 100644 --- a/swagger_parser/lib/src/generator/generator.dart +++ b/swagger_parser/lib/src/generator/generator.dart @@ -37,6 +37,7 @@ final class Generator { bool? freezed, bool? rootClient, String? clientPostfix, + bool? exportFile, String? rootClientName, bool? putClientsInFolder, bool? squashClients, @@ -61,6 +62,7 @@ final class Generator { _freezed = freezed ?? false, _rootClient = rootClient ?? true, _rootClientName = rootClientName ?? 'RestClient', + _exportFile = exportFile ?? true, _clientPostfix = clientPostfix ?? 'Client', _putClientsInFolder = putClientsInFolder ?? false, _squashClients = squashClients ?? false, @@ -87,6 +89,7 @@ final class Generator { freezed: yamlConfig.freezed, rootClient: yamlConfig.rootClient, rootClientName: yamlConfig.rootClientName, + exportFile: yamlConfig.exportFile, clientPostfix: yamlConfig.clientPostfix, putClientsInFolder: yamlConfig.putClientsInFolder, squashClients: yamlConfig.squashClients, @@ -137,6 +140,9 @@ final class Generator { /// Root client name final String _rootClientName; + /// Generate export file + final bool _exportFile; + /// Client postfix final String _clientPostfix; @@ -302,6 +308,7 @@ final class Generator { programmingLanguage: _programmingLanguage, rootClientName: _rootClientName, clientPostfix: _clientPostfix, + exportFileName: _name ?? 'export', freezed: _freezed, putClientsInFolder: _putClientsInFolder, enumsToJson: _enumsToJson, @@ -309,18 +316,35 @@ final class Generator { markFilesAsGenerated: _markFilesAsGenerated, defaultContentType: _defaultContentType, ); - final files = []; - for (final client in _restClients) { - files.add(fillController.fillRestClientContent(client)); - } - for (final dataClass in _dataClasses) { - files.add(fillController.fillDtoContent(dataClass)); - } - if (_rootClient && - _programmingLanguage == ProgrammingLanguage.dart && - _restClients.isNotEmpty) { - files.add(fillController.fillRootClient(_restClients)); - } + + final restClientFiles = + _restClients.map(fillController.fillRestClientContent).toList(); + + final dataClassesFiles = + _dataClasses.map(fillController.fillDtoContent).toList(); + + final rootClientFile = _programmingLanguage == ProgrammingLanguage.dart && + _rootClient && + _restClients.isNotEmpty + ? fillController.fillRootClient(_restClients) + : null; + + final exportFile = + _programmingLanguage == ProgrammingLanguage.dart && _exportFile + ? fillController.fillExportFile( + restClients: restClientFiles, + dataClasses: dataClassesFiles, + rootClient: rootClientFile, + ) + : null; + + final files = [ + ...restClientFiles, + ...dataClassesFiles, + if (rootClientFile != null) rootClientFile, + if (exportFile != null) exportFile, + ]; + return files; } } diff --git a/swagger_parser/lib/src/generator/models/programming_language.dart b/swagger_parser/lib/src/generator/models/programming_language.dart index c29b6740..181c9532 100644 --- a/swagger_parser/lib/src/generator/models/programming_language.dart +++ b/swagger_parser/lib/src/generator/models/programming_language.dart @@ -2,6 +2,7 @@ import 'package:collection/collection.dart'; import '../generator_exception.dart'; import '../templates/dart_enum_dto_template.dart'; +import '../templates/dart_export_file_template.dart'; import '../templates/dart_freezed_dto_template.dart'; import '../templates/dart_json_serializable_dto_template.dart'; import '../templates/dart_retrofit_client_template.dart'; @@ -11,6 +12,7 @@ import '../templates/kotlin_enum_dto_template.dart'; import '../templates/kotlin_moshi_dto_template.dart'; import '../templates/kotlin_retrofit_client_template.dart'; import '../templates/kotlin_typedef_template.dart'; +import 'generated_file.dart'; import 'open_api_info.dart'; import 'universal_data_class.dart'; import 'universal_rest_client.dart'; @@ -132,4 +134,20 @@ enum ProgrammingLanguage { ), kotlin => '', }; + + String exportFileContent({ + required bool markFileAsGenerated, + required List restClients, + required List dataClasses, + GeneratedFile? rootClient, + }) => + switch (this) { + dart => dartExportFileTemplate( + markFileAsGenerated: markFileAsGenerated, + restClients: restClients, + dataClasses: dataClasses, + rootClient: rootClient, + ), + kotlin => '', + }; } diff --git a/swagger_parser/lib/src/generator/templates/dart_export_file_template.dart b/swagger_parser/lib/src/generator/templates/dart_export_file_template.dart new file mode 100644 index 00000000..b6155ef8 --- /dev/null +++ b/swagger_parser/lib/src/generator/templates/dart_export_file_template.dart @@ -0,0 +1,26 @@ +import '../../utils/utils.dart'; +import '../models/generated_file.dart'; + +/// Provides template for generating dart export file +String dartExportFileTemplate({ + required bool markFileAsGenerated, + required List restClients, + required List dataClasses, + GeneratedFile? rootClient, +}) { + final restClientsNames = restClients.map((e) => e.name).toSet(); + final dataClassesNames = dataClasses.map((e) => e.name).toSet(); + final rootClientName = rootClient?.name; + + return '${generatedFileComment( + markFileAsGenerated: markFileAsGenerated, + )}' + '${rootClientName != null ? '// Root client\n' : ''}' + '${rootClientName != null ? "export '$rootClientName';" : ''}' + '${rootClientName != null ? '\n\n' : ''}' + '${restClientsNames.isNotEmpty ? '// Clients\n' : ''}' + '${restClientsNames.map((e) => "export '$e';").join('\n')}' + '${restClientsNames.isNotEmpty ? '\n\n' : ''}' + '${dataClassesNames.isNotEmpty ? '// Data classes\n' : ''}' + '${dataClassesNames.map((e) => "export '$e';").join('\n')}'; +} diff --git a/swagger_parser/lib/src/utils/utils.dart b/swagger_parser/lib/src/utils/utils.dart index 10d86a05..a8a90ec1 100644 --- a/swagger_parser/lib/src/utils/utils.dart +++ b/swagger_parser/lib/src/utils/utils.dart @@ -169,7 +169,7 @@ void summaryStatisticsMessage({ '${formatNumber(statistics.totalRestClients)} clients, ' '${formatNumber(statistics.totalRequests)} requests, ' '${formatNumber(statistics.totalDataClasses)} data classes.\n' - '${formatNumber(statistics.totalFiles)} files with ${formatNumber(statistics.totalLines)} lines of code.', + '${formatNumber(statistics.totalFiles)} files with ${formatNumber(statistics.totalLines)} lines of code.\n', ); }