Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: config property inheritance #283

Merged
merged 15 commits into from
Jan 8, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
187 changes: 50 additions & 137 deletions swagger_parser/lib/src/config/swp_config.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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 = '';
}
Expand All @@ -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<String>? skippedParameters;
if (rawSkippedParameters != null) {
skippedParameters = [];
Expand All @@ -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();
Expand All @@ -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<ReplacementRule>? replacementRules;
if (rawReplacementRules != null) {
replacementRules = [];
Expand All @@ -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);
Expand Down
Loading