Skip to content

Commit

Permalink
refactor: report undefined for no expected value() (#1230)
Browse files Browse the repository at this point in the history
* refactor: report undefined for no expected value()

* Update packages/core/src/interaction-output.ts

Co-authored-by: Jan Romann <[email protected]>

* 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 <[email protected]>
  • Loading branch information
danielpeintner and JKRhb authored Feb 22, 2024
1 parent 6ed05ff commit ad76c37
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
14 changes: 11 additions & 3 deletions packages/core/src/interaction-output.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -78,6 +78,14 @@ export class InteractionOutput implements WoT.InteractionOutput {
}

async value<T extends WoT.DataSchemaValue>(): Promise<T> {
// 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;
Expand All @@ -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?
Expand Down
16 changes: 15 additions & 1 deletion packages/core/test/InteractionOutputTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand Down

0 comments on commit ad76c37

Please sign in to comment.