Repository: GREsau/schemars (rust)

Issue:
Range for standard number types
Is there a reason, why no default minimum and maximum values are generated with schema_for! for rust built-in number types, like i8-i128, u8-u128, isize, usize, f32, f64?

Pull request:
Set `minimum`/`maximum` for i8/u8/i16/u16
Fixes #298

Patch:
diff --git a/schemars/src/json_schema_impls/primitives.rs b/schemars/src/json_schema_impls/primitives.rs
index 42a7a39..31c4de0 100644
--- a/schemars/src/json_schema_impls/primitives.rs
+++ b/schemars/src/json_schema_impls/primitives.rs
@@ -37,13 +37,34 @@ macro_rules! simple_impl {
     };
 }
 
+macro_rules! ranged_impl {
+    ($type:ty => $instance_type:literal, $format:literal) => {
+        impl JsonSchema for $type {
+            inline_schema!();
+
+            fn schema_name() -> Cow<'static, str> {
+                $format.into()
+            }
+
+            fn json_schema(_: &mut SchemaGenerator) -> Schema {
+                json_schema!({
+                    "type": $instance_type,
+                    "format": $format,
+                    "minimum": <$type>::MIN,
+                    "maximum": <$type>::MAX
+                })
+            }
+        }
+    };
+}
+
 simple_impl!(str => "string");
 simple_impl!(String => "string");
 simple_impl!(bool => "boolean");
 simple_impl!(f32 => "number", "float");
 simple_impl!(f64 => "number", "double");
-simple_impl!(i8 => "integer", "int8");
-simple_impl!(i16 => "integer", "int16");
+ranged_impl!(i8 => "integer", "int8");
+ranged_impl!(i16 => "integer", "int16");
 simple_impl!(i32 => "integer", "int32");
 simple_impl!(i64 => "integer", "int64");
 simple_impl!(i128 => "integer", "int128");
@@ -88,8 +109,8 @@ macro_rules! unsigned_impl {
     };
 }
 
-unsigned_impl!(u8 => "integer", "uint8");
-unsigned_impl!(u16 => "integer", "uint16");
+ranged_impl!(u8 => "integer", "uint8");
+ranged_impl!(u16 => "integer", "uint16");
 unsigned_impl!(u32 => "integer", "uint32");
 unsigned_impl!(u64 => "integer", "uint64");
 unsigned_impl!(u128 => "integer", "uint128");


Test patch:
diff --git a/schemars/tests/integration/bytes.rs b/schemars/tests/integration/bytes.rs
index a0653b0..a34c5ff 100644
--- a/schemars/tests/integration/bytes.rs
+++ b/schemars/tests/integration/bytes.rs
@@ -6,19 +6,10 @@ fn bytes() {
     test!(Bytes)
         .assert_snapshot()
         .assert_allows_ser_roundtrip([Bytes::new(), Bytes::from_iter([12; 34])])
-        .assert_matches_de_roundtrip(arbitrary_values_except(
-            is_array_of_u64,
-            "FIXME schema allows out-of-range positive integers",
-        ));
+        .assert_matches_de_roundtrip(arbitrary_values());
 }
 
 #[test]
 fn bytes_mut() {
     test!(BytesMut).assert_identical::<Bytes>();
 }
-
-fn is_array_of_u64(value: &Value) -> bool {
-    value
-        .as_array()
-        .is_some_and(|a| a.iter().all(Value::is_u64))
-}
diff --git a/schemars/tests/integration/snapshots/schemars/tests/integration/bytes.rs~bytes.de.json b/schemars/tests/integration/snapshots/schemars/tests/integration/bytes.rs~bytes.de.json
index 65cbb8a..108de27 100644
--- a/schemars/tests/integration/snapshots/schemars/tests/integration/bytes.rs~bytes.de.json
+++ b/schemars/tests/integration/snapshots/schemars/tests/integration/bytes.rs~bytes.de.json
@@ -8,6 +8,7 @@
   "items": {
     "type": "integer",
     "format": "uint8",
-    "minimum": 0
+    "minimum": 0,
+    "maximum": 255
   }
 }
