File tree Expand file tree Collapse file tree 4 files changed +27
-7
lines changed
internal/tests/mithril-api-spec/src Expand file tree Collapse file tree 4 files changed +27
-7
lines changed Original file line number Diff line number Diff line change @@ -8,7 +8,7 @@ use warp::http::Response;
8
8
use warp:: http:: StatusCode ;
9
9
use warp:: hyper:: body:: Bytes ;
10
10
11
- use crate :: yaml_to_serde :: convert_yaml_to_serde_json ;
11
+ use crate :: spec_parser :: OpenApiSpecParser ;
12
12
13
13
#[ cfg( test) ]
14
14
pub ( crate ) const DEFAULT_SPEC_FILE : & str = "../../../openapi.yaml" ;
@@ -56,12 +56,8 @@ impl<'a> APISpec<'a> {
56
56
57
57
/// APISpec factory from spec
58
58
pub fn from_file ( path : & str ) -> APISpec < ' a > {
59
- use saphyr:: LoadableYamlNode ;
60
-
61
- let yaml_spec = std:: fs:: read_to_string ( path) . unwrap ( ) ;
62
- let openapi = saphyr:: Yaml :: load_from_str ( & yaml_spec) . unwrap ( ) ;
63
59
APISpec {
64
- openapi : convert_yaml_to_serde_json ( & openapi [ 0 ] ) . unwrap ( ) ,
60
+ openapi : OpenApiSpecParser :: parse_yaml ( path ) . unwrap ( ) ,
65
61
path : None ,
66
62
method : None ,
67
63
content_type : Some ( "application/json" ) ,
Original file line number Diff line number Diff line change 2
2
//! This crate provides a toolset to verify conformity of http routes against an Open Api specification.
3
3
4
4
mod apispec;
5
- mod yaml_to_serde ;
5
+ pub ( crate ) mod spec_parser ;
6
6
7
7
pub use apispec:: * ;
8
8
Original file line number Diff line number Diff line change
1
+ mod yaml;
2
+
3
+ /// Parser for OpenApi Specification
4
+ ///
5
+ /// Returns the parsed spec as a [serde_json::Value] as this crate uses a jsonschema validator
6
+ /// to do the validation.
7
+ pub ( crate ) struct OpenApiSpecParser ;
8
+
9
+ impl OpenApiSpecParser {
10
+ pub ( crate ) fn parse_yaml ( spec_path : & str ) -> Result < serde_json:: Value , String > {
11
+ use saphyr:: LoadableYamlNode ;
12
+
13
+ let yaml_spec = std:: fs:: read_to_string ( spec_path)
14
+ . map_err ( |e| format ! ( "Could not read spec file `{spec_path}`: {e}" ) ) ?;
15
+ let openapi = saphyr:: Yaml :: load_from_str ( & yaml_spec)
16
+ . map_err ( |e| format ! ( "Could not parse spec file `{spec_path}`: {e}" ) ) ?;
17
+
18
+ if openapi. is_empty ( ) {
19
+ Err ( "No spec found in file" . to_string ( ) )
20
+ } else {
21
+ yaml:: convert_yaml_to_serde_json ( & openapi[ 0 ] )
22
+ }
23
+ }
24
+ }
File renamed without changes.
You can’t perform that action at this time.
0 commit comments