Complete OpenAPI 3.0/3.1 generation, diagnostics, lifecycle safety, enum and naming mappings, bilingual documentation, examples, and pub.dev release validation.
110 lines
3.2 KiB
Dart
110 lines
3.2 KiB
Dart
import 'dart:io';
|
|
|
|
import 'package:koi_swagger_parser/koi_swagger_parser.dart';
|
|
import 'package:path/path.dart' as p;
|
|
import 'package:test/test.dart';
|
|
|
|
void main() {
|
|
group('DBA fixture generation', () {
|
|
test('正确生成 Koi 风格 API 与模型文件', () async {
|
|
final tempDir = await Directory.systemTemp.createTemp(
|
|
'koi_swagger_parser_dba_',
|
|
);
|
|
addTearDown(() => tempDir.delete(recursive: true));
|
|
|
|
final outputDir = p.join(tempDir.path, 'lib', 'src');
|
|
final fixturePath = p.normalize(
|
|
p.join(Directory.current.path, 'test', 'fixtures', 'dba_openapi.json'),
|
|
);
|
|
|
|
final configFile = File(p.join(tempDir.path, 'koi_swagger_parser.yaml'));
|
|
await configFile.writeAsString('''
|
|
input:
|
|
swagger_urls:
|
|
- url: $fixturePath
|
|
enabled: true
|
|
|
|
output:
|
|
base_dir: $outputDir
|
|
api_dir: ${p.join(outputDir, 'api')}
|
|
models_dir: ${p.join(outputDir, 'api_models')}
|
|
included_tags:
|
|
- stores
|
|
- materials
|
|
split_by_tags: true
|
|
|
|
generation:
|
|
api:
|
|
enabled: true
|
|
use_retrofit: true
|
|
cancel_token_support: true
|
|
client:
|
|
class_name: DbaApiClient
|
|
file_name: dba_api_client
|
|
response_wrapper:
|
|
mode: external_generic
|
|
detect_standard_envelope: true
|
|
success_data_field: data
|
|
external_generic:
|
|
import: package:dba_network/dba_network.dart
|
|
class_name: BaseResult
|
|
models:
|
|
enabled: true
|
|
class_prefix: Koi
|
|
default_non_nullable_list_to_empty: true
|
|
json_serializable:
|
|
checked: true
|
|
explicit_to_json: true
|
|
include_if_null: false
|
|
formatting:
|
|
run_dart_format: false
|
|
''');
|
|
|
|
final result = await const KoiSwaggerParser().generate(
|
|
configPath: configFile.path,
|
|
);
|
|
|
|
expect(result.operationsCount, greaterThan(0));
|
|
expect(result.modelsCount, greaterThan(0));
|
|
|
|
final materialsApi = File(
|
|
p.join(outputDir, 'api', 'v1', 'materials_api.dart'),
|
|
).readAsStringSync();
|
|
expect(
|
|
materialsApi,
|
|
contains('Future<BaseResult<KoiMaterialListResponse>> listMaterials'),
|
|
);
|
|
expect(materialsApi, contains("@Query('store_id') int? storeId"));
|
|
expect(materialsApi, contains("@Query('tag') String? tag"));
|
|
|
|
final storesApi = File(
|
|
p.join(outputDir, 'api', 'v1', 'stores_api.dart'),
|
|
).readAsStringSync();
|
|
expect(
|
|
storesApi,
|
|
contains('Future<BaseResult<List<KoiStoreResponse>>> listStores'),
|
|
);
|
|
expect(
|
|
storesApi,
|
|
contains('Future<BaseResult<Map<String, bool>>> deleteStore'),
|
|
);
|
|
|
|
final storeWrite = File(
|
|
p.join(outputDir, 'api_models', 'request', 'store_write.dart'),
|
|
).readAsStringSync();
|
|
expect(storeWrite, contains('String? address'));
|
|
|
|
final materialResponse = File(
|
|
p.join(outputDir, 'api_models', 'result', 'material_response.dart'),
|
|
).readAsStringSync();
|
|
expect(materialResponse, contains("@JsonKey(name: 'store_id',"));
|
|
expect(materialResponse, contains('required int storeId'));
|
|
|
|
final sharedError = File(
|
|
p.join(outputDir, 'api_models', 'shared', 'http_validation_error.dart'),
|
|
);
|
|
expect(sharedError.existsSync(), isTrue);
|
|
});
|
|
});
|
|
}
|