@@ -2,6 +2,7 @@ use oas3::spec::{Operation, PathItem, Response};
22
33pub type SpecResult < T > = Result < T , Box < dyn std:: error:: Error > > ;
44
5+ /// HTTP methods
56pub enum Method {
67 Get ,
78 Post ,
@@ -29,10 +30,43 @@ impl From<&str> for Method {
2930 }
3031}
3132
33+ /// Load an OpenAPI spec from a file
34+ ///
35+ /// # Arguments
36+ /// * `path` - Path to the OpenAPI spec file
37+ ///
38+ /// # Returns
39+ /// An OpenAPI spec object
40+ ///
41+ /// # Example
42+ /// ```
43+ /// use openapi_mocker::spec::load_spec;
44+ ///
45+ /// let spec = load_spec("tests/testdata/petstore.yaml");
46+ /// assert_eq!(spec.openapi, "3.0.0");
47+ /// ```
3248pub fn load_spec ( path : & str ) -> oas3:: OpenApiV3Spec {
3349 oas3:: from_path ( path) . unwrap ( )
3450}
3551
52+ /// Load an endpoint from an OpenAPI spec
53+ ///
54+ /// # Arguments
55+ /// * `spec` - OpenAPI spec object
56+ /// * `path` - Path to the endpoint
57+ /// * `method` - HTTP method
58+ ///
59+ /// # Returns
60+ /// An OpenAPI operation object
61+ ///
62+ /// # Example
63+ /// ```
64+ /// use openapi_mocker::spec::{load_spec, load_endpoint, Method};
65+ ///
66+ /// let spec = load_spec("tests/testdata/petstore.yaml");
67+ /// let op = load_endpoint(&spec, "/pets", Method::Get).unwrap();
68+ /// assert_eq!(op.operation_id, Some("listPets".to_string()));
69+ /// ```
3670pub fn load_endpoint (
3771 spec : & oas3:: OpenApiV3Spec ,
3872 path : & str ,
@@ -46,6 +80,14 @@ pub fn load_endpoint(
4680 Ok ( op. clone ( ) )
4781}
4882
83+ /// Load a method from a PathItem
84+ ///
85+ /// # Arguments
86+ /// * `method` - HTTP method
87+ /// * `path_item` - PathItem object
88+ ///
89+ /// # Returns
90+ /// An Option with the Operation object
4991fn load_method < ' a > ( method : Method ) -> impl Fn ( & PathItem ) -> Option < & Operation > + ' a {
5092 move |path_item : & PathItem | match method {
5193 Method :: Get => path_item. get . as_ref ( ) ,
@@ -59,6 +101,25 @@ fn load_method<'a>(method: Method) -> impl Fn(&PathItem) -> Option<&Operation> +
59101 }
60102}
61103
104+ /// Load a response from an OpenAPI operation
105+ ///
106+ /// # Arguments
107+ /// * `spec` - OpenAPI spec object
108+ /// * `op` - OpenAPI operation object
109+ /// * `status` - HTTP status code
110+ ///
111+ /// # Returns
112+ /// An OpenAPI response object
113+ ///
114+ /// # Example
115+ /// ```
116+ /// use openapi_mocker::spec::{load_spec, load_endpoint, load_response, Method};
117+ ///
118+ /// let spec = load_spec("tests/testdata/petstore.yaml");
119+ /// let op = load_endpoint(&spec, "/pets", Method::Get).unwrap();
120+ /// let response = load_response(&spec, &op, 200).unwrap();
121+ /// assert_eq!(response.description, Some("A paged array of pets".to_string()));
122+ /// ```
62123pub fn load_response (
63124 spec : & oas3:: OpenApiV3Spec ,
64125 op : & Operation ,
@@ -73,6 +134,40 @@ pub fn load_response(
73134 }
74135}
75136
137+ /// Load an example from an OpenAPI response
138+ ///
139+ /// # Arguments
140+ /// * `spec` - OpenAPI spec object
141+ /// * `response` - OpenAPI response object
142+ /// * `content_type` - Content type
143+ ///
144+ /// # Returns
145+ /// A JSON value with the example
146+ ///
147+ /// # Example
148+ /// ```
149+ /// use openapi_mocker::spec::{load_spec, load_endpoint, load_response, load_example, Method};
150+ /// use serde_json::json;
151+ ///
152+ /// let spec = load_spec("tests/testdata/petstore.yaml");
153+ /// let op = load_endpoint(&spec, "/pets", Method::Get).unwrap();
154+ /// let response = load_response(&spec, &op, 200).unwrap();
155+ /// let content_type = "application/json";
156+ /// let example = load_example(&spec, &response, content_type).unwrap();
157+ /// let expected = json!([
158+ /// {
159+ /// "id": 1,
160+ /// "name": "doggie",
161+ /// "tag": "dog"
162+ /// },
163+ /// {
164+ /// "id": 2,
165+ /// "name": "kitty",
166+ /// "tag": "cat"
167+ /// }
168+ /// ]);
169+ /// assert_eq!(example, expected);
170+ /// ```
76171pub fn load_example (
77172 spec : & oas3:: OpenApiV3Spec ,
78173 response : & Response ,
0 commit comments