Skip to content

Commit

Permalink
fix(core/interactionOutput): support for no-type based root schemas
Browse files Browse the repository at this point in the history
See #1243
  • Loading branch information
relu91 committed Jul 22, 2024
1 parent 17d1090 commit 36b3f79
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
7 changes: 6 additions & 1 deletion packages/core/src/interaction-output.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,12 @@ export class InteractionOutput implements WoT.InteractionOutput {
throw new NotReadableError("No form defined");
}

if (this.schema.type == null) {
if (
this.schema.const == null &&
this.schema.enum == null &&
this.schema.oneOf == null &&
this.schema.type == null
) {
throw new NotReadableError("No schema type defined");
}

Expand Down
36 changes: 36 additions & 0 deletions packages/core/test/InteractionOutputTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,4 +216,40 @@ class InteractionOutputTests {
const result = out.value<number>();
return expect(result).to.eventually.be.rejected;
}

@test async "should return null"() {
const stream = Readable.from(Buffer.from("null", "utf-8"));
const content = new Content("application/json", stream);

const out = new InteractionOutput(content, {}, { type: "null" });
const result = await out.value<null>();
expect(result).to.be.eq(null);
}

@test async "should support oneOf"() {
const stream = Readable.from(Buffer.from('"hello"', "utf-8"));
const content = new Content("application/json", stream);

const out = new InteractionOutput(content, {}, { oneOf: [{ type: "string" }, { type: "null" }] });
const result = await out.value<string | null>();
expect(result).to.be.eq("hello");
}

@test async "should support const"() {
const stream = Readable.from(Buffer.from("42", "utf-8"));
const content = new Content("application/json", stream);

const out = new InteractionOutput(content, {}, { const: 42 });
const result = await out.value<42>();
expect(result).to.be.eq(42);
}

@test async "should support enum"() {
const stream = Readable.from(Buffer.from('"red"', "utf-8"));
const content = new Content("application/json", stream);

const out = new InteractionOutput(content, {}, { enum: ["red", "amber", "green"] });
const result = await out.value<"red" | "amber" | "green">();
expect(result).to.be.eq("red");
}
}

0 comments on commit 36b3f79

Please sign in to comment.