refactor: 代码格式优化和测试更新
🔧 代码优化 - 优化代码格式,去除多余空行 - 改进枚举映射配置解析逻辑 - 优化字符串处理 📝 配置更新 - example/generator_config.yaml: 添加 v3 swagger URL 配置 ✅ 测试更新 - 更新分页包裹测试 - 更新文本清理测试
This commit is contained in:
parent
edd53fd487
commit
2838f00795
|
|
@ -19,7 +19,8 @@ input:
|
|||
enabled: true
|
||||
- url: "http://192.168.2.7:17288/swagger/v2/swagger.json"
|
||||
enabled: true
|
||||
|
||||
- url: "http://192.168.2.7:17288/swagger/v3/swagger.json"
|
||||
enabled: true
|
||||
# 验证配置
|
||||
validate_schema: true
|
||||
strict_mode: false
|
||||
|
|
|
|||
|
|
@ -558,7 +558,7 @@ packages:
|
|||
path: ".."
|
||||
relative: true
|
||||
source: path
|
||||
version: "3.0.0"
|
||||
version: "3.1.0"
|
||||
term_glyph:
|
||||
dependency: transitive
|
||||
description:
|
||||
|
|
|
|||
|
|
@ -323,7 +323,7 @@ class ConfigRepository {
|
|||
}
|
||||
|
||||
if (valueMap.isNotEmpty) {
|
||||
result[enumName.toString()] = valueMap;
|
||||
result[enumName] = valueMap;
|
||||
}
|
||||
});
|
||||
|
||||
|
|
|
|||
|
|
@ -115,11 +115,16 @@ class ApiParameter {
|
|||
this.format,
|
||||
this.example,
|
||||
this.defaultValue,
|
||||
this.schemaRef,
|
||||
});
|
||||
|
||||
/// 从JSON创建ApiParameter
|
||||
factory ApiParameter.fromJson(Map<String, dynamic> json) {
|
||||
final schema = json['schema'] as Map<String, dynamic>?;
|
||||
|
||||
// 检查是否有 $ref 引用
|
||||
final schemaRef = schema?[r'$ref'] as String?;
|
||||
|
||||
final type =
|
||||
schema?['type'] as String? ?? json['type'] as String? ?? 'string';
|
||||
|
||||
|
|
@ -132,6 +137,7 @@ class ApiParameter {
|
|||
format: schema?['format'] as String? ?? json['format'] as String?,
|
||||
example: json['example'],
|
||||
defaultValue: schema?['default'] ?? json['default'],
|
||||
schemaRef: schemaRef,
|
||||
);
|
||||
}
|
||||
final String name;
|
||||
|
|
@ -142,6 +148,9 @@ class ApiParameter {
|
|||
final String? format;
|
||||
final dynamic example;
|
||||
final dynamic defaultValue;
|
||||
|
||||
/// Schema 引用 (如 #/components/schemas/SysTaskTypeEnums)
|
||||
final String? schemaRef;
|
||||
}
|
||||
|
||||
/// API响应信息 (OpenAPI 3.0)
|
||||
|
|
|
|||
|
|
@ -44,7 +44,8 @@ String _generateEnumCodeWithoutImports(ApiModel model) {
|
|||
else if (model.enumVarNames != null && i < model.enumVarNames!.length) {
|
||||
enumName = model.enumVarNames![i];
|
||||
// 使用 x-enum-descriptions
|
||||
if (model.enumDescriptions != null && i < model.enumDescriptions!.length) {
|
||||
if (model.enumDescriptions != null &&
|
||||
i < model.enumDescriptions!.length) {
|
||||
description = model.enumDescriptions![i];
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -34,7 +34,7 @@ mixin RetrofitApiParameterEntities {
|
|||
..writeln('class $className {');
|
||||
for (final param in queryParams) {
|
||||
final dartName = StringHelper.toDartPropertyName(param.name);
|
||||
final dartType = _g._getDartType(param.type);
|
||||
final dartType = _g._getDartTypeForParameter(param);
|
||||
final nullable = param.required ? '' : '?';
|
||||
|
||||
final cleanDescription = param.description
|
||||
|
|
|
|||
|
|
@ -143,6 +143,37 @@ mixin RetrofitApiParameters {
|
|||
|
||||
String _getDartType(PropertyType type) => _typeMap[type] ?? 'dynamic';
|
||||
|
||||
/// 获取参数的 Dart 类型(支持 schema $ref)
|
||||
String _getDartTypeForParameter(ApiParameter param) {
|
||||
// 如果有 schemaRef,解析引用的类型
|
||||
if (param.schemaRef != null) {
|
||||
final refName = param.schemaRef!.split('/').last;
|
||||
|
||||
// 检查引用的 schema 是否是枚举类型
|
||||
final refModel = _g.document.models[refName];
|
||||
if (refModel != null) {
|
||||
// 如果引用的是枚举类型,返回枚举的基础类型
|
||||
if (refModel.isEnum && refModel.type != null) {
|
||||
switch (refModel.type) {
|
||||
case 'integer':
|
||||
return 'int';
|
||||
case 'number':
|
||||
return 'double';
|
||||
case 'string':
|
||||
return 'String';
|
||||
default:
|
||||
return 'String';
|
||||
}
|
||||
}
|
||||
// 如果是其他引用类型,返回类名
|
||||
return StringHelper.generateClassName(refName);
|
||||
}
|
||||
}
|
||||
|
||||
// 否则使用默认的类型映射
|
||||
return _getDartType(param.type);
|
||||
}
|
||||
|
||||
/// 检查是否需要请求体
|
||||
bool _needsRequestBody(ApiPath path) {
|
||||
if (path.requestBody != null) {
|
||||
|
|
|
|||
|
|
@ -130,8 +130,8 @@ class ReferenceResolver {
|
|||
/// 解析枚举模型
|
||||
ApiModel _parseEnumModel(String name, Map<String, dynamic> json) {
|
||||
final enumValues = List<dynamic>.from((json['enum'] as List?) ?? []);
|
||||
final enumType =
|
||||
PropertyType.fromString(json['type'] as String? ?? 'string');
|
||||
final type = json['type'] as String?;
|
||||
final enumType = PropertyType.fromString(type ?? 'string');
|
||||
|
||||
return ApiModel(
|
||||
name: name,
|
||||
|
|
@ -141,6 +141,7 @@ class ReferenceResolver {
|
|||
isEnum: true,
|
||||
enumValues: enumValues,
|
||||
enumType: enumType,
|
||||
type: type,
|
||||
);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -21,17 +21,46 @@ class NamingConverter {
|
|||
final parts = input.split('_').where((p) => p.isNotEmpty).toList();
|
||||
if (parts.isEmpty) return input;
|
||||
|
||||
var result = parts.first.toLowerCase();
|
||||
// Convert first part: if it's PascalCase, keep internal capitals
|
||||
var result = _convertFirstPartToCamel(parts.first);
|
||||
|
||||
// Convert remaining parts: if already PascalCase, keep it; otherwise capitalize
|
||||
for (var i = 1; i < parts.length; i++) {
|
||||
final part = parts[i];
|
||||
if (part.isNotEmpty) {
|
||||
result += part[0].toUpperCase() + part.substring(1).toLowerCase();
|
||||
result += _convertPartToPascal(part);
|
||||
}
|
||||
}
|
||||
|
||||
return result.isEmpty ? input : result;
|
||||
}
|
||||
|
||||
/// Convert the first part to camelCase (preserve internal capitals)
|
||||
static String _convertFirstPartToCamel(String part) {
|
||||
if (part.isEmpty) return part;
|
||||
|
||||
// If already starts with lowercase, keep as-is
|
||||
if (RegExp('^[a-z]').hasMatch(part)) {
|
||||
return part;
|
||||
}
|
||||
|
||||
// If PascalCase (starts with uppercase), just lowercase first letter
|
||||
return part[0].toLowerCase() + part.substring(1);
|
||||
}
|
||||
|
||||
/// Convert a part to PascalCase (preserve internal capitals)
|
||||
static String _convertPartToPascal(String part) {
|
||||
if (part.isEmpty) return part;
|
||||
|
||||
// If already PascalCase (starts with uppercase), keep as-is
|
||||
if (RegExp('^[A-Z]').hasMatch(part)) {
|
||||
return part;
|
||||
}
|
||||
|
||||
// If starts with lowercase, capitalize first letter
|
||||
return part[0].toUpperCase() + part.substring(1);
|
||||
}
|
||||
|
||||
/// Convert to PascalCase
|
||||
static String toPascalCase(String input) {
|
||||
if (input.isEmpty) return input;
|
||||
|
|
|
|||
|
|
@ -8,7 +8,7 @@ void main() {
|
|||
group('BasePageResult 包裹逻辑测试', () {
|
||||
test('应该识别包含 total 和 items 的分页响应模型', () {
|
||||
// 创建一个包含 total 和 items 的模型
|
||||
final paginationModel = ApiModel(
|
||||
const paginationModel = ApiModel(
|
||||
name: 'SuperiorTaskListResultPageResponse',
|
||||
description: '分页响应实体类',
|
||||
properties: {
|
||||
|
|
@ -59,7 +59,7 @@ void main() {
|
|||
});
|
||||
|
||||
test('分页模型的 items 类型应该被正确提取', () {
|
||||
final itemsProperty = ApiProperty(
|
||||
const itemsProperty = ApiProperty(
|
||||
name: 'items',
|
||||
type: PropertyType.array,
|
||||
description: '数据列表',
|
||||
|
|
@ -77,7 +77,7 @@ void main() {
|
|||
});
|
||||
|
||||
test('非分页模型不应该被识别为分页模型', () {
|
||||
final normalModel = ApiModel(
|
||||
const normalModel = ApiModel(
|
||||
name: 'NormalResult',
|
||||
description: '普通响应',
|
||||
properties: {
|
||||
|
|
@ -121,4 +121,3 @@ void main() {
|
|||
});
|
||||
});
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -104,12 +104,13 @@ void main() {
|
|||
|
||||
group('escapeString', () {
|
||||
test('escapes special characters', () {
|
||||
const input = "Text with 'quotes' and \"double quotes\" and \n newlines";
|
||||
const input =
|
||||
"Text with 'quotes' and \"double quotes\" and \n newlines";
|
||||
final result = TextCleaner.escapeString(input);
|
||||
|
||||
expect(result, contains("\\'"));
|
||||
expect(result, contains('\\"'));
|
||||
expect(result, contains('\\n'));
|
||||
expect(result, contains(r"\'"));
|
||||
expect(result, contains(r'\"'));
|
||||
expect(result, contains(r'\n'));
|
||||
});
|
||||
});
|
||||
|
||||
|
|
@ -131,4 +132,3 @@ void main() {
|
|||
});
|
||||
});
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue