{"record":{"id":"bc7599857f25bcf7","repo":"karatelabs/karate","slug":"invalid-callbackport-type-portvalue-getclass","errorCode":null,"errorMessage":"Invalid callbackPort type: \" + portValue.getClass()","messagePattern":"Invalid callbackPort type: \" \\+ portValue\\.getClass\\(\\)","errorType":"validation","errorClass":"OAuth2Exception","httpStatus":null,"severity":"error","filePath":"karate-core/src/main/java/io/karatelabs/http/AuthorizationCodeAuthHandler.java","lineNumber":285,"sourceCode":"        // Default ports (Postman-style)\n        return new int[] { 8080, 8888, 9090, 3000 };\n    }\n\n    /**\n     * Parse a single port value\n     */\n    private int parsePort(Object portValue) {\n        if (portValue instanceof Integer) {\n            return (Integer) portValue;\n        }\n        if (portValue instanceof String) {\n            try {\n                return Integer.parseInt((String) portValue);\n            } catch (NumberFormatException e) {\n                throw new OAuth2Exception(\"Invalid callbackPort: \" + portValue);\n            }\n        }\n        throw new OAuth2Exception(\"Invalid callbackPort type: \" + portValue.getClass());\n    }\n\n    /**\n     * Parse comma-separated port list\n     */\n    private int[] parsePortList(Object portsValue) {\n        if (portsValue instanceof String) {\n            String[] parts = ((String) portsValue).split(\",\");\n            int[] ports = new int[parts.length];\n            for (int i = 0; i < parts.length; i++) {\n                try {\n                    ports[i] = Integer.parseInt(parts[i].trim());\n                } catch (NumberFormatException e) {\n                    throw new OAuth2Exception(\"Invalid port in callbackPorts: \" + parts[i]);\n                }\n            }\n            return ports;\n        }","sourceCodeStart":267,"sourceCodeEnd":303,"githubUrl":"https://github.com/karatelabs/karate/blob/a22eb90246d958d15a47bf436693d0121ad2812d/karate-core/src/main/java/io/karatelabs/http/AuthorizationCodeAuthHandler.java#L267-L303","documentation":"Thrown by parsePort when the callbackPort value is neither an Integer nor a String — e.g. a Long, Double, Boolean, Map, or List. The handler only supports Integer and (parseable) String types for callbackPort and reports the actual offending class.","triggerScenarios":"callbackPort configured as a Long (common when read from JSON/YAML where the number overflows Integer parsing in the host tool), a Double (8080.0), or any non-scalar object.","commonSituations":"Config loaded from JSON/YAML where the value was quoted/typed differently; a JavaScript number arriving as Double from Karate's embedded JS; passing a config object field of the wrong type programmatically.","solutions":["Cast or convert the value to Integer/String before assigning it to callbackPort.","If the value is a Long/Double (e.g. from JS or YAML), convert with ((Number) value).intValue().","Check the source file: unquote it to a plain integer, and ensure no decimal point is present."],"exampleFix":"// before\nconfig.callbackPort = 8080.0; // Double from JS/YAML\n// after\nconfig.callbackPort = (int) 8080; // or Integer.parseInt(String.valueOf(value))","handlingStrategy":"type-guard","validationCode":"Object v = config.get(\"callbackPort\");\nif (!(v instanceof Integer) && !(v instanceof String)) {\n    throw new IllegalArgumentException(\"callbackPort must be Integer or String, got: \" + v.getClass());\n}","typeGuard":"boolean isAcceptedCallbackPortType(Object v) {\n    return v instanceof Integer || v instanceof String;\n}\n// coerce numeric types first:\nInteger coercePort(Object v) {\n    if (v instanceof Number) return ((Number) v).intValue();\n    if (v instanceof String) return Integer.valueOf(((String) v).trim());\n    return null;\n}","tryCatchPattern":"try {\n    handler.token();\n} catch (OAuth2Exception e) {\n    if (e.getMessage().startsWith(\"Invalid callbackPort type:\")) {\n        logger.error(\"Coerce callbackPort to Integer/String: {}\", e.getMessage());\n    }\n}","preventionTips":["Coerce JS/YAML numbers (Double/Long) to int before assigning callbackPort.","Never leave callbackPort as a Map/List/Boolean.","Add a config-schema check in test setup."],"tags":["oauth2","configuration","type-mismatch"],"backgroundTag":"type-mismatch","analyzedSha":"a22eb90246d958d15a47bf436693d0121ad2812d","analyzedAt":"2026-09-12T09:01:00.220Z","contentChangedAt":"2026-09-12T09:01:00.220Z","schemaVersion":2},"datasetVersion":"2026-09-16T19:17:19.609Z"}