feat: support json_serializable configuration (checked, explicit_to_json) in generated models
This commit is contained in:
parent
0b20ad6ab5
commit
1231af9f0b
|
|
@ -558,7 +558,7 @@ packages:
|
||||||
path: ".."
|
path: ".."
|
||||||
relative: true
|
relative: true
|
||||||
source: path
|
source: path
|
||||||
version: "3.1.0"
|
version: "3.1.4"
|
||||||
term_glyph:
|
term_glyph:
|
||||||
dependency: transitive
|
dependency: transitive
|
||||||
description:
|
description:
|
||||||
|
|
|
||||||
|
|
@ -54,6 +54,10 @@ class SwaggerConfig {
|
||||||
static Map<String, Map<dynamic, EnumKeyMapping>>? get enumKeyMappings =>
|
static Map<String, Map<dynamic, EnumKeyMapping>>? get enumKeyMappings =>
|
||||||
ConfigRepository.loadSync().enumKeyMappings;
|
ConfigRepository.loadSync().enumKeyMappings;
|
||||||
|
|
||||||
|
/// 获取 JsonSerializable 配置(从配置文件读取)
|
||||||
|
static JsonSerializableConfig? get jsonSerializableConfig =>
|
||||||
|
ConfigRepository.loadSync().jsonSerializableConfig;
|
||||||
|
|
||||||
/// 默认文档文件名
|
/// 默认文档文件名
|
||||||
static const String defaultDocumentationFile =
|
static const String defaultDocumentationFile =
|
||||||
'generated_api_documentation.md';
|
'generated_api_documentation.md';
|
||||||
|
|
|
||||||
|
|
@ -17,6 +17,28 @@ class EnumKeyMapping {
|
||||||
final String? description;
|
final String? description;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// JSON Serializable 配置
|
||||||
|
class JsonSerializableConfig {
|
||||||
|
const JsonSerializableConfig({
|
||||||
|
this.checked = false,
|
||||||
|
this.explicitToJson = false,
|
||||||
|
this.includeIfNull = true,
|
||||||
|
});
|
||||||
|
|
||||||
|
final bool checked;
|
||||||
|
final bool explicitToJson;
|
||||||
|
final bool includeIfNull;
|
||||||
|
|
||||||
|
static JsonSerializableConfig? fromMap(Map<String, dynamic>? map) {
|
||||||
|
if (map == null) return null;
|
||||||
|
return JsonSerializableConfig(
|
||||||
|
checked: map['checked'] as bool? ?? false,
|
||||||
|
explicitToJson: map['explicit_to_json'] as bool? ?? false,
|
||||||
|
includeIfNull: map['include_if_null'] as bool? ?? true,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// 配置仓库
|
/// 配置仓库
|
||||||
/// 负责加载和提供配置信息
|
/// 负责加载和提供配置信息
|
||||||
class ConfigRepository {
|
class ConfigRepository {
|
||||||
|
|
@ -292,6 +314,15 @@ class ConfigRepository {
|
||||||
return api?['base_page_result_import'] as String? ?? '';
|
return api?['base_page_result_import'] as String? ?? '';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// 获取 JsonSerializable 配置
|
||||||
|
JsonSerializableConfig? get jsonSerializableConfig {
|
||||||
|
final generation = _config['generation'] as Map<String, dynamic>?;
|
||||||
|
final models = generation?['models'] as Map<String, dynamic>?;
|
||||||
|
final jsonSerializable =
|
||||||
|
models?['json_serializable'] as Map<String, dynamic>?;
|
||||||
|
return JsonSerializableConfig.fromMap(jsonSerializable);
|
||||||
|
}
|
||||||
|
|
||||||
/// 获取枚举键名映射配置
|
/// 获取枚举键名映射配置
|
||||||
/// 返回格式: { "EnumName": { value: { "name": "KEY_NAME", "description": "描述" } } }
|
/// 返回格式: { "EnumName": { value: { "name": "KEY_NAME", "description": "描述" } } }
|
||||||
Map<String, Map<dynamic, EnumKeyMapping>>? get enumKeyMappings {
|
Map<String, Map<dynamic, EnumKeyMapping>>? get enumKeyMappings {
|
||||||
|
|
|
||||||
|
|
@ -127,6 +127,18 @@ String _generateAnnotatedModelCodeWithoutImports(
|
||||||
buffer.writeln(StringHelper.generateComment(model.description));
|
buffer.writeln(StringHelper.generateComment(model.description));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
final jsonConfig = SwaggerConfig.jsonSerializableConfig;
|
||||||
|
if (jsonConfig != null) {
|
||||||
|
final params = <String>[];
|
||||||
|
if (jsonConfig.checked) params.add('checked: true');
|
||||||
|
if (jsonConfig.explicitToJson) params.add('explicitToJson: true');
|
||||||
|
if (!jsonConfig.includeIfNull) params.add('includeIfNull: false');
|
||||||
|
|
||||||
|
if (params.isNotEmpty) {
|
||||||
|
buffer.writeln('@JsonSerializable(${params.join(', ')})');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
buffer
|
buffer
|
||||||
..writeln('@freezed')
|
..writeln('@freezed')
|
||||||
..writeln('abstract class $className with _\$$className {')
|
..writeln('abstract class $className with _\$$className {')
|
||||||
|
|
|
||||||
|
|
@ -44,19 +44,17 @@ String buildSingleModelFile(
|
||||||
..writeln();
|
..writeln();
|
||||||
|
|
||||||
if (!model.isEnum) {
|
if (!model.isEnum) {
|
||||||
buffer
|
buffer.writeln(
|
||||||
..writeln(
|
"import 'package:freezed_annotation/freezed_annotation.dart';",
|
||||||
"import 'package:freezed_annotation/freezed_annotation.dart';",
|
);
|
||||||
)
|
|
||||||
..writeln();
|
|
||||||
} else {
|
|
||||||
buffer
|
|
||||||
..writeln(
|
|
||||||
"import 'package:json_annotation/json_annotation.dart';",
|
|
||||||
)
|
|
||||||
..writeln();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
buffer
|
||||||
|
..writeln(
|
||||||
|
"import 'package:json_annotation/json_annotation.dart';",
|
||||||
|
)
|
||||||
|
..writeln();
|
||||||
|
|
||||||
final importedTypes = generator.getImportedTypes(model);
|
final importedTypes = generator.getImportedTypes(model);
|
||||||
if (importedTypes.isNotEmpty) {
|
if (importedTypes.isNotEmpty) {
|
||||||
buffer
|
buffer
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue