Bug: data masking prepare_data leaves dataclasses and Pydantic models unconverted

Abierto
#8,672 1 comentario 0 reacciones 0 asignados Ver en GitHub

Los mantenedores suelen responder en 1 día

@therealemmanueloppong ya está trabajando en esto.

Desde el 1/11/2026.

Evaluación

Dificultad
3/5
Tiempo estimado
1-2 días
Aptitud para principiantes
57/100
Tipo de issue
Error
Claridad
Bien especificado
Estado de actividad
Activo
Stack tecnológico
python
Área
backend

Línea de trabajo

Start with prepare_data in aws_lambda_powertools.utilities.data_masking.base and inspect how _visited is updated during recursive conversion. Run the reproduction in the issue and the existing circular-reference test, then add regression coverage in tests/functional/data_masking/_pydantic/test_data_masking_with_pydantic.py. Done means repeated dataclass and Pydantic instances are converted while circular references remain detected.

Escrito por el modelo de indexación a partir del texto del issue.

Descripción

Expected Behaviour

DataMasking.erase (and the other operations that go through prepare_data) should convert every dataclass and Pydantic model in the input to plain data, however many there are and whether or not the same instance appears twice.

Current Behaviour

prepare_data records the id() of every object it has visited in _visited to detect circular references, and never removes them. That includes the temporary dicts built by dataclasses.asdict() and model_dump(). Once one of those is freed, CPython reuses its id for a later object, which is then taken for a circular reference and returned unconverted. An instance referenced twice is skipped the same way. erase then fails with TypeError: Object of type X is not JSON serializable.

Code snippet
from dataclasses import dataclass
from pydantic import BaseModel
from aws_lambda_powertools.utilities.data_masking import DataMasking
from aws_lambda_powertools.utilities.data_masking.base import prepare_data

class Card(BaseModel):
    number: str

@dataclass
class Item:
    card: Card

print(prepare_data({"items": [Item(Card(number=str(i))) for i in range(5)]}))
# some items still contain Card(...) objects instead of dicts

@dataclass
class Person:
    age: int

p = Person(age=30)
DataMasking().erase({"items": [p, p]}, fields=["items[*].age"])
# TypeError: Object of type Person is not JSON serializable
Possible Solution

Keep in _visited only the objects on the current conversion path (add before recursing, remove in a finally). Circular references are still caught, and the existing circular-reference test still passes. I have this fix with regression tests in tests/functional/data_masking/_pydantic/test_data_masking_with_pydantic.py (fail on develop, pass with the change) and can open a PR once this is triaged.

Steps to Reproduce

Run the snippet above on develop (5785377).

Powertools for AWS Lambda (Python) version

latest (3.37.0, develop)

AWS Lambda function runtime

3.13

Packaging format used

PyPi

Debugging logs
N/A
Lenguaje dominante
Python
Estrellas
3.3k
Forks
511
Merge medio
3 d 8 h
PR fusionados (30 d)
92

Preparar el entorno

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de aws-powertools/powertools-lambda-python

Todos los issues de aws-powertools/powertools-lambda-python

Issues similares

Más issues de Python

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.