-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathopenapi-preprocess.js
More file actions
59 lines (54 loc) · 2.56 KB
/
openapi-preprocess.js
File metadata and controls
59 lines (54 loc) · 2.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import { promises } from 'node:fs'
/**
* helper script to parse and rewrite the openapi.json file
* the openapi-typescript-fetch generator cannot correctly create types based on the existing schema,
* as the discriminator-property is not correctly resolved
* instead of a union-type an extendable interface is created, which causes issue when the base-type is used
* e.g.
* ```ts
* type AuthMethod = AuthMethodTan | AuthMethodDecoupled | AuthMethodChallengeResponse
* ```
* vs
* ```ts
* interface AuthMethod {
* flow: FlowType
* }
*
* interface AuthMethodTan extends AuthMethod {
* flow: FlowType.TAN,
* id: string,
* ...
* }
* ```
*
* also replaces the description-block with a default to avoid updating every file every time it changes
*/
const openApiFile = await promises.readFile('openapi.json', { encoding: 'utf8' });
const openApi = JSON.parse(openApiFile);
for (const [schemaName, schema] of Object.entries(openApi.components.schemas)) {
if (schema.discriminator?.mapping) {
schema.oneOf = schema.oneOf || []; // using one-of allows the typescript-fetch generator to discriminate correctly
for (const [value, path] of Object.entries(schema.discriminator.mapping)) {
schema.oneOf.push({ $ref: path });
const discriminatedName = path.split('/').at(-1); // path like "#/components/schemas/AuthMethodTan", need only "AuthMethodTan"
const discriminatedSchema = openApi.components.schemas[discriminatedName]
// find index of discriminator-entry in discriminated-allOf array
const discriminatorIndex = discriminatedSchema.allOf.findIndex(allOfEntry => allOfEntry.$ref.split('/').at(-1) === schemaName);
if (discriminatorIndex !== -1) {
// overwrite with property entry to correctly allow discrimination
discriminatedSchema.allOf[discriminatorIndex] = {
properties: {
[schema.discriminator.propertyName]: {
type: 'string', // can't use value as type as it does not resolve correctly
enum: [value],
},
},
};
} else {
throw new Error('Discriminator entry unexpectedly not found')
}
}
}
}
openApi.info.description = 'The brokerize API allows clients to implement multi-brokerage with a unified interface. For more information, visit brokerize.com';
await promises.writeFile('temp/openapi.json', JSON.stringify(openApi, null, " "));