Skip to content
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

New Fixes #8

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
5 changes: 3 additions & 2 deletions bin/oas3-to-raml.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const version = require('../package.json').version;

// Import converters
const { convertFromOas3ToOas2, convertFromOas2ToRaml } = require('../lib/converters');
const { fixNullables, fixExtendedTypes, fixExamples } = require('../lib/raml-fixers');
const { fixNullables, fixExtendedTypes, fixExamples, fixFormatType} = require('../lib/raml-fixers');

// Conversion steps, each step contains:
// * desc: Descriptive summary of step (e.g. 'converting from X to Y')
Expand All @@ -16,7 +16,8 @@ const steps = [
{ desc: 'Convert from OpenAPI 2.0 to RAML', action: convertFromOas2ToRaml },
{ desc: 'Fix \'nullable\' types', action: fixNullables },
{ desc: 'Fix extended types', action: fixExtendedTypes },
{ desc: 'Fix numeric examples', action: fixExamples }
{ desc: 'Fix numeric examples', action: fixExamples },
{ desc: 'Fix format type', action: fixFormatType }
];

/**
Expand Down
5 changes: 4 additions & 1 deletion lib/raml-fixers/example-fixer.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,16 @@ const _ = require('lodash');

function customizer (input) {
if (!_.isPlainObject(input) || input.example == null ||
isNaN(input.example) || input.type !== 'string') {
isNaN(input.example) || (input.type !== 'string' && input.type != null) ) {
// Let caller handle cloning
return;
}

const output = _.cloneDeep(input);
output.example = output.example.toString();
if (output.type == null) {
output.type = 'string'
}
return output;
}

Expand Down
35 changes: 35 additions & 0 deletions lib/raml-fixers/format-type-fixer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
const _ = require('lodash');

/**
* Fix string examples that have unquoted numbers, for example:
*
* properties:
* name: E-mail
* type: string
* format: email
*
* -- converts to --
*
* properties:
* name: E-mail
* type: string
* (oas-format): email
*/

function customizer (input) {
if (!_.isPlainObject(input) || input.format == null) {
// Let caller handle cloning
return;
}

const output = _.clone(input);
output['(oas-format)'] = output.format;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sebastian, would it be possible to fix the format in OpenAPI specification?

delete output.format;
return output;
}

function fixFormatType (input) {
return _.cloneDeepWith(input, customizer);
}

module.exports = fixFormatType;
4 changes: 3 additions & 1 deletion lib/raml-fixers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const yaml = require('js-yaml');
const fixNullables = require('./nullable-fixer');
const fixExtendedTypes = require('./extended-type-fixer');
const fixExamples = require('./example-fixer');
const fixFormatType = require('./format-type-fixer');

/**
* Transform input using specified function
Expand All @@ -22,5 +23,6 @@ function transform (ramlTransformer, input) {
module.exports = {
fixNullables: (input) => transform(fixNullables, input),
fixExtendedTypes: (input) => transform(fixExtendedTypes, input),
fixExamples: (input) => transform(fixExamples, input)
fixExamples: (input) => transform(fixExamples, input),
fixFormatType: (input) => transform(fixFormatType, input)
};