From 9ef123d3b72c9a5316fb81e9027e4250d2871a8b Mon Sep 17 00:00:00 2001 From: Muhammad Mohiuddin Date: Wed, 8 Jan 2025 22:36:54 +0100 Subject: [PATCH] Fix: config property inheritance (#283) --- swagger_parser/lib/src/config/swp_config.dart | 187 +++++------------- 1 file changed, 50 insertions(+), 137 deletions(-) diff --git a/swagger_parser/lib/src/config/swp_config.dart b/swagger_parser/lib/src/config/swp_config.dart index fe66c59b..fff86c7f 100644 --- a/swagger_parser/lib/src/config/swp_config.dart +++ b/swagger_parser/lib/src/config/swp_config.dart @@ -70,12 +70,14 @@ class SWPConfig { bool isRootConfig = false, SWPConfig? rootConfig, }) { - var schemaPath = yamlMap['schema_path']?.toString(); + var schemaPath = + yamlMap['schema_path']?.toString() ?? rootConfig?.schemaPath; if (isRootConfig && schemaPath == null) { schemaPath = ''; } - final schemaUrl = yamlMap['schema_url']?.toString(); + final schemaUrl = + yamlMap['schema_url']?.toString() ?? rootConfig?.schemaUrl; if (schemaUrl != null) { final uri = Uri.tryParse(schemaUrl); if (uri == null) { @@ -91,7 +93,8 @@ class SWPConfig { ); } - var outputDirectory = yamlMap['output_directory']?.toString(); + var outputDirectory = + yamlMap['output_directory']?.toString() ?? rootConfig?.outputDirectory; if (isRootConfig && outputDirectory == null) { outputDirectory = ''; } @@ -114,62 +117,22 @@ class SWPConfig { ? (schemaPath ?? schemaUrl)!.split('/').last.split('.').first : rawName; - final defaultContentType = yamlMap['default_content_type']; - if (defaultContentType is! String?) { - throw const ConfigException( - "Config parameter 'default_content_type' must be String.", - ); - } - - final extrasParameterByDefault = yamlMap['extras_parameter_by_default']; - if (extrasParameterByDefault is! bool?) { - throw const ConfigException( - "Config parameter 'extras_parameter_by_default' must be bool.", - ); - } - + final defaultContentType = yamlMap['default_content_type'] as String? ?? + rootConfig?.defaultContentType; + final extrasParameterByDefault = + yamlMap['extras_parameter_by_default'] as bool? ?? + rootConfig?.extrasParameterByDefault; final dioOptionsParameterByDefault = - yamlMap['dio_options_parameter_by_default']; - if (dioOptionsParameterByDefault is! bool?) { - throw const ConfigException( - "Config parameter 'dio_options_parameter_by_default' must be bool.", - ); - } - - final pathMethodName = yamlMap['path_method_name']; - if (pathMethodName is! bool?) { - throw const ConfigException( - "Config parameter 'path_method_name' must be bool.", - ); - } - - final requiredByDefault = yamlMap['required_by_default']; - if (requiredByDefault is! bool?) { - throw const ConfigException( - "Config parameter 'required_by_default' must be bool.", - ); - } - - final mergeClients = yamlMap['merge_clients']; - if (mergeClients is! bool?) { - throw const ConfigException( - "Config parameter 'merge_clients' must be bool.", - ); - } - - final enumsParentPrefix = yamlMap['enums_parent_prefix']; - if (enumsParentPrefix is! bool?) { - throw const ConfigException( - "Config parameter 'enums_parent_prefix' must be bool.", - ); - } - - final rawSkippedParameters = yamlMap['skipped_parameters']; - if (rawSkippedParameters is! YamlList?) { - throw const ConfigException( - "Config parameter 'skipped_parameters' must be list.", - ); - } + yamlMap['dio_options_parameter_by_default'] as bool? ?? + rootConfig?.dioOptionsParameterByDefault; + final pathMethodName = + yamlMap['path_method_name'] as bool? ?? rootConfig?.pathMethodName; + final mergeClients = + yamlMap['merge_clients'] as bool? ?? rootConfig?.mergeClients; + final enumsParentPrefix = yamlMap['enums_parent_prefix'] as bool? ?? + rootConfig?.enumsParentPrefix; + + final rawSkippedParameters = yamlMap['skipped_parameters'] as YamlList?; List? skippedParameters; if (rawSkippedParameters != null) { skippedParameters = []; @@ -181,13 +144,14 @@ class SWPConfig { } skippedParameters.add(p); } + } else if (rootConfig?.skippedParameters != null) { + skippedParameters = List.from(rootConfig!.skippedParameters); } - ProgrammingLanguage? language; final rawLanguage = yamlMap['language']?.toString(); - if (rawLanguage != null) { - language = ProgrammingLanguage.fromString(rawLanguage); - } + final language = rawLanguage == null + ? rootConfig?.language + : ProgrammingLanguage.fromString(rawLanguage); JsonSerializer? jsonSerializer; final rawJsonSerializer = yamlMap['json_serializer']?.toString(); @@ -197,75 +161,26 @@ class SWPConfig { jsonSerializer = rootConfig!.jsonSerializer; } - final rootClient = yamlMap['root_client']; - if (rootClient is! bool?) { - throw const ConfigException( - "Config parameter 'root_client' must be bool.", - ); - } - - final rootClientName = yamlMap['root_client_name']; - if (rootClientName is! String?) { - throw const ConfigException( - "Config parameter 'root_client_name' must be String.", - ); - } - - final clientPostfix = yamlMap['client_postfix']; - if (clientPostfix is! String?) { - throw const ConfigException( - "Config parameter 'client_postfix' must be String.", - ); - } - - final exportFile = yamlMap['export_file']; - if (exportFile is! bool?) { - throw const ConfigException( - "Config parameter 'export_file' must be bool.", - ); - } - - final putClientsInFolder = yamlMap['put_clients_in_folder']; - if (putClientsInFolder is! bool?) { - throw const ConfigException( - "Config parameter 'put_clients_in_folder' must be bool.", - ); - } - - final enumsToJson = yamlMap['enums_to_json']; - if (enumsToJson is! bool?) { - throw const ConfigException( - "Config parameter 'enums_to_json' must be bool.", - ); - } - - final unknownEnumValue = yamlMap['unknown_enum_value']; - if (unknownEnumValue is! bool?) { - throw const ConfigException( - "Config parameter 'unknown_enum_value' must be bool.", - ); - } - - final markFilesAsGenerated = yamlMap['mark_files_as_generated']; - if (markFilesAsGenerated is! bool?) { - throw const ConfigException( - "Config parameter 'mark_files_as_generated' must be bool.", - ); - } - - final originalHttpResponse = yamlMap['original_http_response']; - if (originalHttpResponse is! bool?) { - throw const ConfigException( - "Config parameter 'original_http_response' must be bool.", - ); - } - - final rawReplacementRules = yamlMap['replacement_rules']; - if (rawReplacementRules is! YamlList?) { - throw const ConfigException( - "Config parameter 'replacement_rules' must be list.", - ); - } + final rootClient = + yamlMap['root_client'] as bool? ?? rootConfig?.rootClient; + final rootClientName = + yamlMap['root_client_name'] as String? ?? rootConfig?.rootClientName; + final clientPostfix = + yamlMap['client_postfix'] as String? ?? rootConfig?.clientPostfix; + final exportFile = + yamlMap['export_file'] as bool? ?? rootConfig?.exportFile; + final putClientsInFolder = yamlMap['put_clients_in_folder'] as bool? ?? + rootConfig?.putClientsInFolder; + final enumsToJson = + yamlMap['enums_to_json'] as bool? ?? rootConfig?.enumsToJson; + final unknownEnumValue = + yamlMap['unknown_enum_value'] as bool? ?? rootConfig?.unknownEnumValue; + final markFilesAsGenerated = yamlMap['mark_files_as_generated'] as bool? ?? + rootConfig?.markFilesAsGenerated; + final originalHttpResponse = yamlMap['original_http_response'] as bool? ?? + rootConfig?.originalHttpResponse; + + final rawReplacementRules = yamlMap['replacement_rules'] as YamlList?; List? replacementRules; if (rawReplacementRules != null) { replacementRules = []; @@ -285,14 +200,12 @@ class SWPConfig { ), ); } + } else if (rootConfig?.replacementRules != null) { + replacementRules = List.from(rootConfig!.replacementRules); } - final generateValidator = yamlMap['generate_validator']; - if (generateValidator is! bool?) { - throw const ConfigException( - "Config parameter 'generate_validator' must be bool.", - ); - } + final generateValidator = + yamlMap['generate_validator'] as bool? ?? rootConfig?.generateValidator; // Default config final dc = SWPConfig(name: name, outputDirectory: outputDirectory);