diff --git a/packages/core/src/interaction-output.ts b/packages/core/src/interaction-output.ts index a369a2161..8eab52131 100644 --- a/packages/core/src/interaction-output.ts +++ b/packages/core/src/interaction-output.ts @@ -21,7 +21,7 @@ import { Content } from "./content"; import Ajv from "ajv"; import { createLoggers } from "./logger"; -const { debug } = createLoggers("core", "interaction-output"); +const { debug, warn } = createLoggers("core", "interaction-output"); // Problem: strict mode ajv does not accept unknown keywords in schemas // however property affordances could contain all sort of fields @@ -78,6 +78,14 @@ export class InteractionOutput implements WoT.InteractionOutput { } async value(): Promise { + // is there any value expected at all? + if (this.schema == null) { + warn( + `No schema defined. Hence undefined is reported for value() function. If you are invoking an action with no output that is on purpose, otherwise consider using arrayBuffer().` + ); + return undefined as unknown as T; + } + // the value has been already read? if (this.#value !== undefined) { return this.#value as T; @@ -91,8 +99,8 @@ export class InteractionOutput implements WoT.InteractionOutput { throw new NotReadableError("No form defined"); } - if (this.schema == null || this.schema.type == null) { - throw new NotReadableError("No schema defined"); + if (this.schema.type == null) { + throw new NotReadableError("No schema type defined"); } // is content type valid? diff --git a/packages/core/test/InteractionOutputTest.ts b/packages/core/test/InteractionOutputTest.ts index 8ea250aff..0d66b39dd 100644 --- a/packages/core/test/InteractionOutputTest.ts +++ b/packages/core/test/InteractionOutputTest.ts @@ -59,7 +59,7 @@ class InteractionOutputTests { const stream = Readable.from([1, 2, 3]); const content = new Content("application/json", stream); - const out = new InteractionOutput(content, {}); + const out = new InteractionOutput(content, {}, { type: "string" }); const result = []; const reader = out.data.getReader(); expect(reader).not.to.be.undefined; @@ -73,6 +73,20 @@ class InteractionOutputTests { return expect(out.value()).to.be.rejected; } + @test async "should return undefined for value call if no DataSchema is provided"() { + const stream = Readable.from([]); + const content = new Content("application/json", stream); + + const out = new InteractionOutput(content, {}); + + const result1 = await out.value(); + expect(result1).be.undefined; + + // try a second time also + const result2 = await out.value(); + expect(result2).be.undefined; + } + @test async "should return the value"() { const stream = Readable.from(Buffer.from("true", "utf-8")); const content = new Content("application/json", stream);