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

[BUG] [Dart] "The method 'listFromJson' isn't defiend" for DateTime

Open jhlgns opened this issue 3 years ago • 4 comments

Bug Report Checklist

  • [x] Have you provided a full/minimal spec to reproduce the issue?
  • [x] Have you validated the input using an OpenAPI validator (example)?
  • [x] Have you tested with the latest master to confirm the issue still exists?
  • [x] Have you searched for related issues/PRs?
  • [x] What's the actual output vs expected output?
  • [ ] [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description

When using the generated Dart code, this error get's displayed:

The method 'listFromJson' isn't defined for the type 'DateTime'.
Try correcting the name to the name of an existing method, or defining a method named 'listFromJson'.
openapi-generator version

openapi-generator-cli 5.4.0-SNAPSHOT commit : 35fea62 built : 2022-01-29T18:29:19Z source : https://github.com/openapitools/openapi-generator docs : https://openapi-generator.tech/

OpenAPI declaration file content or url

https://api.apaleo.com/swagger/inventory-v1/swagger.json

Generation Details

n/a

Steps to reproduce
  • docker run --rm -v "${PWD}:/local" openapitools/openapi-generator-cli:latest generate -i /local/swagger.json -g dart -o /local/api
  • Use the generated dart library
Related issues/PRs

n/a

Suggest a fix

I don't know the internals of OpenAPITools - probably the listFromJson implementation for builtin types needs to be fixed?

jhlgns avatar Jan 30 '22 21:01 jhlgns

Just tested this with a 6.0.0 snapshot, docker container was built directly from master, still persists.

jhlgns avatar Feb 03 '22 11:02 jhlgns

I have a similliar problem with MultipartFile:

../openapi/lib/model/file_list.dart:55:30: Error: Member not found: 'MultipartFile.listFromJson'.
            files: MultipartFile.listFromJson(json[r'files']),

hey-nicolasklein avatar May 23 '22 14:05 hey-nicolasklein

We also experience this issue with 6.0.1. Is this related to: https://github.com/OpenAPITools/openapi-generator/issues/4887 ?

aeneasr avatar Jul 28 '22 10:07 aeneasr

You can't create static extension methods for built-in Dart classes: https://github.com/dart-lang/language/issues/723

Until that is possible, you can create an extension for the DateTime class like this:

extension DateTimeHelper on DateTime {
  
  static List<DateTime>? listFromJson(dynamic json, {bool growable = false}) {
    final result = <DateTime>[];
    if (json is List && json.isNotEmpty) {
      for (final row in json) {
        final value = DateTime.tryParse(row);
        if (value != null) {
          result.add(value);
        }
      }
    }
    return result.toList(growable: growable);
  }
}

And then call the extension method using hte name of the extension:

DateTimeHelper.listFromJson()

jtmuller5 avatar Sep 15 '22 14:09 jtmuller5

Seems to happen in typescript fetch for some spring generated swagger specs.

ozialien avatar Jan 24 '23 16:01 ozialien