Add a discriminated union sample (#919)
This commit is contained in:
Родитель
ca0513f840
Коммит
531d86bd34
|
@ -6,7 +6,7 @@ const ctrlOrCmd = process.platform === "darwin" ? "Meta" : "Control";
|
|||
test("compiled http sample", async ({ page }) => {
|
||||
await page.goto(host);
|
||||
const samplesDropDown = page.locator("select.sample-dropdown");
|
||||
await samplesDropDown.selectOption({ label: "Http" });
|
||||
await samplesDropDown.selectOption({ label: "Http service" });
|
||||
const outputContainer = page.locator(".output-content");
|
||||
await expect(outputContainer).toContainText(`"title": "Widget Service"`);
|
||||
});
|
||||
|
|
|
@ -0,0 +1,42 @@
|
|||
import "@cadl-lang/rest";
|
||||
import "@cadl-lang/openapi3";
|
||||
|
||||
@serviceTitle("Widget Service")
|
||||
namespace DemoService;
|
||||
using Cadl.Rest;
|
||||
using Cadl.Http;
|
||||
using OpenAPI;
|
||||
|
||||
model WidgetBase {
|
||||
@key id: string;
|
||||
weight: int32;
|
||||
color: "red" | "blue";
|
||||
}
|
||||
|
||||
enum WidgetKind {
|
||||
Heavy,
|
||||
Light,
|
||||
}
|
||||
|
||||
model HeavyWidget extends WidgetBase {
|
||||
kind: WidgetKind.Heavy;
|
||||
}
|
||||
|
||||
model LightWidget extends WidgetBase {
|
||||
kind: WidgetKind.Light;
|
||||
}
|
||||
|
||||
@discriminator("kind")
|
||||
@oneOf
|
||||
union Widget {
|
||||
heavy: HeavyWidget,
|
||||
light: LightWidget,
|
||||
}
|
||||
|
||||
@error
|
||||
model Error {
|
||||
code: int32;
|
||||
message: string;
|
||||
}
|
||||
|
||||
@get op read(@path id: string): Widget | Error;
|
|
@ -10,9 +10,10 @@ export default definePlaygroundViteConfig({
|
|||
"@cadl-lang/openapi3",
|
||||
],
|
||||
samples: {
|
||||
Http: "samples/http.cadl",
|
||||
"Rest framework": "samples/rest.cadl",
|
||||
"Versioned Rest framework": "samples/versioning.cadl",
|
||||
"API versioning": "samples/versioning.cadl",
|
||||
"Discriminated unions": "samples/unions.cadl",
|
||||
"Http service": "samples/http.cadl",
|
||||
"REST framework": "samples/rest.cadl",
|
||||
},
|
||||
enableSwaggerUI: true,
|
||||
});
|
||||
|
|
Загрузка…
Ссылка в новой задаче