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

[BUG]: (Dart) Null is better than empty array for null/undefined list-type properties #2656

Open
nakamura-work opened this issue Oct 16, 2024 · 0 comments
Labels

Comments

@nakamura-work
Copy link

Issue Type

  • quicktype output

Context (Environment, Version, Language)

Input Format: JSON Schema
Output Language: Dart

CLI, npm, or app.quicktype.io: both
Version: 23.0.170

Description

In DartRenderer.ts#L233, null/undefined value of a list-type property will fall back to an empty array.
However, some APIs, including ours, distinguish between null and [].

e.g. following is the simplified pseudocode.

{
  "$ref": "#/definitions/API",
  "$schema": "http://json-schema.org/draft-07/schema#",
  "definitions": {
    "API": {
      "properties": {
        "onlyFor": {
          "items": {
            "type": "string" 
          },
          "type": "array" 
        }
      },
      "type": "object" 
    }
  }
}
Coordinate(onlyFor: null).toJson(); // This will be `{onlyFor: []}`

This causes problems for the following logic:

type API = {
    onlyFor?: string[];
};

function isEligible(res: API) {
    const onlyFor = res.onlyFor;
    if (onlyFor == null) return true;
    return onlyFor.some((c) => c === process.platform);
}

Input Data

{
  "$ref": "#/definitions/API",
  "$schema": "http://json-schema.org/draft-07/schema#",
  "definitions": {
    "API": {
      "properties": {
        "onlyFor": {
          "items": {
            "type": "string" 
          },
          "type": "array" 
        }
      },
      "type": "object" 
    }
  }
}

Expected Behaviour / Output

class Coordinate {
    List<String>? onlyFor;

    Coordinate({
        this.onlyFor,
    });

    factory Coordinate.fromJson(Map<String, dynamic> json) => Coordinate(
        onlyFor: json["onlyFor"] == null ? null : List<String>.from(json["onlyFor"]!.map((x) => x)),
    );

    Map<String, dynamic> toJson() => {
        "onlyFor": onlyFor == null ? null : List<dynamic>.from(onlyFor!.map((x) => x)),
    };
}

Current Behaviour / Output

class Coordinate {
    List<String>? onlyFor;

    Coordinate({
        this.onlyFor,
    });

    factory Coordinate.fromJson(Map<String, dynamic> json) => Coordinate(
        onlyFor: json["onlyFor"] == null ? [] : List<String>.from(json["onlyFor"]!.map((x) => x)),
    );

    Map<String, dynamic> toJson() => {
        "onlyFor": onlyFor == null ? [] : List<dynamic>.from(onlyFor!.map((x) => x)),
    };
}

Steps to Reproduce

  1. Open https://app.quicktype.io/
  2. Set Source type to JSON Schema
  3. Write Input Data as source
  4. Select Dart language in Options
  5. Confirm that Null Safety is turned on

Possible Solution

return [list, " == null ? [] : ", "List<", itemType, ">.from(", list, "!.map((x) => ", mapper, "))"];

-            return [list, " == null ? [] : ", "List<", itemType, ">.from(", list, "!.map((x) => ", mapper, "))"];
+            return [list, " == null ? null : ", "List<", itemType, ">.from(", list, "!.map((x) => ", mapper, "))"];

We understand that this solution may impact existing APIs.
So we want to explore if there is a better alternative.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant