Skip to content

propagate CompositionOptions to fetchAffordances and _getPartialTDs #57

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

Merged
merged 3 commits into from
Mar 17, 2025
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
8 changes: 7 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,10 @@ junit-tests.xml
packages/web-new/test-results
packages/web-new/playwright-report
**/coverage
**/.nyc_output
**/.nyc_output

# nix
.direnv
.envrc
flake.nix
flake.lock
8 changes: 4 additions & 4 deletions node/thing-model/src/tm-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ export class ThingModelHelpers {
throw new Error(isValid.errors);
}

const modelInput = await this.fetchAffordances(model);
const modelInput = await this.fetchAffordances(model, options);
const extendedModels = await this.composeModel(model, modelInput, options);
return extendedModels;
}
Expand All @@ -304,14 +304,14 @@ export class ThingModelHelpers {
*
* @experimental
*/
private async fetchAffordances(data: ThingModel): Promise<modelComposeInput> {
private async fetchAffordances(data: ThingModel, options?: CompositionOptions): Promise<modelComposeInput> {
const modelInput: modelComposeInput = {};
const extLinks = ThingModelHelpers.getThingModelLinks(data, "tm:extends");
if (extLinks.length > 0) {
modelInput.extends = [] as ThingModel[];
for (const s of extLinks) {
let source = await this.fetchModel(s.href);
[source] = await this._getPartialTDs(source);
[source] = await this._getPartialTDs(source, options);
modelInput.extends.push(source);
}
}
Expand All @@ -325,7 +325,7 @@ export class ThingModelHelpers {
throw new Error(`Missing remote path in ${affUri}`);
}
let source = await this.fetchModel(refObj.uri);
[source] = await this._getPartialTDs(source);
[source] = await this._getPartialTDs(source, options);
delete (data[affType] as DataSchema)[aff]["tm:ref"];
const importedAffordance = this.getRefAffordance(refObj, source) ?? {};
refObj.name = aff; // update the name of the affordance
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"@context": [
"https://www.w3.org/2022/wot/td/v1.1"
],
"@type": "tm:ThingModel",
"title": "base_thing",
"version": {
"model": "0.0.1"
},
"id": "urn:uuid:{{THING_UUID_V4}}",
"description": "The most basic thing",
"securityDefinitions": {
"nosec_sc": {
"scheme": "nosec"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"@context": [
"https://www.w3.org/2022/wot/td/v1.1"
],
"@type": "tm:ThingModel",
"title": "extended_thing",
"version": {
"model": "0.0.1"
},
"description": "Extends the base thing with some properties",
"links": [{
"rel": "tm:extends",
"href": "file://./test/thing-model/tmodels/placeholderExtension/BaseThingWithPlaceholder.tm.jsonld",
"type": "application/tm+json"
}],
"properties": {
"some_property": {
"type": "string",
"description": "Some property",
"forms": [{
"href": "mqtt://{{MQTT_BROKER_ADDR}}",
"mqv:topic": "{{THING_MODEL}}/{{THING_UUID_V4}}/properties/some_property",
"op": "observeproperty",
"mqv:qos": 1,
"mqv:retain": false,
"contentType": "application/json"
}]
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{
"@context": [
"https://www.w3.org/2022/wot/td/v1.1"
],
"@type": "Thing",
"title": "extended_thing",
"id": "urn:uuid:bf2c24bc-9232-454d-ac17-90feb3647b71",
"description": "Extends the base thing with some properties",
"securityDefinitions": {
"nosec_sc": {
"scheme": "nosec"
}
},
"links": [
{
"rel": "type",
"href": "./extended_thing.tm.jsonld",
"type": "application/tm+json"
}
],
"properties": {
"some_property": {
"type": "string",
"description": "Some property",
"forms": [
{
"href": "mqtt://example.org:1883",
"mqv:topic": "extended_thing/bf2c24bc-9232-454d-ac17-90feb3647b71/properties/some_property",
"op": "observeproperty",
"mqv:qos": 1,
"mqv:retain": false,
"contentType": "application/json"
}
]
}
}
}
23 changes: 23 additions & 0 deletions node/thing-model/test/thingModelHelper.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,29 @@ class ThingModelHelperTest {
expect(validated.errors).to.be.equal(`Missing required fields in map for model ${thing.title}`);
}

@test async "should correctly fill placeholders of all linked ThingModels"() {
const modelUri = "file://./test/thing-model/tmodels/placeholderExtension/ThingWithPlaceholder.tm.jsonld";
const placeholderThing = await this.thingModelHelpers.fetchModel(modelUri);

const tdUri = "./test/thing-model/tmodels/placeholderExtension/thingExtended.jsonld";
const td = await fs.readFile(tdUri, "utf-8");
const tdJson = JSON.parse(td);

const map = {
THING_MODEL: "extended_thing",
THING_UUID_V4: "bf2c24bc-9232-454d-ac17-90feb3647b71",
MQTT_BROKER_ADDR: "example.org:1883",
};

const options: CompositionOptions = {
map,
selfComposition: false,
};

const [partialTd] = await this.thingModelHelpers.getPartialTDs(placeholderThing, options);
expect(partialTd).to.be.deep.equal(tdJson);
}

@test async "should fail on unavailable linked ThingModel - http"() {
const thing = {
"@context": ["https://www.w3.org/2022/wot/td/v1.1"],
Expand Down