google / googleapis.dart

Repository for building the googleapis packages
https://pub.dev/packages/googleapis
BSD 3-Clause "New" or "Revised" License
392 stars 118 forks source link

[discoveryapis_generator] Consider adding helpers to to ease use of int-as-string fields #287

Open thachnb85 opened 3 years ago

thachnb85 commented 3 years ago

Hello Google developers,

I got this error when launching discoveryapis_generator

Unhandled exception:
Null check operator used on a null value
#0      main (file:///Users/mac/.pub-cache/hosted/pub.dartlang.org/discoveryapis_generator-1.0.0/bin/generate.dart:86:41)
#1      _delayEntrypointInvocation.<anonymous closure> (dart:isolate-patch/isolate_patch.dart:281:32)
#2      _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:184:12)

My install log

➜  sync-api-generator pub global deactivate discoveryapis_generator
Deactivated package discoveryapis_generator 1.0.0.
➜  sync-api-generator dart pub global activate discoveryapis_generator
Resolving dependencies... (1.4s)
+ _discoveryapis_commons 1.0.1
+ _fe_analyzer_shared 23.0.0
+ analyzer 2.0.0
+ args 2.2.0
+ async 2.8.2
+ charcode 1.3.1
+ cli_util 0.3.3
+ collection 1.15.0
+ convert 3.0.1
+ crypto 3.0.1
+ dart_style 2.0.3
+ discoveryapis_generator 1.0.0
+ file 6.1.2
+ glob 2.0.1
+ http 0.13.3
+ http_parser 4.0.0
+ meta 1.7.0
+ package_config 2.0.0
+ path 1.8.0
+ pedantic 1.11.1
+ pub_semver 2.0.0
+ source_span 1.8.1
+ string_scanner 1.1.0
+ term_glyph 1.2.0
+ typed_data 1.3.0
+ watcher 1.0.0
+ yaml 3.1.0
Precompiling executables... (9.9s)
Precompiled discoveryapis_generator:generate.
Installed executable discoveryapis_generator.
Activated discoveryapis_generator 1.0.0.

Fluttter doctor output

➜  sync-api-generator flutter doctor
Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel master, 2.5.0-7.0.pre.125, on macOS 11.3.1 20E241 darwin-x64, locale en-CA)
[✓] Android toolchain - develop for Android devices (Android SDK version 30.0.2)
[✓] Chrome - develop for the web
[✓] Android Studio (version 2020.3)
[✓] VS Code (version 1.58.0)

I tried both stable channel and master channel, and got the similar error log.

Moreover, when running the generator on my API's json, it throws this error:

➜  sync-api-generator pub global run discoveryapis_generator:generate package -i discovery -o dart_client
Unhandled exception:
type 'int' is not a subtype of type 'String' in type cast
#0      new JsonSchema.fromJson (package:discoveryapis_generator/src/generated_googleapis/discovery/v1.dart:472:35)
#1      new JsonSchema.fromJson.<anonymous closure> (package:discoveryapis_generator/src/generated_googleapis/discovery/v1.dart:514:22)
#2      MapMixin.map (dart:collection/maps.dart:170:28)
#3      new JsonSchema.fromJson (package:discoveryapis_generator/src/generated_googleapis/discovery/v1.dart:511:72)
#4      new RestDescription.fromJson.<anonymous closure> (package:discoveryapis_generator/src/generated_googleapis/discovery/v1.dart:861:22)
#5      MapMixin.map (dart:collection/maps.dart:170:28)
#6      new RestDescription.fromJson (package:discoveryapis_generator/src/generated_googleapis/discovery/v1.dart:858:75)
#7      generateAllLibraries.<anonymous closure> (package:discoveryapis_generator/discoveryapis_generator.dart:46:40)
#8      MappedIterator.moveNext (dart:_internal/iterable.dart:390:20)
#9      new _GrowableList._ofOther (dart:core-patch/growable_array.dart:198:26)
#10     new _GrowableList.of (dart:core-patch/growable_array.dart:152:26)
#11     new List.of (dart:core-patch/array_patch.dart:50:28)
#12     Iterable.toList (dart:core/iterable.dart:366:12)
#13     generateAllLibraries (package:discoveryapis_generator/discoveryapis_generator.dart:49:8)
#14     main (file:///Users/mac/.pub-cache/hosted/pub.dartlang.org/discoveryapis_generator-1.0.0/bin/generate.dart:104:20)
#15     _delayEntrypointInvocation.<anonymous closure> (dart:isolate-patch/isolate_patch.dart:281:32)
#16     _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:184:12)

