@@ -6,6 +6,7 @@ const { parse } = require('@stoplight/yaml');
66const fetch = require ( 'node-fetch' ) ;
77const fs = require ( 'fs' ) ;
88const readFileAsync = require ( 'util' ) . promisify ( fs . readFile ) ;
9+ const oas3schema = require ( './refs/oas3-schema.json' ) ;
910
1011function InvalidTypeError ( message ) {
1112 this . name = 'InvalidTypeError' ;
@@ -52,8 +53,10 @@ function convertSchema(schema, path, parent, parentPath) {
5253
5354 if ( schema . type === 'array' && typeof schema . items === 'undefined' ) {
5455 schema . items = { } ;
55- }
56+ }
5657
58+ // should be called last
59+ schema = convertIllegalKeywordsAsExtensions ( schema ) ;
5760 return schema ;
5861}
5962
@@ -160,6 +163,17 @@ function convertPatternProperties(schema) {
160163 return schema ;
161164}
162165
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+
163177function convertExamples ( schema ) {
164178 if ( schema [ 'examples' ] && Array . isArray ( schema [ 'examples' ] ) ) {
165179 schema [ 'example' ] = schema [ 'examples' ] [ 0 ] ;
@@ -245,4 +259,11 @@ const resolver = new Resolver({
245259 } ,
246260} ) ;
247261
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+
248269module . exports = convert ;
0 commit comments