Skip to content
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

Request JSON: Type 'bool' is not a subtype of type 'String' in type cast #1174

Open
acarlstein opened this issue Apr 4, 2024 · 1 comment
Labels
package:http type-bug Incorrect behavior (everything from a crash to more subtle misbehavior)

Comments

@acarlstein
Copy link

acarlstein commented Apr 4, 2024

Summary

When doing a POST rest call, if the JSON request contains a boolean value instead of a String value, then we get Error: type 'bool' is not a subtype of type 'String' in type cast. This goes against The JavaScript Object Notation (JSON) Data Interchange Format - RFC-7156.

Description

This SomeRequest class:

class SomeRequest {
  String id;
  bool advanced;

  FollowerUpdateRequest({
    required this.id,
    this.advanced = false
  });

  Map<String, dynamic> toJson(){
    return {
      'id': id,
      'advanced': advanced
    };
  }

  @override
  String toString(){
    return jsonEncode(toJson());
  }

}

will create the following JSON (example):

{
  'id': "xxx-xxxx-xxx",
  'advanced': false
};

However, when we use this request with http.post like this:

Future<SomeResponse> fetch(
    SomeRequest data
 ) async {
    Uri uri = Uri.parse("some url");
    final response = await http.post(
      uri,
      body: data.toJson()
    );
    if (response.statusCode == 200){
      SomeResponse resp = SomeResponse.fromJson(
        jsonDecode(response.body)
      );
      return resp;
    } else {
      throw Exception('print this exception');
    }
  }

We get this error:

flutter: [!] Info: Error: type 'bool' is not a subtype of type 'String' in type cast

If we change the type of advanced from bool to String, this error disappears, but the server will reject such JSON because it validates that advanced should be boolean.

If we read The JavaScript Object Notation (JSON) Data Interchange Format - RFC-7156, we could read that JSON can represent four primitive types (strings, numbers, booleans, and null) and two structured types (objects and arrays)..

Acceptance Criteria

Follow the documentation in The JavaScript Object Notation (JSON) Data Interchange Format - RFC-7156 so any request with a body, will accept a JSON body that contains a boolean, following rule of JSON can represent four primitive types (strings, numbers, booleans, and null) and two structured types (objects and arrays).

Extra Information

flutter doctor -v:

[✓] Flutter (Channel stable, 3.19.1, on macOS 14.3.1 23D60 darwin-x64, locale en-US)
    • Flutter version 3.19.1 on channel stable at /usr/local/Caskroom/flutter/3.16.3/flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision abb292a07e (6 weeks ago), 2024-02-20 14:35:05 -0800
    • Engine revision 04817c99c9
    • Dart version 3.3.0
    • DevTools version 2.31.1

[✓] Android toolchain - develop for Android devices (Android SDK version 34.0.0)
    • Android SDK at /Users/agcar/Library/Android/sdk
    • Platform android-34, build-tools 34.0.0
    • ANDROID_HOME = /Users/agcar/Library/Android/sdk
    • ANDROID_SDK_ROOT = /opt/homebrew/share/android-commandlinetools
    • Java binary at: /Applications/Android Studio.app/Contents/jbr/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build 17.0.7+0-17.0.7b1000.6-10550314)
    • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 15.2)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Build 15C500b
    • CocoaPods version 1.15.2

[✓] Chrome - develop for the web
    • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[✓] Android Studio (version 2023.1)
    • Android Studio at /Applications/Android Studio.app/Contents
    • Flutter plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/6351-dart
    • Java version OpenJDK Runtime Environment (build 17.0.7+0-17.0.7b1000.6-10550314)

[✓] IntelliJ IDEA Community Edition (version 2021.3.2)
    • IntelliJ at /Applications/IntelliJ IDEA CE.app
    • Flutter plugin version 68.1.3
    • Dart plugin version 213.7371

[✓] VS Code (version 1.87.2)
    • VS Code at /Applications/Visual Studio Code.app/Contents
    • Flutter extension version 3.86.0

[✓] Connected device (3 available)
    • A’s iPhone (mobile) • XXXXXXXXX-XXXXXXXXXX • ios            • iOS 17.3.1 21D61
    • macOS (desktop)             • macos                     • darwin-x64     • macOS 14.3.1 XXXXX darwin-x64
    • Chrome (web)                • chrome                    • web-javascript • Google Chrome 123.0.6312.105

[✓] Network resources
    • All expected network resources are available.

• No issues found!

Note: I'm hiding PII with X's

@acarlstein acarlstein added package:http type-bug Incorrect behavior (everything from a crash to more subtle misbehavior) labels Apr 4, 2024
@brianquinlan
Copy link
Collaborator

Hi @acarlstein

As far as I can see, this problem isn't related to package:http, it is related to jsonEncode in dart:convert.

Also, I think that something else is going on here since jsonEncode can encode booleans just fine.

Could you try to create a minimal reproduction and, if you are convinced that there is still a bug, file one with the Dart SDK?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
package:http type-bug Incorrect behavior (everything from a crash to more subtle misbehavior)
Projects
None yet
Development

No branches or pull requests

2 participants