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 @@ -301,6 +301,12 @@ private void configureDateLibrary(String srcFolder) {
importMapping.put("Date", "package:" + pubName + "/" + sourceFolder + "/" + modelPackage() + "/date.dart");
supportingFiles.add(new SupportingFile("serialization/built_value/date.mustache", srcFolder + File.separator + modelPackage(), "date.dart"));
supportingFiles.add(new SupportingFile("serialization/built_value/date_serializer.mustache", srcFolder, "date_serializer.dart"));
} else if (SERIALIZATION_LIBRARY_JSON_SERIALIZABLE.equals(library)) {
typeMapping.put("date", "Date");
typeMapping.put("Date", "Date");
importMapping.put("Date", "package:" + pubName + "/" + sourceFolder + "/" + modelPackage() + "/date.dart");
supportingFiles.add(new SupportingFile("serialization/json_serializable/date.mustache", srcFolder + File.separator + modelPackage(), "date.dart"));
supportingFiles.add(new SupportingFile("serialization/json_serializable/date_serializer.mustache", srcFolder + File.separator + modelPackage(), "date_serializer.dart"));
}
break;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ export 'package:{{pubName}}/{{sourceFolder}}/api.dart';
export 'package:{{pubName}}/{{sourceFolder}}/auth/api_key_auth.dart';
export 'package:{{pubName}}/{{sourceFolder}}/auth/basic_auth.dart';
export 'package:{{pubName}}/{{sourceFolder}}/auth/oauth.dart';
{{#useBuiltValue}}export 'package:{{pubName}}/{{sourceFolder}}/serializers.dart';
{{#useDateLibCore}}export 'package:{{pubName}}/{{sourceFolder}}/{{modelPackage}}/date.dart';{{/useDateLibCore}}{{/useBuiltValue}}
{{#useBuiltValue}}export 'package:{{pubName}}/{{sourceFolder}}/serializers.dart';{{/useBuiltValue}}
{{#useDateLibCore}}export 'package:{{pubName}}/{{sourceFolder}}/{{modelPackage}}/date.dart';{{/useDateLibCore}}

{{#apiInfo}}{{#apis}}export 'package:{{pubName}}/{{sourceFolder}}/{{apiPackage}}/{{classFilename}}.dart';
{{/apis}}{{/apiInfo}}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'package:json_annotation/json_annotation.dart';
{{#useDateLibCore}}import 'package:{{pubName}}/{{sourceFolder}}/{{modelPackage}}/date_serializer.dart';{{/useDateLibCore}}

part '{{classFilename}}.g.dart';

Expand Down Expand Up @@ -50,6 +51,12 @@ class {{{classname}}} {
@JsonKey(ignore: true)
{{/isBinary}}

{{#isDate}}
@DateSerializer()
{{/isDate}}
{{#isArray}}{{#items}}{{#isDate}}
@DateSerializer()
{{/isDate}}{{/items}}{{/isArray}}

{{#required}}
{{#finalProperties}}final {{/finalProperties}}{{{datatypeWithEnum}}}{{#isNullable}}?{{/isNullable}} {{{name}}};
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/// A gregorian calendar date generated by
/// OpenAPI generator to differentiate
/// between [DateTime] and [Date] formats.
import 'package:json_annotation/json_annotation.dart';

part 'date.g.dart';

@JsonSerializable(
checked: true,
createToJson: true,
disallowUnrecognizedKeys: false,
explicitToJson: true,
)
class Date {

const Date({
required this.year,
required this.month,
required this.day,
});

factory Date.fromJson(Map<String, dynamic> json) => _$DateFromJson(json);

final int year;

final int month;

final int day;

Map<String, dynamic> toJson() => _$DateToJson(this);

@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';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import 'package:json_annotation/json_annotation.dart';
import 'package:{{pubName}}/src/model/date.dart';

class DateSerializer implements JsonConverter<Date, String> {
const DateSerializer();

@override
Date fromJson(String json) {
final parsed = DateTime.parse(json);
return Date(year: parsed.year, month: parsed.month, day: parsed.day);
}

@override
String toJson(Date object) => object.toString();
}
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ 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/date_serializer.dart
lib/src/model/deprecated_object.dart
lib/src/model/dog.dart
lib/src/model/dog_all_of.dart
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -521,7 +521,7 @@ final int int64 = 789; // int | None
final double float = 3.4; // double | None
final String string = string_example; // String | None
final MultipartFile binary = BINARY_DATA_HERE; // MultipartFile | None
final DateTime date = 2013-10-20; // DateTime | None
final Date date = 2013-10-20; // Date | None
final DateTime dateTime = 2013-10-20T19:20:30+01:00; // DateTime | None
final String password = password_example; // String | None
final String callback = callback_example; // String | None
Expand All @@ -547,7 +547,7 @@ Name | Type | Description | Notes
**float** | **double**| None | [optional]
**string** | **String**| None | [optional]
**binary** | **MultipartFile**| 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** | [**MultipartFile**](MultipartFile.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** | **List&lt;Object&gt;** | | [optional]
**arrayAndItemsNullableProp** | **List&lt;Object&gt;** | | [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/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 @@ -9,6 +9,7 @@ import 'dart:convert';
import 'package:openapi/src/deserialize.dart';
import 'package:dio/dio.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 @@ -943,7 +944,7 @@ _responseData = deserialize<ModelClient, ModelClient>(_response.data!, 'ModelCli
double? float,
String? string,
MultipartFile? binary,
DateTime? date,
Date? date,
DateTime? dateTime,
String? password,
String? callback,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

// ignore_for_file: unused_element
import 'package:json_annotation/json_annotation.dart';
import 'package:openapi/src/model/date_serializer.dart';

part 'additional_properties_class.g.dart';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
// ignore_for_file: unused_element
import 'package:openapi/src/model/single_ref_type.dart';
import 'package:json_annotation/json_annotation.dart';
import 'package:openapi/src/model/date_serializer.dart';

part 'all_of_with_single_ref.g.dart';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

// ignore_for_file: unused_element
import 'package:json_annotation/json_annotation.dart';
import 'package:openapi/src/model/date_serializer.dart';

part 'animal.g.dart';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

// ignore_for_file: unused_element
import 'package:json_annotation/json_annotation.dart';
import 'package:openapi/src/model/date_serializer.dart';

part 'api_response.g.dart';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

// ignore_for_file: unused_element
import 'package:json_annotation/json_annotation.dart';
import 'package:openapi/src/model/date_serializer.dart';

part 'array_of_array_of_number_only.g.dart';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

// ignore_for_file: unused_element
import 'package:json_annotation/json_annotation.dart';
import 'package:openapi/src/model/date_serializer.dart';

part 'array_of_number_only.g.dart';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
// ignore_for_file: unused_element
import 'package:openapi/src/model/read_only_first.dart';
import 'package:json_annotation/json_annotation.dart';
import 'package:openapi/src/model/date_serializer.dart';

part 'array_test.g.dart';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

// ignore_for_file: unused_element
import 'package:json_annotation/json_annotation.dart';
import 'package:openapi/src/model/date_serializer.dart';

part 'capitalization.g.dart';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
// ignore_for_file: unused_element
import 'package:openapi/src/model/animal.dart';
import 'package:json_annotation/json_annotation.dart';
import 'package:openapi/src/model/date_serializer.dart';

part 'cat.g.dart';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

// ignore_for_file: unused_element
import 'package:json_annotation/json_annotation.dart';
import 'package:openapi/src/model/date_serializer.dart';

part 'cat_all_of.g.dart';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

// ignore_for_file: unused_element
import 'package:json_annotation/json_annotation.dart';
import 'package:openapi/src/model/date_serializer.dart';

part 'category.g.dart';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

// ignore_for_file: unused_element
import 'package:json_annotation/json_annotation.dart';
import 'package:openapi/src/model/date_serializer.dart';

part 'class_model.g.dart';

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/// A gregorian calendar date generated by
/// OpenAPI generator to differentiate
/// between [DateTime] and [Date] formats.
import 'package:json_annotation/json_annotation.dart';

part 'date.g.dart';

@JsonSerializable(
checked: true,
createToJson: true,
disallowUnrecognizedKeys: false,
explicitToJson: true,
)
class Date {

const Date({
required this.year,
required this.month,
required this.day,
});

factory Date.fromJson(Map<String, dynamic> json) => _$DateFromJson(json);

final int year;

final int month;

final int day;

Map<String, dynamic> toJson() => _$DateToJson(this);

@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';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import 'package:json_annotation/json_annotation.dart';
import 'package:openapi/src/model/date.dart';

class DateSerializer implements JsonConverter<Date, String> {
const DateSerializer();

@override
Date fromJson(String json) {
final parsed = DateTime.parse(json);
return Date(year: parsed.year, month: parsed.month, day: parsed.day);
}

@override
String toJson(Date object) => object.toString();
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

// ignore_for_file: unused_element
import 'package:json_annotation/json_annotation.dart';
import 'package:openapi/src/model/date_serializer.dart';

part 'deprecated_object.g.dart';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
// ignore_for_file: unused_element
import 'package:openapi/src/model/animal.dart';
import 'package:json_annotation/json_annotation.dart';
import 'package:openapi/src/model/date_serializer.dart';

part 'dog.g.dart';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

// ignore_for_file: unused_element
import 'package:json_annotation/json_annotation.dart';
import 'package:openapi/src/model/date_serializer.dart';

part 'dog_all_of.g.dart';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

// ignore_for_file: unused_element
import 'package:json_annotation/json_annotation.dart';
import 'package:openapi/src/model/date_serializer.dart';

part 'enum_arrays.g.dart';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import 'package:openapi/src/model/outer_enum_default_value.dart';
import 'package:openapi/src/model/outer_enum_integer.dart';
import 'package:openapi/src/model/outer_enum_integer_default_value.dart';
import 'package:json_annotation/json_annotation.dart';
import 'package:openapi/src/model/date_serializer.dart';

part 'enum_test.g.dart';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
// ignore_for_file: unused_element
import 'package:openapi/src/model/model_file.dart';
import 'package:json_annotation/json_annotation.dart';
import 'package:openapi/src/model/date_serializer.dart';

part 'file_schema_test_class.g.dart';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

// ignore_for_file: unused_element
import 'package:json_annotation/json_annotation.dart';
import 'package:openapi/src/model/date_serializer.dart';

part 'foo.g.dart';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
// ignore_for_file: unused_element
import 'package:openapi/src/model/foo.dart';
import 'package:json_annotation/json_annotation.dart';
import 'package:openapi/src/model/date_serializer.dart';

part 'foo_get_default_response.g.dart';

Expand Down
Loading