Skip to content

feat: rewrite illegal keywords as extensions #10

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const { parse } = require('@stoplight/yaml');
const fetch = require('node-fetch');
const fs = require('fs');
const readFileAsync = require('util').promisify(fs.readFile);
const oas3schema = require('./refs/oas3-schema.json');

function InvalidTypeError(message) {
this.name = 'InvalidTypeError';
Expand Down Expand Up @@ -52,8 +53,10 @@ function convertSchema(schema, path, parent, parentPath) {

if (schema.type === 'array' && typeof schema.items === 'undefined') {
schema.items = {};
}
}

// should be called last
schema = convertIllegalKeywordsAsExtensions(schema);
return schema;
}

Expand Down Expand Up @@ -160,6 +163,17 @@ function convertPatternProperties(schema) {
return schema;
}

// keywords (or property names) that are not recognized within OAS3 are rewritten into extensions.
function convertIllegalKeywordsAsExtensions(schema) {
Object.keys(schema)
.filter(keyword => !keyword.startsWith(oasExtensionPrefix) && !allowedKeywords.includes(keyword))
.forEach(keyword => {
schema[oasExtensionPrefix + keyword] = schema[keyword];
delete schema[keyword];
});
return schema;
}

function convertExamples(schema) {
if (schema['examples'] && Array.isArray(schema['examples'])) {
schema['example'] = schema['examples'][0];
Expand Down Expand Up @@ -245,4 +259,11 @@ const resolver = new Resolver({
},
});

const oasExtensionPrefix = 'x-';

// TODO: having definitions inside an oas3 schema isn't exactly valid,
// maybe it is an idea to extract and split them into multiple oas3 schemas and reference to them.
// For now leaving as is.
const allowedKeywords = ['$ref', 'definitions'].concat(Object.keys(oas3schema.definitions.Schema.properties));

module.exports = convert;
Loading