Skip to content

Commit

Permalink
Refactoring & more schema features coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
swallez committed Aug 16, 2023
1 parent 5949351 commit 22c046e
Show file tree
Hide file tree
Showing 11 changed files with 274 additions and 6,499 deletions.
29 changes: 29 additions & 0 deletions openapi-converter/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions openapi-converter/clients_schema_to_openapi/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,7 @@ either_n = "0.2.0"
regex = "1.8"
maplit = "1.0"

argh = "0.1"

tracing = "0.1.37"
tracing-subscriber = "0.3.16"
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use openapiv3::{Components, Parameter, ReferenceOr, RequestBody, Response, Schema, StatusCode};
use clients_schema::{TypeDefinition, TypeName, TypeRegistry};
use crate::schemas::SchemaName;
use crate::utils::SchemaName;

pub struct TypesAndComponents<'a> {
pub types: TypeRegistry<'a>,
Expand Down
25 changes: 20 additions & 5 deletions openapi-converter/clients_schema_to_openapi/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,36 @@
mod paths;
mod schemas;
mod components;
mod utils;

use std::io::Write;
use std::collections::HashSet;
use std::io::{BufWriter, Write};
use std::path::Path;
use openapiv3::{Components, OpenAPI};

use clients_schema::{Endpoint, Model};
use crate::components::TypesAndComponents;

pub fn convert_schema_file(path: impl AsRef<Path>, endpoint_filter: fn(e: &Endpoint) -> bool, out: impl Write) -> anyhow::Result<()> {
let file = std::fs::File::open(path)?;
let model: Model = serde_json::from_reader(file)?;

let openapi = convert_schema(&model, endpoint_filter)?;
// 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 model: Model = 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(", ");
tracing::warn!("Unknown fields found in schema.json: {}", msg);
}

serde_json::to_writer_pretty(out, &openapi)?;
let openapi = convert_schema(&model, endpoint_filter)?;
//serde_json::to_writer_pretty(BufWriter::new(out), &openapi)?;
Ok(())
}

Expand Down
13 changes: 12 additions & 1 deletion openapi-converter/clients_schema_to_openapi/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,27 @@
use tracing::Level;
use tracing_subscriber::fmt::format::FmtSpan;
use tracing_subscriber::FmtSubscriber;

use argh::FromArgs;




fn main() -> anyhow::Result<()> {



let subscriber = FmtSubscriber::builder()
.with_writer(std::io::stderr)
.with_max_level(Level::TRACE)
.with_span_events(FmtSpan::EXIT)
.finish();
tracing::subscriber::set_global_default(subscriber)?;

clients_schema_to_openapi::convert_schema_file(
"../output/schema/schema-no-generics.json",
|e| e.name == "search",
|e| true,
//|e| e.name == "search",
std::io::stdout()
)?;

Expand Down
9 changes: 7 additions & 2 deletions openapi-converter/clients_schema_to_openapi/src/paths.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,13 @@ use crate::components::TypesAndComponents;
///
pub fn add_endpoint(endpoint: &clients_schema::Endpoint, tac: &mut TypesAndComponents, out: &mut Paths) -> anyhow::Result<()> {

if endpoint.request.is_none() || endpoint.response.is_none() {
tracing::warn!("Endpoint {} is missing either request or response", &endpoint.name);
if endpoint.request.is_none() {
tracing::warn!("Endpoint {} is missing a request -- ignored", &endpoint.name);
return Ok(());
}

if endpoint.response.is_none() {
tracing::warn!("Endpoint {} is missing a response -- ignored", &endpoint.name);
return Ok(());
}

Expand Down
Loading

0 comments on commit 22c046e

Please sign in to comment.