Skip to content

[dart][dio] add dateLibrary options ( core, timemachine ) - #4716

Merged
wing328 merged 5 commits into
OpenAPITools:masterfrom
amondnet:dart-dio-datelibrary
Dec 16, 2019
Merged

[dart][dio] add dateLibrary options ( core, timemachine )#4716
wing328 merged 5 commits into
OpenAPITools:masterfrom
amondnet:dart-dio-datelibrary

Conversation

@amondnet

@amondnetamondnet commented Dec 6, 2019

Copy link
Copy Markdown
Contributor

This pr adds dateLibrary option and simple test to dart-dio generator.
dateLibrary has two options.

If dateLibrary is timemachine,

input

openapi: 3.0.0info:
contact:
email: amond@amond.netdescription: testtitle: testversion: 1.0.0paths:
/test:
get:
responses:
200:
content:
application/json:
schema:
$ref: '#/components/schemas/DateContainer'description: OKcomponents:
schemas:
DateContainer:
type: objectproperties:
date:
format: datetype: string

output

import'package:time_machine/time_machine.dart';
import'package:built_value/built_value.dart';
import'package:built_value/serializer.dart';
part'date_container.g.dart';
abstractclassDateContainerimplementsBuilt<DateContainer, DateContainerBuilder> {
@nullable@BuiltValueField(wireName:'date')
LocalDateget date;
// Boilerplate code needed to wire-up generated codeDateContainer._();
factoryDateContainer([updates(DateContainerBuilder b)]) =_$DateContainer;
staticSerializer<DateContainer> get serializer => _$dateContainerSerializer;
}

@ircecho @swipesight @jaumard@nickmeinhold

--

PR checklist

  • Read the contribution guidelines.
  • If contributing template-only or documentation-only changes which will change sample output, build the project before.
  • Run the shell script(s) under ./bin/ (or Windows batch scripts under.\bin\windows) to update Petstore samples related to your fix. This is important, as CI jobs will verify all generator outputs of your HEAD commit, and these must match the expectations made by your contribution. You only need to run ./bin/{LANG}-petstore.sh, ./bin/openapi3/{LANG}-petstore.sh if updating the code or mustache templates for a language ({LANG}) (e.g. php, ruby, python, etc).
  • File the PR against the correct branch: master, 4.3.x, 5.0.x. Default: master.
  • Copy the technical committee to review the pull request if your PR is targeting a particular programming language.

Signed-off-by: Minsu Lee <amond@amond.net>
Signed-off-by: Minsu Lee <amond@amond.net>
@amondnet
amondnetforce-pushed the dart-dio-datelibrary branch from 57a47fc to 7361062CompareDecember 6, 2019 06:34
@amondnet
amondnetforce-pushed the dart-dio-datelibrary branch from ded9d7d to beab4efCompareDecember 9, 2019 11:17
@wing328

Copy link
Copy Markdown
Member

Please run the following to update the doc as well

"./bin/utils/export_docs_generators.sh"
"./bin/utils/copy-to-website.sh"
"./bin/utils/export_generators_readme.sh

@amondnet

Copy link
Copy Markdown
ContributorAuthor

@wing328 I have updated the documentation.

@nickmeinhold

Copy link
Copy Markdown
Contributor

Thanks @amondnet!

So guys I don’t use this generator or know much about it.

Personally I think we should be working toward a single dart generator with options rather than continuing to diverge. I respect that others have different opinions but that is where I want to be spending my limited time rather than trying to stay on top of the different generators.

I’m wondering if, rather than getting silence from the technical committee, @wing328 what do you think about asking @athornz and @amondnet if they want to be the technical committee for the dart-dio generator (and would you guys want to?).

Thanks, sorry I'm not more help.

@josh-burtonjosh-burton left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me, I think this is a nice addition to the generator

@josh-burton

Copy link
Copy Markdown
Contributor

@nickmeinhold I'm happy to be added to the committee for dart dio.

@amondnet

Copy link
Copy Markdown
ContributorAuthor

@nickcmaynard Yes, I would like to join the committee.

@wing328wing328 added this to the 4.2.3 milestone Dec 16, 2019
@wing328wing328 changed the title feat(dart-dio): add dateLibrary options ( core, timemachine )[dart][dio] add dateLibrary options ( core, timemachine )Dec 16, 2019
@wing328

Copy link
Copy Markdown
Member

Add @athornz, @amondnet to Dart technical committee via #4800 as both have at least 3 merged PRs for Dart generators.

Thanks for joining us to help move the project forward.

@wing328
wing328 merged commit c98644a into OpenAPITools:masterDec 16, 2019
@hungify

hungify commented Apr 5, 2025

Copy link
Copy Markdown

This issue persists in the latest version.
#15182 (comment)

Details

Any updates? I'm having a problem, as @AAkira mentioned. I tried to change the template, but it didn't work, and I also switched to timemachine instead of core, but it caused an error.

Could not generate `fromJson` code for `shipDate`.
To support the type `OffsetDateTime` you can:
* Use `JsonConverter`
https://pub.dev/documentation/json_annotation/latest/json_annotation/JsonConverter-class.html
* Use `JsonKey` fields `fromJson` and `toJson`
https://pub.dev/documentation/json_annotation/latest/json_annotation/JsonKey/fromJson.html
https://pub.dev/documentation/json_annotation/latest/json_annotation/JsonKey/toJson.html
package:apiService/src/model/order.dart:79:25
╷
79 │ final OffsetDateTime? shipDate;

I found a date.mustache file, but I’m unsure how to use it.

/// 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) {
return DateTime.utc(year, month, day);
} else {
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);
}

Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants

@amondnet@wing328@nickmeinhold@josh-burton@hungify