Skip to content

Commit

Permalink
test: poc openapi v3 codegen
Browse files Browse the repository at this point in the history
  • Loading branch information
tiagolobocastro committed Oct 2, 2023
1 parent 6032584 commit 509379f
Show file tree
Hide file tree
Showing 286 changed files with 29,367 additions and 29 deletions.
13 changes: 12 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,15 @@ path = "src/bin/main.rs"
required-features = ["cli"]

[dependencies]
#ramhorns = { path = "../ramhorns/ramhorns", default-features = false, features = ["indexes"] }
#ramhorns-derive = { path = "../ramhorns/ramhorns-derive" }
#openapiv3 = { path = "../openapiv3", optional = true }

ramhorns = { git = "https://github.com/tiagolobocastro/ramhorns.git", branch = "paperclip", default-features = false, features = ["indexes"] }
ramhorns-derive = { git = "https://github.com/tiagolobocastro/ramhorns.git", branch = "paperclip" }

openapiv3 = { git = "https://github.com/paperclip-rs/openapiv3.git", branch = "obj-oneof", optional = true }

paperclip-actix = { path = "plugins/actix-web", version = "0.7.0", optional = true }
paperclip-core = { path = "core", version = "0.7.0" }
paperclip-macros = { path = "macros", version = "0.6.3", optional = true }
Expand All @@ -40,7 +49,8 @@ url_dep = { version = ">=1.7,<3", package = "url" }
thiserror = "1.0"
anyhow = "1.0"
once_cell = "1.4"
openapiv3 = { version = "1.0.3", optional = true }
#openapiv3 = { version = "1.0.3", optional = true }
indexmap = { version = "2.0", features = ["serde"], optional = true }

[dev-dependencies]
actix-rt1 = { version = "1.0", package = "actix-rt" }
Expand Down Expand Up @@ -106,6 +116,7 @@ members = [
"core",
"macros",
"plugins/actix-web",
"testgen"
]

[[test]]
Expand Down
4 changes: 2 additions & 2 deletions core/src/v2/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ pub use self::{
pub use paperclip_macros::*;

#[cfg(feature = "codegen")]
use self::resolver::Resolver;
pub(crate) use self::resolver::Resolver;
#[cfg(feature = "codegen")]
use crate::error::ValidationError;
pub(crate) use crate::error::ValidationError;

#[cfg(feature = "codegen")]
impl<S: Schema + Default> ResolvableApi<S> {
Expand Down
23 changes: 2 additions & 21 deletions core/src/v3/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,8 @@
//! Conversion traits and helps functions that help converting openapi v2 types to openapi v3.
//! For the OpenAPI v3 types the crate `openapiv3` is used.
mod contact;
mod external_documentation;
mod header;
mod info;
mod license;
mod openapi;
mod operation;
mod parameter;
mod paths;
mod reference;
mod request_body;
mod response;
mod schema;
mod security_scheme;
mod tag;

use super::v2::{models as v2, models::Either};

use parameter::non_body_parameter_to_v3_parameter;
use reference::invalid_referenceor;
use response::OperationEitherResponse;
use super::v2::models as v2;
mod models;

/// Convert this crates openapi v2 (`DefaultApiRaw`) to `openapiv3::OpenAPI`
pub fn openapiv2_to_v3(v2: v2::DefaultApiRaw) -> openapiv3::OpenAPI {
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
21 changes: 21 additions & 0 deletions core/src/v3/models/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
mod contact;
mod external_documentation;
mod header;
mod info;
mod license;
mod openapi;
mod operation;
mod parameter;
mod paths;
mod reference;
mod request_body;
mod response;
mod schema;
mod security_scheme;
mod tag;

pub use super::super::v2::{models as v2, models::Either};

use parameter::non_body_parameter_to_v3_parameter;
use reference::invalid_referenceor;
use response::OperationEitherResponse;
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
20 changes: 15 additions & 5 deletions src/bin/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ fn parse_spec(s: &str) -> Result<ResolvableApi<DefaultSchema>, Error> {
let fd = File::open(s)?;
Ok(v2::from_reader(fd)?)
}
fn parse_spec_v3(s: &str) -> Result<openapiv3::OpenAPI, Error> {
let fd = File::open(s)?;
Ok(v2::from_reader_v3(fd)?)
}

#[derive(Debug)]
enum OApiVersion {
Expand All @@ -36,8 +40,8 @@ enum OApiVersion {
#[derive(Debug, StructOpt)]
struct Opt {
/// Path to OpenAPI spec in JSON/YAML format (also supports publicly accessible URLs).
#[structopt(parse(try_from_str = parse_spec))]
spec: ResolvableApi<DefaultSchema>,
#[structopt(long)]
spec: std::path::PathBuf,
/// OpenAPI version (e.g., v2).
#[structopt(long = "api", parse(try_from_str = parse_version))]
api: OApiVersion,
Expand All @@ -47,6 +51,9 @@ struct Opt {
/// Emit CLI target instead.
#[structopt(long = "cli")]
cli: bool,
/// Render.
#[structopt(long)]
models: bool,
/// Do not make the crate a root crate.
#[structopt(long = "no-root")]
no_root: bool,
Expand All @@ -60,12 +67,15 @@ struct Opt {
}

fn parse_args_and_run() -> Result<(), Error> {
let opt = Opt::from_args();
let opt: Opt = Opt::from_args();

if let OApiVersion::V3 = opt.api {
return Err(PaperClipError::UnsupportedOpenAPIVersion.into());
let spec = parse_spec_v3(&opt.spec.to_string_lossy().to_string())?;
paperclip::v3::OpenApiV3::new(spec, opt.output).run(opt.models)?;
return Ok(());
}

let spec = opt.spec.resolve()?;
let spec = parse_spec(&opt.spec.to_string_lossy().to_string())?.resolve()?;
let mut state = EmitterState::default();

if let Some(o) = opt.output {
Expand Down
3 changes: 3 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,6 @@ pub mod actix {
#[cfg(feature = "actix4")]
pub use paperclip_core::v2::HttpResponseWrapper;
}

#[cfg(feature = "openapiv3")]
pub mod v3;
27 changes: 27 additions & 0 deletions src/v2/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,3 +133,30 @@ where
api.spec_format = fmt;
Ok(api)
}

/// Deserialize the schema from the given reader. Currently, this only supports
/// JSON and YAML formats.
pub fn from_reader_v3<R>(mut reader: R) -> Result<openapiv3::OpenAPI, PaperClipError>
where
R: Read,
{
let mut buf = [b' '];
while buf[0].is_ascii_whitespace() {
reader.read_exact(&mut buf)?;
}
let reader = buf.as_ref().chain(reader);

let (api, _fmt) = if buf[0] == b'{' {
(
serde_json::from_reader::<_, openapiv3::OpenAPI>(reader)?,
SpecFormat::Json,
)
} else {
(
serde_yaml::from_reader::<_, openapiv3::OpenAPI>(reader)?,
SpecFormat::Yaml,
)
};

Ok(api)
}
Loading

0 comments on commit 509379f

Please sign in to comment.