[BUG] [Dart] Array of array items don't handle required correctly
Created by: 0xNF
Bug Report Checklist
-
Have you provided a full/minimal spec to reproduce the issue? -
Have you validated the input using an OpenAPI validator (example)? -
Have you tested with the latest master to confirm the issue still exists? -
Have you searched for related issues/PRs? -
What's the actual output vs expected output? -
[Optional] Sponsorship to speed up the bug fix or feature request (example)
Description
Double arrays will generate with invalid defaults in the event of null values encountered during deserialization
This will generate the following invalid deserializer:
class WithNonNullableArray {
/// Returns a new [WithNonNullableArray] instance.
WithNonNullableArray({
this.subarray = const [],
});
/* omitted */
List<List<int>> subarray;
static WithNonNullableArray? fromJson(dynamic value) {
if (value is Map) {
final json = value.cast<String, dynamic>();
/* omitted */
return WithNonNullableArray(
subarray: json[r'subarray'] is List
? (json[r'subarray'] as List).map(
(e) => e == null ? null : (e as List).cast<int>()
).toList()
: null,
);
}
return null;
}
}
Null isn't valid here, and so becomes a syntax error. The proper typing of the defaults for this example should be <int>[]
openapi-generator version
Latest commit as of 9/18/22 (https://github.com/OpenAPITools/openapi-generator/commit/863dbc7c3ee39af1ac931d2949388f7f84896ec4)
OpenAPI declaration file content or url
Specfile
openapi: 3.0.3
info:
version: "1.1"
title: Dart Uint8list Demo
servers:
- url: "localhost"
variables:
host:
default: localhost
paths:
/item:
get:
operationId: GetItem
description: "Should return an Item"
responses:
"200":
description: items
content:
application/json:
schema:
$ref: "#components/schemas/WithNonNullableArray"
components:
schemas:
WithNonNullableArray:
type: object
required: [subarray]
properties:
subarray:
type: array
minItems: 12
maxItems: 12
items:
$ref: "#/components/schemas/SubArray"
WithNullableArray:
type: object
required: [subarray]
properties:
months:
type: array
nullable: true
items:
$ref: "#/components/schemas/SubArray"
SubArray:
type: array
items:
type: integer
format: int32
Suggest a fix
Changing the native_class.mustache
file to include
(e) => e == null ? {{#required}}{{#isNullable}}null{{/isNullable}}{{^isNullable}}const <{{items.items.dataType}}>[]{{/isNullable}}{{/required}}{{^required}}null{{/required}}
Changes the output to be more appropriate:
return WithNonNullableArray(
subarray: json[r'subarray'] is List
? (json[r'subarray'] as List).map(
(e) => e == null ? const <int>[] : (e as List).cast<int>()
).toList()
: const <int>[],
);