Skip to content

Commit

Permalink
Merge pull request #11 from kstasik/bugifx/any-type-error
Browse files Browse the repository at this point in the history
fix: anytype, rename fix
  • Loading branch information
kstasik authored Jan 19, 2021
2 parents 6dbe8cc + c34f3c8 commit 8479b38
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 23 deletions.
29 changes: 20 additions & 9 deletions src/codegen/jsonschema/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,17 +79,26 @@ impl Model {
if let Some(s) = &p.name {
Ok(&s)
} else {
Err(Error::NotImplemented)
Err(Error::CodegenCannotNameModelError(format!(
"primitive: {:?}",
self
)))
}
}
Self::ArrayType(p) => {
if let Some(s) = &p.name {
Ok(&s)
} else {
Err(Error::NotImplemented)
Err(Error::CodegenCannotNameModelError(format!(
"array: {:?}",
self
)))
}
}
_ => Err(Error::NotImplemented),
_ => Err(Error::CodegenCannotNameModelError(format!(
"unknown: {:?}",
self
))),
}
}

Expand Down Expand Up @@ -157,11 +166,13 @@ impl ModelContainer {
}
}

pub fn add(&mut self, scope: &mut SchemaScope, model: Model) -> &Model {
if self.exists(&model) {
// log::warn!("{}: Duplicated", scope);
self.models.values().find(|&s| s == &model).unwrap()
} else {
pub fn add(&mut self, scope: &mut SchemaScope, model: Model) {
if let Model::AnyType(_) = model {
log::error!("{}: trying to save anyType as model", scope);
return;
}

if !self.exists(&model) {
let key = scope.path();

if !self.models.contains_key(&key) {
Expand All @@ -175,7 +186,7 @@ impl ModelContainer {
}
}

self.models.entry(key).or_insert(model)
self.models.entry(key).or_insert(model);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/codegen/jsonschema/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ pub struct AnyType {}

impl AnyType {
pub fn model(schema: &Map<String, Value>, scope: &mut SchemaScope) -> super::Model {
log::debug!("{}: {:?}", scope, schema);
log::debug!("{}: {:?} may be invalid json schema", scope, schema);

super::Model::AnyType(Self {})
}
Expand Down
3 changes: 3 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ use thiserror::Error;

#[derive(Error, Debug)]
pub enum Error {
#[error("Cannot name model: {0}")]
CodegenCannotNameModelError(String),

#[error("Json Patch error occured: {0}")]
JsonPatchError(json_patch::PatchError),

Expand Down
29 changes: 19 additions & 10 deletions src/process/name/openapi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,16 +124,25 @@ impl OpenapiNamerOptions {
if let [endpoint, method] = &parts[..] {
let details = node.as_object_mut().unwrap();

let operation_id =
endpoint::Endpoint::new(method.to_string(), endpoint.to_string())
.unwrap()
.get_operation_id(self.resource_method_version);

if !details.contains_key("operationId") || self.overwrite {
log::info!("{}/operationId -> {}", ctx, operation_id);
details.insert("operationId".to_string(), Value::String(operation_id));
} else {
log::info!("{}/operationId -> using original", ctx);
match endpoint::Endpoint::new(method.to_string(), endpoint.to_string()) {
Ok(endpoint) => {
let operation_id =
endpoint.get_operation_id(self.resource_method_version);

if !details.contains_key("operationId") || self.overwrite {
log::info!("{}/operationId -> {}", ctx, operation_id);
details
.insert("operationId".to_string(), Value::String(operation_id));
} else {
log::info!("{}/operationId -> using original", ctx);
}
}
Err(e) => log::error!(
"/paths/{}/{}: cannot parse endpoint: {}",
endpoint,
method,
e
),
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ pub fn path_to_url(path: String) -> Result<Url, Error> {
return Err(Error::SchemaAsReference);
} else if path.starts_with("http") {
// todo: support http path in cli, reconsider different schemes support
return Ok(Url::parse(&path).map_err(|_| Error::SchemaInvalidPath { path })?);
return Url::parse(&path).map_err(|_| Error::SchemaInvalidPath { path });
}

let real_path = PathBuf::from(&path);
Expand Down
3 changes: 1 addition & 2 deletions src/scope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -248,8 +248,7 @@ impl SchemaScope {
.filter_map(|s| scope_to_string(s.clone()))
.collect::<Vec<String>>();

let mut parts = vec![];
parts.push(reference.to_string());
let mut parts = vec![reference.to_string()];
parts.append(&mut post);

parts.join("/")
Expand Down

0 comments on commit 8479b38

Please sign in to comment.