Socket
Socket
Sign inDemoInstall

swagger-parser

Package Overview
Dependencies
Maintainers
1
Versions
81
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

swagger-parser - npm Package Compare versions

Comparing version 1.0.9 to 1.0.10

96

lib/dereference.js
'use strict';
var _ = require('lodash');
var url = require('url');
var read = require('./read');

@@ -10,3 +11,3 @@ var util = require('./util');

// (e.g. "http://company.com", "https://company.com", "./file.yaml", "../../file.yaml")
var externalPointerPattern = /^(https?\:\/\/|\.)/i;
var externalPointerPattern = /^https?\:\/\/|^\.|\.yaml$|\.json$/i;

@@ -94,7 +95,7 @@ module.exports = dereference;

* @param {string} targetProp the property name on targetObj to be updated with the resolved reference
* @param {State} state the state for the current parse operation
* @param {State} state the state for the current parse operation
* @param {function} callback
*/
function resolvePointer(pointerPath, pointerValue, targetObj, targetProp, state, callback) {
var resolved;
var isExternal;

@@ -121,2 +122,9 @@ if (_.isEmpty(pointerValue)) {

try {
if (isExternalPointer(pointerValue)) {
// Normalize the pointer value by resolving the path/URL relative to the Swagger file.
// NOTE: Normalizes the pointer value helps ensure that different pointers end up resolving to the same value
pointerValue = url.resolve(state.baseDir, pointerValue);
isExternal = true;
}
// If we've already resolved this pointer, then return the resolved value

@@ -127,34 +135,7 @@ if (_.has(state.resolvedPointers, pointerValue)) {

if (isExternalPointer(pointerValue)) {
// Set the resolved value to an empty object for now, so other reference pointers
// can point to this object. Once we finish downloading the URL, we can update
// the empty object with the real data.
state.resolvedPointers[pointerValue] = {};
read.fileOrUrl(pointerValue, state,
function(err, data) {
if (!err) {
// Now that we've finished downloaded the data, update the empty object we created earlier
data = _.extend(state.resolvedPointers[pointerValue], data);
}
return returnResolvedValue(err, data);
}
);
if (isExternal) {
resolveExternalPointer(pointerValue, state, returnResolvedValue);
}
else {
var propertyPath;
if (pointerValue.indexOf('#/') === 0) {
// "#/paths/users/responses/200" => "paths.users.responses.200"
propertyPath = pointerValue.substr(2).replace(/\//g, '.');
}
else {
// "pet" => "definitions.pet"
propertyPath = 'definitions.' + pointerValue.replace(/\//g, '.');
}
// Get the property value from the schema
resolved = resultDeep(state.swagger, propertyPath);
state.resolvedPointers[pointerValue] = resolved;
returnResolvedValue(null, resolved);
resolveInternalPointer(pointerValue, state, returnResolvedValue);
}

@@ -178,2 +159,51 @@ }

/**
* Resolves a pointer to a property in the Swagger spec.
* @param {string} pointer The $ref pointer (e.g. "pet", "#/parameters/pet")
* @param {State} state the state for the current parse operation
* @param {function} callback
*/
function resolveInternalPointer(pointer, state, callback) {
var propertyPath;
if (pointer.indexOf('#/') === 0) {
// "#/paths/users/responses/200" => "paths.users.responses.200"
propertyPath = pointer.substr(2).replace(/\//g, '.');
}
else {
// "pet" => "definitions.pet"
propertyPath = 'definitions.' + pointer.replace(/\//g, '.');
}
// Get the property value from the schema
var resolved = resultDeep(state.swagger, propertyPath);
state.resolvedPointers[pointer] = resolved;
callback(null, resolved);
}
/**
* Resolves a pointer to an external file or URL.
* @param {string} pathOrUrl the full, absolute path or URL
* @param {State} state the state for the current parse operation
* @param {function} callback
*/
function resolveExternalPointer(pathOrUrl, state, callback) {
// Set the resolved value to an empty object for now, so other reference pointers
// can point to this object. Once we finish reading the file, we will update
// the empty object with the real data.
state.resolvedPointers[pathOrUrl] = {};
read.fileOrUrl(pathOrUrl, state,
function(err, data) {
if (!err) {
// Now that we've finished downloaded the data, update the empty object we created earlier
data = _.extend(state.resolvedPointers[pathOrUrl], data);
}
return callback(err, data);
}
);
}
/**
* Crawls the property tree to return the value of the specified property.

@@ -180,0 +210,0 @@ */

'use strict';
var path = require('path');
var url = require('url');
var tv4 = require('tv4');

@@ -22,4 +23,4 @@ var swaggerSchema = require('swagger-schema-official/schema');

*
* @param {string} swaggerFile
* the path of a YAML or JSON file.
* @param {string} swaggerPath
* the file path or URL of a YAML or JSON Swagger spec.
*

@@ -32,3 +33,3 @@ * @param {defaults} options

*/
function parse(swaggerFile, options, callback) {
function parse(swaggerPath, options, callback) {
// Shift args if necessary

@@ -46,8 +47,11 @@ if (_.isFunction(options)) {

// Resolve the file path or url, relative to the CWD
swaggerPath = url.resolve(process.cwd(), swaggerPath);
// Create a new state object for this parse operation
var state = new State();
state.baseDir = path.dirname(swaggerFile);
state.baseDir = path.dirname(swaggerPath) + '/';
state.options = options;
read.fileOrUrl(swaggerFile, state, function(err, swaggerObject) {
read.fileOrUrl(swaggerPath, state, function(err, swaggerObject) {
if (err) {

@@ -64,3 +68,3 @@ return util.doCallback(callback, err);

'Error in "%s". \nUnsupported Swagger version: %d. Swagger-Server only supports version %s',
swaggerFile, version, supportedSwaggerVersions.join(', ')));
swaggerPath, version, supportedSwaggerVersions.join(', ')));
}

@@ -81,3 +85,3 @@

if (err) {
err = util.syntaxError('Error in "%s". \n%s', swaggerFile, err.message);
err = util.syntaxError('Error in "%s". \n%s', swaggerPath, err.message);
return util.doCallback(callback, err);

@@ -84,0 +88,0 @@ }

@@ -17,3 +17,3 @@ 'use strict';

* Reads a JSON or YAML file from the local filesystem or a remote URL and returns the parsed POJO.
* @param {string} pathOrUrl Local file path or URL, relative to the Swagger file.
* @param {string} pathOrUrl A full, absolute file path or URL
* @param {State} state The state for the current parse operation

@@ -24,5 +24,2 @@ * @param {function} callback function(err, parsedObject)

try {
// Resolve the path/URL relative to the Swagger file
pathOrUrl = url.resolve(state.baseDir + '/', pathOrUrl);
// Determine whether its a local file or a URL

@@ -29,0 +26,0 @@ var parsedUrl = url.parse(pathOrUrl);

{
"name": "swagger-parser",
"version": "1.0.9",
"version": "1.0.10",
"description": "Parses a JSON or YAML Swagger spec, validates it against the Swagger schema, and dereferences all $ref pointers",

@@ -5,0 +5,0 @@ "keywords": [

Sorry, the diff of this file is too big to display

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is too big to display

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc