From 4df56bb5f073d01138ca084b9e50bd44af847afb Mon Sep 17 00:00:00 2001 From: Max Date: Mon, 12 Jan 2026 15:51:06 +0800 Subject: [PATCH 1/3] fix: SS Start bugs --- lib/utils/string_helper.dart | 4 +--- test/config_prefix_test.dart | 43 +++++++++++++++++++++++++++++++++++- 2 files changed, 43 insertions(+), 4 deletions(-) diff --git a/lib/utils/string_helper.dart b/lib/utils/string_helper.dart index c186241..2d18e1f 100644 --- a/lib/utils/string_helper.dart +++ b/lib/utils/string_helper.dart @@ -52,9 +52,7 @@ class StringHelper { var className = NamingConverter.generateClassName(name); final prefix = SwaggerConfig.modelClassPrefix; if (prefix != null && prefix.isNotEmpty) { - if (!className.startsWith(prefix)) { - className = prefix + className; - } + className = prefix + className; } return className; } diff --git a/test/config_prefix_test.dart b/test/config_prefix_test.dart index 21f31dd..60c843e 100644 --- a/test/config_prefix_test.dart +++ b/test/config_prefix_test.dart @@ -1,6 +1,5 @@ import 'dart:io'; -import 'package:swagger_generator_flutter/core/config.dart'; import 'package:swagger_generator_flutter/core/config_repository.dart'; import 'package:swagger_generator_flutter/utils/string_helper.dart'; import 'package:test/test.dart'; @@ -47,6 +46,48 @@ generator: expect(config.modelClassPrefix, isNull); }); + test('should apply class_prefix using StringHelper.generateClassName', () { + configFile.writeAsStringSync(''' +generator: + name: test + +output: + models: + class_prefix: "S" + +generation: + models: + class_prefix: "S" +'''); + + // Reload config to ensure StringHelper picks up the new prefix + // This relies on SwaggerConfig.modelClassPrefix internally calling ConfigRepository.loadSync() + // which will find the config file in the current working directory (or tempDir in this test context). + // For this test to work, the test runner's CWD must be tempDir, or PathResolver must be mocked. + // Assuming PathResolver.findConfigFile() correctly finds the temp file. + + // The current implementation of StringHelper.generateClassName relies on SwaggerConfig.modelClassPrefix + // which in turn calls ConfigRepository.loadSync() without arguments. + // This means it will try to find the config file in the current working directory or via PathResolver. + // For this test to pass, the `configFile` must be discoverable by `ConfigRepository.loadSync()`. + // This test implicitly relies on the test environment's CWD or PathResolver setup. + + // To make this test robust, StringHelper should ideally take a Config object or + // SwaggerConfig should have a way to inject a test config. + // For now, we proceed with the assumption that `ConfigRepository.loadSync()` will find `configFile`. + + // Force a reload of the static config by accessing it. + // This is a bit of a hack due to the static nature of SwaggerConfig and StringHelper. + // A better approach would be to pass the config explicitly or mock the static dependencies. + ConfigRepository.loadSync(configFile + .path); // Ensure the config is loaded for the static accessor + + expect(StringHelper.generateClassName('User'), 'SUser'); + expect(StringHelper.generateClassName('model'), 'SModel'); + // Test case: prefix is 'S' and name starts with 'S' + expect(StringHelper.generateClassName('SubjectInfo'), 'SSubjectInfo'); + }); + // NOTE: Testing StringHelper.generateClassName directly implies checking if it reads from the GLOBAL config. // However, ConfigRepository.loadSync() creates an instance, but SwaggerConfig accessors call ConfigRepository.loadSync() individually. // Since ConfigRepository.loadSync() without args looks for default file, we need a way to inject the config or point it to our file. From 9fa88e9821e2bd4499f1a5b04b867362a3804e1a Mon Sep 17 00:00:00 2001 From: Max Date: Mon, 12 Jan 2026 15:51:36 +0800 Subject: [PATCH 2/3] fix bugs --- lib/core/config_repository.dart | 5 +++++ test/config_prefix_test.dart | 5 +++-- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/lib/core/config_repository.dart b/lib/core/config_repository.dart index 28e5067..8645041 100644 --- a/lib/core/config_repository.dart +++ b/lib/core/config_repository.dart @@ -66,6 +66,11 @@ class ConfigRepository { static ConfigRepository? _cachedConfig; + /// 设置缓存配置(仅用于测试) + static void setCachedConfig(ConfigRepository config) { + _cachedConfig = config; + } + /// 同步加载配置(用于向后兼容或必须同步的场景) /// 为默认配置路径启用缓存 static ConfigRepository loadSync([String? configPath]) { diff --git a/test/config_prefix_test.dart b/test/config_prefix_test.dart index 60c843e..f7047ee 100644 --- a/test/config_prefix_test.dart +++ b/test/config_prefix_test.dart @@ -79,8 +79,9 @@ generation: // Force a reload of the static config by accessing it. // This is a bit of a hack due to the static nature of SwaggerConfig and StringHelper. // A better approach would be to pass the config explicitly or mock the static dependencies. - ConfigRepository.loadSync(configFile - .path); // Ensure the config is loaded for the static accessor + // Load config and set it as cached so static accessors pick it up + final config = ConfigRepository.loadSync(configFile.path); + ConfigRepository.setCachedConfig(config); expect(StringHelper.generateClassName('User'), 'SUser'); expect(StringHelper.generateClassName('model'), 'SModel'); From 07b9a8c2f59f1d71448e5cce12b562b25d04070a Mon Sep 17 00:00:00 2001 From: Max Date: Mon, 12 Jan 2026 15:52:05 +0800 Subject: [PATCH 3/3] =?UTF-8?q?fix:=20-=20=E2=9C=85=20**=E5=BC=BA=E5=88=B6?= =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=89=8D=E7=BC=80**=EF=BC=9A=E4=BF=AE?= =?UTF-8?q?=E5=A4=8D=E4=BA=86=E5=BD=93=E7=B1=BB=E5=90=8D=E5=B7=B2=E5=8C=85?= =?UTF-8?q?=E5=90=AB=E5=89=8D=E7=BC=80=E6=97=B6=E4=B8=8D=E9=87=8D=E5=A4=8D?= =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=89=8D=E7=BC=80=E7=9A=84=E9=80=BB=E8=BE=91?= =?UTF-8?q?=EF=BC=8C=E7=A1=AE=E4=BF=9D=20`SubjectInfo`=20=E9=85=8D?= =?UTF-8?q?=E7=BD=AE=E5=89=8D=E7=BC=80=20`S`=20=E5=90=8E=E7=94=9F=E6=88=90?= =?UTF-8?q?=20`SSubjectInfo`=E3=80=82=20-=20=E2=9C=85=20**=E6=B5=8B?= =?UTF-8?q?=E8=AF=95=E6=8E=A5=E5=8F=A3=E6=9A=B4=E9=9C=B2**=EF=BC=9A?= =?UTF-8?q?=E4=B8=BA=20`ConfigRepository`=20=E6=B7=BB=E5=8A=A0=20`setCache?= =?UTF-8?q?dConfig`=20=E6=8E=A5=E5=8F=A3=E4=BB=A5=E4=BE=BF=E4=BA=8E?= =?UTF-8?q?=E6=B5=8B=E8=AF=95=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CHANGELOG.md | 7 +++++++ pubspec.yaml | 2 +- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a879d2d..b80e7a0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,13 @@ All notable changes to this project will be documented in this file. +## [3.2.1] - 2026-01-12 + +### 🐛 修复 + +#### 模型类名前缀逻辑修复 +- ✅ **强制添加前缀**:修复了当类名已包含前缀时不重复添加前缀的逻辑,确保 `SubjectInfo` 配置前缀 `S` 后生成 `SSubjectInfo`。 +- ✅ **测试接口暴露**:为 `ConfigRepository` 添加 `setCachedConfig` 接口以便于测试。 ## [3.2.0] - 2026-01-12 ### 🎉 新特性 diff --git a/pubspec.yaml b/pubspec.yaml index d7dcdde..60cac1e 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,7 +1,7 @@ name: swagger_generator_flutter description: A powerful Swagger/OpenAPI code generator for Flutter projects with Dio + Retrofit support -version: 3.2.0 +version: 3.2.1 environment: sdk: '>=3.0.0 <4.0.0'