From 36b3f799d66f0d96859860e581703736a8909035 Mon Sep 17 00:00:00 2001 From: reluc Date: Mon, 22 Jul 2024 15:45:54 +0200 Subject: [PATCH] fix(core/interactionOutput): support for no-type based root schemas See #1243 --- packages/core/src/interaction-output.ts | 7 +++- packages/core/test/InteractionOutputTest.ts | 36 +++++++++++++++++++++ 2 files changed, 42 insertions(+), 1 deletion(-) diff --git a/packages/core/src/interaction-output.ts b/packages/core/src/interaction-output.ts index 2d6080a10..22221a9a0 100644 --- a/packages/core/src/interaction-output.ts +++ b/packages/core/src/interaction-output.ts @@ -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"); } diff --git a/packages/core/test/InteractionOutputTest.ts b/packages/core/test/InteractionOutputTest.ts index 931317cbe..6bc9edafb 100644 --- a/packages/core/test/InteractionOutputTest.ts +++ b/packages/core/test/InteractionOutputTest.ts @@ -216,4 +216,40 @@ class InteractionOutputTests { const result = out.value(); 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(); + 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(); + 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"); + } }