{"record":{"id":"a1cc69e21036787f","repo":"RicoSuter/NSwag","slug":"could-not-parse-variables-ensure-that-they-are-in-the-form","errorCode":null,"errorMessage":"Could not parse variables, ensure that they are in the form 'key1=value1,key2=value2', variables: \" + variables","messagePattern":"Could not parse variables, ensure that they are in the form 'key1=value1,key2=value2', variables: \" \\+ variables","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/NSwag.Commands/NSwagDocumentBase.cs","lineNumber":283,"sourceCode":"            {\r\n                value = JsonConvert.ToString(value);\r\n                return value.Substring(1, value.Length - 2);\r\n            }\r\n\r\n            return string.Empty;\r\n        }\r\n\r\n        private static Dictionary<string, string> ConvertVariables(string variables)\r\n        {\r\n            try\r\n            {\r\n                return (variables ?? \"\")\r\n                    .Split(',').Where(p => !string.IsNullOrEmpty(p))\r\n                    .ToDictionary(p => p.Split('=')[0], p => p.Split('=')[1]);\r\n            }\r\n            catch (Exception exception)\r\n            {\r\n                throw new InvalidOperationException(\"Could not parse variables, ensure that they are \" +\r\n                                                    \"in the form 'key1=value1,key2=value2', variables: \" + variables, exception);\r\n            }\r\n        }\r\n\r\n        private static JsonSerializerSettings GetSerializerSettings()\r\n        {\r\n            return new JsonSerializerSettings\r\n            {\r\n                DefaultValueHandling = DefaultValueHandling.Include,\r\n                NullValueHandling = NullValueHandling.Include,\r\n                ContractResolver = new CamelCasePropertyNamesContractResolver(),\r\n                Converters =\r\n                [\r\n                    new StringEnumConverter()\r\n                ]\r\n            };\r\n        }\r\n\r","sourceCodeStart":265,"sourceCodeEnd":301,"githubUrl":"https://github.com/RicoSuter/NSwag/blob/63daf8fcc3a25151b62eb4b326a1e8ea048a0d41/src/NSwag.Commands/NSwagDocumentBase.cs#L265-L301","documentation":"NSwagDocumentBase.ConvertVariables parses the document's variable string of 'key1=value1,key2=value2' pairs into a dictionary. Any parse failure (missing '=', empty pair, comma/format problems) is wrapped in this InvalidOperationException that echoes the raw variables string.","triggerScenarios":"LoadAsync receives a variables argument that does not strictly match key=value pairs separated by commas — e.g. 'a=1,b' (missing value), 'a==1', 'a' (no '='), or values containing unescaped '='/' , ' patterns where Split('=')[1] is out of range.","commonSituations":"Passing env-style lists with semicolons instead of commas; quoting values; passing '--variables' values from scripts where the value got truncated; a single value with multiple '=' signs (base64 values).","solutions":["Format variables strictly as key=value pairs separated by commas, e.g. 'key1=value1,key2=value2'.","Ensure every pair contains exactly one '=' with non-empty key and value.","Escape or remove commas/'=' inside values, or pass variables via the document/parameter API that supports arrays."],"exampleFix":"// before\nnswag run nswag.json /variables:\"a=1;b=2\"\n// after\nnswag run nswag.json /variables:\"a=1,b=2\"","handlingStrategy":"validation","validationCode":"bool IsValidVariables(string s) => string.IsNullOrEmpty(s) ||\n    s.Split(',').Where(p => !string.IsNullOrEmpty(p)).All(p => p.Split('=').Length == 2 && p.Split('=')[0].Length > 0);","typeGuard":null,"tryCatchPattern":"try { await document.LoadAsync(inputPath, variables, outputPath); }\ncatch (InvalidOperationException ex) when (ex.Message.StartsWith(\"Could not parse variables\"))\n{ Console.Error.WriteLine($\"Fix /variables format (key1=value1,key2=value2): {ex.Message}\"); throw; }","preventionTips":["Use strict 'key=value' pairs separated by commas only.","Avoid '=' or ',' inside values, or escape/encode them.","Shell-quote the whole /variables value to prevent truncation.","Pre-validate the string with the checker above before calling LoadAsync."],"tags":["variables","parsing","invalid-format","cli"],"backgroundTag":"invalid-argument-format","analyzedSha":"63daf8fcc3a25151b62eb4b326a1e8ea048a0d41","analyzedAt":"2026-09-14T11:38:15.205Z","contentChangedAt":"2026-09-14T11:38:15.205Z","schemaVersion":2},"datasetVersion":"2026-09-16T09:17:16.951Z"}