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

Add workaround for missing content types in response fields, use correct affordance keys #14

Merged
merged 8 commits into from
Nov 29, 2024
43 changes: 37 additions & 6 deletions tdd/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
JSONSchemaError,
IDNotFound,
WrongMimeType,
IncorrectlyDefinedParameter,
)
from tdd.td import (
clear_expired_td,
Expand Down Expand Up @@ -67,6 +68,36 @@
TD_TRANSFORMERS = []


def apply_response_content_type_fix(thing_description):
"""
This function applies the workaround described in WoT Discovery issue
https://github.com/w3c/wot-discovery/issues/465 to let the TD exposed
by the TDD pass JSON Schema validation.

The workaround is needed since in the TD specification the contentType field
in the "response" objects has been defined as mandatory without providing a
default, making the TD for TDDs included in the Discovery Recommendation
invalid (also see https://github.com/w3c/wot-thing-description/issues/1780).
This issue should probably be fixed in the 2.0 version of the TD
specification, which render this workaround obsolete.
"""
actionsToFix = ["createThing", "updateThing", "deleteThing"]

print(thing_description["actions"].keys())
for key in actionsToFix:

forms = thing_description["actions"].get(key, {}).get("forms", [])

for form in forms:
response = form.get("response")

if response is None:
continue

if response.get("contentType") is None:
response["contentType"] = "application/x-empty"


def wait_for_sparqlendpoint():
test_num = 0
while test_num < LIMIT_SPARQLENDPOINT_TEST:
Expand Down Expand Up @@ -109,9 +140,7 @@ def create_app():
for entry_point in entry_points(group="tdd_api.plugins.blueprints"):
try:
app.register_blueprint(entry_point.load())
print(
f"Imported {entry_point.value} blueprint"
)
print(f"Imported {entry_point.value} blueprint")
except Exception as exc:
print(f"ERROR ({entry_point.name}): {exc}")
print(
Expand All @@ -121,9 +150,7 @@ def create_app():
for entry_point in entry_points(group="tdd_api.plugins.transformers"):
try:
TD_TRANSFORMERS.append(entry_point.load())
print(
f"Imported {entry_point.value} transformer"
)
print(f"Imported {entry_point.value} transformer")
except Exception as exc:
print(f"ERROR ({entry_point.name}): {exc}")
print(
Expand Down Expand Up @@ -174,6 +201,7 @@ def directory_description():
with files(__package__).joinpath("data/tdd-description.json").open() as strm:
tdd_description = json.load(strm)
tdd_description["base"] = CONFIG["TD_REPO_URL"]
apply_response_content_type_fix(tdd_description)
return Response(
json.dumps(tdd_description), content_type="application/td+json"
)
Expand Down Expand Up @@ -344,6 +372,9 @@ def generate():
f' etag="{get_collection_etag()}"'
)
return response
raise IncorrectlyDefinedParameter(
"'format' parameter is not recognized. Must be 'array' or 'collection' if defined."
)

@app.route("/things/<id>", methods=["GET"])
def describe_td(id):
Expand Down
17 changes: 5 additions & 12 deletions tdd/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
query,
)
from tdd.metadata import insert_metadata, delete_metadata
from tdd.errors import IDNotFound
from tdd.config import CONFIG


Expand Down Expand Up @@ -56,14 +55,11 @@ def json_ld_to_ntriples(ld_content):
input_data = json.dumps(ld_content) + "\n"
with resources.path("tdd.lib", "transform-to-nt.js") as transform_lib_path:
p = subprocess.Popen(
[
"node",
transform_lib_path
],
["node", transform_lib_path],
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
universal_newlines=True
universal_newlines=True,
)
p.stdin.write(input_data)
p.stdin.flush()
Expand Down Expand Up @@ -111,14 +107,11 @@ def frame_nt_content(nt_content, frame):
input_data = json.dumps([ntriples, frame]) + "\n"
with resources.path("tdd.lib", "frame-jsonld.js") as frame_lib_path:
p = subprocess.Popen(
[
"node",
frame_lib_path
],
["node", frame_lib_path],
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
universal_newlines=True
universal_newlines=True,
)
p.stdin.write(input_data)
p.stdin.flush()
Expand All @@ -136,6 +129,6 @@ def get_id_description(uri, content_type, ontology):
if not resp.text.strip() or not (
re.search(r"^[^\#]", resp.text, re.MULTILINE)
): # because some SPARQL endpoint may send "# Empty file" as response
#raise IDNotFound()
# raise IDNotFound()
abort(404)
return resp.text
12 changes: 6 additions & 6 deletions tdd/data/tdd-description.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"security": "no_sec",
"base": "{{TD_REPO_URL}}",
"actions": {
"createTD": {
"createThing": {
"description": "Create a Thing Description",
"uriVariables": {
"id": {
Expand Down Expand Up @@ -63,7 +63,7 @@
}
]
},
"updateTD": {
"updateThing": {
"description": "Update a Thing Description",
"uriVariables": {
"id": {
Expand Down Expand Up @@ -109,7 +109,7 @@
}
]
},
"deleteTD": {
"deleteThing": {
"description": "Delete a Thing Description",
"uriVariables": {
"id": {
Expand Down Expand Up @@ -139,7 +139,7 @@
}
},
"properties": {
"retrieveTD": {
"retrieveThing": {
"description": "Retrieve a Thing Description",
"uriVariables": {
"id": {
Expand Down Expand Up @@ -168,7 +168,7 @@
}
]
},
"retrieveTDs": {
"things": {
"description": "Retrieve Thing Descriptions in batch",
"uriVariables": {
"format": {
Expand All @@ -187,7 +187,7 @@
},
"forms": [
{
"href": "/things?format={format}&offset={offset}&limit={limit}",
"href": "/things{?offset,limit,format}",
"htv:methodName": "GET",
"response": {
"description": "Success response",
Expand Down
4 changes: 4 additions & 0 deletions tdd/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,3 +205,7 @@ def __init__(self, provided_mimetype):
f"Provided mimetype '{provided_mimetype}' is not supported. Only "
f"{', '.join(POSSIBLE_MIMETYPES)}, application/json are allowed"
)


class IncorrectlyDefinedParameter(AppException):
title = "Incorrectly defined parameter"
2 changes: 1 addition & 1 deletion tdd/sparql.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ def query(
sparqlendpoint,
data={"update": querystring},
)

if resp.status_code not in status_codes:
raise FusekiError(resp)
return resp
Expand Down
4 changes: 2 additions & 2 deletions tdd/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,13 +98,13 @@ def custom(self, request, **kwargs):

@pytest.fixture
def mock_sparql_with_one_td(httpx_mock):
graph = SparqlGraph("smart_coffe_machine_init.nquads")
graph = SparqlGraph("smart_coffee_machine_init.nquads")
httpx_mock.add_callback(graph.custom)


@pytest.fixture
def mock_sparql_with_one_expired_td(httpx_mock):
graph = SparqlGraph("smart_coffe_machine_expired.nquads")
graph = SparqlGraph("smart_coffee_machine_expired.nquads")
httpx_mock.add_callback(graph.custom)


Expand Down
12 changes: 6 additions & 6 deletions tdd/tests/data/tdd-description.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"security": "no_sec",
"base": "http://localhost:5050",
"actions": {
"createTD": {
"createThing": {
"description": "Create a Thing Description",
"uriVariables": {
"id": {
Expand Down Expand Up @@ -63,7 +63,7 @@
}
]
},
"updateTD": {
"updateThing": {
"description": "Update a Thing Description",
"uriVariables": {
"id": {
Expand Down Expand Up @@ -109,7 +109,7 @@
}
]
},
"deleteTD": {
"deleteThing": {
"description": "Delete a Thing Description",
"uriVariables": {
"id": {
Expand Down Expand Up @@ -139,7 +139,7 @@
}
},
"properties": {
"retrieveTD": {
"retrieveThing": {
"description": "Retrieve a Thing Description",
"uriVariables": {
"id": {
Expand Down Expand Up @@ -168,7 +168,7 @@
}
]
},
"retrieveTDs": {
"things": {
"description": "Retrieve Thing Descriptions in batch",
"uriVariables": {
"format": {
Expand All @@ -187,7 +187,7 @@
},
"forms": [
{
"href": "/things?format={format}&offset={offset}&limit={limit}",
"href": "/things{?offset,limit,format}",
"htv:methodName": "GET",
"response": {
"description": "Success response",
Expand Down
Loading