Skip to content

Commit

Permalink
Fix: Apply @minItems and @maxItems on model array (#2170)
Browse files Browse the repository at this point in the history
fix #2110

Makes sure 

```tsp
@minItems(1)
@Maxitems(5)
model Endpoints is string[];
```
Get applied correctly in openapi3.
  • Loading branch information
timotheeguerin authored Jul 12, 2023
1 parent 6fae63c commit 09c2561
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"changes": [
{
"packageName": "@typespec/openapi3",
"comment": "Fix: Apply `@minItems` and `@maxItems` decorators on model array.",
"type": "none"
}
],
"packageName": "@typespec/openapi3"
}
12 changes: 6 additions & 6 deletions packages/openapi3/src/openapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1507,9 +1507,7 @@ function createOAPIEmitter(program: Program, options: ResolvedOpenAPI3EmitterOpt
function getSchemaForModel(model: Model, visibility: Visibility) {
const arrayOrRecord = mapTypeSpecIntrinsicModelToOpenAPI(model, visibility);
if (arrayOrRecord) {
const arrayDoc = getDoc(program, model);
arrayOrRecord.description = arrayDoc;
return arrayOrRecord;
return applyIntrinsicDecorators(model, arrayOrRecord);
}

let modelSchema: OpenAPI3Schema & Required<Pick<OpenAPI3Schema, "properties">> = {
Expand Down Expand Up @@ -1646,13 +1644,15 @@ function createOAPIEmitter(program: Program, options: ResolvedOpenAPI3EmitterOpt
}

function applyIntrinsicDecorators(
typespecType: Scalar | ModelProperty,
typespecType: Model | Scalar | ModelProperty,
target: OpenAPI3Schema
): OpenAPI3Schema {
const newTarget = { ...target };
const docStr = getDoc(program, typespecType);
const isString = isStringType(program, getPropertyType(typespecType));
const isNumeric = isNumericType(program, getPropertyType(typespecType));
const isString =
typespecType.kind !== "Model" && isStringType(program, getPropertyType(typespecType));
const isNumeric =
typespecType.kind !== "Model" && isNumericType(program, getPropertyType(typespecType));

if (docStr) {
newTarget.description = docStr;
Expand Down
32 changes: 32 additions & 0 deletions packages/openapi3/test/array.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,38 @@ describe("openapi3: Array", () => {
});
});

it("can specify minItems using @minItems decorator (on array model)", async () => {
const res = await oapiForModel(
"Names",
`
@minItems(1)
model Names is string[];
`
);

deepStrictEqual(res.schemas.Names, {
type: "array",
minItems: 1,
items: { type: "string" },
});
});

it("can specify maxItems using @maxItems decorator (on array model)", async () => {
const res = await oapiForModel(
"Names",
`
@maxItems(3)
model Names is string[];
`
);

deepStrictEqual(res.schemas.Names, {
type: "array",
maxItems: 3,
items: { type: "string" },
});
});

it("can specify array defaults using tuple syntax", async () => {
const res = await oapiForModel(
"Pet",
Expand Down

0 comments on commit 09c2561

Please sign in to comment.