glideapps / quicktype

Generate types and converters from JSON, Schema, and GraphQL
https://app.quicktype.io
Apache License 2.0
12.14k stars 1.06k forks source link

[Dart] Enums with `use-json-annotation` flag missing `@JsonValue()` annotation #2343

Closed kvnxiao closed 1 year ago

kvnxiao commented 1 year ago

E.g. consider the following JSON schema used to generate the code:

{
  "$id": "datamodel",
  "$schema": "https://json-schema.org/draft-07/schema#",
  "$ref": "#/definitions/DocumentType",
  "definitions": {
    "DocumentType": {
      "type": "string",
      "title": "DocumentType",
      "enum": ["abc", "def"]
    }
  }
}

This generates the following dart code:

import 'package:json_annotation/json_annotation.dart';
import 'dart:convert';

part 'generated.g.dart';

enum DocumentType { ABC, DEF }

final documentTypeValues = EnumValues({
    "abc": DocumentType.ABC,
    "def": DocumentType.DEF
});

class EnumValues<T> {
    Map<String, T> map;
    late Map<T, String> reverseMap;

    EnumValues(this.map);

    Map<T, String> get reverse {
        reverseMap = map.map((k, v) => MapEntry(v, k));
        return reverseMap;
    }
}

However, the generated code from build_runner for json_annotations will be

const _$DocumentTypeEnumMap = {
  DocumentType.ABC: 'ABC',
  DocumentType.DEF: 'DEF',
};

The expected output is:

const _$DocumentTypeEnumMap = {
  DocumentType.ABC: 'abc',
  DocumentType.DEF: 'def',
};

and this can be achieved with adding @JsonValue() annotations to each enum entry declaration:

enum DocumentType { @JsonValue("abc") ABC, @JsonValue("def") DEF }

but quicktype does not support this yet.

kvnxiao commented 1 year ago

Created a PR here: https://github.com/quicktype/quicktype/pull/2344

kvnxiao commented 1 year ago

@dvdsgl apologies for the ping here! Would it be possible to know what the cadence for a new version bump / release usually is so that I may close out this issue? Thanks!

dvdsgl commented 1 year ago

Merges to master are related to npm in about a minute, and they release in the web app within a couple days.