Appreciate any idea to resolve this issue! Thank you.

Greatcallie commented 3 years ago

Same here. Any solution?

Greatcallie commented 3 years ago

I think it's a an issue with the api. Will try to see how I can fix mine!

kevmoo commented 3 years ago

I think it's the JSON input. There's a number when we're expecting a string.

Greatcallie commented 3 years ago

The json kind in the sample is a discovery#restDescription but mine is discovery#directoryList, did I build my app engine app wrongly? who knows how I can get it in the former kind?

Greatcallie commented 3 years ago

I solved my own problem here https://github.com/google/googleapis.dart/issues/289#issue-981804466. I don't know if anyone has the same issue.

thachnb85 commented 3 years ago

@Greatcallie I ended up using the older version 0.9.15 and did 2 hijacking spots:

In v1.dart: image

And in dart_resources.dart image

I'm able to generate dart code for my discovery doc.

kevmoo commented 3 years ago

Yup. It seems the json has "description": null, which is not expected/handled by our encoder.

Where are you getting your descriptions? Could you simple remove these null entries?

thachnb85 commented 3 years ago

Thanks @kevmoo for replying.

I checked my json file and that descriptions are not null, here is a part of json file

{
  "auth": {
    "oauth2": {
      "scopes": {
        "https://www.googleapis.com/auth/userinfo.email": {
          "description": "https://www.googleapis.com/auth/userinfo.email"
        }
      }
    }
  },
  "basePath": "/_ah/api/sync/v1/",
  "baseUrl": "http://myapp.appspot.com/_ah/api/sync/v1/",
  "batchPath": "batch",
  "description": "Sync API",
  "discoveryVersion": "v1",
  "icons": {
    "x16": "http://www.google.com/images/icons/product/search-16.gif",
    "x32": "http://www.google.com/images/icons/product/search-32.gif"
  },
  "id": "sync:v1",
  "kind": "discovery#restDescription",
  "methods": {
    "getAccount": {
      "httpMethod": "GET",
      "id": "sync.getAccount",
      "parameterOrder": [
        "id"
      ],
      "parameters": {
        "id": {
          "format": "int64",
          "location": "path",
          "required": true,
          "type": "string"
        }
      },
      "path": "GetAccount/{id}",
      "response": {
        "$ref": "Account"
      },
      "scopes": [
        "https://www.googleapis.com/auth/userinfo.email"
      ]
    },
}

It seems now I saw a lot of error related to data conversion from json to object, (for example expecting int but seeing String and vice versa), I checked the generated code (v1.dart) and found that I have to do this manually everywhere to match int/String type when converting (fromJson methods for all objects)

One example of converting String to int, which isn't handled by generator: image

Any suggests? Thank you!

kevmoo commented 3 years ago

Look here: https://github.com/google/googleapis.dart/blob/516dbc59b675d0b15a214e06924876a9939f653c/discoveryapis_generator/lib/src/dart_schemas.dart#L260-L304

So we're set up now to continue to treat int64 like a String – this is because Javascript cannot support numbers beyond the range of pow(2, 53) without truncating values.

So we leave the values as String.

I know this is annoying, but it's a limitation we've kinda lived with.

I could imagine having helpers here to expose the value as an int by converting on-the-fly.