-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
300 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
/target | ||
openapi.json | ||
.env | ||
types.js | ||
.env |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,18 @@ | ||
[package] | ||
name = "openapi-to-jsdoc" | ||
version = "0.1.0" | ||
version = "0.1.1" | ||
edition = "2021" | ||
description = "Generate JSDoc typedefs from OpenAPI 3.0.x spec schemas" | ||
license = "MIT" | ||
repository = "https://github.com/kshyr/openapi-to-jsdoc" | ||
|
||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[dependencies] | ||
dotenv = "0.15.0" | ||
oas3 = "0.4" | ||
reqwest = { version = "0.12", features = ["json", "blocking"] } | ||
serde_json = "1.0" | ||
tokio = { version = "1", features = ["full"] } | ||
clap = { version = "4.5.3", features = ["derive"] } | ||
oas3 = "0.4.0" | ||
openapiv3-extended = "6.0.0" | ||
reqwest = { version = "0.12.1", features = ["json", "blocking"] } | ||
serde_json = "1.0.114" | ||
tokio = { version = "1.36.0", features = ["full"] } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,3 @@ | ||
### Environmental variables (`.env`) | ||
### Usage | ||
|
||
``` | ||
OPENAPI_JSON_URL = "https://api.example.com/openapi.json" | ||
``` | ||
`cargo install openapi-to-jsdoc` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,83 @@ | ||
use std::fs; | ||
|
||
use openapiv3::{OpenAPI, RefOr, SchemaKind, Type}; | ||
|
||
pub mod builder; | ||
|
||
pub fn generate_typedef(file: String, spec: &OpenAPI, schema_name: &str) -> String { | ||
let mut jsdoc = builder::JSDocBuilder::new(); | ||
let schema = spec | ||
.components | ||
.schemas | ||
.get(schema_name) | ||
.expect("Schema not found") | ||
.as_item() // This is safe because we know the schema exists and it's not a reference | ||
.unwrap(); | ||
|
||
jsdoc.add_tag_line( | ||
builder::JSDocTag::Typedef, | ||
&format!("{{Object}} {}", schema_name), | ||
); | ||
|
||
for (prop_name, prop) in schema.properties() { | ||
let prop_type = match prop { | ||
RefOr::Reference { reference } => reference | ||
.strip_prefix("#/components/schemas/") | ||
.unwrap() | ||
.to_string(), | ||
RefOr::Item(item) => match &item.kind { | ||
SchemaKind::Type(t) => match t { | ||
Type::String(_) => "string".to_string(), | ||
Type::Number(_) => "number".to_string(), | ||
Type::Integer(_) => "number".to_string(), | ||
Type::Boolean {} => "boolean".to_string(), | ||
Type::Object(_) => "Object".to_string(), | ||
Type::Array(array) => { | ||
let item = *array.items.clone().unwrap(); | ||
let item_type: String = match item { | ||
RefOr::Reference { reference } => { | ||
let mut reference = reference | ||
.strip_prefix("#/components/schemas/") | ||
.unwrap() | ||
.to_string(); | ||
reference.push_str("[]"); | ||
reference | ||
} | ||
RefOr::Item(item) => match &item.kind { | ||
SchemaKind::Type(t) => match t { | ||
Type::String(_) => "string[]".to_string(), | ||
Type::Number(_) => "number[]".to_string(), | ||
Type::Integer(_) => "number[]".to_string(), | ||
Type::Boolean {} => "boolean[]".to_string(), | ||
Type::Object(_) => "Object[]".to_string(), | ||
_ => todo!(), | ||
}, | ||
_ => todo!(), | ||
}, | ||
}; | ||
item_type | ||
} | ||
}, | ||
_ => todo!(), | ||
}, | ||
}; | ||
|
||
let prop_name = if schema.get_required().unwrap().contains(prop_name) { | ||
prop_name.to_string() | ||
} else { | ||
format!("[{}]", prop_name) | ||
}; | ||
|
||
jsdoc.add_tag_line( | ||
builder::JSDocTag::Property, | ||
&format!("{{{}}} {}", prop_type, prop_name), | ||
); | ||
} | ||
if fs::File::open(&file).is_err() { | ||
fs::write(&file, "").unwrap(); | ||
} | ||
let file_contents = fs::read_to_string(&file).unwrap(); | ||
let doc = format!("{}\n{}", &file_contents, jsdoc.build().as_str()); | ||
fs::write(file, &doc).unwrap(); | ||
doc | ||
} |
Oops, something went wrong.