FasterXML/jackson-databind · error · IllegalArgumentException

Cannot set maxDeserializerCacheSize to a negative value

Error message

Cannot set maxDeserializerCacheSize to a negative value

What it means

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.

Solutions

  1. Pass a non-negative integer: use 0 to disable caching, or a positive number to set the cap.
  2. Clamp external config: Math.max(0, configuredValue) before passing to the builder.
  3. If you want the default, simply do not call maxDeserializerCacheSize() at all.

Example fix

// before
int size = config.getInt("cache.deser.size", -1);
DefaultCacheProvider.builder().maxDeserializerCacheSize(size); // throws
// after
int size = config.getInt("cache.deser.size", DefaultCacheProvider.DEFAULT_MAX_CACHE_SIZE);
if (size < 0) size = 0;
DefaultCacheProvider.builder().maxDeserializerCacheSize(size);
Defensive patterns

Strategy: validation

Validate before calling

// Validate before passing to builder
if (deserCacheSize < 0) {
    throw new IllegalArgumentException("deserCacheSize must be >= 0, got: " + deserCacheSize);
}
DefaultCacheProvider.builder().maxDeserializerCacheSize(deserCacheSize);

Type guard

boolean isValidCacheSize(int size) {
    return size >= 0;
}

Prevention

When it happens

Trigger: 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).

Common situations: 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.

Related errors


AI-assisted analysis of FasterXML/jackson-databind@87876ca5c0 (2026-08-11). Data as JSON: /api/errors/6e05e72e6dd2bdac. Report an issue: GitHub.

Appendix: source

Thrown at src/main/java/tools/jackson/databind/cfg/DefaultCacheProvider.java:166

         * Corresponds to {@link DefaultCacheProvider#_maxTypeFactoryCacheSize}.
         */
        private int _maxTypeFactoryCacheSize = TypeFactory.DEFAULT_MAX_CACHE_SIZE;

        Builder() { }

        /**
         * Define the maximum size of the {@link LookupCache} instance constructed by {@link #forDeserializerCache(DeserializationConfig)}
         * and {@link #_buildCache(int)}.
         * <p>
         * Note that specifying a maximum size of zero prevents values from being retained in the cache.
         *
         * @param maxDeserializerCacheSize Size for the {@link LookupCache} to use within {@link DeserializerCache}
         * @return this builder
         * @throws IllegalArgumentException if {@code maxDeserializerCacheSize} is negative
         */
        public Builder maxDeserializerCacheSize(int maxDeserializerCacheSize) {
            if (maxDeserializerCacheSize < 0) {
                throw new IllegalArgumentException("Cannot set maxDeserializerCacheSize to a negative value");
            }
            _maxDeserializerCacheSize = maxDeserializerCacheSize;
            return this;
        }

        /**
         * Define the maximum size of the {@link LookupCache} instance constructed by {@link #forSerializerCache(SerializationConfig)}
         * and {@link #_buildCache(int)}
         * <p>
         * Note that specifying a maximum size of zero prevents values from being retained in the cache.
         *
         * @param maxSerializerCacheSize Size for the {@link LookupCache} to use within {@link SerializerCache}
         * @return this builder
         * @throws IllegalArgumentException if {@code maxSerializerCacheSize} is negative
         */
        public Builder maxSerializerCacheSize(int maxSerializerCacheSize) {
            if (maxSerializerCacheSize < 0) {
                throw new IllegalArgumentException("Cannot set maxSerializerCacheSize to a negative value");

View on GitHub (pinned to 87876ca5c0)