Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JSON Schema wrong for u128/i128 fields #1605

Closed
webmaster128 opened this issue Feb 1, 2023 · 3 comments · Fixed by #1632
Closed

JSON Schema wrong for u128/i128 fields #1605

webmaster128 opened this issue Feb 1, 2023 · 3 comments · Fixed by #1632

Comments

@webmaster128
Copy link
Member

When using u128/i128 in messages, the generated schema wrongly outputs "type": "integer" instead of string.

      {
        "description": "Allocate large amounts of memory without consuming much gas",
        "type": "object",
        "required": [
          "allocate_large_memory"
        ],
        "properties": {
          "allocate_large_memory": {
            "type": "object",
            "required": [
              "pages"
            ],
            "properties": {
              "pages": {
                "type": "integer",
                "format": "uint128",
                "minimum": 0.0
              }
            },
            "additionalProperties": false
          }
        },
        "additionalProperties": false
      },

However, both json-serde and json-serde-wasm use strings for u128/i128. This is probably a bug to be fixed in schemars.

@webmaster128 webmaster128 added the bug Something isn't working label Feb 1, 2023
@aeryz
Copy link

aeryz commented Feb 26, 2023

I think there is a difference between serde_json and serde_json_wasm in terms of u128/i128 (de)serialization. See, serde_json serializes 128 bit integers as numbers here: https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=607431b2668d64506e870a973b3d00d4
But when you run the same code with serde_json_wasm, it serializes it to string.

For more context, see serde_json implementation expecting the value to be a number:
https://github.com/serde-rs/json/blob/master/src/de.rs#L1441

serde_json_wasm expects it to be a string:
https://github.com/CosmWasm/serde-json-wasm/blob/main/src/de/mod.rs#L361

and in schemars it is parsed as number:
https://github.com/GREsau/schemars/blob/master/schemars/src/json_schema_impls/primitives.rs#L84

Why u128/i128 are serialized as strings in serde-json-wasm btw?

@webmaster128
Copy link
Member Author

Thanks a lot for pointing this out. This is a big source of future problems.

Why u128/i128 are serialized as strings in serde-json-wasm btw?

Because whoever implemented it did not check serde_json compatibility. Also the numberic version is hardly usable by clients that use floats to parse numbers, especially JavaScript but also jq.

@webmaster128
Copy link
Member Author

Because whoever implemented it did not check serde_json compatibility.

This one is on me. I silently approved this by inaction due to holidays and other stuff in 2021.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants