Carapacik / swagger_parser

Dart package that takes an OpenApi definition file and generates REST clients based on retrofit and data classes for your project.
https://pub.dev/packages/swagger_parser
MIT License
94 stars 43 forks source link

[dart] "format date" generate class without properties instead of using native DateTime #16

Closed billyandco closed 1 year ago

billyandco commented 1 year ago

while using the generator on a swagger generated by Java, Date is not recognize as dart DateTime

swagger

    RangeDate:
      type: object
      properties:
        startDate:
          $ref: '#/components/schemas/Date'
        endDate:
          $ref: '#/components/schemas/Date'
    Date:
      format: date
      type: string
      example: 2022-03-10

generated

import 'package:json_annotation/json_annotation.dart';

import 'date.dart';

part 'range_date.g.dart';

@JsonSerializable()
class RangeDate {
  const RangeDate({
    required this.startDate,
    required this.endDate,
  });

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

  final Date startDate;
  final Date endDate;

  Map<String, dynamic> toJson() => _$RangeDateToJson(this);
}
import 'package:json_annotation/json_annotation.dart';

part 'date.g.dart';

@JsonSerializable()
class Date {
  const Date();

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

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

expected

import 'package:json_annotation/json_annotation.dart';

part 'range_date.g.dart';

@JsonSerializable()
class RangeDate {
  const RangeDate({
    required this.startDate,
    required this.endDate,
  });

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

  final DateTime startDate;
  final DateTime endDate;

  Map<String, dynamic> toJson() => _$RangeDateToJson(this);
}
billyandco commented 1 year ago

suggestion https://github.com/Carapacik/swagger_parser/blob/main/swagger_parser/lib/src/utils/type_utils.dart

replacing line from 15 to 20

      case 'string':
        switch (format) {
          case 'binary':
            // Single MultipartFile is not generated due an error
            return 'File';
          case 'date':
          case 'date-time':
            return 'DateTime';
          default:
            return 'String';
        }
Carapacik commented 1 year ago

Look at the changes in version 0.8.1

billyandco commented 1 year ago

Thank you for your reactivity !

Unfortunately, i don't think it is enough It still generate me the empty Date object

Maybe by replacing "$ref": "#/components/schemas/Date" to DateTime type for dart lang

Carapacik commented 1 year ago

Try to do it yourself and open a PR

Carapacik commented 1 year ago

"$ref": "#/components/schemas/Date" to DateTime type for dart lang

It will not be very correct, because other users may have a Date class with other fields.

billyandco commented 1 year ago

Yes, then the reference will be an object, not a string

Perfect, i'm going to try to do it

billyandco commented 1 year ago

Just a reminder of the subject this use case is not implemented

image

source

Carapacik commented 1 year ago

Fixed in 0.9.0