\ No newline at end of file
diff --git a/schemars/tests/integration/snapshots/schemars/tests/integration/bytes.rs~bytes.ser.json b/schemars/tests/integration/snapshots/schemars/tests/integration/bytes.rs~bytes.ser.json
index f54610b..c011c5c 100644
--- a/schemars/tests/integration/snapshots/schemars/tests/integration/bytes.rs~bytes.ser.json
+++ b/schemars/tests/integration/snapshots/schemars/tests/integration/bytes.rs~bytes.ser.json
@@ -5,6 +5,7 @@
   "items": {
     "type": "integer",
     "format": "uint8",
-    "minimum": 0
+    "minimum": 0,
+    "maximum": 255
   }
 }
\ No newline at end of file
diff --git a/schemars/tests/integration/snapshots/schemars/tests/integration/default.rs~default_fields.de.json b/schemars/tests/integration/snapshots/schemars/tests/integration/default.rs~default_fields.de.json
index e3168e2..68805b7 100644
--- a/schemars/tests/integration/snapshots/schemars/tests/integration/default.rs~default_fields.de.json
+++ b/schemars/tests/integration/snapshots/schemars/tests/integration/default.rs~default_fields.de.json
@@ -36,7 +36,9 @@
   "$defs": {
     "NotSerialize": {
       "type": "integer",
-      "format": "int8"
+      "format": "int8",
+      "minimum": -128,
+      "maximum": 127
     }
   }
 }
