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: advanced data type API spec and permission name #20128

Merged
merged 5 commits into from
May 20, 2022
Merged
Show file tree
Hide file tree
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
44 changes: 16 additions & 28 deletions superset/advanced_data_type/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@
from flask_appbuilder.api import BaseApi, expose, permission_name, protect, rison, safe
from flask_babel import lazy_gettext as _

from superset.advanced_data_type.schemas import advanced_data_type_convert_schema
from superset.advanced_data_type.schemas import (
advanced_data_type_convert_schema,
AdvancedDataTypeSchema,
)
from superset.advanced_data_type.types import AdvancedDataTypeResponse
from superset.extensions import event_logger

Expand All @@ -40,26 +43,28 @@ class AdvancedDataTypeRestApi(BaseApi):
allow_browser_login = True
include_route_methods = {"get", "get_types"}
resource_name = "advanced_data_type"
class_permission_name = "AdvancedDataType"

openapi_spec_tag = "Advanced Data Type"
apispec_parameter_schemas = {
"advanced_data_type_convert_schema": advanced_data_type_convert_schema,
}
openapi_spec_component_schemas = (AdvancedDataTypeSchema,)

@protect()
@safe
@expose("/convert", methods=["GET"])
@permission_name("get")
@permission_name("read")
@event_logger.log_this_with_context(
action=lambda self, *args, **kwargs: f"{self.__class__.__name__}.get",
log_to_statsd=False, # pylint: disable-arguments-renamed
)
@rison()
@rison(advanced_data_type_convert_schema)
def get(self, **kwargs: Any) -> Response:
"""Returns a AdvancedDataTypeResponse object populated with the passed in args
---
get:
description: >-
summary: >-
Returns a AdvancedDataTypeResponse object populated with the passed in args.
parameters:
- in: query
Expand All @@ -75,18 +80,7 @@ def get(self, **kwargs: Any) -> Response:
content:
application/json:
schema:
type: object
properties:
status:
type: string
values:
type: array
formatted_value:
type: string
error_message:
type: string
valid_filter_operators:
type: string
$ref: '#/components/schemas/AdvancedDataTypeSchema'
400:
$ref: '#/components/responses/400'
401:
Expand All @@ -96,15 +90,9 @@ def get(self, **kwargs: Any) -> Response:
500:
$ref: '#/components/responses/500'
"""
items = kwargs["rison"]
advanced_data_type = items.get("type")
if not advanced_data_type:
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rison automatically parses the query string from the defined json schema

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch 👍

return self.response(
400, message=_("Missing advanced data type in request")
)
values = items["values"]
if not values:
return self.response(400, message=_("Missing values in request"))
item = kwargs["rison"]
advanced_data_type = item["type"]
values = item["values"]
addon = ADVANCED_DATA_TYPES.get(advanced_data_type)
if not addon:
return self.response(
Expand All @@ -124,7 +112,7 @@ def get(self, **kwargs: Any) -> Response:
@protect()
@safe
@expose("/types", methods=["GET"])
@permission_name("get")
@permission_name("read")
@event_logger.log_this_with_context(
action=lambda self, *args, **kwargs: f"{self.__class__.__name__}.get",
log_to_statsd=False, # pylint: disable-arguments-renamed
Expand All @@ -147,8 +135,8 @@ def get_types(self) -> Response:
properties:
result:
type: array
400:
$ref: '#/components/responses/400'
items:
type: string
401:
$ref: '#/components/responses/401'
404:
Expand Down
28 changes: 22 additions & 6 deletions superset/advanced_data_type/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,30 @@
"""
Schemas for advanced data types
"""
from marshmallow import fields, Schema

advanced_data_type_convert_schema = {
"type": "array",
"items": {
"type": "object",
"properties": {
"type": {"type": "string"},
"values": {"type": "array"},
"type": "object",
"properties": {
"type": {"type": "string", "default": "port"},
"values": {
"type": "array",
"items": {"default": "http"},
"minItems": 1,
},
},
"required": ["type", "values"],
}


class AdvancedDataTypeSchema(Schema):
"""
AdvancedDataType response schema
"""

error_message = fields.String()
values = fields.List(fields.String(description="parsed value (can be any value)"))
display_value = fields.String(
description="The string representation of the parsed values"
)
valid_filter_operators = fields.List(fields.String())