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 for IpNetwork does not match its serialized form #156

Closed
ahl opened this issue Jun 24, 2022 · 1 comment · Fixed by #157
Closed

JSON schema for IpNetwork does not match its serialized form #156

ahl opened this issue Jun 24, 2022 · 1 comment · Fixed by #157

Comments

@ahl
Copy link
Contributor

ahl commented Jun 24, 2022

See: https://github.com/ahl/ipnetwork/tree/fail

$ cargo test --features=schemars test_ipnetwork_json
    Blocking waiting for file lock on build directory
   Compiling does-it-json v0.0.1 (/Users/ahl/src/does-it-json)
   Compiling ipnetwork v0.19.0 (/Users/ahl/src/ipnetwork)
    Finished test [unoptimized + debuginfo] target(s) in 2.20s
     Running unittests src/lib.rs (target/debug/deps/ipnetwork-5bc3e27d373077a5)

running 0 tests

test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 63 filtered out; finished in 0.00s

     Running tests/test_json.rs (target/debug/deps/test_json-c7e0909f58ffe5cf)

running 1 test
test tests::test_ipnetwork_json ... FAILED

failures:

---- tests::test_ipnetwork_json stdout ----
thread 'tests::test_ipnetwork_json' panicked at 'error: "127.1.0.0/24" did not conform to the schema at #/definitions/IpNetwork.oneOf: value validated against 0 of 2 `oneOf` schemas (rather than 1)
schema: {
  "$schema": "http://json-schema.org/draft-07/schema#",
  "title": "MyStruct",
  "type": "object",
  "required": [
    "ipnetwork"
  ],
  "properties": {
    "ipnetwork": {
      "type": "array",
      "items": {
        "$ref": "#/definitions/IpNetwork"
      }
    }
  },
  "definitions": {
    "IpNetwork": {
      "description": "Represents a generic network range. This type can have two variants: the v4 and the v6 case.",
      "oneOf": [
        {
          "type": "object",
          "required": [
            "V4"
          ],
          "properties": {
            "V4": {
              "$ref": "#/definitions/Ipv4Network"
            }
          },
          "additionalProperties": false
        },
        {
          "type": "object",
          "required": [
            "V6"
          ],
          "properties": {
            "V6": {
              "$ref": "#/definitions/Ipv6Network"
            }
          },
          "additionalProperties": false
        }
      ]
    },
    "Ipv4Network": {
      "description": "Represents a network range where the IP addresses are of v4",
      "type": "object",
      "required": [
        "addr",
        "prefix"
      ],
      "properties": {
        "addr": {
          "type": "string",
          "format": "ipv4"
        },
        "prefix": {
          "type": "integer",
          "format": "uint8",
          "minimum": 0.0
        }
      }
    },
    "Ipv6Network": {
      "description": "Represents a network range where the IP addresses are of v6",
      "type": "object",
      "required": [
        "addr",
        "prefix"
      ],
      "properties": {
        "addr": {
          "type": "string",
          "format": "ipv6"
        },
        "prefix": {
          "type": "integer",
          "format": "uint8",
          "minimum": 0.0
        }
      }
    }
  }
}
value: {
  "ipnetwork": [
    "127.1.0.0/24",
    "::1/0"
  ]
}', tests/test_json.rs:69:13
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace


failures:
    tests::test_ipnetwork_json

test result: FAILED. 0 passed; 1 failed; 0 ignored; 0 measured; 2 filtered out; finished in 0.00s

error: test failed, to rerun pass '--test test_json'

The problem is that #153 simply derived JsonSchema rather than generating a schema that matches the implementation of Serialize. I would be happy to submit a PR; just let me know.

@achanda
Copy link
Owner

achanda commented Jun 25, 2022

Thanks for opening this issue @ahl. Please feel free to submit a PR.

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