{"record":{"id":"a80c260b737ed71a","repo":"alibaba/nacos","slug":"can-t-get-batched-data-from-single-data","errorCode":null,"errorMessage":"Can't get batched data from single data.","messagePattern":"Can't get batched data from single data\\.","errorType":"exception","errorClass":"UnsupportedOperationException","httpStatus":null,"severity":"error","filePath":"client/src/main/java/com/alibaba/nacos/client/ai/remote/redo/AgentEndpointWrapper.java","lineNumber":64,"sourceCode":"    \n    public static AgentEndpointWrapper wrap(Collection<AgentEndpoint> data) {\n        return new AgentEndpointWrapper(data, true);\n    }\n    \n    public boolean isBatch() {\n        return isBatch;\n    }\n    \n    public AgentEndpoint getData() {\n        if (isBatch) {\n            throw new UnsupportedOperationException(\"Can't get single data from batched data.\");\n        }\n        return data.iterator().next();\n    }\n    \n    public Collection<AgentEndpoint> getBatchData() {\n        if (!isBatch) {\n            throw new UnsupportedOperationException(\"Can't get batched data from single data.\");\n        }\n        return data;\n    }\n    \n    /**\n     * Return the exact Agent Version shared by this wrapper's Endpoint payload.\n     *\n     * @return exact Version\n     */\n    public String getVersion() {\n        return data.iterator().next().getVersion();\n    }\n    \n    private static Collection<AgentEndpoint> copyEndpoints(Collection<AgentEndpoint> source) {\n        List<AgentEndpoint> result = new ArrayList<AgentEndpoint>(source.size());\n        for (AgentEndpoint endpoint : source) {\n            result.add(copyEndpoint(endpoint));\n        }","sourceCodeStart":46,"sourceCodeEnd":82,"githubUrl":"https://github.com/alibaba/nacos/blob/9b989acdf181d00898f2e8839257bb2b2a3cefe3/client/src/main/java/com/alibaba/nacos/client/ai/remote/redo/AgentEndpointWrapper.java#L46-L82","documentation":"Thrown as UnsupportedOperationException by AgentEndpointWrapper.getBatchData() when the wrapper was created in single mode (via wrap(AgentEndpoint)). getBatchData() is the batch accessor and is only valid on wrappers created via wrap(Collection<AgentEndpoint>). This is a programming error indicating a type/access-pattern mismatch.","triggerScenarios":"Code calls wrapper.getBatchData() on a wrapper instance created by AgentEndpointWrapper.wrap(AgentEndpoint) — i.e., the wrapper holds a single endpoint but the caller expects a collection. This happens when redo/registration logic processes a single endpoint but a code path assumes batch semantics.","commonSituations":"Agent endpoint registration where single and batch code paths share a consumer. A developer refactored to use batch access but the wrapper was still created in single mode. Generic processing code that always calls getBatchData() without checking isBatch().","solutions":["Check wrapper.isBatch() before calling getBatchData() and branch to getData() when false.","If the consumer needs batch semantics, ensure the wrapper is created via wrap(Collection) at the source.","For generic code, normalize: if !isBatch(), wrap getData() result in Collections.singletonList() before processing as a collection."],"exampleFix":"// before: always calls getBatchData, throws on single wrapper\nCollection<AgentEndpoint> endpoints = wrapper.getBatchData();\n\n// after: normalize to collection regardless of mode\nCollection<AgentEndpoint> endpoints = wrapper.isBatch()\n    ? wrapper.getBatchData()\n    : Collections.singletonList(wrapper.getData());","handlingStrategy":"type-guard","validationCode":null,"typeGuard":"// Type guard: normalize to collection regardless of wrapper mode\npublic Collection<AgentEndpoint> safeGetAll(AgentEndpointWrapper wrapper) {\n    if (wrapper.isBatch()) {\n        return wrapper.getBatchData();\n    }\n    return Collections.singletonList(wrapper.getData());\n}","tryCatchPattern":"try {\n    Collection<AgentEndpoint> endpoints = wrapper.getBatchData();\n} catch (UnsupportedOperationException e) {\n    // wrapper is single mode — use getData() instead\n    AgentEndpoint single = wrapper.getData();\n    endpoints = Collections.singletonList(single);\n}","preventionTips":["Use a normalization helper that checks isBatch() and returns a collection in both cases.","Keep wrap() call sites and consumer code in sync — if you change one, update the other.","Add unit tests covering both single and batch wrapper modes."],"tags":["nacos","ai-client","agent-endpoint","type-mismatch","programming-error"],"backgroundTag":null,"analyzedSha":"9b989acdf181d00898f2e8839257bb2b2a3cefe3","analyzedAt":"2026-08-14T07:17:31.569Z","schemaVersion":2},"datasetVersion":"2026-08-14T10:17:34.591Z"}