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

Incorrect Type Validation for spec Field: Interprets Numeric Field as String #191

Closed
dasionov opened this issue Dec 17, 2024 · 1 comment

Comments

@dasionov
Copy link

dasionov commented Dec 17, 2024

Description:

When validating OpenAPI schemas using the github.com/go-openapi/validate v0.20.2 package, a numeric field within a nested structure incorrectly fails validation when its type is specified to be any of string or integer.

when I try to set:

spec:
  domain:
    memory:
      guest: 2312241152

Error Example:

[spec.template.spec.domain.memory.guest in body must be of type string: "number"]

of course it works fine with

spec:
  domain:
    memory:
      guest: "2312241152"

the schema in the crd:

guest:
  anyOf:
  - type: integer
  - type: string
@fredbi
Copy link
Member

fredbi commented Mar 14, 2025

@dasionov

I am unable to reproduce your issue with latest tagged release (v0.24)

The following repro test sucessfully validates both types of input. But perhaps there is something I didn't get correctly.

func TestSchemaValidator_Issue191(t *testing.T) {
	t.Run("with anyOf schema specifying string or number", func(t *testing.T) {
		var schemaJSON = `
{
	"type": "object",
	"properties": {
		"spec": {
			"type": "object",
			"properties": {
			  "domain": {
			    "type": "object",
			    "properties": {
			      "memory": {
	                        "type": "object",
	                        "properties": {
		                    "guest": {
		                        "anyOf": [
		                          {
		                            "type": "integer"
		                          },
		                          {
		                            "type": "string"
		                          }
		                       ]
		                   }
		             }
		        }
	        }
			  }
	    }
	  }
	}
}`

		schema := new(spec.Schema)
		require.NoError(t, json.Unmarshal([]byte(schemaJSON), schema))

		t.Run("string should validate against schema", func(t *testing.T) {
			var input map[string]interface{}
			const inputJSON = `{"spec": { "domain": {"memory": {"guest": "2312241152"}}}}`

			require.NoError(t, json.Unmarshal([]byte(inputJSON), &input))

			t.Run("validate with pure jsonschema", func(t *testing.T) {
				require.NoError(t, AgainstSchema(schema, input, strfmt.Default))
			})

			t.Run("validate with swagger flavor of jsonschema", func(t *testing.T) {
				require.NoError(t, AgainstSchema(schema, input, strfmt.Default, SwaggerSchema(true)))
			})
		})

		t.Run("integer should validate against schema", func(t *testing.T) {
			var input map[string]interface{}
			const inputJSON = `{"spec": { "domain": {"memory": {"guest": 2312241152}}}}`

			require.NoError(t, json.Unmarshal([]byte(inputJSON), &input))

			t.Run("validate with pure jsonschema", func(t *testing.T) {
				require.NoError(t, AgainstSchema(schema, input, strfmt.Default))
			})

			t.Run("validate with swagger flavor of jsonschema", func(t *testing.T) {
				require.NoError(t, AgainstSchema(schema, input, strfmt.Default, SwaggerSchema(true)))
			})
		})

		t.Run("float should NOT validate against schema", func(t *testing.T) {
			var input map[string]interface{}
			const inputJSON = `{"spec": { "domain": {"memory": {"guest": 23122411.52}}}}`

			require.NoError(t, json.Unmarshal([]byte(inputJSON), &input))

			t.Run("do not validate with pure jsonschema", func(t *testing.T) {
				require.Error(t, AgainstSchema(schema, input, strfmt.Default))
			})
		})
	})
}
go test -v -run 191
=== RUN   TestSchemaValidator_Issue191
=== RUN   TestSchemaValidator_Issue191/with_anyOf_schema_specifying_string_or_number
=== RUN   TestSchemaValidator_Issue191/with_anyOf_schema_specifying_string_or_number/string_should_validate_against_schema
=== RUN   TestSchemaValidator_Issue191/with_anyOf_schema_specifying_string_or_number/string_should_validate_against_schema/validate_with_pure_jsonschema
=== RUN   TestSchemaValidator_Issue191/with_anyOf_schema_specifying_string_or_number/string_should_validate_against_schema/validate_with_swagger_flavor_of_jsonschema
=== RUN   TestSchemaValidator_Issue191/with_anyOf_schema_specifying_string_or_number/integer_should_validate_against_schema
=== RUN   TestSchemaValidator_Issue191/with_anyOf_schema_specifying_string_or_number/integer_should_validate_against_schema/validate_with_pure_jsonschema
=== RUN   TestSchemaValidator_Issue191/with_anyOf_schema_specifying_string_or_number/integer_should_validate_against_schema/validate_with_swagger_flavor_of_jsonschema
=== RUN   TestSchemaValidator_Issue191/with_anyOf_schema_specifying_string_or_number/float_should_NOT_validate_against_schema
=== RUN   TestSchemaValidator_Issue191/with_anyOf_schema_specifying_string_or_number/float_should_NOT_validate_against_schema/do_not_validate_with_pure_jsonschema
--- PASS: TestSchemaValidator_Issue191 (0.00s)
    --- PASS: TestSchemaValidator_Issue191/with_anyOf_schema_specifying_string_or_number (0.00s)
        --- PASS: TestSchemaValidator_Issue191/with_anyOf_schema_specifying_string_or_number/string_should_validate_against_schema (0.00s)
            --- PASS: TestSchemaValidator_Issue191/with_anyOf_schema_specifying_string_or_number/string_should_validate_against_schema/validate_with_pure_jsonschema (0.00s)
            --- PASS: TestSchemaValidator_Issue191/with_anyOf_schema_specifying_string_or_number/string_should_validate_against_schema/validate_with_swagger_flavor_of_jsonschema (0.00s)
        --- PASS: TestSchemaValidator_Issue191/with_anyOf_schema_specifying_string_or_number/integer_should_validate_against_schema (0.00s)
            --- PASS: TestSchemaValidator_Issue191/with_anyOf_schema_specifying_string_or_number/integer_should_validate_against_schema/validate_with_pure_jsonschema (0.00s)
            --- PASS: TestSchemaValidator_Issue191/with_anyOf_schema_specifying_string_or_number/integer_should_validate_against_schema/validate_with_swagger_flavor_of_jsonschema (0.00s)
        --- PASS: TestSchemaValidator_Issue191/with_anyOf_schema_specifying_string_or_number/float_should_NOT_validate_against_schema (0.00s)
            --- PASS: TestSchemaValidator_Issue191/with_anyOf_schema_specifying_string_or_number/float_should_NOT_validate_against_schema/do_not_validate_with_pure_jsonschema (0.00s)
PASS
ok  	github.com/go-openapi/validate	0.005s

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

No branches or pull requests

2 participants