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,7 @@
---
changeKind: fix
packages:
- "@typespec/http-server-csharp"
---

Import `JsonNode` collection types when generating models containing record arrays.
Original file line number Diff line number Diff line change
Expand Up @@ -185,9 +185,13 @@ export function isValueType($: ReturnType<typeof useTsp>["$"], type: Type): bool
/** Returns true if any property of the model uses Record<T> (mapped to JsonObject). */
export function modelNeedsJsonNodes($: ReturnType<typeof useTsp>["$"], model: Model): boolean {
for (const prop of model.properties.values()) {
if (prop.type.kind === "Model" && $.record.is(prop.type)) {
let type = prop.type;
while (type.kind === "Model" && $.array.is(type) && type.indexer?.value) {
type = type.indexer.value;
}
if (type.kind === "Model" && $.record.is(type)) {
// Only need JsonNodes for Record<unknown> (maps to JsonObject)
const valueType = prop.type.indexer?.value;
const valueType = type.indexer?.value;
if (valueType?.kind === "Intrinsic" && valueType.name === "unknown") return true;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { ClassDeclaration } from "@typespec/emitter-framework/csharp";
import { HttpCanonicalizer } from "@typespec/http-canonicalization";
import { beforeEach, expect, it } from "vitest";
import { resolveServiceTypes } from "../../service-resolution.js";
import { modelNeedsJsonNodes } from "./model-helpers.js";

let runner: TesterInstance;

Expand Down Expand Up @@ -130,6 +131,21 @@ it("renders a model with nullable union property", async () => {
`);
});

it("detects Record<unknown> nested in array properties", async () => {
const { JsonObjectArray, StringMapArray } = await runner.compile(t.code`
model ${t.model("JsonObjectArray")} {
items: Record<unknown>[][];
}
model ${t.model("StringMapArray")} {
items: Record<string>[];
}
`);
const tk = $(runner.program);

expect(modelNeedsJsonNodes(tk, JsonObjectArray)).toBe(true);
expect(modelNeedsJsonNodes(tk, StringMapArray)).toBe(false);
});

it("does not emit a model class for an @useAuth scheme model", async () => {
await runner.compile(`
@service
Expand Down
Loading