FasterXML/jackson-databind · error · IllegalArgumentException
Cannot set maxSerializerCacheSize to a negative value
Error message
Cannot set maxSerializerCacheSize to a negative value
What it means
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.
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.
Example fix
// before DefaultCacheProvider.builder().maxSerializerCacheSize(configuredSize); // throws if -1 // after DefaultCacheProvider.builder().maxSerializerCacheSize(Math.max(0, configuredSize));
Defensive patterns
Strategy: validation
Validate before calling
// Validate before passing to builder
if (serCacheSize < 0) {
throw new IllegalArgumentException("serCacheSize must be >= 0, got: " + serCacheSize);
}
DefaultCacheProvider.builder().maxSerializerCacheSize(serCacheSize); Type guard
boolean isValidCacheSize(int size) {
return size >= 0;
} Prevention
- Clamp external configuration: Math.max(0, configuredValue).
- Use 0 to disable caching intentionally; never pass negative.
- Validate external config at startup.
When it happens
Trigger: Calling DefaultCacheProvider.builder().maxSerializerCacheSize(-1) or passing a negative value derived from an external configuration or arithmetic error.
Common situations: 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.
Related errors
- Cannot set maxDeserializerCacheSize to a negative value
- Cannot set maxTypeFactoryCacheSize to a negative value
- AnnotationIntrospector.
- AnnotationIntrospector returned `Class<
- AnnotationIntrospector returned Class
AI-assisted analysis of FasterXML/jackson-databind@87876ca5c0 (2026-08-11).
Data as JSON: /api/errors/23a634e6ac77f50f.
Report an issue: GitHub.
Appendix: source
Thrown at src/main/java/tools/jackson/databind/cfg/DefaultCacheProvider.java:184
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");
}
_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");View on GitHub (pinned to 87876ca5c0)