Skip to content

Commit

Permalink
Merge pull request #14 from eclipse-thingweb/fix-td-validation
Browse files Browse the repository at this point in the history
Add workaround for missing content types in response fields, use correct affordance keys
  • Loading branch information
wiresio authored Nov 29, 2024
2 parents ec0545f + 48ddc1c commit 5e2f4c1
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 14 deletions.
31 changes: 31 additions & 0 deletions tdd/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,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 @@ -171,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
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: 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

0 comments on commit 5e2f4c1

Please sign in to comment.