jsonsystems / json-schema

JSONSchema.Net Public Repository
Apache License 2.0
663 stars 64 forks source link

Support integers instead of changing them to doubles/floats/decimals. #78

Closed josephspurrier closed 4 years ago

josephspurrier commented 4 years ago

When I paste in this code to the generator (https://jsonschema.net/home), it comes back with an example that says 1.0 instead of 1.

{
    "id": 1
}

Here is what comes back:

{
    "$schema": "http://json-schema.org/draft-07/schema",
    "$id": "http://example.com/example.json",
    "type": "object",
    "title": "The Root Schema",
    "description": "The root schema comprises the entire JSON document.",
    "default": {},
    "additionalProperties": true,
    "required": [
        "id"
    ],
    "properties": {
        "id": {
            "$id": "#/properties/id",
            "type": "integer",
            "title": "The Id Schema",
            "description": "An explanation about the purpose of this instance.",
            "default": 0,
            "examples": [
                1.0
            ]
        }
    }
}

Is there a way for the example to not have a decimal?

jackwootton commented 4 years ago

@josephspurrier Another good issue and thanks for posting it. This has been on my list of tasks for some time. It's caused by the use of Google's Protocol Buffers on the backend which converts everything to a number:

from google.protobuf import json_format, struct_pb2

if __name__ == '__main__':
    list_value = struct_pb2.ListValue()
    list_value.append(1)
    result = json_format.MessageToDict(list_value)
    print(result)

Running this Python code outputs: [1.0]. The examples keyword is implemented using ListValue.

I am going to look at this again and see if there's a reasonably nice way around it.

jackwootton commented 4 years ago

@josephspurrier I'm working on significant improvements to the examples, default, const, and enum keywords which will fix this. I'll keep this issue updated.

jackwootton commented 4 years ago

@josephspurrier I've improved the handling of types on www.jsonschema.net. Does it now behave as you wanted?

jackwootton commented 4 years ago

Closing this as it's now working and there's not update from OP.