Skip to content

Commit 5b88154

Browse files
committed
Update OpenAPI schema transforms
Signed-off-by: Andrew Richardson <[email protected]>
1 parent 30ab663 commit 5b88154

File tree

3 files changed

+40
-16
lines changed

3 files changed

+40
-16
lines changed

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
"lint": "eslint \"lib/*.ts\"",
1313
"lint:fix": "eslint \"lib/*.ts\" --fix",
1414
"format": "prettier --write \"lib/*.ts\" \"examples/**/*.ts\"",
15-
"schema": "ts-node scripts/schema.ts"
15+
"schema": "ts-node scripts/parseSchema.ts"
1616
},
1717
"repository": {
1818
"type": "git",
@@ -45,4 +45,4 @@
4545
"form-data": "^4.0.0",
4646
"ws": "^8.17.1"
4747
}
48-
}
48+
}

scripts/parseSchema.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import { promises as fs } from 'fs';
2+
import openapiTS, { astToString } from 'openapi-typescript';
3+
import * as ts from 'typescript';
4+
import { pathToFileURL } from 'url';
5+
6+
const STRING = ts.factory.createKeywordTypeNode(ts.SyntaxKind.StringKeyword);
7+
const ANY = ts.factory.createKeywordTypeNode(ts.SyntaxKind.AnyKeyword);
8+
9+
async function run() {
10+
const localPath = pathToFileURL('../firefly/doc-site/docs/swagger/swagger.yaml');
11+
console.log(`Generating types from ${localPath}`);
12+
const output = await openapiTS(localPath.toString(), {
13+
transform: (schemaObject, options) => {
14+
if (schemaObject.type === 'object' && schemaObject.additionalProperties) {
15+
// For objects with arbitrary properties, use "any" instead of "unknown"
16+
return ANY;
17+
}
18+
19+
if (
20+
schemaObject.type === 'string' &&
21+
schemaObject.format === 'uuid' &&
22+
schemaObject.nullable &&
23+
options.path &&
24+
options.path.indexOf('/responses/') >= 0 &&
25+
options.path.endsWith('/id')
26+
) {
27+
// For IDs in a response body, override to be non-nullable
28+
schemaObject.nullable = false;
29+
return STRING;
30+
}
31+
},
32+
});
33+
await fs.writeFile('lib/schema.ts', astToString(output));
34+
}
35+
36+
run().catch((err) => {
37+
console.error('Error running script', err);
38+
});

scripts/schema.ts

Lines changed: 0 additions & 14 deletions
This file was deleted.

0 commit comments

Comments
 (0)