FasterXML/jackson-databind · error · IllegalArgumentException

Cannot set maxTypeFactoryCacheSize to a negative value

Error message

Cannot set maxTypeFactoryCacheSize to a negative value

What it means

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.

Solutions

  1. Pass a non-negative integer. Use a reasonable positive value (e.g., 200+) to avoid performance degradation.
  2. Clamp external config: Math.max(0, configuredValue) but warn if the result is 0.
  3. Omit the call to use the default TypeFactory.DEFAULT_MAX_CACHE_SIZE.

Example fix

// before
DefaultCacheProvider.builder().maxTypeFactoryCacheSize(configValue); // throws if negative
// after
int size = configValue < 0 ? TypeFactory.DEFAULT_MAX_CACHE_SIZE : configValue;
DefaultCacheProvider.builder().maxTypeFactoryCacheSize(size);
Defensive patterns

Strategy: validation

Validate before calling

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

Type guard

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

Prevention

When it happens

Trigger: Calling DefaultCacheProvider.builder().maxTypeFactoryCacheSize(-1) or passing a negative value from an external source or computation.

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

Related errors


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

Appendix: source

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

                throw new IllegalArgumentException("Cannot set maxSerializerCacheSize to a negative value");
            }
            _maxSerializerCacheSize = maxSerializerCacheSize;
            return this;
        }

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

        /**
         * Constructs a {@link DefaultCacheProvider} with the provided configuration values, using defaults where not specified.
         *
         * @return A {@link DefaultCacheProvider} instance with the specified configuration
         */
        public DefaultCacheProvider build() {
            return new DefaultCacheProvider(_maxDeserializerCacheSize, _maxSerializerCacheSize, _maxTypeFactoryCacheSize);
        }
    }
}

View on GitHub (pinned to 87876ca5c0)