|
1 | 1 | mod paths;
|
2 | 2 | mod schemas;
|
| 3 | +mod components; |
3 | 4 |
|
4 | 5 | use std::io::Write;
|
5 | 6 | use std::path::Path;
|
| 7 | +use openapiv3::{Components, OpenAPI}; |
6 | 8 |
|
7 |
| -use clients_schema::{Model, Property, TypeDefinition, TypeName, TypeRegistry, ValueOf}; |
| 9 | +use clients_schema::{Endpoint, Model}; |
| 10 | +use crate::components::TypesAndComponents; |
8 | 11 |
|
9 |
| -pub fn convert_schema(path: impl AsRef<Path>, out: impl Write) -> anyhow::Result<()> { |
| 12 | +pub fn convert_schema_file(path: impl AsRef<Path>, endpoint_filter: fn(e: &Endpoint) -> bool, out: impl Write) -> anyhow::Result<()> { |
10 | 13 | let file = std::fs::File::open(path)?;
|
11 |
| - |
12 | 14 | let model: Model = serde_json::from_reader(file)?;
|
13 |
| - let types = model.type_registry(); |
14 |
| - |
15 |
| - let mut openapi = openapiv3::OpenAPI::default(); |
16 | 15 |
|
17 |
| - openapi.openapi = "3.1.0".into(); |
| 16 | + let openapi = convert_schema(&model, endpoint_filter)?; |
18 | 17 |
|
19 |
| - openapi.info = openapiv3::Info { |
20 |
| - title: "Elasticsearch API".to_string(), |
21 |
| - description: None, |
22 |
| - terms_of_service: None, |
23 |
| - contact: None, |
24 |
| - license: license(&model), |
25 |
| - version: "".to_string(), |
26 |
| - extensions: Default::default(), |
27 |
| - }; |
| 18 | + serde_json::to_writer_pretty(out, &openapi)?; |
| 19 | + Ok(()) |
| 20 | +} |
28 | 21 |
|
29 |
| - // Endpoints |
30 |
| - let paths = paths::build_paths(&model.endpoints, &types)?; |
| 22 | +pub fn convert_schema(model: &Model, endpoint_filter: fn(e: &Endpoint) -> bool) -> anyhow::Result<OpenAPI> { |
31 | 23 |
|
32 |
| - openapi.paths = openapiv3::Paths { |
33 |
| - paths: paths, |
| 24 | + let mut openapi = OpenAPI { |
| 25 | + openapi: "3.0.3".into(), |
| 26 | + info: info(model), |
| 27 | + servers: vec![], |
| 28 | + paths: Default::default(), |
| 29 | + components: Some(Components { |
| 30 | + security_schemes: Default::default(), |
| 31 | + // Filled from endpoints |
| 32 | + responses: Default::default(), |
| 33 | + // Filled from endpoints |
| 34 | + // TODO: add common request parameters and common cat parameters? |
| 35 | + parameters: Default::default(), |
| 36 | + examples: Default::default(), |
| 37 | + // Filled from endpoints |
| 38 | + request_bodies: Default::default(), |
| 39 | + headers: Default::default(), |
| 40 | + // Filled with type definitions |
| 41 | + schemas: Default::default(), |
| 42 | + links: Default::default(), |
| 43 | + callbacks: Default::default(), |
| 44 | + extensions: Default::default(), |
| 45 | + }), |
| 46 | + security: None, |
| 47 | + tags: vec![], |
| 48 | + external_docs: None, |
34 | 49 | extensions: Default::default(),
|
35 | 50 | };
|
36 | 51 |
|
37 |
| - // Types |
38 |
| - let components = openapiv3::Components { |
39 |
| - security_schemes: Default::default(), |
40 |
| - responses: Default::default(), |
41 |
| - parameters: Default::default(), |
42 |
| - examples: Default::default(), |
43 |
| - request_bodies: Default::default(), |
44 |
| - headers: Default::default(), |
45 |
| - schemas: Default::default(), |
46 |
| - links: Default::default(), |
47 |
| - callbacks: Default::default(), |
48 |
| - extensions: Default::default(), |
49 |
| - }; |
| 52 | + let mut tac = TypesAndComponents::new(&model.types, openapi.components.as_mut().unwrap()); |
50 | 53 |
|
51 |
| - openapi.components = Some(components); |
| 54 | + // Endpoints |
| 55 | + for endpoint in model.endpoints.iter().filter(|e| endpoint_filter(e)) { |
| 56 | + paths::add_endpoint(endpoint, &mut tac, &mut openapi.paths)?; |
| 57 | + } |
| 58 | + //let paths = paths::build_paths(model.endpoints, &types)?; |
52 | 59 |
|
53 |
| - serde_json::to_writer_pretty(out, &openapi)?; |
54 |
| - Ok(()) |
| 60 | + //openapi.paths = openapiv3::Paths { |
| 61 | + // paths: paths, |
| 62 | + // extensions: Default::default(), |
| 63 | + //}; |
| 64 | + |
| 65 | + Ok(openapi) |
55 | 66 | }
|
56 | 67 |
|
57 |
| -fn license(model: &Model) -> Option<openapiv3::License> { |
58 |
| - if let Some(info) = &model.info { |
59 |
| - Some(openapiv3::License { |
60 |
| - name: info.license.name.clone(), |
61 |
| - url: Some(info.license.url.clone()), |
62 |
| - extensions: Default::default(), |
63 |
| - }) |
| 68 | +fn info(model: &Model) -> openapiv3::Info { |
| 69 | + let (title, license) = if let Some(info) = &model.info { |
| 70 | + ( |
| 71 | + info.title.clone(), |
| 72 | + Some(openapiv3::License { |
| 73 | + name: info.license.name.clone(), |
| 74 | + url: Some(info.license.url.clone()), |
| 75 | + extensions: Default::default(), |
| 76 | + }) |
| 77 | + ) |
64 | 78 | } else {
|
65 |
| - None |
| 79 | + ("".to_string(), None) |
| 80 | + }; |
| 81 | + |
| 82 | + openapiv3::Info { |
| 83 | + title, |
| 84 | + description: None, |
| 85 | + terms_of_service: None, |
| 86 | + contact: None, |
| 87 | + license, |
| 88 | + version: "".to_string(), // TODO |
| 89 | + extensions: Default::default(), |
66 | 90 | }
|
67 | 91 | }
|
0 commit comments