{"record":{"id":"23a634e6ac77f50f","repo":"FasterXML/jackson-databind","slug":"cannot-set-maxserializercachesize-to-a-negative-va","errorCode":null,"errorMessage":"Cannot set maxSerializerCacheSize to a negative value","messagePattern":"Cannot set maxSerializerCacheSize to a negative value","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/tools/jackson/databind/cfg/DefaultCacheProvider.java","lineNumber":184,"sourceCode":"                throw new IllegalArgumentException(\"Cannot set maxDeserializerCacheSize to a negative value\");\n            }\n            _maxDeserializerCacheSize = maxDeserializerCacheSize;\n            return this;\n        }\n\n        /**\n         * Define the maximum size of the {@link LookupCache} instance constructed by {@link #forSerializerCache(SerializationConfig)}\n         * and {@link #_buildCache(int)}\n         * <p>\n         * Note that specifying a maximum size of zero prevents values from being retained in the cache.\n         *\n         * @param maxSerializerCacheSize Size for the {@link LookupCache} to use within {@link SerializerCache}\n         * @return this builder\n         * @throws IllegalArgumentException if {@code maxSerializerCacheSize} is negative\n         */\n        public Builder maxSerializerCacheSize(int maxSerializerCacheSize) {\n            if (maxSerializerCacheSize < 0) {\n                throw new IllegalArgumentException(\"Cannot set maxSerializerCacheSize to a negative value\");\n            }\n            _maxSerializerCacheSize = maxSerializerCacheSize;\n            return this;\n        }\n\n        /**\n         * Define the maximum size of the {@link LookupCache} instance constructed by {@link #forTypeFactory()}\n         * and {@link #_buildCache(int)}\n         * <p>\n         * Note that specifying a maximum size of zero prevents values from being retained in the cache.\n         *\n         * @param maxTypeFactoryCacheSize Size for the {@link LookupCache} to use within {@link tools.jackson.databind.type.TypeFactory}\n         * @return this builder\n         * @throws IllegalArgumentException if {@code maxTypeFactoryCacheSize} is negative\n         */\n        public Builder maxTypeFactoryCacheSize(int maxTypeFactoryCacheSize) {\n            if (maxTypeFactoryCacheSize < 0) {\n                throw new IllegalArgumentException(\"Cannot set maxTypeFactoryCacheSize to a negative value\");","sourceCodeStart":166,"sourceCodeEnd":202,"githubUrl":"https://github.com/FasterXML/jackson-databind/blob/87876ca5c0569b4933aec2d30d6225e4b9ba3a43/src/main/java/tools/jackson/databind/cfg/DefaultCacheProvider.java#L166-L202","documentation":"Thrown by DefaultCacheProvider.Builder.maxSerializerCacheSize(int) when the argument is negative. The serializer cache is a bounded LookupCache; a negative size is meaningless. Zero disables caching; negative is rejected as a programming error.","triggerScenarios":"Calling DefaultCacheProvider.builder().maxSerializerCacheSize(-1) or passing a negative value derived from an external configuration or arithmetic error.","commonSituations":"External configuration with a sentinel -1 default. Arithmetic underflow in computed cache sizes. Copy-paste of a value that was valid for a different API.","solutions":["Pass a non-negative integer: 0 to disable, positive to set the cap.","Clamp external config: Math.max(0, configuredValue).","Omit the call entirely if you want the default cache size."],"exampleFix":"// before\nDefaultCacheProvider.builder().maxSerializerCacheSize(configuredSize); // throws if -1\n// after\nDefaultCacheProvider.builder().maxSerializerCacheSize(Math.max(0, configuredSize));","handlingStrategy":"validation","validationCode":"// Validate before passing to builder\nif (serCacheSize < 0) {\n    throw new IllegalArgumentException(\"serCacheSize must be >= 0, got: \" + serCacheSize);\n}\nDefaultCacheProvider.builder().maxSerializerCacheSize(serCacheSize);","typeGuard":"boolean isValidCacheSize(int size) {\n    return size >= 0;\n}","tryCatchPattern":null,"preventionTips":["Clamp external configuration: Math.max(0, configuredValue).","Use 0 to disable caching intentionally; never pass negative.","Validate external config at startup."],"tags":["cache","configuration","builder","validation","serializer-cache"],"backgroundTag":null,"analyzedSha":"87876ca5c0569b4933aec2d30d6225e4b9ba3a43","analyzedAt":"2026-08-11T12:55:24.033Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}