@@ -6,6 +6,7 @@ const { parse } = require('@stoplight/yaml');
6
6
const fetch = require ( 'node-fetch' ) ;
7
7
const fs = require ( 'fs' ) ;
8
8
const readFileAsync = require ( 'util' ) . promisify ( fs . readFile ) ;
9
+ const oas3schema = require ( './refs/oas3-schema.json' ) ;
9
10
10
11
function InvalidTypeError ( message ) {
11
12
this . name = 'InvalidTypeError' ;
@@ -52,8 +53,10 @@ function convertSchema(schema, path, parent, parentPath) {
52
53
53
54
if ( schema . type === 'array' && typeof schema . items === 'undefined' ) {
54
55
schema . items = { } ;
55
- }
56
+ }
56
57
58
+ // should be called last
59
+ schema = convertIllegalKeywordsAsExtensions ( schema ) ;
57
60
return schema ;
58
61
}
59
62
@@ -160,6 +163,17 @@ function convertPatternProperties(schema) {
160
163
return schema ;
161
164
}
162
165
166
+ // keywords (or property names) that are not recognized within OAS3 are rewritten into extensions.
167
+ function convertIllegalKeywordsAsExtensions ( schema ) {
168
+ Object . keys ( schema )
169
+ . filter ( keyword => ! keyword . startsWith ( oasExtensionPrefix ) && ! allowedKeywords . includes ( keyword ) )
170
+ . forEach ( keyword => {
171
+ schema [ oasExtensionPrefix + keyword ] = schema [ keyword ] ;
172
+ delete schema [ keyword ] ;
173
+ } ) ;
174
+ return schema ;
175
+ }
176
+
163
177
function convertExamples ( schema ) {
164
178
if ( schema [ 'examples' ] && Array . isArray ( schema [ 'examples' ] ) ) {
165
179
schema [ 'example' ] = schema [ 'examples' ] [ 0 ] ;
@@ -245,4 +259,11 @@ const resolver = new Resolver({
245
259
} ,
246
260
} ) ;
247
261
262
+ const oasExtensionPrefix = 'x-' ;
263
+
264
+ // TODO: having definitions inside an oas3 schema isn't exactly valid,
265
+ // maybe it is an idea to extract and split them into multiple oas3 schemas and reference to them.
266
+ // For now leaving as is.
267
+ const allowedKeywords = [ '$ref' , 'definitions' ] . concat ( Object . keys ( oas3schema . definitions . Schema . properties ) ) ;
268
+
248
269
module . exports = convert ;
0 commit comments