oxidecomputer / typify

compiler from JSON Schema into idiomatic Rust types
Apache License 2.0
418 stars 57 forks source link

Adding a `"maximum"` limit to an integer changes the inner Rust type #589

Open jgallagher opened 4 months ago

jgallagher commented 4 months ago

In the following JSON schema, all three ByteCountN variants have the same type and format, and only differ by maximum:

{
    "$schema": "http://json-schema.org/draft-07/schema#",
    "definitions": {
        "ByteCount1": {
            "description": "Byte count to express memory or storage capacity.",
            "type": "integer",
            "format": "uint64",
            "minimum": 0
        },
        "ByteCount2": {
            "description": "Byte count to express memory or storage capacity.",
            "type": "integer",
            "format": "uint64",
            "minimum": 0,
            "maximum": 9223372036854775807
        },
        "ByteCount3": {
            "description": "Byte count to express memory or storage capacity.",
            "type": "integer",
            "format": "uint64",
            "minimum": 0,
            "maximum": 9223372036854775808
        }
    }
}

I expected all of these to produce a struct wrapping a u64, but ByteCount{2,3} use i64 instead:

% cargo typify bytecount.json && rg 'struct ByteCount' bytecount.rs
48:pub struct ByteCount1(pub u64);
114:pub struct ByteCount2(pub i64);
180:pub struct ByteCount3(pub i64);
ahl commented 4 months ago

Oof.. ByteCount3 seems particularly stupid.