elnabo / json2object

Type safe Haxe/JSON (de)serializer
MIT License
66 stars 17 forks source link

@:default values in JsonSchemaWriter #56

Closed Gama11 closed 5 years ago

Gama11 commented 5 years ago
import json2object.utils.JsonSchemaWriter;

class Main {
    static function main() {
        trace(new JsonSchemaWriter<Data>("\t").schema);
    }
}

typedef Data = {
    @:default(10)
    var foo:Int;
    @:default("hello world")
    var bar:String;
}

Generates:

{
        "$schema": "http://json-schema.org/draft-07/schema#",
        "$ref": "#/definitions/Data",
        "definitions": {
                "Data": {
                        "additionalProperties": false,
                        "properties": {
                                "bar": {
                                        "type": "string"
                                },
                                "foo": {
                                        "type": "integer"
                                }
                        },
                        "required": [
                                "bar",
                                "foo"
                        ],
                        "type": "object"
                }
        }
}

What it should be generated as (important to include the default field for code completion):

{
    "$schema": "http://json-schema.org/draft-07/schema#",
    "$ref": "#/definitions/Data",
    "definitions": {
        "Data": {
            "additionalProperties": false,
            "properties": {
                "bar": {
                    "type": "string",
                    "default": "hello world"
                },
                "foo": {
                    "type": "integer",
                    "default": 10
                }
            },
            "required": [
                "bar",
                "foo"
            ],
            "type": "object"
        }
    }
}
elnabo commented 5 years ago

This is supported since c357b2c443a89a099ff83bdae3b49f40359969ec.

Gama11 commented 5 years ago

Hm, doesn't seem to compile anymore.

json2object\src\json2object/utils/schema/DataBuilder.hx:268: characters 77-83 : Unknown identifier : putils
json2object\src\json2object/utils/schema/DataBuilder.hx:268: characters 77-83 : For optional function argument 'putils'
elnabo commented 5 years ago

Ok, I see the problem.

It's a problem with @:default(auto) and a bad copy/paste.

I should be able to fix it later today.

elnabo commented 5 years ago

Should be fixed by 30cae7c7afa4a8b447f577cf4d73535476d3dded