Skip to content

Commit

Permalink
change list parsing to use json and pre_load
Browse files Browse the repository at this point in the history
  • Loading branch information
DenizYil committed Aug 10, 2023
1 parent 2cda97e commit 7af8519
Showing 1 changed file with 22 additions and 12 deletions.
34 changes: 22 additions & 12 deletions terracotta/server/metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
Flask route to handle /metadata calls.
"""

from marshmallow import Schema, fields, validate, ValidationError
from typing import Any, Mapping, Dict
import json

from marshmallow import Schema, fields, validate, pre_load, ValidationError
from flask import jsonify, Response, request

from terracotta.server.flask_api import METADATA_API
Expand Down Expand Up @@ -50,21 +53,28 @@ class Meta:
)


class CommaSeparatedListField(fields.Field):
def _deserialize(self, value, attr, data, **kwargs):
try:
assert value[0] == "[" and value[-1] == "]"
class MetadataColumnsSchema(Schema):
columns = fields.List(
fields.String(),
description="List of columns to return",
required=False,
)

if value == "[]":
return []
@pre_load
def validate_columns(self, data: Mapping[str, Any], **kwargs: Any) -> Dict[str, Any]:
columns = data.get("columns")

return value[1:-1].split(", ")
except ValueError:
raise ValidationError("Invalid input for a list of values.")
if columns:
data = dict(data.items())

try:
data["columns"] = json.loads(columns)
except json.decoder.JSONDecodeError as exc:
raise ValidationError(
"columns must be a JSON list"
) from exc

class MetadataColumnsSchema(Schema):
columns = CommaSeparatedListField(description="Columns of dataset to be returned")
return data


class MultipleMetadataDatasetsSchema(Schema):
Expand Down

0 comments on commit 7af8519

Please sign in to comment.