{"record":{"id":"4de4420675aa9009","repo":"FasterXML/jackson-databind","slug":"cannot-deserialize-singleton-container-from-size","errorCode":null,"errorMessage":"Cannot deserialize Singleton container from \"+size+\" entries","messagePattern":"Cannot deserialize Singleton container from \"\\+size\\+\" entries","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/tools/jackson/databind/deser/jdk/JavaUtilCollectionsDeserializers.java","lineNumber":266,"sourceCode":"                return value;\n            }\n        }\n\n        @Override\n        public JavaType getInputType(TypeFactory typeFactory) {\n            return _inputType;\n        }\n\n        @Override\n        public JavaType getOutputType(TypeFactory typeFactory) {\n            // we don't actually care, so:\n            return _inputType;\n        }\n\n        private void _checkSingleton(int size) {\n            if (size != 1) {\n                // not the best error ever but... has to do\n                throw new IllegalArgumentException(\"Cannot deserialize Singleton container from \"+size+\" entries\");\n            }\n        }\n    }\n}\n","sourceCodeStart":248,"sourceCodeEnd":271,"githubUrl":"https://github.com/FasterXML/jackson-databind/blob/87876ca5c0569b4933aec2d30d6225e4b9ba3a43/src/main/java/tools/jackson/databind/deser/jdk/JavaUtilCollectionsDeserializers.java#L248-L271","documentation":"JavaUtilCollectionsDeserializers uses a Converter to rebuild values whose declared type is one of java.util.Collections' singleton* return types (SingletonList, SingletonSet, SingletonMap). The converter _checkSingleton enforces that the incoming collection has exactly one element, because a singleton container cannot hold more (or fewer) than one. Anything else throws IllegalArgumentException.","triggerScenarios":"A field/parameter whose runtime type is Collections.singletonList(...), Collections.singleton(...), or Collections.singletonMap(...), and the JSON being deserialized contains zero or two-or-more entries; serializing a singleton, mutating the JSON to add entries, then deserializing back.","commonSituations":"Using Collections.singletonList(x) as a constant and then feeding JSON with multiple values into a round-trip; APIs that expose List<T> but assign a singleton list and let Jackson infer the concrete singleton type; tests that hand-craft JSON payloads against singleton-typed fixtures.","solutions":["Change the target type to a plain List/Set/Map (e.g. ArrayList, HashSet, HashMap) so multiple entries are accepted.","Fix the JSON payload so it contains exactly one entry when the target must remain a singleton type.","If the singleton type was inferred unintentionally, declare the field as List<T> rather than letting the runtime singleton type leak into serialization metadata."],"exampleFix":"// before\npublic class Config {\n    public List<String> names = Collections.singletonList(\"default\");\n}\n// JSON: {\"names\":[\"a\",\"b\"]}  -> throws\n\n// after\npublic class Config {\n    public List<String> names = new ArrayList<>(List.of(\"default\"));\n}","handlingStrategy":"validation","validationCode":"// Do not use Collections.singleton* as field runtime types for mutable data.\nif (field.getType().isAssignableFrom(Collections.singletonList(\"\").getClass())) {\n    throw new IllegalStateException(\"Refusing singleton list as a mutable target\");\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Declare collection fields as List/Set/Map interfaces backed by ArrayList/HashSet/HashMap, not by Collections.singleton*.","Validate JSON payloads for singleton-typed targets contain exactly one entry before deserialization.","Avoid reusing singleton instances as deserialize targets in round-trip tests."],"tags":["deserialization","collections","singleton","converter","illegal-argument"],"backgroundTag":null,"analyzedSha":"87876ca5c0569b4933aec2d30d6225e4b9ba3a43","analyzedAt":"2026-08-11T12:55:24.033Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}