-
Notifications
You must be signed in to change notification settings - Fork 93
/
Copy pathlib.rs
146 lines (129 loc) · 4.67 KB
/
lib.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
// Licensed to Elasticsearch B.V. under one or more contributor
// license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright
// ownership. Elasticsearch B.V. licenses this file to you under
// the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
mod paths;
mod schemas;
mod components;
mod utils;
use std::collections::HashSet;
use std::io::{BufWriter, Write};
use std::path::Path;
use openapiv3::{Components, OpenAPI};
use tracing::warn;
use clients_schema::{Availabilities, Endpoint, IndexedModel};
use crate::components::TypesAndComponents;
pub fn convert_schema_file(
path: impl AsRef<Path>,
filter: Option<fn(&Option<Availabilities>) -> bool>,
endpoint_filter: fn(e: &Endpoint) -> bool,
out: impl Write
) -> anyhow::Result<()> {
// Parsing from a string is faster than using a buffered reader when there is a need for look-ahead
// See https://github.com/serde-rs/json/issues/160
let json = &std::fs::read_to_string(path)?;
let json_deser = &mut serde_json::Deserializer::from_str(&json);
let mut unused = HashSet::new();
let mut model: IndexedModel = serde_ignored::deserialize(json_deser, |path| {
if let serde_ignored::Path::Map {parent: _, key} = path {
unused.insert(key);
}
})?;
if !unused.is_empty() {
let msg = unused.into_iter().collect::<Vec<_>>().join(", ");
warn!("Unknown fields found in schema.json: {}", msg);
}
if let Some(filter) = filter {
model = clients_schema::transform::filter_availability(model, filter)?;
}
model.endpoints.retain(endpoint_filter);
let openapi = convert_schema(&model)?;
serde_json::to_writer_pretty(BufWriter::new(out), &openapi)?;
Ok(())
}
pub fn convert_schema(
model: &IndexedModel,
) -> anyhow::Result<OpenAPI> {
let mut openapi = OpenAPI {
openapi: "3.0.3".into(),
info: info(model),
servers: vec![],
paths: Default::default(),
components: Some(Components {
security_schemes: Default::default(),
// Filled from endpoints
responses: Default::default(),
// Filled from endpoints
// TODO: add common request parameters and common cat parameters?
parameters: Default::default(),
examples: Default::default(),
// Filled from endpoints
request_bodies: Default::default(),
headers: Default::default(),
// Filled with type definitions
schemas: Default::default(),
links: Default::default(),
callbacks: Default::default(),
extensions: Default::default(),
}),
security: None,
tags: vec![],
external_docs: None,
extensions: Default::default(),
};
let mut tac = TypesAndComponents::new(&model, openapi.components.as_mut().unwrap());
// Endpoints
for endpoint in &model.endpoints {
paths::add_endpoint(endpoint, &mut tac, &mut openapi.paths)?;
}
// // Sort maps to ensure output stability
// openapi.paths.extensions.sort_keys();
// if let Some(ref mut comp) = openapi.components {
// comp.callbacks.sort_keys();
// comp.examples.sort_keys();
// comp.extensions.sort_keys();
// comp.headers.sort_keys();
// comp.links.sort_keys();
// comp.parameters.sort_keys();
// comp.request_bodies.sort_keys();
// comp.responses.sort_keys();
// comp.schemas.sort_keys();
// comp.security_schemes.sort_keys();
// }
Ok(openapi)
}
fn info(model: &IndexedModel) -> openapiv3::Info {
let (title, license) = if let Some(info) = &model.info {
(
info.title.clone(),
Some(openapiv3::License {
name: info.license.name.clone(),
url: Some(info.license.url.clone()),
extensions: Default::default(),
})
)
} else {
("".to_string(), None)
};
openapiv3::Info {
title,
description: None,
terms_of_service: None,
contact: None,
license,
version: "".to_string(), // TODO
extensions: Default::default(),
}
}