Skip to content
This repository has been archived by the owner on Nov 28, 2022. It is now read-only.

Commit

Permalink
fix: oas-to-har now supports common parameters (#462)
Browse files Browse the repository at this point in the history
  • Loading branch information
erunion authored Jan 29, 2020
1 parent 3e177ed commit dafd181
Show file tree
Hide file tree
Showing 4 changed files with 147 additions and 58 deletions.
112 changes: 54 additions & 58 deletions example/swagger-files/common-parameters.json
Original file line number Diff line number Diff line change
@@ -1,67 +1,63 @@
{
"openapi": "3.0.0",
"servers": [
{
"url": "http://httpbin.org"
}
],
"info": {
"version": "1.0.0",
"title": "An example of how we render $ref usage on resource parameters"
},
"paths": {
"/anything/{id}": {
"parameters": [
{
"in": "path",
"name": "id",
"schema": {
"type": "number"
}
"openapi": "3.0.0",
"servers": [
{
"url": "http://httpbin.org"
}
],
"info": {
"version": "1.0.0",
"title": "An example of how we render $ref usage on resource parameters"
},
"paths": {
"/anything/{id}": {
"parameters": [
{
"in": "path",
"name": "id",
"schema": {
"type": "number"
}
],
"get": {
"summary": "Get anything",
"description": "",
"responses": {}
},
"post": {
"summary": "Post anything",
"description": "",
"parameters": [
{
"$ref": "#/components/parameters/limitParam"
}
],
"responses": {}
}
}
},
"components": {
"parameters": {
"limitParam": {
"in": "query",
"name": "limit",
"required": false,
{
"in": "header",
"name": "x-extra-id",
"schema": {
"type": "integer",
"minimum": 1,
"maximum": 50,
"default": 20
},
"description": "The numbers of items to return."
"type": "string"
}
}
],
"get": {
"summary": "Get anything",
"description": "",
"responses": {}
},
"schemas": {
"string_enum": {
"name": "string",
"enum": [
"available",
"pending",
"sold"
],
"type": "string"
}
"post": {
"summary": "Post anything",
"description": "",
"parameters": [
{
"$ref": "#/components/parameters/limitParam"
}
],
"responses": {}
}
}
},
"components": {
"parameters": {
"limitParam": {
"in": "query",
"name": "limit",
"required": false,
"schema": {
"type": "integer",
"minimum": 1,
"maximum": 50,
"default": 20
},
"description": "The numbers of items to return."
}
}
}
}
54 changes: 54 additions & 0 deletions packages/oas-to-har/__tests__/fixtures/common-parameters.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
{
"openapi": "3.0.0",
"servers": [
{
"url": "http://httpbin.org"
}
],
"paths": {
"/anything/{id}": {
"parameters": [
{
"in": "path",
"name": "id",
"schema": {
"type": "number"
}
},
{
"in": "header",
"name": "x-extra-id",
"schema": {
"type": "string"
}
}
],
"post": {
"summary": "Post anything",
"description": "",
"parameters": [
{
"$ref": "#/components/parameters/limitParam"
}
],
"responses": {}
}
}
},
"components": {
"parameters": {
"limitParam": {
"in": "query",
"name": "limit",
"required": false,
"schema": {
"type": "integer",
"minimum": 1,
"maximum": 50,
"default": 20
},
"description": "The numbers of items to return."
}
}
}
}
27 changes: 27 additions & 0 deletions packages/oas-to-har/__tests__/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const extensions = require('@readme/oas-extensions');
const Oas = require('oas');

const oasToHar = require('../src/index');
const commonParameters = require('./fixtures/common-parameters');

const oas = new Oas();

Expand Down Expand Up @@ -1111,6 +1112,32 @@ describe('formData values', () => {
});
});

describe('common parameters', () => {
it('should work for common parameters', () => {
expect(
oasToHar(
new Oas(commonParameters),
{
...commonParameters.paths['/anything/{id}'].post,
path: '/anything/{id}',
method: 'post',
},
{
path: { id: 1234 },
header: { 'x-extra-id': 'abcd' },
query: { limit: 10 },
},
).log.entries[0].request,
).toStrictEqual({
headers: [{ name: 'x-extra-id', value: 'abcd' }],
queryString: [{ name: 'limit', value: '10' }],
postData: {},
method: 'POST',
url: 'http://httpbin.org/anything/1234',
});
});
});

describe('auth', () => {
it('should work for header', () => {
expect(
Expand Down
12 changes: 12 additions & 0 deletions packages/oas-to-har/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,18 @@ module.exports = (
har.url = `https://try.readme.io/${har.url}`;
}

if (!pathOperation.parameters) {
// eslint-disable-next-line no-param-reassign
pathOperation.parameters = [];
}

// Does this operation have any common parameters?
if (oas.paths && oas.paths[pathOperation.path] && oas.paths[pathOperation.path].parameters) {
oas.paths[pathOperation.path].parameters.forEach(param => {
pathOperation.parameters.push(param);
});
}

if (pathOperation.parameters) {
pathOperation.parameters.forEach((param, i, params) => {
if (param.$ref) {
Expand Down

0 comments on commit dafd181

Please sign in to comment.