openapi-generator icon indicating copy to clipboard operation
openapi-generator copied to clipboard

[dart-dio][dart-dio-next] Support Date Type

Open jannikrav opened this issue 5 years ago • 13 comments

Is your feature request related to a problem? Please describe.

The OpenAPI definition differentiates between Date and DateTime. The dart-dio-next generator supports only DateTime without the additional timemachine library, which causes deserialization errors for Date on the server side.

Describe the solution you'd like

The goal is to distinguish between Date and DateTime in serialization. Due to BuiltValue only one serializer can be specified for one class. While adding the serializer, FullType supports optional parameters, which maybe provides a possibility to distinguish between Date and DateTime using {{isDate}}. The Iso8601DateTimeSerializer could be extendend by checking these parameters and use DateFormat('yyyy-MM-dd') to serialize Date.

Describe alternatives you've considered

Alternative approaches are to use the dart generator, which already distinguishes between DateTime and Date, or to use the timemachine library. The first approach unfortunately lacks the wide possibilities of Dio like interceptors and the timemachine library adds more dependencies and often requires a conversion between timemachine classes and DateTime.

Additional context

As mentioned in #8869, support for Date would be a useful extension to the next generation dart dio generator @agilob @kuhnroyal. I would like to thank you for the current version of the new generator, it looks very promising.

jannikrav avatar Mar 26 '21 16:03 jannikrav

Thanks for the issue. The problem I see is, that there is no Date type in Dart but FullType() requires a type. So for both date and date-time this has to be FullType(DateTime) which does not work obviously. That is the good thing about timemachine. It has types for date and date-time.

kuhnroyal avatar Mar 26 '21 16:03 kuhnroyal

I used timemachine, but it leads to more effort if DateTime from Dart is used in the rest of the project. Do you have any information if and when timemachine will support null-safety? Nothing happened in the library issue yet.

I just tested a "quick" implementation in my generated code and it works, but is probably not the nicest way. Add any FullType while adding the serializer. Here it is a dummy class Date without any other functions.

..add(serializers.serialize(object.trainingDate, specifiedType: const FullType(DateTime, [FullType(Date)])));

Then you can check for it in the serialize. This Example is based on the Iso8601DateTimeSerializer from BuiltValue.

class Iso8601DateSerializer implements PrimitiveSerializer<DateTime> {
  final bool structured = false;
  @override
  final Iterable<Type> types = BuiltList<Type>([DateTime]);
  @override
  final String wireName = 'DateTime';

  @override
  Object serialize(Serializers serializers, DateTime dateTime,
      {FullType specifiedType = FullType.unspecified}) {
    if (!dateTime.isUtc) {
      throw ArgumentError.value(
          dateTime, 'dateTime', 'Must be in utc for serialization.');
    }

    if (specifiedType.parameters.contains(FullType(Date))) {
      return DateFormat('yyyy-MM-dd').format(dateTime);
    }

    return dateTime.toIso8601String();
  }

  @override
  DateTime deserialize(Serializers serializers, Object? serialized,
      {FullType specifiedType = FullType.unspecified}) {
    return DateTime.parse(serialized as String).toUtc();
  }
}

What do you think about it?

jannikrav avatar Mar 26 '21 16:03 jannikrav

Don't like it but it could work.

kuhnroyal avatar Mar 26 '21 17:03 kuhnroyal

Actually I am trying to get rid of the custom self-generated serializers atm. This is not possible without them.

kuhnroyal avatar Mar 30 '21 12:03 kuhnroyal

We could create a custom Date class with helpers for to/fromDateTime() and use that instead.

kuhnroyal avatar Mar 30 '21 12:03 kuhnroyal

Don't like it but it could work.

I agree with that. In any case, it is not the most beautiful solution.

A separate class would have advantages and disadvantages. It would mean significantly more effort in the implementation and would be partly a duplication with timemachine and the use of the standard data types is not possible. On the other hand it is a unique class for use in the API and could be mapped by calling the API functions. DateTime parameters would remain untouched.

Your thoughts show nicely that the generator is definitely maintained and can be used in a practical way. Thanks.

jannikrav avatar Mar 30 '21 13:03 jannikrav

A separate date class is definitely better. The proposed workaround introduces problems with different time zones, which makes a conversion of the local time as UTC necessary.

extension ApiDateExtension on DateTime {
  DateTime toDate() {
    return DateTime.utc(this.year, this.month, this.day);
  }
}

The class could look like the following, with basic functionality.

class Date {
  final int year;
  final int month;
  final int day;
  
  // Constructs a Date instance.
  Date(
    this.year,
    this.month,
    this.day,
  );
  
  // Constructs a Date instance based on a DateTime instance.
  Date.fromDateTime(DateTime dateTime) : this(
    dateTime.year,
    dateTime.month,
    dateTime.day,
  );
  
  // Constructs a DateTime instance specified in the local time zone.
  DateTime toDateTime() => DateTime(
    year,
    month,
    day,
  );
  
  // Constructs a DateTime instance specified in the UTC time zone.
  DateTime toDateTimeUtc() => DateTime.utc(
    year,
    month,
    day,
  );
  
  @override
  String toString() {
    final yyyy = year.toString().padLeft(4, '0');
    final mm = month.toString().padLeft(2, '0');
    final dd = day.toString().padLeft(2, '0');
    
    return '$yyyy-$mm-$dd';
  }
}

jannikrav avatar Apr 03 '21 12:04 jannikrav

Did some initial work on this, would be great if you guys can try and give some feedback.

~~General problem is that we can not support a custom format with either date or date-time, no matter if using timemachine or not.~~

kuhnroyal avatar Apr 05 '21 18:04 kuhnroyal

General problem is that we can not support a custom format with either date or date-time, no matter if using timemachine or not.

Please disregard this. I was under the expression that date and date-time fields in OpenAPI allow for an additional format. This is not the case.

Need to update the branch but from my tests this seems to work.

kuhnroyal avatar Apr 06 '21 17:04 kuhnroyal

Is the branch from PR already up to date? I downloaded this this afternoon and would test it in my application tonight/tomorrow morning. The customized code already looks promising.

jannikrav avatar Apr 06 '21 18:04 jannikrav

Merged master just now.

kuhnroyal avatar Apr 06 '21 18:04 kuhnroyal

Is it possibile to add the support to Date type also for json_serializable serializer?

francescoberardi avatar Aug 16 '22 16:08 francescoberardi

Follow-up for json_serializable https://github.com/OpenAPITools/openapi-generator/issues/15182

kuhnroyal avatar Jul 17 '23 11:07 kuhnroyal