fix: SS Start bugs

This commit is contained in:
Max 2026-01-12 15:51:06 +08:00
parent e7ab35b853
commit 4df56bb5f0
2 changed files with 43 additions and 4 deletions

View File

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

View File

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