From 0659b8a12502955ebd78c261f12a6a8bbed03abc Mon Sep 17 00:00:00 2001 From: Madan Kumar Date: Thu, 19 Nov 2026 15:48:54 +0530 Subject: [PATCH] fix(csharp-parser): mark parameters with a default value as optional (#10185) * fix(csharp-parser): mark parameters with a default value as optional C# main parameters that declare a default (e.g. `Main(string s = "hi")`) were emitted with has_default = false even though their parsed default was retained, so the inferred schema marked them as required. Derive has_default from whether a default was parsed, consistent with the other language parsers. * fix(csharp): give omitted arguments their declared default and mark every defaulted parameter optional Co-Authored-By: Claude Opus 5.5 * fix(csharp): bind verbatim @-named arguments and test declared defaults end to end Co-Authored-By: Claude Opus 5.5 * fix(csharp): find declared defaults by parameter name so a params array does not break the lookup Co-Authored-By: Claude Opus 5.5 --------- Co-authored-by: hugocasa Co-authored-by: Ruben Fiszel Co-authored-by: Claude Opus 5.5 --- .../parsers/windmill-parser-csharp/src/lib.rs | 46 ++++++++- backend/tests/worker.rs | 58 +++++++++++ .../windmill-worker/src/csharp_executor.rs | 95 +++++++++++++++---- 3 files changed, 178 insertions(+), 21 deletions(-) diff --git a/backend/parsers/windmill-parser-csharp/src/lib.rs b/backend/parsers/windmill-parser-csharp/src/lib.rs index 2c068f0901..f39aac738a 100644 --- a/backend/parsers/windmill-parser-csharp/src/lib.rs +++ b/backend/parsers/windmill-parser-csharp/src/lib.rs @@ -69,15 +69,26 @@ pub fn parse_csharp_sig_meta(code: &str) -> anyhow::Result { for p_list_node in param_list.children(&mut param_list.walk()) { if p_list_node.kind() == "parameter" { let mut default = None; + let mut has_default = false; for a in p_list_node.children(&mut p_list_node.walk()) { if a.kind() == "=" { if let Some(node) = a.next_sibling() { + // Optional even when the default (1.5f, 'x', a const) is not JSON. + has_default = true; default = csharp_param_default_value(node, code); } } } let (otyp, typ, name) = parse_csharp_typ(p_list_node, code)?; - args.push(Arg { name, otyp, typ, default, has_default: false, oidx: None, otyp_inferred: false }); + args.push(Arg { + name, + otyp, + typ, + default, + has_default, + oidx: None, + otyp_inferred: false, + }); } } } @@ -279,14 +290,47 @@ class LilProgram assert_eq!(ret.args[0].name, "myString"); assert_eq!(ret.args[0].otyp, Some("string".to_string())); assert_eq!(ret.args[0].typ, Typ::Str(None)); + assert_eq!(ret.args[0].default, Some(serde_json::json!("World"))); + assert_eq!(ret.args[0].has_default, true); assert_eq!(ret.args[1].name, "myInt"); assert_eq!(ret.args[1].otyp, Some("int".to_string())); assert_eq!(ret.args[1].typ, Typ::Int); + assert_eq!(ret.args[1].default, None); + assert_eq!(ret.args[1].has_default, false); assert_eq!(ret.args[2].name, "jj"); assert_eq!(ret.args[2].otyp, Some("string[]".to_string())); assert_eq!(ret.args[2].typ, Typ::List(Box::new(Typ::Str(None)))); + assert_eq!(ret.args[2].default, None); + assert_eq!(ret.args[2].has_default, false); + } + + #[test] + fn a_default_that_is_not_json_still_makes_the_argument_optional() { + let code = r#" +class Script +{ + const int Answer = 42; + public static string Main(double ratio = 1.5f, char c = 'x', int n = Answer, int required) + { + return ""; + } +}"#; + let args = parse_csharp_signature(code).unwrap().args; + let summary: Vec<_> = args + .iter() + .map(|a| (a.name.as_str(), a.has_default, a.default.clone())) + .collect(); + assert_eq!( + summary, + vec![ + ("ratio", true, None), + ("c", true, None), + ("n", true, None), + ("required", false, None), + ] + ); } #[test] diff --git a/backend/tests/worker.rs b/backend/tests/worker.rs index ee622009b3..5f5326ca76 100644 --- a/backend/tests/worker.rs +++ b/backend/tests/worker.rs @@ -1287,6 +1287,64 @@ class Script Ok(()) } +#[cfg(feature = "csharp")] +#[sqlx::test(fixtures("base"))] +async fn test_csharp_job_omitted_args_take_declared_defaults( + db: Pool, +) -> anyhow::Result<()> { + initialize_tracing().await; + let server = ApiServer::start(db.clone()).await?; + let port = server.addr.port(); + + let job = || { + RunJob::from(JobPayload::Code(RawCode { + hash: None, + content: r#" +class Script +{ + const int Answer = 42; + // `params` is not in the parsed signature, so the wrapper never passes it. + public static string Main(string s = "World", int n = Answer, string @class = "c", params string[] rest) + { + return $"{s ?? ""} {n} {@class} {rest.Length}"; + } +} +"# + .to_owned(), + path: None, + lock: None, + language: ScriptLang::CSharp, + cache_ttl: None, + cache_ignore_s3_path: None, + dedicated_worker: None, + concurrency_settings: windmill_common::runnable_settings::ConcurrencySettings::default( + ) + .into(), + debouncing_settings: windmill_common::runnable_settings::DebouncingSettings::default(), + modules: None, + tag: None, + })) + }; + + let omitted = job() + .run_until_complete(&db, false, port) + .await + .json_result() + .unwrap(); + assert_eq!(omitted, json!("World 42 c 0")); + + let explicit = job() + .arg("s", json!(null)) + .arg("n", json!(1)) + .arg("@class", json!("k")) + .run_until_complete(&db, false, port) + .await + .json_result() + .unwrap(); + assert_eq!(explicit, json!(" 1 k 0")); + Ok(()) +} + #[sqlx::test(fixtures("base"))] async fn test_bash_job(db: Pool) -> anyhow::Result<()> { initialize_tracing().await; diff --git a/backend/windmill-worker/src/csharp_executor.rs b/backend/windmill-worker/src/csharp_executor.rs index 87213223e7..293401a508 100644 --- a/backend/windmill-worker/src/csharp_executor.rs +++ b/backend/windmill-worker/src/csharp_executor.rs @@ -72,6 +72,9 @@ lazy_static::lazy_static! { const CSHARP_OBJECT_STORE_PREFIX: &str = const_format::concatcp!(crate::global_cache::TARGET, "_csharpbin/"); +#[cfg(feature = "csharp")] +const CSHARP_WRAPPER_VERSION: &str = "wrapper-v2"; + /// Cache key of a C# build. The run path and the deploy-time prebuild must derive it the /// same way or the prebuilt binary is never found and gets rebuilt on first run. #[cfg(feature = "csharp")] @@ -83,11 +86,14 @@ async fn csharp_cache_key( ) -> String { // The SDK project globs every `.cs` under the job dir, so companion modules are // compiled into the binary this key names and have to be part of it. + // The generated wrapper is compiled into the binary too: bump the version when it + // changes, or binaries built with the previous wrapper keep running. let base = format!( - "{}{}{}", + "{}{}{}{}", code, requirements_o.unwrap_or(""), - DOTNET_TARGET_FRAMEWORK.as_str() + DOTNET_TARGET_FRAMEWORK.as_str(), + CSHARP_WRAPPER_VERSION ); let mut hash = crate::worker::artifact_cache_name(base, modules); hash.push_str(&crate::workspace_registry_cache_suffix(w_id).await); @@ -266,28 +272,74 @@ fn gen_cs_proj( write_file(job_dir, "Script.cs", code.as_str())?; let sig_meta = windmill_parser_csharp::parse_csharp_sig_meta(code.as_str())?; - let sig = sig_meta.main_sig; - let spread = &sig - .args - .clone() - .into_iter() - .map(|x| format!("parsedArgs.{}", &x.name)) - .join(", "); - let args_class_body = &sig - .args - .into_iter() + let args = &sig_meta.main_sig.args; + let class_name = sig_meta.class_name.unwrap_or("Script".to_string()); + let otyp = |x: &windmill_parser::Arg| { + x.otyp + .clone() + .ok_or(anyhow!("Type not found for argument {}", x.name)) + }; + let args_class_body = &args + .iter() .map(|x| { + // The schema keys a verbatim identifier (`@class`) with its `@`, which is not + // part of the C# name the serializer would match on. + let json_name = if x.name.starts_with('@') { + format!( + "[System.Text.Json.Serialization.JsonPropertyName(\"{}\")] ", + x.name + ) + } else { + String::new() + }; Ok(format!( - " public {} {} {{ get; set; }}", - x.otyp - .ok_or(anyhow!("Type not found for argument {}", x.name))?, - &x.name, + " {json_name}public {} {} {{ get; set; }}", + otyp(x)?, + &x.name )) }) .collect::, anyhow::Error>>()? .join("\n"); - let class_name = sig_meta.class_name.unwrap_or("Script".to_string()); + // An argument left out of args.json must take the default declared on Main, not + // default(T). It is read back from the compiled method because the default + // expression may name a constant the wrapper cannot see. The parser does not list + // every parameter (`params T[] x` is not one), so Main is matched on the names it + // does list and each default is looked up by name, never by position. + let mut resolve_defaults = String::new(); + if args.iter().any(|x| x.has_default) { + let names = args + .iter() + .map(|x| format!("\"{}\"", x.name.trim_start_matches('@'))) + .join(", "); + resolve_defaults.push_str(&format!( + r#" + using JsonDocument __argsDoc = JsonDocument.Parse(__argsJson); + var __mainParams = typeof({class_name}) + .GetMethods(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Static) + .First(m => m.Name == "Main" && new string[] {{ {names} }}.All(n => m.GetParameters().Any(p => p.Name == n))) + .GetParameters();"# + )); + for (i, x) in args.iter().enumerate().filter(|(_, x)| x.has_default) { + resolve_defaults.push_str(&format!( + "\n {t} __arg{i} = __argsDoc.RootElement.TryGetProperty(\"{n}\", out _) ? parsedArgs.{n} : DeclaredDefault<{t}>(__mainParams.First(p => p.Name == \"{c}\"));", + t = otyp(x)?, + n = x.name, + c = x.name.trim_start_matches('@'), + )); + } + } + let spread = &args + .iter() + .enumerate() + .map(|(i, x)| { + if x.has_default { + format!("__arg{i}") + } else { + format!("parsedArgs.{}", &x.name) + } + }) + .join(", "); let script_call = match (sig_meta.is_async, sig_meta.returns_void) { (true, true) => format!( @@ -327,6 +379,7 @@ fn gen_cs_proj( &format!( r#"using System; using System.IO; +using System.Linq; using System.Text.Json; using System.Threading.Tasks; @@ -338,13 +391,15 @@ namespace WindmillScriptCSharpInternal {{ class Wrapper {{ + static T DeclaredDefault(System.Reflection.ParameterInfo p) => p.DefaultValue is T v ? v : default; + static void Main(string[] args) {{ - using FileStream fs = File.OpenRead("args.json"); - Args parsedArgs = JsonSerializer.Deserialize(fs); + string __argsJson = File.ReadAllText("args.json"); + Args parsedArgs = JsonSerializer.Deserialize(__argsJson); try - {{ + {{{resolve_defaults} {script_call} }} catch (Exception ex)