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 @@ -205,13 +205,19 @@ private void configureDateLibrary(String srcFolder) {
imports.put("OffsetDate", "package:time_machine/time_machine.dart");
imports.put("OffsetDateTime", "package:time_machine/time_machine.dart");
if (SERIALIZATION_LIBRARY_BUILT_VALUE.equals(library)) {
supportingFiles.add(new SupportingFile("serialization/built_value/local_date_serializer.mustache", srcFolder, "local_date_serializer.dart"));
supportingFiles.add(new SupportingFile("serialization/built_value/offset_date_serializer.mustache", srcFolder, "local_date_serializer.dart"));
}
break;
default:
case DATE_LIBRARY_CORE:
// this option uses the dart core classes
additionalProperties.put("useDateLibCore", "true");
if (SERIALIZATION_LIBRARY_BUILT_VALUE.equals(library)) {
typeMapping.put("date", "Date");
typeMapping.put("Date", "Date");
importMapping.put("Date", "package:" + pubName + "/src/model/date.dart");
supportingFiles.add(new SupportingFile("serialization/built_value/date.mustache", srcFolder + File.separator + "model", "date.dart"));
supportingFiles.add(new SupportingFile("serialization/built_value/date_serializer.mustache", srcFolder, "date_serializer.dart"));
}
break;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ export 'package:{{pubName}}/src/api.dart';
export 'package:{{pubName}}/src/auth/api_key_auth.dart';
export 'package:{{pubName}}/src/auth/basic_auth.dart';
export 'package:{{pubName}}/src/auth/oauth.dart';
{{#useBuiltValue}}export 'package:{{pubName}}/src/serializers.dart';{{/useBuiltValue}}
{{#useBuiltValue}}export 'package:{{pubName}}/src/serializers.dart';
{{#useDateLibCore}}export 'package:{{pubName}}/src/model/date.dart';{{/useDateLibCore}}{{/useBuiltValue}}

{{#apiInfo}}{{#apis}}export 'package:{{pubName}}/src/api/{{classFilename}}.dart';
{{/apis}}{{/apiInfo}}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/// A gregorian calendar date generated by
/// OpenAPI generator to differentiate
/// between [DateTime] and [Date] formats.
class Date implements Comparable<Date> {
final int year;

/// January is 1.
final int month;

/// First day is 1.
final int day;

Date(this.year, this.month, this.day);

/// The current date
static Date now({bool utc = false}) {
var now = DateTime.now();
if (utc) {
now = now.toUtc();
}
return now.toDate();
}

/// Convert to a [DateTime].
DateTime toDateTime({bool utc = false}) {
if (utc) {
DateTime.utc(year, month, day);
}
return DateTime(year, month, day);
}

@override
int compareTo(Date other) {
int d = year.compareTo(other.year);
if (d != 0) {
return d;
}
d = month.compareTo(other.month);
if (d != 0) {
return d;
}
return day.compareTo(other.day);
}

@override
bool operator ==(Object other) =>
identical(this, other) ||
other is Date &&
runtimeType == other.runtimeType &&
year == other.year &&
month == other.month &&
day == other.day;

@override
int get hashCode => year.hashCode ^ month.hashCode ^ day.hashCode;

@override
String toString() {
final yyyy = year.toString();
final mm = month.toString().padLeft(2, '0');
final dd = day.toString().padLeft(2, '0');

return '$yyyy-$mm-$dd';
}
}

extension DateTimeToDate on DateTime {
Date toDate() => Date(year, month, day);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{{>header}}
import 'package:built_collection/built_collection.dart';
import 'package:built_value/serializer.dart';
import 'package:{{pubName}}/src/model/date.dart';

class DateSerializer implements PrimitiveSerializer<Date> {

const DateSerializer();

@override
Iterable<Type> get types => BuiltList.of([Date]);

@override
String get wireName => 'Date';

@override
Date deserialize(Serializers serializers, Object serialized,
{FullType specifiedType = FullType.unspecified}) {
final parsed = DateTime.parse(serialized as String);
return Date(parsed.year, parsed.month, parsed.day);
}

@override
Object serialize(Serializers serializers, Date date,
{FullType specifiedType = FullType.unspecified}) {
return date.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class OffsetDateSerializer implements PrimitiveSerializer<OffsetDate> {
const OffsetDateSerializer();

@override
Iterable<Type> get types => BuiltList<Type>([OffsetDate]);
Iterable<Type> get types => BuiltList.of([OffsetDate]);

@override
String get wireName => 'OffsetDate';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
{{>header}}
// ignore_for_file: unused_import

import 'package:built_collection/built_collection.dart';
import 'package:built_value/json_object.dart';
import 'package:built_value/serializer.dart';
import 'package:built_value/standard_json_plugin.dart';
{{#useDateLibCore}}import 'package:built_value/iso_8601_date_time_serializer.dart';{{/useDateLibCore}}
{{#useDateLibCore}}import 'package:built_value/iso_8601_date_time_serializer.dart';
import 'package:{{pubName}}/src/date_serializer.dart';
import 'package:{{pubName}}/src/model/date.dart';{{/useDateLibCore}}
{{#useDateLibTimeMachine}}import 'package:time_machine/time_machine.dart';
import 'package:{{pubName}}/src/local_date_serializer.dart';{{/useDateLibTimeMachine}}
import 'package:{{pubName}}/src/offset_date_serializer.dart';{{/useDateLibTimeMachine}}
{{#models}}{{#model}}import 'package:{{pubName}}/src/model/{{classFilename}}.dart';
{{/model}}{{/models}}
part 'serializers.g.dart';
Expand All @@ -25,8 +29,9 @@ Serializers serializers = (_$serializers.toBuilder(){{#apiInfo}}{{#apis}}{{#seri
{{/isMap}}
){{/serializers}}{{/apis}}{{/apiInfo}}{{#useDateLibTimeMachine}}
..add(const OffsetDateSerializer())
..add(const OffsetDateTimeSerializer()){{/useDateLibTimeMachine}}
..add(Iso8601DateTimeSerializer()))
..add(const OffsetDateTimeSerializer()){{/useDateLibTimeMachine}}{{#useDateLibCore}}
..add(const DateSerializer())
..add(Iso8601DateTimeSerializer())){{/useDateLibCore}}
.build();

Serializers standardSerializers =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ lib/src/auth/api_key_auth.dart
lib/src/auth/auth.dart
lib/src/auth/basic_auth.dart
lib/src/auth/oauth.dart
lib/src/date_serializer.dart
lib/src/model/additional_properties_class.dart
lib/src/model/animal.dart
lib/src/model/api_response.dart
Expand All @@ -77,6 +78,7 @@ lib/src/model/cat.dart
lib/src/model/cat_all_of.dart
lib/src/model/category.dart
lib/src/model/class_model.dart
lib/src/model/date.dart
lib/src/model/dog.dart
lib/src/model/dog_all_of.dart
lib/src/model/enum_arrays.dart
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -478,7 +478,7 @@ var int64 = 789; // int | None
var float = 3.4; // double | None
var string = string_example; // String | None
var binary = BINARY_DATA_HERE; // Uint8List | None
var date = 2013-10-20; // DateTime | None
var date = 2013-10-20; // Date | None
var dateTime = 2013-10-20T19:20:30+01:00; // DateTime | None
var password = password_example; // String | None
var callback = callback_example; // String | None
Expand All @@ -504,7 +504,7 @@ Name | Type | Description | Notes
**float** | **double**| None | [optional]
**string** | **String**| None | [optional]
**binary** | **Uint8List**| None | [optional]
**date** | **DateTime**| None | [optional]
**date** | **Date**| None | [optional]
**dateTime** | **DateTime**| None | [optional]
**password** | **String**| None | [optional]
**callback** | **String**| None | [optional]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Name | Type | Description | Notes
**string** | **String** | | [optional]
**byte** | **String** | |
**binary** | [**Uint8List**](Uint8List.md) | | [optional]
**date** | [**DateTime**](DateTime.md) | |
**date** | [**Date**](Date.md) | |
**dateTime** | [**DateTime**](DateTime.md) | | [optional]
**uuid** | **String** | | [optional]
**password** | **String** | |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Name | Type | Description | Notes
**numberProp** | **num** | | [optional]
**booleanProp** | **bool** | | [optional]
**stringProp** | **String** | | [optional]
**dateProp** | [**DateTime**](DateTime.md) | | [optional]
**dateProp** | [**Date**](Date.md) | | [optional]
**datetimeProp** | [**DateTime**](DateTime.md) | | [optional]
**arrayNullableProp** | [**BuiltList<JsonObject>**](JsonObject.md) | | [optional]
**arrayAndItemsNullableProp** | [**BuiltList<JsonObject>**](JsonObject.md) | | [optional]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export 'package:openapi/src/auth/api_key_auth.dart';
export 'package:openapi/src/auth/basic_auth.dart';
export 'package:openapi/src/auth/oauth.dart';
export 'package:openapi/src/serializers.dart';
export 'package:openapi/src/model/date.dart';

export 'package:openapi/src/api/another_fake_api.dart';
export 'package:openapi/src/api/default_api.dart';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import 'package:dio/dio.dart';
import 'dart:typed_data';
import 'package:built_collection/built_collection.dart';
import 'package:openapi/src/api_util.dart';
import 'package:openapi/src/model/date.dart';
import 'package:openapi/src/model/file_schema_test_class.dart';
import 'package:openapi/src/model/health_check_result.dart';
import 'package:openapi/src/model/model_client.dart';
Expand Down Expand Up @@ -826,7 +827,7 @@ class FakeApi {
double? float,
String? string,
Uint8List? binary,
DateTime? date,
Date? date,
DateTime? dateTime,
String? password,
String? callback,
Expand Down Expand Up @@ -875,7 +876,7 @@ class FakeApi {
r'pattern_without_delimiter': encodeFormParameter(_serializers, patternWithoutDelimiter, const FullType(String)),
r'byte': encodeFormParameter(_serializers, byte, const FullType(String)),
if (binary != null) r'binary': MultipartFile.fromBytes(binary, filename: r'binary'),
if (date != null) r'date': encodeFormParameter(_serializers, date, const FullType(DateTime)),
if (date != null) r'date': encodeFormParameter(_serializers, date, const FullType(Date)),
if (dateTime != null) r'dateTime': encodeFormParameter(_serializers, dateTime, const FullType(DateTime)),
if (password != null) r'password': encodeFormParameter(_serializers, password, const FullType(String)),
if (callback != null) r'callback': encodeFormParameter(_serializers, callback, const FullType(String)),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
//
// AUTO-GENERATED FILE, DO NOT MODIFY!
//

import 'package:built_collection/built_collection.dart';
import 'package:built_value/serializer.dart';
import 'package:openapi/src/model/date.dart';

class DateSerializer implements PrimitiveSerializer<Date> {

const DateSerializer();

@override
Iterable<Type> get types => BuiltList.of([Date]);

@override
String get wireName => 'Date';

@override
Date deserialize(Serializers serializers, Object serialized,
{FullType specifiedType = FullType.unspecified}) {
final parsed = DateTime.parse(serialized as String);
return Date(parsed.year, parsed.month, parsed.day);
}

@override
Object serialize(Serializers serializers, Date date,
{FullType specifiedType = FullType.unspecified}) {
return date.toString();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/// A gregorian calendar date generated by
/// OpenAPI generator to differentiate
/// between [DateTime] and [Date] formats.
class Date implements Comparable<Date> {
final int year;

/// January is 1.
final int month;

/// First day is 1.
final int day;

Date(this.year, this.month, this.day);

/// The current date
static Date now({bool utc = false}) {
var now = DateTime.now();
if (utc) {
now = now.toUtc();
}
return now.toDate();
}

/// Convert to a [DateTime].
DateTime toDateTime({bool utc = false}) {
if (utc) {
DateTime.utc(year, month, day);
}
return DateTime(year, month, day);
}

@override
int compareTo(Date other) {
int d = year.compareTo(other.year);
if (d != 0) {
return d;
}
d = month.compareTo(other.month);
if (d != 0) {
return d;
}
return day.compareTo(other.day);
}

@override
bool operator ==(Object other) =>
identical(this, other) ||
other is Date &&
runtimeType == other.runtimeType &&
year == other.year &&
month == other.month &&
day == other.day;

@override
int get hashCode => year.hashCode ^ month.hashCode ^ day.hashCode;

@override
String toString() {
final yyyy = year.toString();
final mm = month.toString().padLeft(2, '0');
final dd = day.toString().padLeft(2, '0');

return '$yyyy-$mm-$dd';
}
}

extension DateTimeToDate on DateTime {
Date toDate() => Date(year, month, day);
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
//

import 'dart:typed_data';
import 'package:openapi/src/model/date.dart';
import 'package:built_value/built_value.dart';
import 'package:built_value/serializer.dart';

Expand Down Expand Up @@ -42,7 +43,7 @@ abstract class FormatTest implements Built<FormatTest, FormatTestBuilder> {
Uint8List? get binary;

@BuiltValueField(wireName: r'date')
DateTime get date;
Date get date;

@BuiltValueField(wireName: r'dateTime')
DateTime? get dateTime;
Expand Down Expand Up @@ -141,7 +142,7 @@ class _$FormatTestSerializer implements StructuredSerializer<FormatTest> {
result
..add(r'date')
..add(serializers.serialize(object.date,
specifiedType: const FullType(DateTime)));
specifiedType: const FullType(Date)));
if (object.dateTime != null) {
result
..add(r'dateTime')
Expand Down Expand Up @@ -226,7 +227,7 @@ class _$FormatTestSerializer implements StructuredSerializer<FormatTest> {
break;
case r'date':
result.date = serializers.deserialize(value,
specifiedType: const FullType(DateTime)) as DateTime;
specifiedType: const FullType(Date)) as Date;
break;
case r'dateTime':
result.dateTime = serializers.deserialize(value,
Expand Down
Loading