diff --git a/src/codegen/jsonschema/mod.rs b/src/codegen/jsonschema/mod.rs index 4da7ce5..e0395ae 100644 --- a/src/codegen/jsonschema/mod.rs +++ b/src/codegen/jsonschema/mod.rs @@ -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 + ))), } } @@ -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) { @@ -175,7 +186,7 @@ impl ModelContainer { } } - self.models.entry(key).or_insert(model) + self.models.entry(key).or_insert(model); } } diff --git a/src/codegen/jsonschema/types.rs b/src/codegen/jsonschema/types.rs index 5d073ce..c192325 100644 --- a/src/codegen/jsonschema/types.rs +++ b/src/codegen/jsonschema/types.rs @@ -248,7 +248,7 @@ pub struct AnyType {} impl AnyType { pub fn model(schema: &Map, scope: &mut SchemaScope) -> super::Model { - log::debug!("{}: {:?}", scope, schema); + log::debug!("{}: {:?} may be invalid json schema", scope, schema); super::Model::AnyType(Self {}) } diff --git a/src/error.rs b/src/error.rs index 055a97c..5ef67ba 100644 --- a/src/error.rs +++ b/src/error.rs @@ -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), diff --git a/src/process/name/openapi.rs b/src/process/name/openapi.rs index a64f724..f219da1 100644 --- a/src/process/name/openapi.rs +++ b/src/process/name/openapi.rs @@ -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 + ), } } diff --git a/src/schema.rs b/src/schema.rs index 00ba2c6..0987534 100644 --- a/src/schema.rs +++ b/src/schema.rs @@ -109,7 +109,7 @@ pub fn path_to_url(path: String) -> Result { 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); diff --git a/src/scope.rs b/src/scope.rs index 1ada92e..78483f9 100644 --- a/src/scope.rs +++ b/src/scope.rs @@ -248,8 +248,7 @@ impl SchemaScope { .filter_map(|s| scope_to_string(s.clone())) .collect::>(); - let mut parts = vec![]; - parts.push(reference.to_string()); + let mut parts = vec![reference.to_string()]; parts.append(&mut post); parts.join("/")