glideapps / quicktype

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

[Dart] Add missing output @JsonKey output for @freezed option #2348

Closed kvnxiao closed 1 year ago

kvnxiao commented 1 year ago

Dart language enhancements:

Excerpts:

./script/quicktype --src-lang schema generated_freezed.json -l dart --use-json-annotation --use-freezed --coders-in-class generated_freezed.json

{
    "$id": "datamodel",
    "$schema": "https://json-schema.org/draft-07/schema#",
    "$ref": "#/definitions/DocumentType",
    "definitions": {
        "DocumentType": {
            "title": "DocumentType",
            "type": "object",
            "required": ["time_created"],
            "properties": {
                "time_created": {
                    "type": ["string", "null"],
                    "format": "date-time"
                }
            }
        }
    }
}
import 'package:freezed_annotation/freezed_annotation.dart';
import 'dart:convert';

part 'document_type.freezed.dart';
part 'document_type.g.dart';

@freezed
class DocumentType with _$DocumentType {
    const factory DocumentType({
        @JsonKey(name: "time_created")
        required DateTime? timeCreated,
    }) = _DocumentType;

    factory DocumentType.fromJson(Map<String, dynamic> json) => _$DocumentTypeFromJson(json);
}

Notice that time_created is nullable, but also specified as required in the JSON Schema, and this is properly reflected in the generated code.

kvnxiao commented 1 year ago

Hi @dvdsgl !

May I kindly also request for this to be reviewed and for a new build to be released so that we may use these fixes in production without having to run our local fork?

Thank you!

dvdsgl commented 1 year ago

It should have been released immediately.

kvnxiao commented 1 year ago

Thanks! Let me know if there's any changes needed for this PR to be merged.

kvnxiao commented 1 year ago

Hi @dvdsgl , apologies for the ping again but would you have some time to review this PR for this to get merged?

Thank you!

dvdsgl commented 1 year ago

Thank you!