\ No newline at end of file
diff --git a/schemars/tests/integration/snapshots/schemars/tests/integration/remote_derive.rs~type_param.json b/schemars/tests/integration/snapshots/schemars/tests/integration/remote_derive.rs~type_param.json
index 48568fd..2a21dc4 100644
--- a/schemars/tests/integration/snapshots/schemars/tests/integration/remote_derive.rs~type_param.json
+++ b/schemars/tests/integration/snapshots/schemars/tests/integration/remote_derive.rs~type_param.json
@@ -20,7 +20,8 @@
         {
           "type": "integer",
           "format": "uint8",
-          "minimum": 0
+          "minimum": 0,
+          "maximum": 255
         },
         {
           "type": "boolean"
diff --git a/schemars/tests/integration/snapshots/schemars/tests/integration/settings.rs~draft07.de.json b/schemars/tests/integration/snapshots/schemars/tests/integration/settings.rs~draft07.de.json
index a6b613e..67371c7 100644
--- a/schemars/tests/integration/snapshots/schemars/tests/integration/settings.rs~draft07.de.json
+++ b/schemars/tests/integration/snapshots/schemars/tests/integration/settings.rs~draft07.de.json
@@ -42,7 +42,8 @@
           {
             "type": "integer",
             "format": "uint8",
-            "minimum": 0
+            "minimum": 0,
+            "maximum": 255
           },
           {
             "type": "integer",
diff --git a/schemars/tests/integration/snapshots/schemars/tests/integration/settings.rs~draft07.ser.json b/schemars/tests/integration/snapshots/schemars/tests/integration/settings.rs~draft07.ser.json
index d76d990..88e330d 100644
--- a/schemars/tests/integration/snapshots/schemars/tests/integration/settings.rs~draft07.ser.json
+++ b/schemars/tests/integration/snapshots/schemars/tests/integration/settings.rs~draft07.ser.json
@@ -42,7 +42,8 @@
           {
             "type": "integer",
             "format": "uint8",
-            "minimum": 0
+            "minimum": 0,
+            "maximum": 255
           },
           {
             "type": "integer",
diff --git a/schemars/tests/integration/snapshots/schemars/tests/integration/settings.rs~draft2019_09.de.json b/schemars/tests/integration/snapshots/schemars/tests/integration/settings.rs~draft2019_09.de.json
index f704755..df05691 100644
--- a/schemars/tests/integration/snapshots/schemars/tests/integration/settings.rs~draft2019_09.de.json
+++ b/schemars/tests/integration/snapshots/schemars/tests/integration/settings.rs~draft2019_09.de.json
@@ -42,7 +42,8 @@
           {
             "type": "integer",
             "format": "uint8",
-            "minimum": 0
+            "minimum": 0,
+            "maximum": 255
           },
           {
             "type": "integer",
diff --git a/schemars/tests/integration/snapshots/schemars/tests/integration/settings.rs~draft2019_09.ser.json b/schemars/tests/integration/snapshots/schemars/tests/integration/settings.rs~draft2019_09.ser.json
index 41c3237..6e2fdc2 100644
--- a/schemars/tests/integration/snapshots/schemars/tests/integration/settings.rs~draft2019_09.ser.json
+++ b/schemars/tests/integration/snapshots/schemars/tests/integration/settings.rs~draft2019_09.ser.json
@@ -42,7 +42,8 @@
           {
             "type": "integer",
             "format": "uint8",
-            "minimum": 0
+            "minimum": 0,
+            "maximum": 255
           },
           {
             "type": "integer",
diff --git a/schemars/tests/integration/snapshots/schemars/tests/integration/settings.rs~draft2020_12.de.json b/schemars/tests/integration/snapshots/schemars/tests/integration/settings.rs~draft2020_12.de.json
index 1f51161..f378ab9 100644
--- a/schemars/tests/integration/snapshots/schemars/tests/integration/settings.rs~draft2020_12.de.json
+++ b/schemars/tests/integration/snapshots/schemars/tests/integration/settings.rs~draft2020_12.de.json
@@ -40,7 +40,8 @@
           {
             "type": "integer",
             "format": "uint8",
-            "minimum": 0
+            "minimum": 0,
+            "maximum": 255
           },
           {
             "type": "integer",
diff --git a/schemars/tests/integration/snapshots/schemars/tests/integration/settings.rs~draft2020_12.ser.json b/schemars/tests/integration/snapshots/schemars/tests/integration/settings.rs~draft2020_12.ser.json
index a200584..39782a4 100644
--- a/schemars/tests/integration/snapshots/schemars/tests/integration/settings.rs~draft2020_12.ser.json
+++ b/schemars/tests/integration/snapshots/schemars/tests/integration/settings.rs~draft2020_12.ser.json
@@ -40,7 +40,8 @@
           {
             "type": "integer",
             "format": "uint8",
-            "minimum": 0
+            "minimum": 0,
+            "maximum": 255
           },
           {
             "type": "integer",
diff --git a/schemars/tests/integration/snapshots/schemars/tests/integration/settings.rs~openapi3.de.json b/schemars/tests/integration/snapshots/schemars/tests/integration/settings.rs~openapi3.de.json
index 0a93bee..885f815 100644
--- a/schemars/tests/integration/snapshots/schemars/tests/integration/settings.rs~openapi3.de.json
+++ b/schemars/tests/integration/snapshots/schemars/tests/integration/settings.rs~openapi3.de.json
@@ -40,7 +40,8 @@
           {
             "type": "integer",
             "format": "uint8",
-            "minimum": 0
+            "minimum": 0,
+            "maximum": 255
           },
           {
             "type": "integer",
diff --git a/schemars/tests/integration/snapshots/schemars/tests/integration/settings.rs~openapi3.ser.json b/schemars/tests/integration/snapshots/schemars/tests/integration/settings.rs~openapi3.ser.json
index 0401add..395f935 100644
--- a/schemars/tests/integration/snapshots/schemars/tests/integration/settings.rs~openapi3.ser.json
+++ b/schemars/tests/integration/snapshots/schemars/tests/integration/settings.rs~openapi3.ser.json
@@ -40,7 +40,8 @@
           {
             "type": "integer",
             "format": "uint8",
-            "minimum": 0
+            "minimum": 0,
+            "maximum": 255
           },
           {
             "type": "integer",