refactor: 代码格式优化和测试更新

🔧 代码优化
- 优化代码格式,去除多余空行
- 改进枚举映射配置解析逻辑
- 优化字符串处理

📝 配置更新
- example/generator_config.yaml: 添加 v3 swagger URL 配置

 测试更新
- 更新分页包裹测试
- 更新文本清理测试
This commit is contained in:
Max 2025-12-03 17:25:25 +08:00
parent edd53fd487
commit 2838f00795
12 changed files with 119 additions and 48 deletions

View File

@ -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

View File

@ -558,7 +558,7 @@ packages:
path: ".."
relative: true
source: path
version: "3.0.0"
version: "3.1.0"
term_glyph:
dependency: transitive
description:

View File

@ -323,7 +323,7 @@ class ConfigRepository {
}
if (valueMap.isNotEmpty) {
result[enumName.toString()] = valueMap;
result[enumName] = valueMap;
}
});

View File

@ -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)

View File

@ -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];
}
}

View File

@ -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

View File

@ -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) {

View File

@ -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,
);
}

View File

@ -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;

View File

@ -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() {
});
});
}

View File

@ -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() {
});
});
}