Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for “Object of type UUID is not JSON serializable” exception for TAXII2 #251

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion opentaxii/taxii2/http.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
"""Taxii2 http helper functions."""
import json
import uuid
from typing import Dict, Optional

from flask import Response, make_response

class JsonUUIDEncoder(json.JSONEncoder):
def default(self, obj):
if isinstance(obj, uuid.UUID):
return str(obj)
return super().default(self, obj)

def make_taxii2_response(data, status: Optional[int] = 200, extra_headers: Optional[Dict] = None) -> Response:
"""Turn input data into valid taxii2 response."""
if not isinstance(data, str):
data = json.dumps(data)
data = json.dumps(data, cls=JsonUUIDEncoder)
response = make_response((data, status))
response.content_type = "application/taxii+json;version=2.1"
response.headers.update(extra_headers or {})
Expand Down