Azure / Azure/azure-functions-durable-python

Custom object serialization broken for orchestrator return values

Ouverte Adaptée aux débutants
#568 1 commentaire 0 réactions 0 personnes assignées Voir sur GitHub
bug fixed-in-v2 P2
Langage dominant
Python
Étoiles
157
Forks
70
Merge moyen
2 j 10 h
PR mergées (30 j)
2

Description

🐛 **Describe the bug**
Azure Durable Functions Python SDK fails to serialize custom objects that implement a `to_json()` method when returning from orchestrator functions. The error "_Object of type [CustomClass] is not JSON serializable_" is raised.

🤔 **Expected behavior**
Custom objects implementing a `to_json()` method should be automatically serialized when returned from orchestrator functions.

☕ **Steps to reproduce**
Basic orchestration with custom return types.

1. Create a custom class with `to_json()` and `from_json()` methods:
```python
from dataclasses import dataclass

import azure.durable_functions as df
import azure.functions as func

app = df.DFApp(http_auth_level=func.AuthLevel.ANONYMOUS)

@dataclass
class CustomResult():
message: str
success: bool = True

def to_json(self):
return {"message": self.message, "success": self.success}

@classmethod
def from_json(cls, data):
return cls(message=data["message"], success=data["success"])

@app.orchestration_trigger(context_name="context")
def orchestrator_function(context: df.DurableOrchestrationContext):
return CustomResult(message="Hello from orchestrator!", success=True)

@app.route(route="test", methods=["GET"])
@app.durable_client_input(client_name="client")
async def http_start_conversion(req: func.HttpRequest, client: df.DurableOrchestrationClient) -> func.HttpResponse:
instance_id = await client.start_new(
orchestration_function_name="orchestrator_function"
)

return client.create_check_status_response(req, instance_id)

```

2. Execute the orchestration - it will fail with "Object of type CustomResult is not JSON serializable"

**Environment**: Running locally with
Azure Functions Core Tools Version: 4.1.0
azure-functions-durable: 1.3.2

**Root cause**: In `TaskOrchestrationExecutor.get_orchestrator_state_str()`, the code calls dumps without a default argument:
https://github.com/Azure/azure-functions-durable-python/blob/15ed958555b9410629f97cd7884d8783cb0e460e/azure/durable_functions/models/TaskOrchestrationExecutor.py#L284

But should call:
`json.dumps(self._data, default=_serialize_custom_object)` as in other models or entities.

⚡**If deployed to Azure**
Didn't test yet. Since the bug is in the Python SDK code itself, I expect the same issue to occur.

Guide de contribution

Ouvrir le guide de contribution

Piste de recherche

Commencez dans azure/durable_functions/models/TaskOrchestrationExecutor.py, au niveau de TaskOrchestrationExecutor.get_orchestrator_state_str(), et comparez sa sérialisation JSON avec celle des autres modèles ou entités mentionnés dans l’issue. Reproduisez l’orchestration à l’aide de l’exemple CustomResult et vérifiez qu’un objet avec to_json() peut être renvoyé sans que l’erreur not-serializable ne se produise.

Rédigé par le modèle d'indexation à partir du texte de l'issue.

Évaluation

Stack technique
python
Domaine
backend
Type d'issue
Bug
Difficulté
2/5
Temps estimé
1-3 heures
Activité
Calme
Clarté
Clairement spécifiée
Accessibilité débutants
75/100

Recevez les nouvelles issues par e-mail

Un résumé court des issues GitHub adaptées aux débutants.