Skip to content

Commit 9244698

Browse files
committed
Fix issue where apiProperty alter the class property
1 parent 96fc424 commit 9244698

File tree

12 files changed

+113
-16
lines changed

12 files changed

+113
-16
lines changed

.changeset/chilled-rules-judge.md

-5
This file was deleted.

packages/openapi-adonis/CHANGELOG.md

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# openapi-adonis
2+
3+
## 0.0.0-20240826235548
4+
5+
### Patch Changes
6+
7+
- Updated dependencies []:
8+
9+
10+
## 0.0.0-20240826232507
11+
12+
### Patch Changes
13+
14+
- Automatically append apiParam from route pattern
15+
16+
## 0.0.0-20240826230314
17+
18+
### Patch Changes
19+
20+
- Updated dependencies []:
21+
22+
23+
## 0.0.0-20240826225846
24+
25+
### Patch Changes
26+
27+
- Updated dependencies []:
28+
29+
30+
## 0.0.0-20240826224734
31+
32+
### Major Changes
33+
34+
- Initial release
35+
36+
### Patch Changes
37+
38+
- Updated dependencies []:
39+

packages/openapi-adonis/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "openapi-adonis",
33
"description": "Auto-Generate OpenAPI specifications for AdonisJS",
4-
"version": "0.0.1",
4+
"version": "0.0.0-20240826235548",
55
"author": {
66
"name": "Martin PAUCOT",
77
"email": "[email protected]"

packages/openapi-adonis/src/loaders/loadRoute.ts

+11-3
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
import type { OpenAPIV3 } from "openapi-types";
22
import { OperationBuilder, type DocumentBuilder } from "openapi-metadata/builders";
33
import type { AdonisRoute } from "../types";
4-
import { loadApiOperation, loadControllerOperation } from "openapi-metadata/loaders";
4+
import { loadApiOperation, loadApiParam, loadControllerOperation } from "openapi-metadata/loaders";
55
import { normalizeRoutePattern } from "../utils/normalizeRoutePattern";
66

77
export async function loadRoute(document: DocumentBuilder, route: AdonisRoute) {
88
if (typeof route.handler !== "object" || !Array.isArray(route.handler.reference)) {
99
return;
1010
}
1111

12-
const importer = route.handler.reference[0] as () => Promise<{ default: any }>;
12+
const importer = route.handler.reference[0] as () => Promise<{
13+
default: any;
14+
}>;
1315
const propertyKey = route.handler.reference[1] as string;
1416

1517
const target = (await importer().then((t: any) => t.default)) as any;
@@ -21,11 +23,17 @@ export async function loadRoute(document: DocumentBuilder, route: AdonisRoute) {
2123

2224
const operation = new OperationBuilder();
2325

26+
const { params, pattern } = normalizeRoutePattern(route.pattern);
27+
2428
loadApiOperation(operation, {
2529
method: method.toLowerCase() as `${OpenAPIV3.HttpMethods}`,
26-
pattern: normalizeRoutePattern(route.pattern),
30+
pattern,
2731
});
2832

33+
for (const name of params) {
34+
loadApiParam(document, operation, { name, type: "string" });
35+
}
36+
2937
loadControllerOperation(document, operation, target.prototype, propertyKey);
3038

3139
document.addOperation(operation);

packages/openapi-adonis/src/resolvers/vine-type.resolver.ts

+5
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@ export class VineTypeResolver<
3232
return this.#name;
3333
}
3434

35+
public static supports(type: any): boolean {
36+
// console.log(type);
37+
return true;
38+
}
39+
3540
async schema(): Promise<OpenAPIV3.SchemaObject> {
3641
const validator = this.validator.toJSON();
3742
const node = validator.schema.schema;
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
11
export const normalizeRoutePattern = (pattern: string) => {
22
const segments = pattern.split("/");
33

4-
return segments
4+
const params: string[] = [];
5+
const path = segments
56
.map((s) => {
67
if (s.startsWith(":")) {
7-
return `{${s.replace(":", "")}}`;
8+
const param = s.replace(":", "");
9+
params.push(param);
10+
return `{${param}}`;
811
}
912

1013
return s;
1114
})
1215
.join("/");
16+
17+
return { pattern: path, params };
1318
};

packages/openapi-adonis/test/vine.test.ts

+12-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
import vine from "@vinejs/vine";
2-
import { enrichType, parseArrayNode, parseLiteralNode, parseObjectNode } from "../src/resolvers/vine-type.resolver";
2+
import {
3+
VineTypeResolver,
4+
enrichType,
5+
parseArrayNode,
6+
parseLiteralNode,
7+
parseObjectNode,
8+
} from "../src/resolvers/vine-type.resolver";
9+
import { DocumentBuilder } from "openapi-metadata/builders";
310

411
describe("parseLiteralNode", () => {
512
// INFO: This test is not really necessary
@@ -27,13 +34,15 @@ describe("parseObjectNode", () => {
2734
it("should parse object", () => {
2835
const validator = vine.compile(
2936
vine.object({
30-
example: vine.string(),
37+
email: vine.string().email(),
38+
password: vine.string(),
3139
}),
3240
);
3341
const json = validator.toJSON();
3442
const result: any = parseObjectNode(json.schema.schema as any, json.refs);
3543

36-
expect(result.properties.example).toBeDefined();
44+
expect(result.properties.email).toBeDefined();
45+
expect(result.properties.password).toBeDefined();
3746
});
3847

3948
it("should parse nested object", () => {
+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# openapi-metadata
2+
3+
## 0.0.0-20240826235548
4+
5+
### Patch Changes
6+
7+
- Fix issue where apiProperty alter the class property
8+
9+
## 0.0.0-20240826230314
10+
11+
### Patch Changes
12+
13+
- Add ability to provide arrays as type
14+
15+
## 0.0.0-20240826225846
16+
17+
### Major Changes
18+
19+
- Add ability to provide arrays as type
20+
21+
## 0.0.0-20240826224734
22+
23+
### Major Changes
24+
25+
- Initial release

packages/openapi-metadata/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "openapi-metadata",
33
"description": "Auto-Generate OpenAPI specifications from Typescript decorators",
4-
"version": "0.0.1",
4+
"version": "0.0.0-20240826235548",
55
"author": {
66
"name": "Martin PAUCOT",
77
"email": "[email protected]"

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

-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ export function apiProperty(options: ApiPropertyOptions = {}): PropertyDecorator
1616
}
1717

1818
Reflect.defineMetadata(`${ApiPropertyMetadataKeyPrefix}${propertyKey.toString()}`, options, target);
19-
return target;
2019
};
2120
}
2221

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

+7
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,13 @@ export async function resolveType(
66
document: DocumentBuilder,
77
type: SchemaType,
88
): Promise<OpenAPIV3.ReferenceObject | OpenAPIV3.SchemaObject> {
9+
if (Array.isArray(type)) {
10+
return {
11+
type: "array",
12+
items: await resolveType(document, type[0]),
13+
};
14+
}
15+
916
if (typeof type === "string") {
1017
return {
1118
type: type as OpenAPIV3.NonArraySchemaObjectType, // TODO: Fix that

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

+5
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,11 @@ describe("loaders", () => {
210210
expect(await resolveType(document, Number)).toEqual({ type: "number" });
211211
expect(await resolveType(document, String)).toEqual({ type: "string" });
212212
});
213+
214+
it("should properly resolve array", async () => {
215+
const document = new DocumentBuilder();
216+
expect(await resolveType(document, [Boolean])).toEqual({ type: "array", items: { type: "boolean" } });
217+
});
213218
});
214219
});
215220

0 commit comments

Comments
 (0)