Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
# Change versionKind to one of: internal, fix, dependencies, feature, deprecation, breaking
changeKind: fix
packages:
- "@typespec/http"
---

Fix empty response models with `statusCode` defined in a base model
2 changes: 1 addition & 1 deletion packages/http/src/http-property.ts
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ export function resolvePayloadProperties(
const diagnostics = createDiagnosticCollector();
const httpProperties = new Map<ModelProperty, HttpProperty>();

if (type.kind !== "Model" || type.properties.size === 0) {
if (type.kind !== "Model" || (type.properties.size === 0 && !type.baseModel)) {
return diagnostics.wrap([]);
}

Expand Down
31 changes: 30 additions & 1 deletion packages/http/test/responses.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { Model } from "@typespec/compiler";
import { expectDiagnosticEmpty, expectDiagnostics } from "@typespec/compiler/testing";
import { deepStrictEqual, ok, strictEqual } from "assert";
import { describe, it } from "vitest";
import { describe, expect, it } from "vitest";
import { compileOperations, getOperationsWithServiceNamespace } from "./test-host.js";

describe("body resolution", () => {
Expand Down Expand Up @@ -214,3 +214,32 @@ it("chooses correct content-type for extensible union body", async () => {
ok(body);
deepStrictEqual(body.contentTypes, ["text/plain"]);
});

describe("status code", () => {
async function getResponse(code: string) {
const [routes, diagnostics] = await getOperationsWithServiceNamespace(code);
expectDiagnosticEmpty(diagnostics);
expect(routes).toHaveLength(1);
expect(routes[0].responses).toHaveLength(1);
return routes[0].responses[0];
}

it("resolve from a property at the root", async () => {
const response = await getResponse(`op test1(): { @statusCode code: 201 };`);
expect(response.statusCodes).toEqual(201);
});

it("resolve from a property nested", async () => {
const response = await getResponse(`op test1(): { nested: { @statusCode code: 201 } };`);
expect(response.statusCodes).toEqual(201);
});

it("resolve from parent model with no local props", async () => {
const response = await getResponse(`
model Created { @statusCode code: 201 }
model Res extends Created {};
op test1(): Res;
`);
expect(response.statusCodes).toEqual(201);
});
});
Loading