Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,9 @@ class {{{classname}}} {
{{^vendorExtensions.x-is-optional}}
{{{name}}}: {{#isNullable}}json[r'{{{baseName}}}'] == null
? {{#defaultValue}}{{{.}}}{{/defaultValue}}{{^defaultValue}}null{{/defaultValue}}
: {{/isNullable}}{{{datatypeWithEnum}}}.parse('${json[r'{{{baseName}}}']}'),
: {{/isNullable}}{{^isNullable}}{{^required}}json[r'{{{baseName}}}'] == null
? {{#defaultValue}}{{{.}}}{{/defaultValue}}{{^defaultValue}}null{{/defaultValue}}
: {{/required}}{{/isNullable}}{{{datatypeWithEnum}}}.parse('${json[r'{{{baseName}}}']}'),
{{/vendorExtensions.x-is-optional}}
{{/isNumber}}
{{^isNumber}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,22 @@ public void testRequiredNullableFieldsDoNotAssertNonNull() throws Exception {
"json[r'nickname'] != null");
}

@Test(description = "Optional numeric fields should guard against null values before parsing")
public void testOptionalNumericFieldsGuardAgainstNull() throws Exception {
List<File> files = generateDartNativeFromSpec(
"src/test/resources/3_0/dart/dart-native-deserialization-bugs.yaml");

File modelFile = files.stream()
.filter(f -> f.getName().equals("optional_number_model.dart"))
.findFirst()
.orElseThrow(() -> new AssertionError("optional_number_model.dart not found in generated files"));

TestUtils.assertFileContains(modelFile.toPath(),
"problem: json[r'problem'] == null");
TestUtils.assertFileContains(modelFile.toPath(),
": num.parse('${json[r'problem']}')");
}

@Test(description = "Nullable nested arrays of complex types should preserve null entries")
public void testNullableNestedComplexArraysPreserveNullEntries() throws Exception {
List<File> files = generateDartNativeFromSpec(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ components:
nickname:
type: string
nullable: true
OptionalNumberModel:
type: object
properties:
problem:
type: number
ComplexNestedArrayNullableModel:
type: object
properties:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,9 @@ class FakeBigDecimalMap200Response {
}());

return FakeBigDecimalMap200Response(
someId: num.parse('${json[r'someId']}'),
someId: json[r'someId'] == null
? null
: num.parse('${json[r'someId']}'),
someMap: mapCastOfType<String, num>(json, r'someMap') ?? const {},
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,9 @@ class NumberOnly {
}());

return NumberOnly(
justNumber: num.parse('${json[r'JustNumber']}'),
justNumber: json[r'JustNumber'] == null
? null
: num.parse('${json[r'JustNumber']}'),
);
}
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,9 @@ class ObjectWithDeprecatedFields {

return ObjectWithDeprecatedFields(
uuid: mapValueOfType<String>(json, r'uuid'),
id: num.parse('${json[r'id']}'),
id: json[r'id'] == null
? null
: num.parse('${json[r'id']}'),
deprecatedRef: DeprecatedObject.fromJson(json[r'deprecatedRef']),
bars: json[r'bars'] is Iterable
? (json[r'bars'] as Iterable).cast<String>().toList(growable: false)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,9 @@ class OuterComposite {
}());

return OuterComposite(
myNumber: num.parse('${json[r'my_number']}'),
myNumber: json[r'my_number'] == null
? null
: num.parse('${json[r'my_number']}'),
myString: mapValueOfType<String>(json, r'my_string'),
myBoolean: mapValueOfType<bool>(json, r'my_boolean'),
);
Expand Down
Loading