int64 handling
int64 is being handled as an Elm Int, which is unsafe. It should be String or some BigInt type.
@ronanyeah do you have an example schema (or portion of one) with an int64?
https://github.com/ronanyeah/openapi-rust-elm/blob/master/api.json
Thank you! This puts further emphasis on needing to figure out how to handle formats. There's been some discussion in Discord (most recent as of now) about having an elm-review like config of sorts. Essentially, we need a way for users of the CLI tool to be able to add semi-arbitrary encoding, decoding, and types.
I don't really know what this config looks like yet but more discussion in the Discord is very welcome.
One issue specific to int64 is that Elm uses JSON.parse under the hood, so by the time we get a value it has already lost precision. The way to fix this would be to implement the JSON parsing in Elm directly which can be done but it's going to be slower for everyone else.
I'm personally up for considering it as a config option in the new config-file approach, but I'm not going to write a JSON parser in Elm from scratch :sweat_smile:
Compare what is needed in JS to use BigInts for big numbers:
const bigJSON = '{"gross_gdp": 12345678901234567890}';
const bigObj = JSON.parse(bigJSON, (key, value, context) => {
if (key === "gross_gdp") {
// Ignore the value because it has already lost precision
return BigInt(context.source);
}
return value;
});
If you control the server the simplest path is to encode the number as a string and then parse it on the client after the JSON decoding
This would be an int64 format. Notice the problematic encoder and decoder.
int64Format : CliMonad.Format
int64Format =
{ basicType = Common.Integer
, format = "int64"
, annotation = Gen.Int64.annotation_.int64
, encode =
\int64 ->
int64
|> Gen.Int64.toSignedString
|> Gen.String.call_.toInt
|> Gen.Maybe.withDefault (Elm.int 0)
|> Gen.Json.Encode.call_.int
, decoder = Gen.Json.Decode.int |> Gen.Json.Decode.map Gen.Int64.call_.fromInt
, toParamString = Gen.Int64.toSignedString
, sharedDeclarations = []
, requiresPackages = [ "folkertdev/elm-int64" ]
}