diff --git a/.chronus/changes/openapi3-fix-stray-items-array-encode-2026-6-8-0-0-1.md b/.chronus/changes/openapi3-fix-stray-items-array-encode-2026-6-8-0-0-1.md new file mode 100644 index 00000000000..72768760867 --- /dev/null +++ b/.chronus/changes/openapi3-fix-stray-items-array-encode-2026-6-8-0-0-1.md @@ -0,0 +1,7 @@ +--- +changeKind: fix +packages: + - "@typespec/openapi3" +--- + +Fix stray `items` being emitted on a property whose array type is encoded to a scalar via `@encode` (e.g. `ArrayEncoding.commaDelimited`). diff --git a/packages/openapi3/src/encoding.ts b/packages/openapi3/src/encoding.ts index a7e9de5832d..4ad68983afc 100644 --- a/packages/openapi3/src/encoding.ts +++ b/packages/openapi3/src/encoding.ts @@ -33,6 +33,10 @@ export function applyEncoding( } const newType = getSchemaForStdScalars(encodeData.type as any, options); targetObject.type = newType.type; + if (newType.type !== "array") { + delete targetObject.items; + delete targetObject.prefixItems; + } // If the target already has a format it takes priority. (e.g. int32) targetObject[encodedFieldName] = mergeFormatAndEncoding( targetObject[encodedFieldName], diff --git a/packages/openapi3/test/array.test.ts b/packages/openapi3/test/array.test.ts index 6e33222a247..d3e9aec0447 100644 --- a/packages/openapi3/test/array.test.ts +++ b/packages/openapi3/test/array.test.ts @@ -1,5 +1,6 @@ import { deepStrictEqual, ok, strictEqual } from "assert"; -import { it } from "vitest"; +import { expect, it } from "vitest"; +import { openApiFor } from "./test-host.js"; import { supportedVersions, worksFor } from "./works-for.js"; worksFor(supportedVersions, ({ oapiForModel, openApiFor }) => { @@ -403,3 +404,23 @@ worksFor(["3.1.0"], ({ oapiForModel }) => { }); }); }); + +it("removes array items when an array property is encoded to a scalar", async () => { + const res = await openApiFor( + ` + model Bar { + @encode(ArrayEncoding.commaDelimited) + prop: string[]; + } + + op getBar(): Bar; + `, + { "openapi-versions": ["3.1.0"] }, + ); + + const schema = res.components.schemas.Bar.properties.prop; + expect(schema.type).toBe("string"); + expect(schema.format).toBe("ArrayEncoding.commaDelimited"); + expect(schema.items).toBeUndefined(); + expect(schema.prefixItems).toBeUndefined(); +});