Skip to content

Commit 6fc7194

Browse files
authored
Merge pull request #1 from bushaHQ/fix/include-if-null-spec-nullable
fix: `includeIfNull: false` silently dropping null values for spec-nu…
2 parents 753bdeb + 34a48a4 commit 6fc7194

File tree

3 files changed

+39
-12
lines changed

3 files changed

+39
-12
lines changed

lib/src/code_generators/swagger_models_generator.dart

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -466,11 +466,15 @@ abstract class SwaggerModelsGenerator extends SwaggerGeneratorBase {
466466
}
467467
}
468468

469-
String generateIncludeIfNullString() {
469+
String generateIncludeIfNullString(SwaggerSchema prop) {
470470
if (options.includeIfNull == null) {
471471
return '';
472472
}
473473

474+
if (prop.isSpecNullable && options.includeIfNull == false) {
475+
return ', includeIfNull: true';
476+
}
477+
474478
return ', includeIfNull: ${options.includeIfNull}';
475479
}
476480

@@ -543,7 +547,7 @@ class $className implements json.JsonConverter<${value.type}, dynamic> {
543547

544548
final dateToJsonValue = generateToJsonForDate(prop);
545549

546-
final includeIfNullString = generateIncludeIfNullString();
550+
final includeIfNullString = generateIncludeIfNullString(prop);
547551

548552
if (typeName != kDynamic &&
549553
(prop.shouldBeNullable || options.nullableModels.contains(typeName))) {
@@ -731,7 +735,7 @@ static $returnType $fromJsonFunction($valueType? value) => $enumNameCamelCase$fr
731735
);
732736
}
733737

734-
final includeIfNullString = generateIncludeIfNullString();
738+
final includeIfNullString = generateIncludeIfNullString(prop);
735739

736740
final allEnumsNamesWithoutPrefix = allEnumNames
737741
.map((e) => e.replaceFirst('enums.', ''))
@@ -909,7 +913,7 @@ static $returnType $fromJsonFunction($valueType? value) => $enumNameCamelCase$fr
909913
);
910914

911915
final dateToJsonValue = generateToJsonForDate(resolvedSchemaForDetails);
912-
final includeIfNullString = generateIncludeIfNullString();
916+
final includeIfNullString = generateIncludeIfNullString(prop);
913917

914918
final jsonKeyContent =
915919
"@JsonKey(name: '$propertyKey'$includeIfNullString$dateToJsonValue${unknownEnumValue.jsonKey})\n";
@@ -952,7 +956,7 @@ static $returnType $fromJsonFunction($valueType? value) => $enumNameCamelCase$fr
952956
typeName = basicTypesMap[typeName]!;
953957
}
954958

955-
final includeIfNullString = generateIncludeIfNullString();
959+
final includeIfNullString = generateIncludeIfNullString(prop);
956960

957961
final unknownEnumValue = generateEnumValue(
958962
allEnumNames: allEnumNames,
@@ -1038,7 +1042,7 @@ static $returnType $fromJsonFunction($valueType? value) => $enumNameCamelCase$fr
10381042
typeName = 'List<$typeName>';
10391043
}
10401044

1041-
final includeIfNullString = generateIncludeIfNullString();
1045+
final includeIfNullString = generateIncludeIfNullString(prop);
10421046

10431047
final jsonKeyContent =
10441048
"@JsonKey(name: '${_validatePropertyKey(propertyKey)}'$includeIfNullString${unknownEnumValue.jsonKey})\n";
@@ -1111,7 +1115,7 @@ static $returnType $fromJsonFunction($valueType? value) => $enumNameCamelCase$fr
11111115
isNullable: isNullable(className, requiredProperties, propertyKey, prop),
11121116
);
11131117

1114-
final includeIfNullString = generateIncludeIfNullString();
1118+
final includeIfNullString = generateIncludeIfNullString(prop);
11151119

11161120
var enumPropertyName = className.capitalize + key.capitalize;
11171121

@@ -1243,7 +1247,7 @@ static $returnType $fromJsonFunction($valueType? value) => $enumNameCamelCase$fr
12431247
isNullable: false,
12441248
);
12451249

1246-
final includeIfNullString = generateIncludeIfNullString();
1250+
final includeIfNullString = generateIncludeIfNullString(prop);
12471251
final validatedPropertyKey = _validatePropertyKey(propertyKey);
12481252

12491253
String jsonKeyContent;
@@ -1301,7 +1305,7 @@ static $returnType $fromJsonFunction($valueType? value) => $enumNameCamelCase$fr
13011305
required List<String> requiredProperties,
13021306
required bool isDeprecated,
13031307
}) {
1304-
final includeIfNullString = generateIncludeIfNullString();
1308+
final includeIfNullString = generateIncludeIfNullString(prop);
13051309
final jsonConverterAnnotation = generatePropertyJsonConverterAnnotation(prop);
13061310

13071311
var jsonKeyContent =

lib/src/swagger_models/responses/swagger_schema.dart

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -112,12 +112,15 @@ class SwaggerSchema {
112112
@JsonKey(name: 'nullable')
113113
bool? isNullable;
114114

115-
bool get shouldBeNullable =>
115+
bool get isSpecNullable =>
116116
isNullable == true ||
117-
readOnly ||
118-
writeOnly ||
119117
(_type is List && (_type as List).contains('null'));
120118

119+
bool get shouldBeNullable =>
120+
isSpecNullable ||
121+
readOnly ||
122+
writeOnly;
123+
121124
@JsonKey(name: 'schema')
122125
SwaggerSchema? schema;
123126

test/generator_tests/models_generator_test.dart

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,26 @@ void main() {
226226
expect(result, contains(', includeIfNull: false'));
227227
});
228228

229+
test('Should generate includeIfNull: true for spec-nullable fields even when option is false', () {
230+
final propertyEntryMap = SwaggerSchema(originalRef: 'Pet', isNullable: true);
231+
const propertyName = 'shipDate';
232+
final result = SwaggerModelsGeneratorV3(GeneratorOptions(
233+
inputFolder: '',
234+
outputFolder: '',
235+
includeIfNull: false,
236+
)).generatePropertyContentByDefault(
237+
prop: propertyEntryMap,
238+
propertyName: propertyName,
239+
propertyKey: propertyName,
240+
allEnumNames: [],
241+
allEnumListNames: [],
242+
requiredProperties: [],
243+
isDeprecated: false,
244+
);
245+
246+
expect(result, contains(', includeIfNull: true'));
247+
});
248+
229249
test('Should NOT generate includeIfNull if option is false', () {
230250
final propertyEntryMap = SwaggerSchema(originalRef: 'Pet');
231251
const propertyName = 'shipDate';

0 commit comments

Comments
 (0)