diff --git a/src/serializers/type_serializers/dataclass.rs b/src/serializers/type_serializers/dataclass.rs index 2d7b04598..5b3c425df 100644 --- a/src/serializers/type_serializers/dataclass.rs +++ b/src/serializers/type_serializers/dataclass.rs @@ -43,18 +43,17 @@ impl BuildSerializer for DataclassArgsBuilder { let key_py: Py = PyString::new(py, &name).into(); - if field_info.get_as(intern!(py, "init_only"))? == Some(true) { - // skip fields that are init_only - continue; - } else if field_info.get_as(intern!(py, "serialization_exclude"))? == Some(true) { - fields.insert(name, SerField::new(py, key_py, None, None, true)); - } else { - let schema = field_info.get_as_req(intern!(py, "schema"))?; - let serializer = CombinedSerializer::build(&schema, config, definitions) - .map_err(|e| py_schema_error_type!("Field `{}`:\n {}", index, e))?; - - let alias = field_info.get_as(intern!(py, "serialization_alias"))?; - fields.insert(name, SerField::new(py, key_py, alias, Some(serializer), true)); + if !field_info.get_as(intern!(py, "init_only"))?.unwrap_or(false) { + if field_info.get_as(intern!(py, "serialization_exclude"))? == Some(true) { + fields.insert(name, SerField::new(py, key_py, None, None, true)); + } else { + let schema = field_info.get_as_req(intern!(py, "schema"))?; + let serializer = CombinedSerializer::build(&schema, config, definitions) + .map_err(|e| py_schema_error_type!("Field `{}`:\n {}", index, e))?; + + let alias = field_info.get_as(intern!(py, "serialization_alias"))?; + fields.insert(name, SerField::new(py, key_py, alias, Some(serializer), true)); + } } }