From a60f6a089946f05839d2f94b6a4e6c16f5da50f5 Mon Sep 17 00:00:00 2001 From: Manu Bretelle Date: Tue, 24 Jan 2023 15:13:39 -0800 Subject: [PATCH] [github] Use Option<&str> instead of Option for `list_workflow_runs`'s `created` parameter. The issue is described in more details in https://github.com/github/rest-api-description/issues/2088 For now, this change will treat the created parameter as an optional str, which it would probably do if the parameter had a custom format defined. Alternatively, one oculd try to implement a proper datetime rnage that follows the [Query for dates](https://docs.github.com/en/search-github/getting-started-with-searching-on-github/understanding-the-search-syntax#query-for-dates) format, but this is maybe more work than one would benefit by implementing the search formating on the user side. --- generator/src/functions.rs | 7 ++++--- generator/src/main.rs | 20 +++++++++++++++----- generator/src/template.rs | 5 +++++ 3 files changed, 24 insertions(+), 8 deletions(-) diff --git a/generator/src/functions.rs b/generator/src/functions.rs index 0295c112..479ba709 100644 --- a/generator/src/functions.rs +++ b/generator/src/functions.rs @@ -138,7 +138,7 @@ pub fn generate_files( content }; - let docs = get_fn_docs(o, m, p, parameters, ts)?; + let docs = get_fn_docs(proper_name, o, m, p, parameters, ts)?; let mut bounds: Vec = Vec::new(); @@ -727,7 +727,7 @@ fn get_fn_params( let nam = &to_snake_case(¶meter_data.name); if !fn_params.contains(nam) && !fn_params.contains(&format!("{}_", nam)) { - let typ = parameter_data.render_type(¶m_name, ts)?; + let typ = parameter_data.render_type(proper_name, ¶m_name, ts)?; if nam == "ref" || nam == "type" || nam == "foo" @@ -1143,6 +1143,7 @@ fn get_fn_inner( } fn get_fn_docs( + proper_name: &str, o: &openapiv3::Operation, m: &str, p: &str, @@ -1215,7 +1216,7 @@ fn get_fn_docs( } let nam = &to_snake_case(&clean_name(¶meter_data.name)); - let typ = parameter_data.render_type(¶m_name, ts)?; + let typ = parameter_data.render_type(proper_name, ¶m_name, ts)?; if nam == "ref" || nam == "type" diff --git a/generator/src/main.rs b/generator/src/main.rs index 71e2e9ba..3ce18346 100644 --- a/generator/src/main.rs +++ b/generator/src/main.rs @@ -169,11 +169,11 @@ where } trait ParameterDataExt { - fn render_type(&self, name: &str, ts: &mut TypeSpace) -> Result; + fn render_type(&self, proper_name: &str, name: &str, ts: &mut TypeSpace) -> Result; } impl ParameterDataExt for openapiv3::ParameterData { - fn render_type(&self, name: &str, ts: &mut TypeSpace) -> Result { + fn render_type(&self, proper_name: &str, name: &str, ts: &mut TypeSpace) -> Result { use openapiv3::{SchemaKind, Type}; // Cleanup the name. @@ -250,9 +250,20 @@ impl ParameterDataExt for openapiv3::ParameterData { //println!("XXX min/max length"); //} - match &st.format { - Item(DateTime) => "chrono::DateTime".to_string(), + // FIXME: In [list_workflow_runs](https://docs.rs/octorust/latest/octorust/actions/struct.Actions.html#method.list_workflow_runs), the created + // parameter is an optional DateTime as specified in github spec, but in practice, it is some date-time range of sort as specificed by the + // [search syntax](https://docs.github.com/en/search-github/getting-started-with-searching-on-github/understanding-the-search-syntax#query-for-dates). + // Convert this to an Option<&str> so the interface do not change for people not + // using the parameter currently, if they were, it was not working as they expected unless they looked for a specified date-time. + // https://github.com/github/rest-api-description/issues/2088 + Item(DateTime) => { + if proper_name == "GitHub" && self.name == "created" { + "Option<&str>".to_string() + } else { + "chrono::DateTime".to_string() + } + } Item(Date) => "chrono::NaiveDate".to_string(), Item(Password) => "&str".to_string(), // TODO: as per the spec this is base64 encoded chars. @@ -1419,7 +1430,6 @@ impl TypeSpace { } else { "".to_string() }; - let mut s = sc.clone(); // If we have an additional description and it is better than our original, diff --git a/generator/src/template.rs b/generator/src/template.rs index e0aa23d3..30406220 100644 --- a/generator/src/template.rs +++ b/generator/src/template.rs @@ -62,6 +62,11 @@ impl Template { r#"if {} {{ query_args.push(("{}".to_string(), {}.to_string())); }}"#, nam, prop, nam )); + } else if value == "Option<&str>" { + a(&format!( + r#"if let Some(s) = {} {{ if !s.is_empty() {{ query_args.push(("{}".to_string(), s.to_string())); }} }}"#, + nam, prop + )); } else if value == "&str" { a(&format!( r#"if !{}.is_empty() {{ query_args.push(("{}".to_string(), {}.to_string())); }}"#,