Skip to content

Commit

Permalink
PR changes
Browse files Browse the repository at this point in the history
  • Loading branch information
tjosepo committed Jul 17, 2024
1 parent abe13b6 commit 2df9bcb
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 11 deletions.
2 changes: 1 addition & 1 deletion packages/create-schemas/src/plugins/header-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ interface HeaderPluginOptions {
header?: string;
}

export function headerPlugin({ header = "Do not modify. This file has been generated by @workleap/create-schemas" }: HeaderPluginOptions = {}): Plugin {
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 {
return {
name: "internal:header-plugin",
async transform({ code }) {
Expand Down
9 changes: 6 additions & 3 deletions packages/create-schemas/src/plugins/types-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,13 @@ export function typesPlugin(): Plugin {
.map(name => {
if (RESERVED_IDENTIFIERS.has(name)) {
throw new Error(`Invalid schema name: ${name}`);
} else {
return name;
}

if (toSafeName(name).length === 0) {
throw new Error(`Invalid schema name: ${name}`);
}

return name;
})
.map(name => `export type ${toSafeName(name)} = ${componentsIdentifier}["${schemasIdentifier}"]["${name}"];`)
.flatMap(stringToAST) as ts.Node[];
Expand All @@ -60,7 +64,6 @@ export function isComponentsSchema(node: ts.Node): node is ts.PropertySignature
&& node.name.text === schemasIdentifier;
}


/**
* OpenAPI field names must match `^[a-zA-Z0-9\.\-_]+$` which allows names that
* are not valid JavaScript/TypeScript identifiers. This function converts an
Expand Down
10 changes: 5 additions & 5 deletions packages/create-schemas/tests/__snapshots__/e2e.test.ts.snap
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

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

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

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

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

exports[`e2e > petstore.json 1`] = `
"/** Do not modify. This file has been generated by @workleap/create-schemas */
"/** This file has been generated by @workleap/create-schemas (https://github.com/gsoft-inc/wl-openapi-typescript). Do not modify manually. */
export interface paths {
"/pets": {
parameters: {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

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

exports[`watch > changing the input 2`] = `
"/** Do not modify. This file has been generated by @workleap/create-schemas */
"/** This file has been generated by @workleap/create-schemas (https://github.com/gsoft-inc/wl-openapi-typescript). Do not modify manually. */
export interface paths {
"/task": {
parameters: {
Expand Down
18 changes: 18 additions & 0 deletions packages/create-schemas/tests/data/invalid-name.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"openapi": "3.1.0",
"info": {
"version": "1.0.0",
"title": "Ambiguous Name",
"license": {
"name": "MIT",
"url": "https://opensource.org/licenses/MIT"
}
},
"components": {
"schemas": {
"...": {
"type": "string"
}
}
}
}
10 changes: 10 additions & 0 deletions packages/create-schemas/tests/generate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,14 @@ describe.concurrent("generate", () => {

expect(fn).rejects.toThrow();
});

test("reject invalid names", async ({ expect }) => {
const fn = async () => {
await generate(await resolveConfig({
input: join(dataFolder, "invalid-name.json")
}));
};

expect(fn).rejects.toThrow();
});
});

0 comments on commit 2df9bcb

Please sign in to comment.