diff --git a/Cargo.lock b/Cargo.lock index a4fd13ba..d4ecbf58 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -685,7 +685,7 @@ dependencies = [ "rust-embed", "serde", "serde_json", - "serde_yaml", + "serde_yaml 0.8.15", "shellexpand", "structopt", "tokei", @@ -780,6 +780,7 @@ name = "coco_swagger" version = "0.1.0" dependencies = [ "core_model", + "openapi", ] [[package]] @@ -859,7 +860,7 @@ dependencies = [ "lazy_static", "serde", "serde_json", - "serde_yaml", + "serde_yaml 0.8.15", "url", ] @@ -1286,6 +1287,15 @@ dependencies = [ "serde", ] +[[package]] +name = "error-chain" +version = "0.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d9435d864e017c3c6afeac1654189b06cdb491cf2ff73dbf0d73b0f292f42ff8" +dependencies = [ + "backtrace", +] + [[package]] name = "error-chain" version = "0.12.4" @@ -2383,6 +2393,19 @@ version = "0.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "624a8340c38c1b80fd549087862da4ba43e08858af025b236e509b6649fc13d5" +[[package]] +name = "openapi" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "739caefcf8a5f4638175df8971aba0be5f4651b58d4a217c36f5dd665f83ea00" +dependencies = [ + "error-chain 0.10.0", + "serde", + "serde_derive", + "serde_json", + "serde_yaml 0.7.5", +] + [[package]] name = "openssl" version = "0.10.32" @@ -3194,6 +3217,18 @@ dependencies = [ "serde", ] +[[package]] +name = "serde_yaml" +version = "0.7.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ef8099d3df28273c99a1728190c7a9f19d444c941044f64adf986bee7ec53051" +dependencies = [ + "dtoa", + "linked-hash-map", + "serde", + "yaml-rust", +] + [[package]] name = "serde_yaml" version = "0.8.15" @@ -3274,7 +3309,7 @@ checksum = "7a6deb8efaf3ad8fd784139db8bbd51806bfbcee87c7be7578e9c930981fb808" dependencies = [ "bytecount", "cargo_metadata 0.10.0", - "error-chain", + "error-chain 0.12.4", "glob", "pulldown-cmark", "tempfile", diff --git a/_fixtures/swagger/petstore.yaml b/_fixtures/swagger/petstore.yaml new file mode 100644 index 00000000..9c484cf3 --- /dev/null +++ b/_fixtures/swagger/petstore.yaml @@ -0,0 +1,109 @@ +openapi: "3.0.0" +info: + version: 1.0.0 + title: Swagger Petstore + license: + name: MIT +servers: + - url: http://petstore.swagger.io/v1 +paths: + /pets: + get: + summary: List all pets + operationId: listPets + tags: + - pets + parameters: + - name: limit + in: query + description: How many items to return at one time (max 100) + required: false + schema: + type: integer + format: int32 + responses: + '200': + description: An paged array of pets + headers: + x-next: + description: A link to the next page of responses + schema: + type: string + content: + application/json: + schema: + $ref: "#/components/schemas/Pets" + default: + description: unexpected error + content: + application/json: + schema: + $ref: "#/components/schemas/Error" + post: + summary: Create a pet + operationId: createPets + tags: + - pets + responses: + '201': + description: Null response + default: + description: unexpected error + content: + application/json: + schema: + $ref: "#/components/schemas/Error" + /pets/{petId}: + get: + summary: Info for a specific pet + operationId: showPetById + tags: + - pets + parameters: + - name: petId + in: path + required: true + description: The id of the pet to retrieve + schema: + type: string + responses: + '200': + description: Expected response to a valid request + content: + application/json: + schema: + $ref: "#/components/schemas/Pets" + default: + description: unexpected error + content: + application/json: + schema: + $ref: "#/components/schemas/Error" +components: + schemas: + Pet: + required: + - id + - name + properties: + id: + type: integer + format: int64 + name: + type: string + tag: + type: string + Pets: + type: array + items: + $ref: "#/components/schemas/Pet" + Error: + required: + - code + - message + properties: + code: + type: integer + format: int32 + message: + type: string diff --git a/plugins/coco_swagger/Cargo.toml b/plugins/coco_swagger/Cargo.toml index f013f553..e3c0d007 100644 --- a/plugins/coco_swagger/Cargo.toml +++ b/plugins/coco_swagger/Cargo.toml @@ -7,6 +7,7 @@ edition = "2018" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] +openapi = "0.1.5" [dependencies.core_model] path = "../../core_model" diff --git a/plugins/coco_swagger/src/coco_swagger_plugin.rs b/plugins/coco_swagger/src/coco_swagger_plugin.rs new file mode 100644 index 00000000..5edf7b71 --- /dev/null +++ b/plugins/coco_swagger/src/coco_swagger_plugin.rs @@ -0,0 +1,39 @@ +extern crate openapi; + +use std::path::Path; + +pub fn analysis(path: &Path) { + match openapi::from_path(path) { + Ok(spec) => println!("spec: {:?}", spec), + Err(err) => println!("error: {}", err), + } +} + +#[cfg(test)] +mod tests { + use crate::coco_swagger_plugin::analysis; + use std::path::PathBuf; + + pub fn swagger_dir() -> PathBuf { + let root_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR")) + .parent() + .unwrap() + .parent() + .unwrap() + .to_path_buf(); + let ctags_dir = root_dir + .clone() + .join("_fixtures") + .join("swagger") + .join("petstore.yaml"); + + return ctags_dir; + } + + #[test] + fn should_run_openapi_analysis() { + analysis(&*swagger_dir()); + // println!("{:?}", spec); + // assert_eq!("", spec.host.unwrap()); + } +} diff --git a/plugins/coco_swagger/src/lib.rs b/plugins/coco_swagger/src/lib.rs index 786619fe..0b836867 100644 --- a/plugins/coco_swagger/src/lib.rs +++ b/plugins/coco_swagger/src/lib.rs @@ -1,6 +1,8 @@ use core_model::CocoConfig; use core_model::PluginInterface; +pub mod coco_swagger_plugin; + pub struct CocoSwagger {} impl PluginInterface for CocoSwagger {