Skip to content

Commit 7e41240

Browse files
committed
lint and changeset
1 parent a08584e commit 7e41240

File tree

5 files changed

+30
-93
lines changed

5 files changed

+30
-93
lines changed

.changeset/selfish-items-jump.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"openapi-metadata": minor
3+
---
4+
5+
Handle array types and fix ApiProperty decorator type

packages/openapi-metadata/src/loaders/type.ts

+10-28
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,7 @@ export const ArrayTypeLoader: TypeLoaderFn = async (context, value) => {
4949

5050
// TODO: Better warn stack trace
5151
if (!itemsSchema) {
52-
context.logger.warn(
53-
"You tried to specify an array type with an item that resolves to undefined.",
54-
);
52+
context.logger.warn("You tried to specify an array type with an item that resolves to undefined.");
5553
return;
5654
}
5755

@@ -72,32 +70,22 @@ export const ClassTypeLoader: TypeLoaderFn = async (context, value) => {
7270
return { $ref: schemaPath(model) };
7371
}
7472

75-
const schema: SetRequired<OpenAPIV3.SchemaObject, "properties" | "required"> =
76-
{
77-
type: "object",
78-
properties: {},
79-
required: [],
80-
};
73+
const schema: SetRequired<OpenAPIV3.SchemaObject, "properties" | "required"> = {
74+
type: "object",
75+
properties: {},
76+
required: [],
77+
};
8178

8279
const properties = PropertyMetadataStorage.getMetadata(value.prototype);
8380

8481
if (!properties) {
85-
context.logger.warn(
86-
`You tried to use '${model}' as a type but it does not contain any ApiProperty.`,
87-
);
82+
context.logger.warn(`You tried to use '${model}' as a type but it does not contain any ApiProperty.`);
8883
}
8984

9085
context.schemas[model] = schema;
9186

9287
for (const [key, property] of Object.entries(properties)) {
93-
const {
94-
required,
95-
type,
96-
name,
97-
enum: e,
98-
schema: s,
99-
...metadata
100-
} = property as any;
88+
const { required, type, name, enum: e, schema: s, ...metadata } = property as any;
10189
schema.properties[key] = {
10290
...(await loadType(context, property)),
10391
...metadata,
@@ -137,18 +125,12 @@ export async function loadType(
137125
const thunk = isThunk(options.type);
138126
const value = thunk ? (options.type as Function)(context) : options.type;
139127

140-
for (const loader of [
141-
PrimitiveTypeLoader,
142-
...context.typeLoaders,
143-
ClassTypeLoader,
144-
]) {
128+
for (const loader of [PrimitiveTypeLoader, ...context.typeLoaders, ClassTypeLoader]) {
145129
const result = await loader(context, value, options.type);
146130
if (result) {
147131
return result;
148132
}
149133
}
150134

151-
context.logger.warn(
152-
`You tried to use '${options.type.toString()}' as a type but no loader supports it ${thunk}`,
153-
);
135+
context.logger.warn(`You tried to use '${options.type.toString()}' as a type but no loader supports it ${thunk}`);
154136
}

packages/openapi-metadata/src/metadata/property.ts

+2-6
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,11 @@ import type { OpenAPIV3 } from "openapi-types";
22
import type { TypeOptions } from "../types.js";
33
import { createMetadataStorage } from "./factory.js";
44

5-
export type PropertyMetadata = Omit<
6-
OpenAPIV3.NonArraySchemaObject,
7-
"type" | "enum" | "properties" | "required"
8-
> & {
5+
export type PropertyMetadata = Omit<OpenAPIV3.NonArraySchemaObject, "type" | "enum" | "properties" | "required"> & {
96
name: string;
107
required: boolean;
118
} & TypeOptions;
129

1310
export const PropertyMetadataKey = Symbol("Property");
1411

15-
export const PropertyMetadataStorage =
16-
createMetadataStorage<Record<string, PropertyMetadata>>(PropertyMetadataKey);
12+
export const PropertyMetadataStorage = createMetadataStorage<Record<string, PropertyMetadata>>(PropertyMetadataKey);

packages/openapi-metadata/test/decorators.test.ts

+11-53
Original file line numberDiff line numberDiff line change
@@ -24,23 +24,15 @@ import {
2424
OperationSecurityMetadataStorage,
2525
PropertyMetadataStorage,
2626
} from "../src/metadata/index.js";
27-
import {
28-
ApiBasicAuth,
29-
ApiBearerAuth,
30-
ApiCookieAuth,
31-
ApiOauth2,
32-
} from "../src/decorators/api-security.js";
27+
import { ApiBasicAuth, ApiBearerAuth, ApiCookieAuth, ApiOauth2 } from "../src/decorators/api-security.js";
3328

3429
test("@ApiOperation", () => {
3530
class MyController {
3631
@ApiOperation({ summary: "Hello", path: "/test", methods: ["get"] })
3732
operation() {}
3833
}
3934

40-
const metadata = OperationMetadataStorage.getMetadata(
41-
MyController.prototype,
42-
"operation",
43-
);
35+
const metadata = OperationMetadataStorage.getMetadata(MyController.prototype, "operation");
4436

4537
expect(metadata).toEqual({
4638
summary: "Hello",
@@ -55,10 +47,7 @@ test("@ApiBody", () => {
5547
operation() {}
5648
}
5749

58-
const metadata = OperationBodyMetadataStorage.getMetadata(
59-
MyController.prototype,
60-
"operation",
61-
);
50+
const metadata = OperationBodyMetadataStorage.getMetadata(MyController.prototype, "operation");
6251

6352
expect(metadata).toEqual({
6453
type: "string",
@@ -73,11 +62,7 @@ test("@ApiParam", () => {
7362
operation() {}
7463
}
7564

76-
const metadata = OperationParameterMetadataStorage.getMetadata(
77-
MyController.prototype,
78-
"operation",
79-
true,
80-
);
65+
const metadata = OperationParameterMetadataStorage.getMetadata(MyController.prototype, "operation", true);
8166

8267
expect(metadata).toEqual([
8368
{ in: "path", name: "test" },
@@ -92,11 +77,7 @@ test("@ApiHeader", () => {
9277
operation() {}
9378
}
9479

95-
const metadata = OperationParameterMetadataStorage.getMetadata(
96-
MyController.prototype,
97-
"operation",
98-
true,
99-
);
80+
const metadata = OperationParameterMetadataStorage.getMetadata(MyController.prototype, "operation", true);
10081

10182
expect(metadata).toEqual([
10283
{ in: "header", name: "test" },
@@ -111,11 +92,7 @@ test("@ApiCookie", () => {
11192
operation() {}
11293
}
11394

114-
const metadata = OperationParameterMetadataStorage.getMetadata(
115-
MyController.prototype,
116-
"operation",
117-
true,
118-
);
95+
const metadata = OperationParameterMetadataStorage.getMetadata(MyController.prototype, "operation", true);
11996

12097
expect(metadata).toEqual([
12198
{ in: "cookie", name: "test" },
@@ -130,11 +107,7 @@ test("@ApiQuery", () => {
130107
operation() {}
131108
}
132109

133-
const metadata = OperationParameterMetadataStorage.getMetadata(
134-
MyController.prototype,
135-
"operation",
136-
true,
137-
);
110+
const metadata = OperationParameterMetadataStorage.getMetadata(MyController.prototype, "operation", true);
138111

139112
expect(metadata).toEqual([
140113
{ in: "query", name: "test" },
@@ -149,11 +122,7 @@ test("@ApiResponse", () => {
149122
operation() {}
150123
}
151124

152-
const metadata = OperationResponseMetadataStorage.getMetadata(
153-
MyController.prototype,
154-
"operation",
155-
true,
156-
);
125+
const metadata = OperationResponseMetadataStorage.getMetadata(MyController.prototype, "operation", true);
157126

158127
expect(metadata).toEqual({
159128
default: { status: "default", mediaType: "text/html", type: "string" },
@@ -168,11 +137,7 @@ test("@ApiTags", () => {
168137
operation() {}
169138
}
170139

171-
const metadata = OperationMetadataStorage.getMetadata(
172-
MyController.prototype,
173-
"operation",
174-
true,
175-
);
140+
const metadata = OperationMetadataStorage.getMetadata(MyController.prototype, "operation", true);
176141

177142
expect(metadata.tags).toEqual(["Root", "Hello", "World"]);
178143
});
@@ -187,11 +152,7 @@ test("@ApiSecurity", () => {
187152
operation() {}
188153
}
189154

190-
const metadata = OperationSecurityMetadataStorage.getMetadata(
191-
MyController.prototype,
192-
"operation",
193-
true,
194-
);
155+
const metadata = OperationSecurityMetadataStorage.getMetadata(MyController.prototype, "operation", true);
195156

196157
expect(metadata).toEqual({
197158
custom: [],
@@ -216,10 +177,7 @@ test("@ApiExcludeOperation", () => {
216177
operation() {}
217178
}
218179

219-
const metadata = ExcludeMetadataStorage.getMetadata(
220-
MyController.prototype,
221-
"operation",
222-
);
180+
const metadata = ExcludeMetadataStorage.getMetadata(MyController.prototype, "operation");
223181
expect(metadata).toBe(true);
224182
});
225183

packages/openapi-metadata/test/loaders/array.test.ts

+2-6
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,11 @@ test("simple array", async () => {
2424
test("empty array should warn", async () => {
2525
// @ts-expect-error
2626
expect(await ArrayTypeLoader(context, [])).toEqual(undefined);
27-
expect(error).toContain(
28-
"You tried to specify an array type without any item",
29-
);
27+
expect(error).toContain("You tried to specify an array type without any item");
3028
});
3129

3230
test("array with multiple items should warn", async () => {
3331
// @ts-expect-error
3432
expect(await ArrayTypeLoader(context, [String, Number])).toEqual(undefined);
35-
expect(error).toContain(
36-
"You tried to specify an array type with multiple items.",
37-
);
33+
expect(error).toContain("You tried to specify an array type with multiple items.");
3834
});

0 commit comments

Comments
 (0)