Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Dec 16, 2019

Conversation

amondnet
Copy link
Contributor

@amondnet amondnet commented Dec 6, 2019

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

If dateLibrary is timemachine,

input

openapi: 3.0.0
info:
  contact:
    email: [email protected]
  description: test
  title: test
  version: 1.0.0
paths:
  /test:
    get:
      responses:
        200:
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/DateContainer'
          description: OK

components:
    schemas:
      DateContainer:
        type: object
        properties:
          date:
            format: date
            type: 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';

abstract class DateContainer
    implements Built<DateContainer, DateContainerBuilder> {
  @nullable
  @BuiltValueField(wireName: 'date')
  LocalDate get date;

  // Boilerplate code needed to wire-up generated code
  DateContainer._();

  factory DateContainer([updates(DateContainerBuilder b)]) = _$DateContainer;
  static Serializer<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.

@amondnet amondnet force-pushed the dart-dio-datelibrary branch from 57a47fc to 7361062 Compare December 6, 2019 06:34
@amondnet amondnet force-pushed the dart-dio-datelibrary branch from ded9d7d to beab4ef Compare December 9, 2019 11:17
@wing328
Copy link
Member

wing328 commented Dec 12, 2019

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
Contributor Author

@wing328 I have updated the documentation.

@nickmeinhold
Copy link
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.

Copy link
Contributor

@josh-burton josh-burton left a comment

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
Contributor

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

@amondnet
Copy link
Contributor Author

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

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

wing328 commented Dec 16, 2019

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:master Dec 16, 2019
@hungify
Copy link

hungify commented Apr 5, 2025

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 free to 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