Skip to content

Commit

Permalink
Merge pull request #24 from IBM/dont-flag-response-files
Browse files Browse the repository at this point in the history
fix: dont error on schemas with type file for oas2 specs
  • Loading branch information
dpopp07 authored Feb 19, 2019
2 parents 21af082 + ebbe1b5 commit 9ccd7f7
Show file tree
Hide file tree
Showing 2 changed files with 173 additions and 8 deletions.
28 changes: 24 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, contextPath, 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, path, isOAS3) {
if (property.$ref) {
return true;
}
Expand Down Expand Up @@ -169,6 +169,12 @@ 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
// note: type file is only allowed for root schemas (not properties, etc.)
valid = !isOAS3 && isRootSchema(path);
break;
default:
valid = false;
}
Expand Down Expand Up @@ -303,3 +309,17 @@ function checkEnumValues(schema, contextPath, config) {

return result;
}

// NOTE: this function is Swagger 2 specific and would need to be adapted to be used with OAS
function isRootSchema(path) {
const current = path[path.length - 1];
const parent = path[path.length - 2];

// `schema` can only exist in parameter or response objects
// root schemas can also appear under a variable key in the `definitions` section
// if it is the top level `definitions` section (rather than some property named "definitions"),
// the path length will be 2
return (
current === 'schema' || (parent === 'definitions' && path.length === 2)
);
}
153 changes: 149 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,105 @@ 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 non return an error for a definition with root type of file', () => {
const config = {
schemas: {
invalid_type_format_pair: 'error'
}
};

const spec = {
definitions: {
SomeSchema: {
type: 'file',
description: 'file schema, used for parameter or response'
}
}
};

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

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

const spec = {
paths: {
'/pets': {
get: {
responses: {
'200': {
description: 'legal response',
schema: {
properties: {
this_is_bad: {
type: 'file',
description: 'non-root type of file is bad'
}
}
}
}
}
}
}
}
};

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

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 +673,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 +692,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 +783,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 +849,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 +895,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 9ccd7f7

Please sign in to comment.