Description
Description of the bug
When using a reusable definition for an array of dates, the Dart code generator interprets the schema as a custom type and incorrectly generates the following code:
disabled: DateTime.listFromJson(json[r'disabled']),
This is generated even when the schema references an array definition meant to represent an array of date strings. The expected generated code should ideally convert a list of date-time formatted strings into DateTime objects similar to:
disabled: (json[r'disabled'] as List<dynamic>)
.map((e) => DateTime.parse(e as String))
.toList(),
Steps to Reproduce:
- Define the JSON schema for a model with an array property as follows:
- Create a reusable definition for
ArrayOfDates
with:
- Create a reusable definition for
const arrayOfDates = {
title: 'ArrayOfDates',
type: 'array',
items: { type: 'Date' } // or simply { type: 'string', format: 'date-time' }
};
- Reference the reusable definition:
model.properties.disabled = {
$ref: '#/definitions/ArrayOfDates',
default: []
};
- Ensure the schema is used by the generator (via Swagger or similar tooling that feeds into the Dart generator).
- Run the code generation process.
Observed Behavior:
The generator outputs conversion code that calls a non-existent static method DateTime.listFromJson
and falls back to a generic Iterable cast:
disabled: json[r'disabled'] is Iterable
? (json[r'disabled'] as Iterable).cast<String>().toList(growable: false)
: const [],
This behavior is problematic because it does not parse the strings into DateTime objects as expected.
Expected Behavior:
The generator should interpret the schema correctly, especially when the items are defined as date-time formatted strings, and generate conversion code that transforms each item via DateTime.parse(e as String)
.
Workarounds Tried:
- Changing the item type definition explicitly to a string with a date-time format:
const arrayOfDates = {
type: 'array',
items: { type: 'string', format: 'date-time' }
};
- Adding vendor extensions like
x-dart-type
withList<DateTime>
as its value.
Neither approach resulted in the expected code generation.
Environment:
- Generator Tool Version: (specify your version, if applicable)
- Dart version: (specify)
- Project: FeathersJS/Swagger setup
- Relevant Schema and Service Code: (as described above)
Additional Context:
This issue affects our ability to rely on the generated code to correctly convert date strings into DateTime objects. If the generator's behavior could be modified—either through improved schema inference, vendor extensions, or customization of templates—it would significantly ease integration for users expecting proper DateTime parsing.
Any guidance or fixes for this scenario would be greatly appreciated!
Steps to reproduce
Steps to Reproduce:
- Define the JSON schema for a model with an array property as follows:
- Create a reusable definition for
ArrayOfDates
with:
- Create a reusable definition for
const arrayOfDates = {
title: 'ArrayOfDates',
type: 'array',
items: { type: 'Date' } // or simply { type: 'string', format: 'date-time' }
};
- Reference the reusable definition:
model.properties.disabled = {
$ref: '#/definitions/ArrayOfDates',
default: []
};
- Ensure the schema is used by the generator (via Swagger or similar tooling that feeds into the Dart generator).
- Run the code generation process.
Minimal openapi specification
{
"info": {
"title": "Test",
"description": "Test",
"version": "0.9.0"
},
"paths": {
},
"schemes": [
"https",
"http"
],
"definitions": {
"ArrayOfDates": {
"title": "ArrayOfDates",
"type": "array",
"items": {
"type": "string",
"format": "date-time"
}
},
"vistypes": {
"title": "vistypes",
"type": "object",
"properties": {
"name": {
"type": "string"
},
"vistype": {
"type": "number"
},
"description": {
"type": "string"
},
"modalDescription": {
"type": "string"
},
"hideOnModal": {
"type": "boolean"
},
"timing": {
"type": "string"
},
"titleDropdownHistory": {
"type": "string"
},
"titleBoxVisurePage": {
"type": "string"
},
"titleDropdownVisurePage": {
"type": "string"
},
"urlExample": {
"type": "string"
},
"isBeta": {
"type": "boolean"
},
"disabled": {
"$ref": "#/definitions/ArrayOfDates",
"default": []
},
"altVistype": {
"type": "string"
},
"packetType": {
"type": "string",
"enum": [
"indagini",
"monitoraggio_immobili",
"monitoraggio_soggetti",
"scopri_proprietari",
"crediti_plus",
"zone",
"visure_catastali",
"visure_ipotecarie",
"visure_camerali",
"visure_whuis",
"planimetria_catastale",
"ape",
"experian"
]
},
"useCreditsPlusAbb": {
"type": "boolean"
},
"useCreditsAbb": {
"type": "boolean"
},
"useCreditsPacket": {
"type": "boolean"
},
"calculatePriceCreditsPlus": {
"type": "boolean"
},
"creditsPlusPrice": {
"type": "number"
},
"creditsPrice": {
"type": "number",
"default": 1
},
"stampaNegativaCreditsPlusPrice": {
"type": "number"
},
"b2cEnabled": {
"type": "boolean"
},
"b2cPrice": {
"type": "number"
},
"category": {
"type": "string"
},
"platform": {
"type": "array",
"items": {
"type": "string"
},
"enum": [
"web",
"app"
],
"default": []
},
"type": {
"type": "string",
"enum": [
"catastale",
"ipotecaria",
"camerale",
"documents"
]
},
"soggetto": {
"type": "array",
"items": {
"type": "string"
},
"default": []
},
"priceCreditiPlus": {
"type": "number"
},
"externalPrice": {
"type": "number"
},
"priceEuro": {
"type": "number"
},
"hideVisurePage": {
"type": "boolean"
},
"hideOnCronologiaInAttesa": {
"type": "boolean"
},
"from": {
"type": "string"
},
"titleHeaderSlider": {
"type": "string"
},
"_id": {
"type": "string",
"pattern": "^[0-9a-fA-F]{24}$"
},
"createdAt": {
"type": "string",
"format": "date-time"
},
"updatedAt": {
"type": "string",
"format": "date-time"
},
"__v": {
"type": "number"
}
},
"required": [
"name",
"vistype",
"isBeta",
"altVistype",
"useCreditsPlusAbb",
"useCreditsAbb",
"useCreditsPacket",
"calculatePriceCreditsPlus",
"creditsPrice",
"b2cEnabled",
"category",
"type"
]
}
},
"swagger": "2.0",
"tags": [],
"basePath": "/",
"consumes": [
"application/json"
],
"produces": [
"application/json"
]
}
Annotation used
@Openapi(
inputSpec: InputSpec(path: 'lib/engine/swagger/openapi.json'),
generatorName: Generator.dart,
outputDirectory: "lib/engine/swagger/generated",
)
Expected behavior
The generator should interpret the schema correctly, especially when the items are defined as date-time formatted strings, and generate conversion code that transforms each item via DateTime.parse(e as String)
.
The expected generated code should ideally convert a list of date-time formatted strings into DateTime objects similar to:
disabled: (json[r'disabled'] as List<dynamic>)
.map((e) => DateTime.parse(e as String))
.toList(),
Logs
Screenshots
No response
Platform
macOS
Library version
5.0.2
Flutter version
3.19.6
Flutter channel
stable
Additional context