Skip to content

Commit

Permalink
Add test and address feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
Vlad Barosan committed Mar 15, 2018
1 parent 2fbf9d8 commit 1530142
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 37 deletions.
82 changes: 49 additions & 33 deletions lib/util/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -759,42 +759,58 @@ function isPropertyRequired(propName, model) {
*/
exports.statusCodeStringToStatusCode = lodash.invert(lodash.mapValues(http.STATUS_CODES, value => { return value.replace(/ |-/g, "").toLowerCase(); }));


/**
* Models an ARM cloud error.
* Models an ARM cloud error schema.
*/
exports.CloudError = {
exports.CloudErrorSchema = {
description: "Error response describing why the operation failed.",
schema: {
properties: {
error: {
properties: {
code: {
type: "string",
description: "An identifier for the error. Codes are invariant and are intended to be consumed programmatically."
},
message: {
type: "string",
description: "A message describing the error, intended to be suitable for display in a user interface."
},
target: {
type: "string",
description: "The target of the particular error. For example, the name of the property in error."
},
details: {
type: "array",
items: { type: "object" },
description: "A list of additional details about the error."
},
additionalInfo: {
type: "array",
items: { type: "object" },
description: "A list of additional info about an error."
}
},
additionalProperties: false
}
},
additionalProperties: false
"$ref": "#/definitions/CloudErrorWrapper"
}
};

/**
* Models an ARM cloud error wrapper.
*/
exports.CloudErrorWrapper = {
type: "object",
properties: {
error: {
"$ref": "#/definitions/CloudError"
}
},
additionalProperties: false
};

/**
* Models a Cloud Error
*/
exports.CloudError = {
type: "object",
properties: {
code: {
type: "string",
description: "An identifier for the error. Codes are invariant and are intended to be consumed programmatically."
},
message: {
type: "string",
description: "A message describing the error, intended to be suitable for display in a user interface."
},
target: {
type: "string",
description: "The target of the particular error. For example, the name of the property in error."
},
details: {
type: "array",
items: { type: "object" },
description: "A list of additional details about the error."
},
additionalInfo: {
type: "array",
items: { type: "object" },
description: "A list of additional info about an error."
}
},
required: ["code", "message"],
additionalProperties: false
};
7 changes: 5 additions & 2 deletions lib/validators/specResolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -607,12 +607,15 @@ class SpecResolver {
modelImplicitDefaultResponse() {
let self = this;
let spec = self.specInJson;
if (!spec.definitions.CloudError) {
spec.definitions.CloudErrorWrapper = utils.CloudErrorWrapper;
spec.definitions.CloudError = utils.CloudError;
}
for (let pathObj of utils.getValues(spec.paths)) {
for (let operation of utils.getValues(pathObj)) {

// going through responses
if (operation.responses && !operation.responses.default) {
operation.responses.default = utils.CloudError;
operation.responses.default = utils.CloudErrorSchema;
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 25 additions & 1 deletion test/liveValidatorTests.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ var assert = require('assert'),
os = require('os'),
glob = require('glob'),
LiveValidator = require('../lib/validators/liveValidator.js'),
Constants = require('../lib/util/constants');
Constants = require('../lib/util/constants'),
utils = require('../lib/util/utils');

const livePaths = glob.sync(path.join(__dirname, 'liveValidation/swaggers/**/live/*.json'));
describe('Live Validator', function () {
Expand Down Expand Up @@ -260,6 +261,29 @@ describe('Live Validator', function () {
done();
}).catch(done);
});
it('it should create an implicit default response and find it', function (done) {
let options = {
"directory": "./test/liveValidation/swaggers/specification/scenarios",
"swaggerPathsPattern": "**/*.json",
"shouldModelImplicitDefaultResponse": true
};
let apiUrl = "https://management.azure.com/subscriptions/subscriptionId/providers/Microsoft.Test/storageAccounts?api-version=2016-01-01";

let validator = new LiveValidator(options);
validator.initialize().then(() => {
// Operations to match is StorageAccounts_List
let operations = validator.cache['microsoft.test']['2016-01-01']['post'];

for (const operation of operations) {
assert(operation.responses.default);
assert.deepEqual(operation.responses.default.schema.properties.error, utils.CloudError);
}
done();
}).catch((err) => {
assert.ifError(err);
done();
}).catch(done);
});
});

describe('Initialize cache and validate', function () {
Expand Down

0 comments on commit 1530142

Please sign in to comment.