diff --git a/common/changes/@cadl-lang/openapi3/skip-never_2022-10-05-13-09.json b/common/changes/@cadl-lang/openapi3/skip-never_2022-10-05-13-09.json new file mode 100644 index 0000000000..1c1fee81c3 --- /dev/null +++ b/common/changes/@cadl-lang/openapi3/skip-never_2022-10-05-13-09.json @@ -0,0 +1,10 @@ +{ + "changes": [ + { + "packageName": "@cadl-lang/openapi3", + "comment": "Exclude properties of type `never` when emitting model schemas", + "type": "patch" + } + ], + "packageName": "@cadl-lang/openapi3" +} \ No newline at end of file diff --git a/packages/openapi3/src/openapi.ts b/packages/openapi3/src/openapi.ts index ae60ee4ecc..9f165ffd8c 100644 --- a/packages/openapi3/src/openapi.ts +++ b/packages/openapi3/src/openapi.ts @@ -1046,6 +1046,11 @@ function createOAPIEmitter(program: Program, options: ResolvedOpenAPI3EmitterOpt continue; } + if (isNeverType(prop.type)) { + // If the property has a type of 'never', don't include it in the schema + continue; + } + if (!prop.optional) { if (!modelSchema.required) { modelSchema.required = []; diff --git a/packages/openapi3/test/models.test.ts b/packages/openapi3/test/models.test.ts index a58139f79a..b1703758fe 100644 --- a/packages/openapi3/test/models.test.ts +++ b/packages/openapi3/test/models.test.ts @@ -779,4 +779,34 @@ describe("openapi3: models", () => { expectDiagnostics(diagnostics, [{ code: "@cadl-lang/openapi3/inline-cycle" }]); }); + + it("excludes properties with type 'never'", async () => { + const res = await oapiForModel( + "Bar", + ` + model Foo { + y: int32; + nope: never; + }; + model Bar extends Foo { + x: int32; + }` + ); + + ok(res.isRef); + ok(res.schemas.Foo, "expected definition named Foo"); + ok(res.schemas.Bar, "expected definition named Bar"); + deepStrictEqual(res.schemas.Bar, { + type: "object", + properties: { x: { type: "integer", format: "int32" } }, + allOf: [{ $ref: "#/components/schemas/Foo" }], + required: ["x"], + }); + + deepStrictEqual(res.schemas.Foo, { + type: "object", + properties: { y: { type: "integer", format: "int32" } }, + required: ["y"], + }); + }); });