Skip to content
Open
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
Loading