{"record":{"id":"a6cf7d3ba0784e29","repo":"OpenAPITools/openapi-generator","slug":"cachescope-requires-key-content-or-key-ncontent","errorCode":null,"errorMessage":"cacheScope requires 'key|content' or 'key\\ncontent'","messagePattern":"cacheScope requires 'key\\|content' or 'key\\\\ncontent'","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"modules/openapi-generator/src/main/java/org/openapitools/codegen/templating/mustache/CacheLambda.java","lineNumber":58,"sourceCode":"    public CacheLambda(CacheContent cacheContent) {\n        this.cacheContent = cacheContent;\n    }\n\n    @Override\n    public void execute(Fragment fragment, Writer writer) throws IOException {\n        String content = fragment.execute();\n\n        // Accept either \"key|content\" or a multiline form where the first line is the key.\n        int separatorIndex = content.indexOf('|');\n        if (separatorIndex < 0) {\n            int lineBreakIndex = findFirstLineBreakIndex(content);\n            if (lineBreakIndex >= 0) {\n                separatorIndex = lineBreakIndex;\n            }\n        }\n\n        if (separatorIndex < 0) {\n            throw new IllegalArgumentException(\"cacheScope requires 'key|content' or 'key\\\\ncontent'\");\n        }\n\n        String key = content.substring(0, separatorIndex).trim();\n        if (key.isEmpty()) {\n            throw new IllegalArgumentException(\"cacheScope key cannot be empty\");\n        }\n\n        int contentStartIndex = separatorIndex + 1;\n        // For CRLF, skip both '\\r' and '\\n'. For LF-only/CR-only, skip just one character.\n        if (separatorIndex < content.length() && content.charAt(separatorIndex) == '\\r') {\n            if (contentStartIndex < content.length() && content.charAt(contentStartIndex) == '\\n') {\n                contentStartIndex++;\n            }\n        }\n\n        this.cacheContent.contentByKey.put(key, content.substring(contentStartIndex));\n    }\n","sourceCodeStart":40,"sourceCodeEnd":76,"githubUrl":"https://github.com/OpenAPITools/openapi-generator/blob/fcec517be3cf5b7964296bcba25fbc97541484e7/modules/openapi-generator/src/main/java/org/openapitools/codegen/templating/mustache/CacheLambda.java#L40-L76","documentation":"CacheLambda is a mustache lambda (registered as 'cache' by AbstractCSharpCodegen for C# generators) that stores a template fragment for later reuse: its body must be 'key|content' or a first-line key followed by content. If the rendered fragment contains neither '|' nor a line break, no separator exists to split key from content and an IllegalArgumentException is thrown at template render time. This is an authoring error in the mustache template, not in the OpenAPI spec.","triggerScenarios":"A custom/modified mustache template (e.g. a C# modelTemplate) containing {{#cache}}UserModel{{/cache}} — the whole body is one line with no '|', so separatorIndex stays -1 and the lambda throws while rendering.","commonSituations":"Teams editing bundled mustache templates to add caching of repeated blocks; forgetting the pipe convention when copying from {{#recall}} examples; whitespace-stripping template pipelines that collapse the newline of a multiline key/content form.","solutions":["Add a separator: write {{#cache}}key|content{{/cache}}, or put the key alone on the first line and the content on following lines.","Verify with a trivial test render (one model) before running full generation so template errors surface quickly.","Check you are using {{#cache}} for storing and {{#recall}}/{{#recallOnce}} for reading — mixing them up produces empty or malformed bodies."],"exampleFix":"{{! before }}\n{{#cache}}UserModel{{/cache}}\n\n{{! after }}\n{{#cache}}UserModel|public partial class UserModel { }{{/cache}}","handlingStrategy":"validation","validationCode":"# crude template lint: every cache block must contain '|' or span multiple lines\ngrep -rnE '\\{\\{#cache\\}\\}[^|}]*\\{\\{/cache\\}\\}' templates/ && {\n  echo \"ERROR: {{#cache}} body needs 'key|content' or a newline after the key\" >&2; exit 1;\n}","typeGuard":null,"tryCatchPattern":"try {\n    template.execute(scope, writer);\n} catch (IllegalArgumentException e) {\n    // template authoring bug: report file + block, not the raw stack\n    throw new RuntimeException(\"cache lambda misuse in template: \" + e.getMessage(), e);\n}","preventionTips":["Author cache blocks in the fixed 'key|content' shape and lint templates for it.","Do a single-model smoke generation after every template edit so lambda errors surface immediately."],"tags":["mustache","template","csharp","codegen","cache-lambda"],"backgroundTag":"mustache-lambda-template-error","analyzedSha":"fcec517be3cf5b7964296bcba25fbc97541484e7","analyzedAt":"2026-08-22T11:13:11.613Z","schemaVersion":2},"datasetVersion":"2026-08-22T14:17:55.899Z"}