-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathopenapi31to30.js
70 lines (63 loc) · 2 KB
/
openapi31to30.js
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
60
61
62
63
64
65
66
67
68
69
70
/**
* @copyright Copyright 2021 Kevin Locke <kevin@kevinlocke.name>
* @license MIT
* @module "procore-docs-to-openapi/openapi31to30.js"
*/
import OpenApi31To30Transformer
from '@kevinoid/openapi-transformers/openapi31to30.js';
import RemoveTypeIfTransformer
from '@kevinoid/openapi-transformers/remove-type-if.js';
import OpenApiTransformerBase from 'openapi-transformer-base';
/** Predicate which matches a schema type constraint that validates all
* non-null primitive types, excluding 'array' and 'object'.
*
* Removing a type constraint matching this predicate expands the types which
* the schema validates to include 'array' and 'object' (and 'null' in OAS 3.1).
* Since few generators support union types, and this is currently only used
* for custom_field_*, which includes `array`, unconstrained type is likely
* preferable to splitting such schemas using anyOf/oneOf.
*
* @private
*/
function allPrimitiveTypes(type) {
return Array.isArray(type)
&& type.length >= 3
&& [
'boolean',
'number',
'string',
].every((t) => type.includes(t));
}
/**
* Transforms an OpenAPI 3.1.* document for the Procore API to OpenAPI 3.0.3.
*/
// eslint-disable-next-line import/no-unused-modules
export default class ProcoreOpenApi31To30Transformer
extends OpenApiTransformerBase {
constructor() {
super();
this.transformers = [
new RemoveTypeIfTransformer(allPrimitiveTypes),
new OpenApi31To30Transformer(),
];
}
transformOpenApi(openApi) {
if (typeof openApi !== 'object'
|| openApi === null
|| Array.isArray(openApi)) {
this.warn('Ignoring non-object OpenAPI', openApi);
return openApi;
}
if (typeof openApi.openapi !== 'string'
|| !openApi.openapi.startsWith('3.1.')) {
this.warn('Expected OpenAPI 3.1, got', openApi.openapi);
}
for (const transformer of this.transformers) {
openApi = transformer.transformOpenApi(openApi);
}
return {
...openApi,
openapi: '3.0.3',
};
}
}