Skip to content

Commit 2df9bcb

Browse files
committedJul 17, 2024·
PR changes
1 parent abe13b6 commit 2df9bcb

File tree

6 files changed

+42
-11
lines changed

6 files changed

+42
-11
lines changed
 

‎packages/create-schemas/src/plugins/header-plugin.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ interface HeaderPluginOptions {
44
header?: string;
55
}
66

7-
export function headerPlugin({ header = "Do not modify. This file has been generated by @workleap/create-schemas" }: HeaderPluginOptions = {}): Plugin {
7+
export function headerPlugin({ header = "This file has been generated by @workleap/create-schemas (https://github.com/gsoft-inc/wl-openapi-typescript). Do not modify manually." }: HeaderPluginOptions = {}): Plugin {
88
return {
99
name: "internal:header-plugin",
1010
async transform({ code }) {

‎packages/create-schemas/src/plugins/types-plugin.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,13 @@ export function typesPlugin(): Plugin {
3434
.map(name => {
3535
if (RESERVED_IDENTIFIERS.has(name)) {
3636
throw new Error(`Invalid schema name: ${name}`);
37-
} else {
38-
return name;
3937
}
38+
39+
if (toSafeName(name).length === 0) {
40+
throw new Error(`Invalid schema name: ${name}`);
41+
}
42+
43+
return name;
4044
})
4145
.map(name => `export type ${toSafeName(name)} = ${componentsIdentifier}["${schemasIdentifier}"]["${name}"];`)
4246
.flatMap(stringToAST) as ts.Node[];
@@ -60,7 +64,6 @@ export function isComponentsSchema(node: ts.Node): node is ts.PropertySignature
6064
&& node.name.text === schemasIdentifier;
6165
}
6266

63-
6467
/**
6568
* OpenAPI field names must match `^[a-zA-Z0-9\.\-_]+$` which allows names that
6669
* are not valid JavaScript/TypeScript identifiers. This function converts an

‎packages/create-schemas/tests/__snapshots__/e2e.test.ts.snap

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
22

33
exports[`e2e > officevice.yaml / file URLs 1`] = `
4-
"/** Do not modify. This file has been generated by @workleap/create-schemas */
4+
"/** This file has been generated by @workleap/create-schemas (https://github.com/gsoft-inc/wl-openapi-typescript). Do not modify manually. */
55
export interface paths {
66
"/good-vibes-points/{userId}": {
77
parameters: {
@@ -86,7 +86,7 @@ export type Endpoints = keyof paths;
8686
`;
8787

8888
exports[`e2e > officevice.yaml / file paths 1`] = `
89-
"/** Do not modify. This file has been generated by @workleap/create-schemas */
89+
"/** This file has been generated by @workleap/create-schemas (https://github.com/gsoft-inc/wl-openapi-typescript). Do not modify manually. */
9090
export interface paths {
9191
"/good-vibes-points/{userId}": {
9292
parameters: {
@@ -171,7 +171,7 @@ export type Endpoints = keyof paths;
171171
`;
172172

173173
exports[`e2e > officevice.yaml / relative path 1`] = `
174-
"/** Do not modify. This file has been generated by @workleap/create-schemas */
174+
"/** This file has been generated by @workleap/create-schemas (https://github.com/gsoft-inc/wl-openapi-typescript). Do not modify manually. */
175175
export interface paths {
176176
"/good-vibes-points/{userId}": {
177177
parameters: {
@@ -256,7 +256,7 @@ export type Endpoints = keyof paths;
256256
`;
257257

258258
exports[`e2e > petstore.json / remote URL 1`] = `
259-
"/** Do not modify. This file has been generated by @workleap/create-schemas */
259+
"/** This file has been generated by @workleap/create-schemas (https://github.com/gsoft-inc/wl-openapi-typescript). Do not modify manually. */
260260
export interface paths {
261261
"/pet": {
262262
parameters: {
@@ -1267,7 +1267,7 @@ export type Endpoints = keyof paths;
12671267
`;
12681268

12691269
exports[`e2e > petstore.json 1`] = `
1270-
"/** Do not modify. This file has been generated by @workleap/create-schemas */
1270+
"/** This file has been generated by @workleap/create-schemas (https://github.com/gsoft-inc/wl-openapi-typescript). Do not modify manually. */
12711271
export interface paths {
12721272
"/pets": {
12731273
parameters: {

‎packages/create-schemas/tests/__snapshots__/watch.test.ts.snap

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
22

33
exports[`watch > changing the input 1`] = `
4-
"/** Do not modify. This file has been generated by @workleap/create-schemas */
4+
"/** This file has been generated by @workleap/create-schemas (https://github.com/gsoft-inc/wl-openapi-typescript). Do not modify manually. */
55
export interface paths {
66
"/pets": {
77
parameters: {
@@ -166,7 +166,7 @@ export type Endpoints = keyof paths;
166166
`;
167167

168168
exports[`watch > changing the input 2`] = `
169-
"/** Do not modify. This file has been generated by @workleap/create-schemas */
169+
"/** This file has been generated by @workleap/create-schemas (https://github.com/gsoft-inc/wl-openapi-typescript). Do not modify manually. */
170170
export interface paths {
171171
"/task": {
172172
parameters: {
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"openapi": "3.1.0",
3+
"info": {
4+
"version": "1.0.0",
5+
"title": "Ambiguous Name",
6+
"license": {
7+
"name": "MIT",
8+
"url": "https://opensource.org/licenses/MIT"
9+
}
10+
},
11+
"components": {
12+
"schemas": {
13+
"...": {
14+
"type": "string"
15+
}
16+
}
17+
}
18+
}

‎packages/create-schemas/tests/generate.test.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,14 @@ describe.concurrent("generate", () => {
3131

3232
expect(fn).rejects.toThrow();
3333
});
34+
35+
test("reject invalid names", async ({ expect }) => {
36+
const fn = async () => {
37+
await generate(await resolveConfig({
38+
input: join(dataFolder, "invalid-name.json")
39+
}));
40+
};
41+
42+
expect(fn).rejects.toThrow();
43+
});
3444
});

0 commit comments

Comments
 (0)
Please sign in to comment.