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 integer type #117

Open
SteadBytes opened this issue Aug 18, 2020 · 0 comments · May be fixed by #118
Open

Incorrect integer type #117

SteadBytes opened this issue Aug 18, 2020 · 0 comments · May be fixed by #118

Comments

@SteadBytes
Copy link

SteadBytes commented Aug 18, 2020

fields.Integer should produce a JSON schema with "type": "integer" as per https://json-schema.org/understanding-json-schema/reference/numeric.html?highlight=integer#integer . However, a schema with "type": "number", "format": "integer" is produced. The number docs do not mention integer as a format and AFAIK JSON schema validators will not perform the intended integer validation.

This behaviour is defined by:

int: {"type": "number", "format": "integer"},

Related (I think) issue: #40

Failing Test Case

diff --git a/tests/test_dump.py b/tests/test_dump.py
index ee63cda..02a191c 100644
--- a/tests/test_dump.py
+++ b/tests/test_dump.py
@@ -1,10 +1,32 @@
 import pytest
+import jsonschema
 from marshmallow import Schema, fields, validate
 
 from marshmallow_jsonschema import JSONSchema, UnsupportedValueError
+
 from . import UserSchema, validate_and_dump
 
 
+class TestIntegerField:
+    class Foo(Schema):
+        bar = fields.Integer()
+
+    def test_schema_type(self):
+        schema = JSONSchema().dump(self.Foo())
+        bar_property = schema["definitions"]["Foo"]["properties"]["bar"]
+
+        assert bar_property["type"] == "integer"
+        assert "format" not in bar_property
+
+    def test_validation(self):
+        schema = JSONSchema().dump(self.Foo())
+
+        jsonschema.validate({"bar": 1}, schema)
+
+        with pytest.raises(jsonschema.ValidationError):
+            jsonschema.validate({"bar": 1.1}, schema)
+
+
 def test_dump_schema():
     schema = UserSchema()

I'm happy to submit a PR for this if an appropriate course of action is agreed upon 😄

@SteadBytes SteadBytes linked a pull request Aug 19, 2020 that will close this issue
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.

1 participant