{"record":{"id":"ea95ffc4eec44701","repo":"FasterXML/jackson-databind","slug":"cannot-set-maxtypefactorycachesize-to-a-negative-v","errorCode":null,"errorMessage":"Cannot set maxTypeFactoryCacheSize to a negative value","messagePattern":"Cannot set maxTypeFactoryCacheSize to a negative value","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/tools/jackson/databind/cfg/DefaultCacheProvider.java","lineNumber":202,"sourceCode":"                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\");\n            }\n            _maxTypeFactoryCacheSize = maxTypeFactoryCacheSize;\n            return this;\n        }\n\n        /**\n         * Constructs a {@link DefaultCacheProvider} with the provided configuration values, using defaults where not specified.\n         *\n         * @return A {@link DefaultCacheProvider} instance with the specified configuration\n         */\n        public DefaultCacheProvider build() {\n            return new DefaultCacheProvider(_maxDeserializerCacheSize, _maxSerializerCacheSize, _maxTypeFactoryCacheSize);\n        }\n    }\n}\n","sourceCodeStart":184,"sourceCodeEnd":218,"githubUrl":"https://github.com/FasterXML/jackson-databind/blob/87876ca5c0569b4933aec2d30d6225e4b9ba3a43/src/main/java/tools/jackson/databind/cfg/DefaultCacheProvider.java#L184-L218","documentation":"Thrown by DefaultCacheProvider.Builder.maxTypeFactoryCacheSize(int) when the argument is negative. The TypeFactory cache stores resolved JavaType instances; a negative size is invalid. Zero disables caching (forces every type to be re-resolved); negative is rejected.","triggerScenarios":"Calling DefaultCacheProvider.builder().maxTypeFactoryCacheSize(-1) or passing a negative value from an external source or computation.","commonSituations":"External configuration defaults. Note: setting this to 0 has a significant performance impact since TypeFactory resolution is called frequently; ensure the value is not accidentally zero or negative.","solutions":["Pass a non-negative integer. Use a reasonable positive value (e.g., 200+) to avoid performance degradation.","Clamp external config: Math.max(0, configuredValue) but warn if the result is 0.","Omit the call to use the default TypeFactory.DEFAULT_MAX_CACHE_SIZE."],"exampleFix":"// before\nDefaultCacheProvider.builder().maxTypeFactoryCacheSize(configValue); // throws if negative\n// after\nint size = configValue < 0 ? TypeFactory.DEFAULT_MAX_CACHE_SIZE : configValue;\nDefaultCacheProvider.builder().maxTypeFactoryCacheSize(size);","handlingStrategy":"validation","validationCode":"// Validate before passing to builder\nif (typeFactoryCacheSize < 0) {\n    throw new IllegalArgumentException(\"typeFactoryCacheSize must be >= 0, got: \" + typeFactoryCacheSize);\n}\nDefaultCacheProvider.builder().maxTypeFactoryCacheSize(typeFactoryCacheSize);","typeGuard":"boolean isValidCacheSize(int size) {\n    return size >= 0;\n}","tryCatchPattern":null,"preventionTips":["Never set type factory cache to 0 or negative in production (severe performance impact).","Clamp external configuration to at least TypeFactory.DEFAULT_MAX_CACHE_SIZE.","Validate external config at startup."],"tags":["cache","configuration","builder","validation","type-factory"],"backgroundTag":null,"analyzedSha":"87876ca5c0569b4933aec2d30d6225e4b9ba3a43","analyzedAt":"2026-08-11T12:55:24.033Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}