{"record":{"id":"d1f59e482444f795","repo":"reactiveui/refit","slug":"cannot-convert-an-empty-value-to-typeof-tenum","errorCode":null,"errorMessage":"Cannot convert an empty value to {typeof(TEnum)}.","messagePattern":"Cannot convert an empty value to (.+?)\\.","errorType":"exception","errorClass":"JsonException","httpStatus":null,"severity":"error","filePath":"src/Refit/CamelCaseStringEnumConverter.cs","lineNumber":208,"sourceCode":"\n                valuesToNames[value] = preferredName;\n            }\n\n            return (namesToValues, namesToValuesIgnoreCase, valuesToNames);\n        }\n\n        /// <summary>Reads an enum value from either a string name or a numeric value.</summary>\n        /// <param name=\"reader\">The reader positioned on the value to read.</param>\n        /// <returns>The parsed enum value.</returns>\n        /// <exception cref=\"JsonException\">The value is an empty or whitespace string, a name that maps to no enum field, or a token that is neither a string nor a number.</exception>\n        internal TEnum ReadValue(ref Utf8JsonReader reader)\n        {\n            if (reader.TokenType is JsonTokenType.String or JsonTokenType.PropertyName)\n            {\n                var value = reader.GetString();\n                if (value is null || string.IsNullOrWhiteSpace(value))\n                {\n                    throw new JsonException($\"Cannot convert an empty value to {typeof(TEnum)}.\");\n                }\n\n                if (_namesToValues.TryGetValue(value!, out var namedValue))\n                {\n                    return namedValue;\n                }\n\n                if (_namesToValuesIgnoreCase.TryGetValue(value!, out var namedValueIgnoreCase))\n                {\n                    return namedValueIgnoreCase;\n                }\n\n                throw new JsonException($\"Unable to convert '{value}' to {typeof(TEnum)}.\");\n            }\n\n            if (reader.TokenType == JsonTokenType.Number)\n            {\n                return EnumHelpers.Info<TEnum>.ReadJsonNumericValue(ref reader);","sourceCodeStart":190,"sourceCodeEnd":226,"githubUrl":"https://github.com/reactiveui/refit/blob/b455f65ecc4c97d092317e349cb775f9cfc6bcdf/src/Refit/CamelCaseStringEnumConverter.cs#L190-L226","documentation":"Thrown by CamelCaseStringEnumConverter when the JSON reader is on a string (or property name) token whose value is null or entirely whitespace, and the target type is an enum. An empty value cannot map to any enum field, so deserialization fails with a JsonException.","triggerScenarios":"Deserializing JSON into an enum-typed property where the JSON value is \"\" or \"   \", e.g. `{ \"status\": \"\" }`. This can occur for either a non-nullable enum or, depending on the converter path, a nullable enum whose value is present but blank.","commonSituations":"An API returns an empty string for an absent enum value instead of null or omitting the field; data from a form/CSV import that produced blank enum cells; a default value mis-set to empty string.","solutions":["Make the receiving property nullable (MyEnum?) and ensure the JSON omits the field or sends null for absent values.","Fix the server/source to send a valid enum name or null instead of an empty string.","Pre-process/sanitize the JSON (replace blank enum strings with null) before deserialization if you cannot change the source."],"exampleFix":"// before — non-nullable enum, API sends \"\"\npublic sealed record Payload(Status Status);\nvar p = JsonSerializer.Deserialize<Payload>(\"{\\\"status\\\":\\\"\\\"}\"); // throws\n\n// after — nullable enum tolerates missing/blank (after sanitizing)\npublic sealed record Payload(Status? Status);\n// or have the API send null / omit the field / send a valid name","handlingStrategy":"validation","validationCode":"// Make the enum nullable so blank values can be tolerated, and sanitize input.\npublic sealed record Payload(Status? Status);\n// Before deserializing, replace blank enum strings with null if you cannot change the source:\nvar sanitized = Regex.Replace(json, @\"\"\"(\\w+)\"\"\\s*:\\s*\"\"\\s*\"\"\", \"$1:null\");","typeGuard":null,"tryCatchPattern":"try { return JsonSerializer.Deserialize<Payload>(json); }\ncatch (JsonException) { /* log/handle bad enum value */ return null; }","preventionTips":["Make enum DTO properties nullable when the source may omit or blank them.","Have the server send null or omit the field instead of an empty string for absent enum values.","Add a JSON-schema/contract test that enum fields only contain valid names."],"tags":["json","enum","deserialization","system-text-json"],"backgroundTag":null,"analyzedSha":"b455f65ecc4c97d092317e349cb775f9cfc6bcdf","analyzedAt":"2026-08-13T21:20:57.878Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}