go-openapi / validate

openapi toolkit validation helpers
Apache License 2.0
115 stars 53 forks source link

What is the default maximum value of the Integer type? #142

Closed believening closed 2 years ago

believening commented 2 years ago

https://github.com/go-openapi/validate/blob/cd349c24ea7afd13ea2935f95dfd92c09031149e/schema_test.go#L30

I added a use case to test the default maximum value of the Integer type, which I expected to be the int64 upper bound, which is 9223372036854775807, but it doesn't look like that.

func TestSchemaValidator_MaxInterger(t *testing.T) {
    var schemaJSON = `{
    "properties": {
        "spec": {
            "properties": {
                "testInterger": {
                    "type": "integer"
                }
            },
            "required": [
                "testInterger"
            ]
        }
    }
}`
    schema := new(spec.Schema)
    require.NoError(t, json.Unmarshal([]byte(schemaJSON), schema))

    var input map[string]interface{}
    var inputJSON = `{"spec":{"testInterger": 9223372036854775807}}`
    assert.NoError(t, json.Unmarshal([]byte(inputJSON), &input))

    s := NewSchemaValidator(schema, nil, "", strfmt.Default)
    result := s.Validate(input)
    for i, e := range result.Errors {
        t.Logf("%d: %s", i, e.Error())
    }
    assert.True(t, result.IsValid())
}

the result of the unit test function is as follows:

Running tool: /usr/local/go/bin/go test -timeout 30s -run ^TestSchemaValidator_MaxInterger$ github.com/go-openapi/validate -v

=== RUN   TestSchemaValidator_MaxInterger
    /root/go/src/github.com/go-openapi/validate/schema_test.go:56: 0: spec.testInterger in body must be of type integer: "number"
    /root/go/src/github.com/go-openapi/validate/schema_test.go:58:
            Error Trace:    schema_test.go:58
            Error:          Should be true
            Test:           TestSchemaValidator_MaxInterger
--- FAIL: TestSchemaValidator_MaxInterger (0.00s)
FAIL
FAIL    github.com/go-openapi/validate  0.006s
believening commented 2 years ago

This seems to be a mistake in using the standard JSON library. I printed the deserialized test input, and it turns out that the input number have changed.

func main() {
    var input map[string]interface{}
    var inputJSON = `{
    "testIntergerA": 9223372036854775807,
    "testIntergerB": 9223372036854775296,
    "testIntergerC": 9223372036854775295
}`
    _ = json.Unmarshal([]byte(inputJSON), &input)
    fmt.Printf("%+v\n", input)

    for k, v := range input {
        tpe := reflect.TypeOf(v)
        kind := tpe.Kind()
        fmt.Println(k, ":", kind)

        value := reflect.ValueOf(v)
        fmt.Println(k, ":", value)
    }
}

// output:
// map[testIntergerA:9.223372036854776e+18 testIntergerB:9.223372036854776e+18 testIntergerC:9.223372036854775e+18]
// testIntergerA : float64
// testIntergerA : 9.223372036854776e+18
// testIntergerB : float64
// testIntergerB : 9.223372036854776e+18
// testIntergerC : float64
// testIntergerC : 9.223372036854775e+18