diff --git a/.chronus/changes/main-2024-5-13-14-52-31.md b/.chronus/changes/main-2024-5-13-14-52-31.md new file mode 100644 index 0000000000..e5fa0ec174 --- /dev/null +++ b/.chronus/changes/main-2024-5-13-14-52-31.md @@ -0,0 +1,8 @@ +--- +# Change versionKind to one of: internal, fix, dependencies, feature, deprecation, breaking +changeKind: fix +packages: + - "@typespec/openapi3" +--- + +Emit diagnostic when an invalid type is used as a property instead of crashing. diff --git a/packages/openapi3/src/lib.ts b/packages/openapi3/src/lib.ts index 8b45badd1c..64ef47e267 100644 --- a/packages/openapi3/src/lib.ts +++ b/packages/openapi3/src/lib.ts @@ -245,6 +245,12 @@ export const libDef = { default: paramMessage`Status code range '${"start"} to '${"end"}' is not supported. OpenAPI 3.0 can only represent range 1XX, 2XX, 3XX, 4XX and 5XX. Example: \`@minValue(400) @maxValue(499)\` for 4XX.`, }, }, + "invalid-model-property": { + severity: "error", + messages: { + default: paramMessage`'${"type"}' cannot be specified as a model property.`, + }, + }, "unsupported-auth": { severity: "warning", messages: { diff --git a/packages/openapi3/src/schema-emitter.ts b/packages/openapi3/src/schema-emitter.ts index 44f10ae4a5..3e05cd9978 100644 --- a/packages/openapi3/src/schema-emitter.ts +++ b/packages/openapi3/src/schema-emitter.ts @@ -351,7 +351,14 @@ export class OpenAPI3SchemaEmitter extends TypeEmitter< }); if (refSchema.kind !== "code") { - throw new Error("Unexpected non-code result from emit reference"); + reportDiagnostic(program, { + code: "invalid-model-property", + format: { + type: prop.type.kind, + }, + target: prop, + }); + return {}; } const isRef = refSchema.value instanceof Placeholder || "$ref" in refSchema.value;