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:
parent
90d16a7d16
commit
9bd7267b93
|
|
@ -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) {
|
||||||
// 检查是否是枚举类型(这里需要更复杂的逻辑来判断)
|
// 检查是否是枚举类型(这里需要更复杂的逻辑来判断)
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue