Skip to content

Commit

Permalink
fix: dont error on schemas with type file for oas2 specs
Browse files Browse the repository at this point in the history
  • Loading branch information
dpopp07 committed Feb 14, 2019
1 parent e98e383 commit eeb826a
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 8 deletions.
13 changes: 9 additions & 4 deletions src/plugins/validation/2and3/semantic-validators/schema-ibm.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ module.exports.validate = function({ jsSpec, isOAS3 }, config) {
});

schemas.forEach(({ schema, path }) => {
let res = generateFormatErrors(schema, path, config);
let res = generateFormatErrors(schema, path, config, isOAS3);
errors.push(...res.error);
warnings.push(...res.warning);

Expand All @@ -104,7 +104,7 @@ module.exports.validate = function({ jsSpec, isOAS3 }, config) {

// Flag as an error any property that does not have a recognized "type" and "format" according to the
// [Swagger 2.0 spec](https://github.com/OAI/OpenAPI-Specification/blob/master/versions/2.0.md#data-types)
function generateFormatErrors(schema, contextPath, config) {
function generateFormatErrors(schema, contextPath, config, isOAS3) {
const result = {};
result.error = [];
result.warning = [];
Expand All @@ -125,7 +125,7 @@ function generateFormatErrors(schema, contextPath, config) {
}

checkStatus = config.invalid_type_format_pair;
if (checkStatus !== 'off' && !formatValid(schema)) {
if (checkStatus !== 'off' && !formatValid(schema, isOAS3)) {
const path = contextPath.concat(['type']);
const message = 'Property type+format is not well-defined.';
result[checkStatus].push({ path, message });
Expand All @@ -134,7 +134,7 @@ function generateFormatErrors(schema, contextPath, config) {
return result;
}

function formatValid(property) {
function formatValid(property, isOAS3) {
if (property.$ref) {
return true;
}
Expand Down Expand Up @@ -169,6 +169,11 @@ function formatValid(property) {
case 'array':
valid = true;
break;
case 'file':
// schemas of type file are allowed in swagger2 for responses and parameters
// of type 'formData' - the violating parameters are caught by parameters-ibm
valid = !isOAS3;
break;
default:
valid = false;
}
Expand Down
83 changes: 79 additions & 4 deletions test/plugins/validation/2and3/schema-ibm.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,35 @@ describe('validation plugin - semantic - schema-ibm - Swagger 2', () => {
expect(res.warnings.length).toEqual(0);
});

it('should non return an error for a response schema of type file', () => {
const config = {
schemas: {
invalid_type_format_pair: 'error'
}
};

const spec = {
paths: {
'/pets': {
get: {
responses: {
'200': {
description: 'legal response',
schema: {
type: 'file'
}
}
}
}
}
}
};

const res = validate({ jsSpec: spec }, config);
expect(res.errors.length).toEqual(0);
expect(res.warnings.length).toEqual(0);
});

it('should return a warning when a property name is not snake case', () => {
const config = {
schemas: {
Expand Down Expand Up @@ -574,7 +603,7 @@ describe('validation plugin - semantic - schema-ibm - OpenAPI 3', () => {
}
};

const res = validate({ jsSpec: spec }, config);
const res = validate({ jsSpec: spec, isOAS3: true }, config);
expect(res.errors.length).toEqual(1);
expect(res.errors[0].path).toEqual([
'components',
Expand All @@ -593,6 +622,52 @@ describe('validation plugin - semantic - schema-ibm - OpenAPI 3', () => {
expect(res.warnings.length).toEqual(0);
});

it('should return an error for a response schema of type file', () => {
const config = {
schemas: {
invalid_type_format_pair: 'error'
}
};

const spec = {
paths: {
'/pets': {
get: {
responses: {
'200': {
content: {
'application/json': {
schema: {
type: 'file'
}
}
}
}
}
}
}
}
};

const res = validate({ jsSpec: spec, isOAS3: true }, config);
expect(res.errors.length).toEqual(1);
expect(res.errors[0].path).toEqual([
'paths',
'/pets',
'get',
'responses',
'200',
'content',
'application/json',
'schema',
'type'
]);
expect(res.errors[0].message).toEqual(
'Property type+format is not well-defined.'
);
expect(res.warnings.length).toEqual(0);
});

it('should not validate an example when it contains the resemblence of a problem', () => {
const config = {
schemas: {
Expand Down Expand Up @@ -638,7 +713,7 @@ describe('validation plugin - semantic - schema-ibm - OpenAPI 3', () => {
}
};

const res = validate({ jsSpec: spec }, config);
const res = validate({ jsSpec: spec, isOAS3: true }, config);
expect(res.errors.length).toEqual(1);
expect(res.errors[0].path).toEqual([
'paths',
Expand Down Expand Up @@ -704,7 +779,7 @@ describe('validation plugin - semantic - schema-ibm - OpenAPI 3', () => {
}
};

const res = validate({ jsSpec: spec }, config);
const res = validate({ jsSpec: spec, isOAS3: true }, config);
expect(res.errors.length).toEqual(0);
expect(res.warnings.length).toEqual(1);
expect(res.warnings[0].path).toEqual([
Expand Down Expand Up @@ -750,7 +825,7 @@ describe('validation plugin - semantic - schema-ibm - OpenAPI 3', () => {
}
};

const res = validate({ jsSpec: spec }, config);
const res = validate({ jsSpec: spec, isOAS3: true }, config);
expect(res.errors.length).toEqual(0);
expect(res.warnings.length).toEqual(1);
expect(res.warnings[0].path).toEqual([
Expand Down

0 comments on commit eeb826a

Please sign in to comment.