feat: 强制所有String字段为非空且required

- String类型字段(非date-time/date)强制为非空,忽略Swagger的nullable标记
- 所有String字段自动添加 @JsonKey(defaultValue: '')
- 所有非空String字段在构造函数中需要required修饰符
- 保证类型安全的同时,在JSON反序列化时提供默认值兜底

优势:
- 类型安全:避免String?的空值处理
- 明确意图:required强制手动构造时提供值
- 容错性好:JSON反序列化时有defaultValue兜底
- 行为一致:所有String字段处理统一
This commit is contained in:
Max 2025-10-11 16:58:37 +08:00
parent 90d16a7d16
commit 9bd7267b93
1 changed files with 63 additions and 8 deletions

View File

@ -85,8 +85,15 @@ class ModelCodeGenerator extends ModelGenerator {
// //
model.properties.forEach((propName, property) { model.properties.forEach((propName, property) {
final dartType = getDartPropertyType(property); final dartType = getDartPropertyType(property);
// nullable: true //
final nullable = property.nullable ? '?' : ''; // 1. String date-time/date Swagger nullable
// 2. defaultValue json_annotation
// 3. nullable
final isNormalString = property.type == PropertyType.string &&
property.format != 'date-time' &&
property.format != 'date';
final hasDefaultValue = property.defaultValue != null || isNormalString;
final nullable = hasDefaultValue ? '' : (property.nullable ? '?' : '');
final dartPropName = StringUtils.toDartPropertyName(propName); final dartPropName = StringUtils.toDartPropertyName(propName);
if (property.description.isNotEmpty) { if (property.description.isNotEmpty) {
@ -113,8 +120,14 @@ class ModelCodeGenerator extends ModelGenerator {
buffer.writeln(' const $className({'); buffer.writeln(' const $className({');
model.properties.forEach((propName, property) { model.properties.forEach((propName, property) {
final dartPropName = StringUtils.toDartPropertyName(propName); final dartPropName = StringUtils.toDartPropertyName(propName);
// required // required
final shouldBeRequired = !property.nullable; // 1. String date-time/date required Swagger nullable
// 2. required
// 3. required
final isNormalString = property.type == PropertyType.string &&
property.format != 'date-time' &&
property.format != 'date';
final shouldBeRequired = isNormalString || !property.nullable;
final required = shouldBeRequired ? 'required ' : ''; final required = shouldBeRequired ? 'required ' : '';
buffer.writeln(' ${required}this.$dartPropName,'); buffer.writeln(' ${required}this.$dartPropName,');
}); });
@ -303,8 +316,15 @@ class ModelCodeGenerator extends ModelGenerator {
// //
model.properties.forEach((propName, property) { model.properties.forEach((propName, property) {
final dartType = getDartPropertyType(property); final dartType = getDartPropertyType(property);
// nullable: true //
final nullable = property.nullable ? '?' : ''; // 1. String date-time/date Swagger nullable
// 2. defaultValue json_annotation
// 3. nullable
final isNormalString = property.type == PropertyType.string &&
property.format != 'date-time' &&
property.format != 'date';
final hasDefaultValue = property.defaultValue != null || isNormalString;
final nullable = hasDefaultValue ? '' : (property.nullable ? '?' : '');
final dartPropName = StringUtils.toDartPropertyName(propName); final dartPropName = StringUtils.toDartPropertyName(propName);
if (property.description.isNotEmpty) { if (property.description.isNotEmpty) {
@ -331,8 +351,14 @@ class ModelCodeGenerator extends ModelGenerator {
buffer.writeln(' const $className({'); buffer.writeln(' const $className({');
model.properties.forEach((propName, property) { model.properties.forEach((propName, property) {
final dartPropName = StringUtils.toDartPropertyName(propName); final dartPropName = StringUtils.toDartPropertyName(propName);
// required // required
final shouldBeRequired = !property.nullable; // 1. String date-time/date required Swagger nullable
// 2. required
// 3. required
final isNormalString = property.type == PropertyType.string &&
property.format != 'date-time' &&
property.format != 'date';
final shouldBeRequired = isNormalString || !property.nullable;
final required = shouldBeRequired ? 'required ' : ''; final required = shouldBeRequired ? 'required ' : '';
buffer.writeln(' ${required}this.$dartPropName,'); buffer.writeln(' ${required}this.$dartPropName,');
}); });
@ -388,6 +414,21 @@ class ModelCodeGenerator extends ModelGenerator {
annotations.add('name: \'$propName\''); annotations.add('name: \'$propName\'');
} }
// String类型默认值处理
if (property.type == PropertyType.string &&
property.format != 'date-time' &&
property.format != 'date') {
// String类型添加默认值为空字符串
if (property.defaultValue != null) {
// OpenAPI文档中有明确的默认值使
final defaultVal = property.defaultValue.toString();
annotations.add('defaultValue: \'$defaultVal\'');
} else {
// 使
annotations.add('defaultValue: \'\'');
}
}
// DateTime类型需要特殊处理 // DateTime类型需要特殊处理
if (property.type == PropertyType.string && if (property.type == PropertyType.string &&
(property.format == 'date-time' || property.format == 'date')) { (property.format == 'date-time' || property.format == 'date')) {
@ -395,6 +436,20 @@ class ModelCodeGenerator extends ModelGenerator {
// annotations.add('fromJson: DateTime.parse, toJson: _dateTimeToString'); // annotations.add('fromJson: DateTime.parse, toJson: _dateTimeToString');
} }
//
if (property.type != PropertyType.string && property.defaultValue != null) {
final defaultVal = property.defaultValue;
if (property.type == PropertyType.integer ||
property.type == PropertyType.number) {
annotations.add('defaultValue: $defaultVal');
} else if (property.type == PropertyType.boolean) {
annotations.add('defaultValue: $defaultVal');
} else {
//
annotations.add('defaultValue: \'$defaultVal\'');
}
}
// //
if (property.type == PropertyType.reference) { if (property.type == PropertyType.reference) {
// //