{"record":{"id":"6e05e72e6dd2bdac","repo":"FasterXML/jackson-databind","slug":"cannot-set-maxdeserializercachesize-to-a-negative","errorCode":null,"errorMessage":"Cannot set maxDeserializerCacheSize to a negative value","messagePattern":"Cannot set maxDeserializerCacheSize to a negative value","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/tools/jackson/databind/cfg/DefaultCacheProvider.java","lineNumber":166,"sourceCode":"         * Corresponds to {@link DefaultCacheProvider#_maxTypeFactoryCacheSize}.\n         */\n        private int _maxTypeFactoryCacheSize = TypeFactory.DEFAULT_MAX_CACHE_SIZE;\n\n        Builder() { }\n\n        /**\n         * Define the maximum size of the {@link LookupCache} instance constructed by {@link #forDeserializerCache(DeserializationConfig)}\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 maxDeserializerCacheSize Size for the {@link LookupCache} to use within {@link DeserializerCache}\n         * @return this builder\n         * @throws IllegalArgumentException if {@code maxDeserializerCacheSize} is negative\n         */\n        public Builder maxDeserializerCacheSize(int maxDeserializerCacheSize) {\n            if (maxDeserializerCacheSize < 0) {\n                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\");","sourceCodeStart":148,"sourceCodeEnd":184,"githubUrl":"https://github.com/FasterXML/jackson-databind/blob/87876ca5c0569b4933aec2d30d6225e4b9ba3a43/src/main/java/tools/jackson/databind/cfg/DefaultCacheProvider.java#L148-L184","documentation":"Thrown by DefaultCacheProvider.Builder.maxDeserializerCacheSize(int) when the argument is negative. The deserializer cache is a bounded LookupCache; a negative size is meaningless. A value of zero is allowed (disables caching) but anything below zero is rejected.","triggerScenarios":"Calling DefaultCacheProvider.builder().maxDeserializerCacheSize(-1) or passing a negative computed value (e.g., derived from a subtraction or an external config that was not validated).","commonSituations":"Configuration from external sources (YAML, properties files) where a cache size defaults to -1 meaning 'unset'. Arithmetic errors in computing cache size (e.g., baseSize - overhead where overhead > baseSize). Misunderstanding that 0 disables caching while negative is invalid.","solutions":["Pass a non-negative integer: use 0 to disable caching, or a positive number to set the cap.","Clamp external config: Math.max(0, configuredValue) before passing to the builder.","If you want the default, simply do not call maxDeserializerCacheSize() at all."],"exampleFix":"// before\nint size = config.getInt(\"cache.deser.size\", -1);\nDefaultCacheProvider.builder().maxDeserializerCacheSize(size); // throws\n// after\nint size = config.getInt(\"cache.deser.size\", DefaultCacheProvider.DEFAULT_MAX_CACHE_SIZE);\nif (size < 0) size = 0;\nDefaultCacheProvider.builder().maxDeserializerCacheSize(size);","handlingStrategy":"validation","validationCode":"// Validate before passing to builder\nif (deserCacheSize < 0) {\n    throw new IllegalArgumentException(\"deserCacheSize must be >= 0, got: \" + deserCacheSize);\n}\nDefaultCacheProvider.builder().maxDeserializerCacheSize(deserCacheSize);","typeGuard":"boolean isValidCacheSize(int size) {\n    return size >= 0;\n}","tryCatchPattern":null,"preventionTips":["Clamp external configuration: Math.max(0, configuredValue).","Remember: 0 disables caching, negative is invalid.","Validate all cache sizes from external sources at application startup."],"tags":["cache","configuration","builder","validation","deserializer-cache"],"backgroundTag":null,"analyzedSha":"87876ca5c0569b4933aec2d30d6225e4b9ba3a43","analyzedAt":"2026-08-11T12:55:24.033Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}