Skip to content
Merged
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/openapi3"
---

Emit `minItems` and `maxItems` for tuple types so generated schemas enforce the tuple's exact length.
3 changes: 3 additions & 0 deletions packages/openapi3/src/schema-emitter-3-1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -389,9 +389,12 @@ export class OpenAPI31SchemaEmitter extends OpenAPI3SchemaEmitterBase<OpenAPISch
}

tupleLiteral(tuple: Tuple): EmitterOutput<Record<string, any>> {
const size = tuple.values.length;
return new ObjectBuilder({
type: "array",
prefixItems: this.emitter.emitTupleLiteralValues(tuple),
minItems: size,
maxItems: size,
});
}

Expand Down
3 changes: 2 additions & 1 deletion packages/openapi3/src/schema-emitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -845,7 +845,8 @@ export class OpenAPI3SchemaEmitterBase<
}

tupleLiteral(tuple: Tuple): EmitterOutput<Record<string, any>> {
return { type: "array", items: {} };
const size = tuple.values.length;
return { type: "array", items: {}, minItems: size, maxItems: size };
Comment thread
ddadamhooper marked this conversation as resolved.
}
#getSchemaForScalar(scalar: Scalar): Schema {
let result: Schema = {} as Schema;
Expand Down
50 changes: 50 additions & 0 deletions packages/openapi3/test/array.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -268,21 +268,43 @@ worksFor(["3.0.0"], ({ oapiForModel }) => {
deepStrictEqual(res.schemas.Pet.properties.names, {
type: "array",
items: {},
minItems: 2,
maxItems: 2,
default: ["bismarck", 12],
});

deepStrictEqual(res.schemas.Pet.properties.decimals, {
type: "array",
items: {},
minItems: 2,
maxItems: 2,
default: ["hi", 456.7],
});

deepStrictEqual(res.schemas.Pet.properties.decimal128s, {
type: "array",
items: {},
minItems: 2,
maxItems: 2,
default: ["hi", 456.7],
});
});

it("emits minItems and maxItems of 0 for empty tuple", async () => {
const res = await oapiForModel(
"Pet",
`
model Pet { names: [] };
`,
);

deepStrictEqual(res.schemas.Pet.properties.names, {
type: "array",
items: {},
minItems: 0,
maxItems: 0,
});
});
});

worksFor(["3.1.0"], ({ oapiForModel }) => {
Expand Down Expand Up @@ -369,18 +391,24 @@ worksFor(["3.1.0"], ({ oapiForModel }) => {
deepStrictEqual(res.schemas.Pet.properties.names, {
type: "array",
prefixItems: [{ type: "string" }, { type: "integer", format: "int32" }],
minItems: 2,
maxItems: 2,
default: ["bismarck", 12],
});

deepStrictEqual(res.schemas.Pet.properties.decimals, {
type: "array",
prefixItems: [{ type: "string" }, { type: "number", format: "decimal" }],
minItems: 2,
maxItems: 2,
default: ["hi", 456.7],
});

deepStrictEqual(res.schemas.Pet.properties.decimal128s, {
type: "array",
prefixItems: [{ type: "string" }, { type: "number", format: "decimal128" }],
minItems: 2,
maxItems: 2,
default: ["hi", 456.7],
});
});
Expand All @@ -401,6 +429,8 @@ worksFor(["3.1.0"], ({ oapiForModel }) => {
{ type: "string", enum: ["bismark"] },
{ type: "number", enum: [32] },
],
minItems: 2,
maxItems: 2,
});
});
});
Expand All @@ -418,6 +448,8 @@ worksFor(["3.1.0", "3.2.0"], ({ oapiForModel }) => {
deepStrictEqual(res.schemas.Pet.properties.names, {
type: "array",
prefixItems: [{ $ref: "#/components/schemas/Bar" }],
minItems: 1,
maxItems: 1,
});
});

Expand All @@ -432,6 +464,24 @@ worksFor(["3.1.0", "3.2.0"], ({ oapiForModel }) => {
deepStrictEqual(res.schemas.Pet.properties.names, {
type: "array",
prefixItems: [{ $ref: "#/components/schemas/Pet" }],
minItems: 1,
maxItems: 1,
});
});

it("emits minItems and maxItems of 0 for empty tuple", async () => {
const res = await oapiForModel(
"Pet",
`
model Pet { names: [] };
`,
);

deepStrictEqual(res.schemas.Pet.properties.names, {
type: "array",
prefixItems: [],
minItems: 0,
maxItems: 0,
});
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ components:
scopes:
type: array
items: {}
minItems: 2
maxItems: 2
securitySchemes:
BearerAuth:
type: http
Expand Down
Loading