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;
88use warp:: http:: StatusCode ;
99use warp:: hyper:: body:: Bytes ;
1010
11- use crate :: yaml_to_serde :: convert_yaml_to_serde_json ;
11+ use crate :: spec_parser :: OpenApiSpecParser ;
1212
1313#[ cfg( test) ]
1414pub ( crate ) const DEFAULT_SPEC_FILE : & str = "../../../openapi.yaml" ;
@@ -56,12 +56,8 @@ impl<'a> APISpec<'a> {
5656
5757 /// APISpec factory from spec
5858 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 ( ) ;
6359 APISpec {
64- openapi : convert_yaml_to_serde_json ( & openapi [ 0 ] ) . unwrap ( ) ,
60+ openapi : OpenApiSpecParser :: parse_yaml ( path ) . unwrap ( ) ,
6561 path : None ,
6662 method : None ,
6763 content_type : Some ( "application/json" ) ,
Original file line number Diff line number Diff line change 22//! This crate provides a toolset to verify conformity of http routes against an Open Api specification.
33
44mod apispec;
5- mod yaml_to_serde ;
5+ pub ( crate ) mod spec_parser ;
66
77pub use apispec:: * ;
88
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