@@ -995,3 +995,85 @@ mod impl_serde {
995995 "invalid value: floating point `NaN`, expected float (but not NaN)" ) ;
996996 }
997997}
998+ #[ cfg( all( feature = "std" , feature = "schemars" ) ) ]
999+ mod impl_schemars {
1000+ extern crate schemars;
1001+ use super :: { OrderedFloat , NotNan } ;
1002+ use self :: schemars:: schema:: { Schema , InstanceType , SchemaObject } ;
1003+ use self :: schemars:: gen:: SchemaGenerator ;
1004+
1005+ macro_rules! primitive_float_impl {
1006+ ( $type: ty, $schema_name: literal) => {
1007+ impl schemars:: JsonSchema for $type {
1008+ fn is_referenceable( ) -> bool {
1009+ false
1010+ }
1011+
1012+ fn schema_name( ) -> std:: string:: String {
1013+ std:: string:: String :: from( $schema_name)
1014+ }
1015+
1016+ fn json_schema( _: & mut SchemaGenerator ) -> Schema {
1017+ SchemaObject {
1018+ instance_type: Some ( InstanceType :: Number . into( ) ) ,
1019+ format: Some ( std:: string:: String :: from( $schema_name) ) ,
1020+ ..Default :: default ( )
1021+ }
1022+ . into( )
1023+ }
1024+ }
1025+ }
1026+ }
1027+
1028+ primitive_float_impl ! ( OrderedFloat <f32 >, "float" ) ;
1029+ primitive_float_impl ! ( OrderedFloat <f64 >, "double" ) ;
1030+ primitive_float_impl ! ( NotNan <f32 >, "float" ) ;
1031+ primitive_float_impl ! ( NotNan <f64 >, "double" ) ;
1032+
1033+ #[ test]
1034+ fn schema_generation_does_not_panic_for_common_floats ( ) {
1035+ {
1036+ let schema = schemars:: gen:: SchemaGenerator :: default ( ) . into_root_schema_for :: < OrderedFloat < f32 > > ( ) ;
1037+ assert_eq ! ( schema. schema. instance_type, Some ( schemars:: schema:: SingleOrVec :: Single ( std:: boxed:: Box :: new( schemars:: schema:: InstanceType :: Number ) ) ) ) ;
1038+ assert_eq ! ( schema. schema. metadata. unwrap( ) . title. unwrap( ) , std:: string:: String :: from( "float" ) ) ;
1039+ }
1040+ {
1041+ let schema = schemars:: gen:: SchemaGenerator :: default ( ) . into_root_schema_for :: < OrderedFloat < f64 > > ( ) ;
1042+ assert_eq ! ( schema. schema. instance_type, Some ( schemars:: schema:: SingleOrVec :: Single ( std:: boxed:: Box :: new( schemars:: schema:: InstanceType :: Number ) ) ) ) ;
1043+ assert_eq ! ( schema. schema. metadata. unwrap( ) . title. unwrap( ) , std:: string:: String :: from( "double" ) ) ;
1044+ }
1045+ {
1046+ let schema = schemars:: gen:: SchemaGenerator :: default ( ) . into_root_schema_for :: < NotNan < f32 > > ( ) ;
1047+ assert_eq ! ( schema. schema. instance_type, Some ( schemars:: schema:: SingleOrVec :: Single ( std:: boxed:: Box :: new( schemars:: schema:: InstanceType :: Number ) ) ) ) ;
1048+ assert_eq ! ( schema. schema. metadata. unwrap( ) . title. unwrap( ) , std:: string:: String :: from( "float" ) ) ;
1049+ }
1050+ {
1051+ let schema = schemars:: gen:: SchemaGenerator :: default ( ) . into_root_schema_for :: < NotNan < f64 > > ( ) ;
1052+ assert_eq ! ( schema. schema. instance_type, Some ( schemars:: schema:: SingleOrVec :: Single ( std:: boxed:: Box :: new( schemars:: schema:: InstanceType :: Number ) ) ) ) ;
1053+ assert_eq ! ( schema. schema. metadata. unwrap( ) . title. unwrap( ) , std:: string:: String :: from( "double" ) ) ;
1054+ }
1055+ }
1056+ #[ test]
1057+ fn ordered_float_schema_match_primitive_schema ( ) {
1058+ {
1059+ let of_schema = schemars:: gen:: SchemaGenerator :: default ( ) . into_root_schema_for :: < OrderedFloat < f32 > > ( ) ;
1060+ let prim_schema = schemars:: gen:: SchemaGenerator :: default ( ) . into_root_schema_for :: < f32 > ( ) ;
1061+ assert_eq ! ( of_schema, prim_schema) ;
1062+ }
1063+ {
1064+ let of_schema = schemars:: gen:: SchemaGenerator :: default ( ) . into_root_schema_for :: < OrderedFloat < f64 > > ( ) ;
1065+ let prim_schema = schemars:: gen:: SchemaGenerator :: default ( ) . into_root_schema_for :: < f64 > ( ) ;
1066+ assert_eq ! ( of_schema, prim_schema) ;
1067+ }
1068+ {
1069+ let of_schema = schemars:: gen:: SchemaGenerator :: default ( ) . into_root_schema_for :: < NotNan < f32 > > ( ) ;
1070+ let prim_schema = schemars:: gen:: SchemaGenerator :: default ( ) . into_root_schema_for :: < f32 > ( ) ;
1071+ assert_eq ! ( of_schema, prim_schema) ;
1072+ }
1073+ {
1074+ let of_schema = schemars:: gen:: SchemaGenerator :: default ( ) . into_root_schema_for :: < NotNan < f64 > > ( ) ;
1075+ let prim_schema = schemars:: gen:: SchemaGenerator :: default ( ) . into_root_schema_for :: < f64 > ( ) ;
1076+ assert_eq ! ( of_schema, prim_schema) ;
1077+ }
1078+ }
1079+ }
0 commit comments