Skip to content

Commit ad76c37

Browse files
refactor: report undefined for no expected value() (#1230)
* refactor: report undefined for no expected value() * Update packages/core/src/interaction-output.ts Co-authored-by: Jan Romann <jan.romann@uni-bremen.de> * fix: test by adding DataSchema * refactor: make undefined return more explicitly * refactor: remove unnecessary comment * refactor: add test case for value() returning undefined if no DataSchema is provided * refactor: add warn logging in case undefined is returned * reword warning based on comment from @relu91 --------- Co-authored-by: Jan Romann <jan.romann@uni-bremen.de>
1 parent 6ed05ff commit ad76c37

2 files changed

Lines changed: 26 additions & 4 deletions

File tree

packages/core/src/interaction-output.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import { Content } from "./content";
2121
import Ajv from "ajv";
2222
import { createLoggers } from "./logger";
2323

24-
const { debug } = createLoggers("core", "interaction-output");
24+
const { debug, warn } = createLoggers("core", "interaction-output");
2525

2626
// Problem: strict mode ajv does not accept unknown keywords in schemas
2727
// however property affordances could contain all sort of fields
@@ -78,6 +78,14 @@ export class InteractionOutput implements WoT.InteractionOutput {
7878
}
7979

8080
async value<T extends WoT.DataSchemaValue>(): Promise<T> {
81+
// is there any value expected at all?
82+
if (this.schema == null) {
83+
warn(
84+
`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().`
85+
);
86+
return undefined as unknown as T;
87+
}
88+
8189
// the value has been already read?
8290
if (this.#value !== undefined) {
8391
return this.#value as T;
@@ -91,8 +99,8 @@ export class InteractionOutput implements WoT.InteractionOutput {
9199
throw new NotReadableError("No form defined");
92100
}
93101

94-
if (this.schema == null || this.schema.type == null) {
95-
throw new NotReadableError("No schema defined");
102+
if (this.schema.type == null) {
103+
throw new NotReadableError("No schema type defined");
96104
}
97105

98106
// is content type valid?

packages/core/test/InteractionOutputTest.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ class InteractionOutputTests {
5959
const stream = Readable.from([1, 2, 3]);
6060
const content = new Content("application/json", stream);
6161

62-
const out = new InteractionOutput(content, {});
62+
const out = new InteractionOutput(content, {}, { type: "string" });
6363
const result = [];
6464
const reader = out.data.getReader();
6565
expect(reader).not.to.be.undefined;
@@ -73,6 +73,20 @@ class InteractionOutputTests {
7373
return expect(out.value()).to.be.rejected;
7474
}
7575

76+
@test async "should return undefined for value call if no DataSchema is provided"() {
77+
const stream = Readable.from([]);
78+
const content = new Content("application/json", stream);
79+
80+
const out = new InteractionOutput(content, {});
81+
82+
const result1 = await out.value();
83+
expect(result1).be.undefined;
84+
85+
// try a second time also
86+
const result2 = await out.value();
87+
expect(result2).be.undefined;
88+
}
89+
7690
@test async "should return the value"() {
7791
const stream = Readable.from(Buffer.from("true", "utf-8"));
7892
const content = new Content("application/json", stream);

0 commit comments

Comments
 (0)