1515// SPDX-License-Identifier: Apache-2.0
1616//
1717
18+ #[ cfg( feature = "openrpc_validation" ) ]
1819use openrpc_validator:: jsonschema:: JSONSchema ;
20+ #[ cfg( feature = "openrpc_validation" ) ]
21+ use openrpc_validator:: { FireboltOpenRpc as FireboltOpenRpcValidator , RpcMethodValidator } ;
22+
23+ #[ cfg( not( feature = "openrpc_validation" ) ) ]
24+ pub mod openrpc_validator {
25+ pub mod jsonschema {
26+ #[ derive( Debug , Clone ) ]
27+ pub struct JSONSchema ;
28+
29+ impl JSONSchema {
30+ pub fn validate ( & self , _value : & serde_json:: Value ) -> Result < ( ) , Vec < String > > {
31+ Ok ( ( ) )
32+ }
33+ }
34+ }
35+
36+ #[ derive( Debug , Clone ) ]
37+ pub struct RpcMethodValidator ;
38+
39+ impl RpcMethodValidator {
40+ pub fn new ( ) -> Self {
41+ RpcMethodValidator
42+ }
43+
44+ pub fn add_schema ( & mut self , _schema : super :: FireboltOpenRpcValidator ) {
45+ // No-op when validation is disabled
46+ }
47+
48+ pub fn get_method ( & self , _name : & str ) -> Option < super :: RpcMethod > {
49+ None
50+ }
51+
52+ pub fn get_closest_result_properties_schema (
53+ & self ,
54+ _name : & str ,
55+ _sample_map : & serde_json:: Map < String , serde_json:: Value > ,
56+ ) -> Option < serde_json:: Map < String , serde_json:: Value > > {
57+ None
58+ }
59+
60+ pub fn get_result_ref_schema (
61+ & self ,
62+ _reference_path : & str ,
63+ ) -> Option < serde_json:: Map < String , serde_json:: Value > > {
64+ None
65+ }
66+
67+ pub fn params_validator (
68+ & self ,
69+ _version : String ,
70+ _method : & str ,
71+ ) -> Result < jsonschema:: JSONSchema , super :: ValidationError > {
72+ Ok ( jsonschema:: JSONSchema )
73+ }
74+ }
75+
76+ #[ derive( Debug , Clone ) ]
77+ pub struct FireboltOpenRpcValidator ;
78+
79+ #[ derive( Debug ) ]
80+ pub enum ValidationError {
81+ SpecVersionNotFound ,
82+ }
83+
84+ #[ derive( Debug , Clone ) ]
85+ pub struct RpcMethod {
86+ pub name : String ,
87+ }
88+ }
89+
90+ #[ cfg( not( feature = "openrpc_validation" ) ) ]
91+ use openrpc_validator:: jsonschema:: JSONSchema ;
92+ #[ cfg( not( feature = "openrpc_validation" ) ) ]
93+ use openrpc_validator:: { RpcMethod , RpcMethodValidator , ValidationError } ;
94+ #[ cfg( not( feature = "openrpc_validation" ) ) ]
95+ type FireboltOpenRpcValidator = openrpc_validator:: FireboltOpenRpcValidator ;
96+
1997use ripple_sdk:: log:: { debug, error, info} ;
2098use ripple_sdk:: { api:: firebolt:: fb_openrpc:: CapabilityPolicy , serde_json} ;
2199use ripple_sdk:: {
@@ -38,8 +116,6 @@ use std::{
38116 sync:: { Arc , RwLock } ,
39117} ;
40118
41- use openrpc_validator:: { FireboltOpenRpc as FireboltOpenRpcValidator , RpcMethodValidator } ;
42-
43119#[ derive( Debug , Clone ) ]
44120pub enum ApiSurface {
45121 Firebolt ,
@@ -77,6 +153,7 @@ pub struct OpenRpcState {
77153 provider_relation_map : Arc < RwLock < HashMap < String , ProviderRelationSet > > > ,
78154 openrpc_validator : Arc < RwLock < RpcMethodValidator > > ,
79155 provider_registrations : Arc < Vec < String > > ,
156+ #[ cfg( feature = "openrpc_validation" ) ]
80157 json_schema_cache : Arc < RwLock < HashMap < String , JSONSchema > > > ,
81158}
82159
@@ -125,10 +202,19 @@ impl OpenRpcState {
125202 . expect ( "Failed parsing FireboltVersionManifest from open RPC file" ) ;
126203 let firebolt_open_rpc: FireboltOpenRpc = version_manifest. clone ( ) . into ( ) ;
127204 let ripple_open_rpc: FireboltOpenRpc = FireboltOpenRpc :: default ( ) ;
128- let openrpc_validator: FireboltOpenRpcValidator = serde_json:: from_str ( & open_rpc_path)
129- . expect ( "Failed parsing FireboltOpenRpcValidator from open RPC file" ) ;
130- let mut rpc_method_validator = RpcMethodValidator :: new ( ) ;
131- rpc_method_validator. add_schema ( openrpc_validator) ;
205+
206+ #[ cfg( feature = "openrpc_validation" ) ]
207+ let rpc_method_validator = {
208+ let openrpc_validator: FireboltOpenRpcValidator = serde_json:: from_str ( & open_rpc_path)
209+ . expect ( "Failed parsing FireboltOpenRpcValidator from open RPC file" ) ;
210+ let mut validator = RpcMethodValidator :: new ( ) ;
211+ validator. add_schema ( openrpc_validator) ;
212+ validator
213+ } ;
214+
215+ #[ cfg( not( feature = "openrpc_validation" ) ) ]
216+ let rpc_method_validator = RpcMethodValidator :: new ( ) ;
217+
132218 let v = OpenRpcState {
133219 firebolt_cap_map : Arc :: new ( RwLock :: new ( firebolt_open_rpc. get_methods_caps ( ) ) ) ,
134220 ripple_cap_map : Arc :: new ( RwLock :: new ( ripple_open_rpc. get_methods_caps ( ) ) ) ,
@@ -139,6 +225,7 @@ impl OpenRpcState {
139225 provider_relation_map : Arc :: new ( RwLock :: new ( HashMap :: new ( ) ) ) ,
140226 openrpc_validator : Arc :: new ( RwLock :: new ( rpc_method_validator) ) ,
141227 provider_registrations : Arc :: new ( provider_registrations) ,
228+ #[ cfg( feature = "openrpc_validation" ) ]
142229 json_schema_cache : Arc :: new ( RwLock :: new ( HashMap :: new ( ) ) ) ,
143230 } ;
144231 v. build_provider_relation_sets ( & firebolt_open_rpc. methods ) ;
@@ -172,21 +259,29 @@ impl OpenRpcState {
172259
173260 // Add extension open rpc to the validator
174261 pub fn add_extension_open_rpc_to_validator ( & self , path : String ) -> Result < ( ) , RippleError > {
175- let extension_open_rpc_string = load_extension_open_rpc ( path) ;
176- if let Some ( open_rpc) = extension_open_rpc_string {
177- return match serde_json:: from_str :: < FireboltOpenRpcValidator > ( & open_rpc) {
178- Ok ( additional_open_rpc_validator) => {
179- let mut validator = self . openrpc_validator . write ( ) . unwrap ( ) ;
180- validator. add_schema ( additional_open_rpc_validator) ;
181- return Ok ( ( ) ) ;
182- }
183- Err ( e) => {
184- error ! ( "Error parsing openrpc validator from e={:?}" , e) ;
185- Err ( RippleError :: ParseError )
186- }
262+ #[ cfg( feature = "openrpc_validation" ) ]
263+ {
264+ let extension_open_rpc_string = load_extension_open_rpc ( path) ;
265+ if let Some ( open_rpc) = extension_open_rpc_string {
266+ return match serde_json:: from_str :: < FireboltOpenRpcValidator > ( & open_rpc) {
267+ Ok ( additional_open_rpc_validator) => {
268+ let mut validator = self . openrpc_validator . write ( ) . unwrap ( ) ;
269+ validator. add_schema ( additional_open_rpc_validator) ;
270+ return Ok ( ( ) ) ;
271+ }
272+ Err ( e) => {
273+ error ! ( "Error parsing openrpc validator from e={:?}" , e) ;
274+ Err ( RippleError :: ParseError )
275+ }
276+ } ;
187277 } ;
188- } ;
189- Err ( RippleError :: ParseError )
278+ Err ( RippleError :: ParseError )
279+ }
280+ #[ cfg( not( feature = "openrpc_validation" ) ) ]
281+ {
282+ let _ = path; // Suppress unused variable warning
283+ Ok ( ( ) ) // No-op when validation is disabled
284+ }
190285 }
191286
192287 pub fn is_excluded ( & self , method : String , app_id : String ) -> bool {
@@ -434,11 +529,18 @@ impl OpenRpcState {
434529 . extend ( provider_relation_sets)
435530 }
436531
532+ #[ cfg( feature = "openrpc_validation" ) ]
437533 pub fn add_json_schema_cache ( & self , method : String , schema : JSONSchema ) {
438534 let mut json_cache = self . json_schema_cache . write ( ) . unwrap ( ) ;
439535 let _ = json_cache. insert ( method, schema) ;
440536 }
441537
538+ #[ cfg( not( feature = "openrpc_validation" ) ) ]
539+ pub fn add_json_schema_cache ( & self , method : String , schema : JSONSchema ) {
540+ let _ = ( method, schema) ; // Suppress unused variable warnings
541+ }
542+
543+ #[ cfg( feature = "openrpc_validation" ) ]
442544 pub fn validate_schema ( & self , method : & str , value : & Value ) -> Result < ( ) , Option < String > > {
443545 let json_cache = self . json_schema_cache . read ( ) . unwrap ( ) ;
444546 if let Some ( schema) = json_cache. get ( method) {
@@ -455,6 +557,12 @@ impl OpenRpcState {
455557 Err ( None )
456558 }
457559 }
560+
561+ #[ cfg( not( feature = "openrpc_validation" ) ) ]
562+ pub fn validate_schema ( & self , method : & str , value : & Value ) -> Result < ( ) , Option < String > > {
563+ let _ = ( method, value) ; // Suppress unused variable warnings
564+ Err ( None ) // Always return "not found" when validation is disabled
565+ }
458566}
459567
460568fn load_firebolt_open_rpc_from_file ( fb_open_rpc_file : & str ) -> Result < String , RippleError > {
@@ -516,6 +624,7 @@ fn load_firebolt_open_rpc_path() -> Result<String, RippleError> {
516624 load_firebolt_open_rpc_from_file ( "/etc/ripple/openrpc/firebolt-open-rpc.json" )
517625}
518626
627+ #[ allow( dead_code) ]
519628fn load_extension_open_rpc ( path : String ) -> Option < String > {
520629 match std:: fs:: read_to_string ( & path) {
521630 Ok ( content) => {
0 commit comments