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

Commit

Permalink
Resolves issues where we're filtering out non-endpoint documents (#279)
Browse files Browse the repository at this point in the history
* Moving non-HTTP code filtering down into create-docs.

* Updating package-lock

* Running Prettier on create-docs.

* Cleaning up how we're rendering common parameters.

* Removing some commented out code.

* Adding a unit test.
  • Loading branch information
erunion authored Aug 27, 2019
1 parent b8e52be commit 09b10c7
Show file tree
Hide file tree
Showing 4 changed files with 320 additions and 3 deletions.
67 changes: 67 additions & 0 deletions example/swagger-files/common-parameters.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
{
"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,
"schema": {
"type": "integer",
"minimum": 1,
"maximum": 50,
"default": 20
},
"description": "The numbers of items to return."
}
},
"schemas": {
"string_enum": {
"name": "string",
"enum": [
"available",
"pending",
"sold"
],
"type": "string"
}
}
}
}
229 changes: 229 additions & 0 deletions packages/api-explorer/__tests__/fixtures/parameters/common.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,229 @@
{
"openapi": "3.0.0",
"servers": [
{
"url": "http://petstore.swagger.io/v2"
}
],
"info": {
"description": "This is a sample server Petstore server. You can find out more about Swagger at [http://swagger.io](http://swagger.io) or on [irc.freenode.net, #swagger](http://swagger.io/irc/). For this sample, you can use the api key `special-key` to test the authorization filters.",
"version": "1.0.0",
"title": "Swagger Petstore",
"termsOfService": "http://swagger.io/terms/",
"contact": {
"email": "apiteam@swagger.io"
},
"license": {
"name": "Apache 2.0",
"url": "http://www.apache.org/licenses/LICENSE-2.0.html"
}
},
"externalDocs": {
"description": "Find out more about Swagger",
"url": "http://swagger.io"
},
"tags": [
{
"name": "pet",
"description": "Everything about your Pets",
"externalDocs": {
"description": "Find out more",
"url": "http://swagger.io"
}
}
],
"paths": {
"/pet/{petId}": {
"parameters": [
{
"name": "petId",
"in": "path",
"description": "ID of the pet",
"required": true,
"schema": {
"type": "integer",
"format": "int64"
}
}
],
"get": {
"tags": ["pet"],
"summary": "Find pet by ID",
"description": "Returns a single pet",
"operationId": "getPetById",
"responses": {
"200": {
"description": "successful operation",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Pet"
}
}
}
},
"400": {
"description": "Invalid ID supplied"
},
"404": {
"description": "Pet not found"
}
},
"security": [
{
"api_key": []
}
]
},
"post": {
"tags": ["pet"],
"summary": "Updates a pet in the store with form data",
"description": "",
"operationId": "updatePetWithForm",
"responses": {
"405": {
"description": "Invalid input"
}
},
"security": [
{
"petstore_auth": ["write:pets", "read:pets"]
}
],
"requestBody": {
"content": {
"application/x-www-form-urlencoded": {
"schema": {
"type": "object",
"properties": {
"name": {
"description": "Updated name of the pet",
"type": "string"
},
"status": {
"description": "Updated status of the pet",
"type": "string"
}
}
}
}
}
}
},
"delete": {
"tags": ["pet"],
"summary": "Deletes a pet",
"description": "",
"operationId": "deletePet",
"parameters": [
{
"name": "api_key",
"in": "header",
"required": false,
"schema": {
"type": "string"
}
}
],
"responses": {
"400": {
"description": "Invalid ID supplied"
},
"404": {
"description": "Pet not found"
}
},
"security": [
{
"petstore_auth": ["write:pets", "read:pets"]
}
]
}
}
},
"components": {
"schemas": {
"Category": {
"type": "object",
"properties": {
"id": {
"type": "integer",
"format": "int64"
},
"name": {
"type": "string"
}
}
},
"Tag": {
"type": "object",
"properties": {
"id": {
"type": "integer",
"format": "int64"
},
"name": {
"type": "string"
}
}
},
"Pet": {
"type": "object",
"required": ["name", "photoUrls"],
"properties": {
"id": {
"type": "integer",
"format": "int64",
"readOnly": true
},
"category": {
"$ref": "#/components/schemas/Category"
},
"name": {
"type": "string",
"example": "doggie"
},
"photoUrls": {
"type": "array",
"items": {
"type": "string"
}
},
"tags": {
"type": "array",
"items": {
"$ref": "#/components/schemas/Tag"
}
},
"status": {
"type": "string",
"description": "pet status in the store",
"enum": ["available", "pending", "sold"]
}
}
}
},
"securitySchemes": {
"petstore_auth": {
"type": "oauth2",
"flows": {
"implicit": {
"authorizationUrl": "http://petstore.swagger.io/oauth/dialog",
"scopes": {
"write:pets": "modify pets in your account",
"read:pets": "read your pets"
}
}
}
},
"api_key": {
"type": "apiKey",
"name": "api_key",
"in": "header"
}
}
},
"x-explorer-enabled": true,
"x-samples-enabled": true,
"x-samples-languages": ["curl", "node", "ruby", "javascript", "python"]
}
15 changes: 15 additions & 0 deletions packages/api-explorer/__tests__/index.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const WrappedApiExplorer = require('../src');
const { ApiExplorer } = WrappedApiExplorer;

const oas = require('./fixtures/petstore/oas');
const oasCommon = require('./fixtures/parameters/common');

const createDocs = require('../lib/create-docs');

Expand All @@ -33,6 +34,20 @@ test('ApiExplorer renders a doc for each', () => {
expect(explorer.find('Doc').length).toBe(docs.length);
});

test('ApiExplorer should not render a common parameter OAS operation method', () => {
const docsCommon = createDocs(oasCommon, 'api-setting');
const propsCommon = Object.assign({}, props, {
docs: docsCommon,
oasFiles: {
'api-setting': oasCommon,
},
});

const explorer = shallow(<ApiExplorer {...propsCommon} />);

expect(explorer.find('Doc').length).toBe(docsCommon.length - 1);
});

test('Should display an error message if it fails to render (wrapped in ErrorBoundary)', () => {
// Prompting an error with an array of nulls instead of Docs
// This is to simulate some unknown error state during initial render
Expand Down
12 changes: 9 additions & 3 deletions packages/api-explorer/src/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ const SelectedAppContext = require('@readme/variable/contexts/SelectedApp');
const ErrorBoundary = require('./ErrorBoundary');
const Doc = require('./Doc');

const methods = ['get', 'post', 'put', 'patch', 'delete', 'head', 'options', 'trace'];

const getAuth = require('./lib/get-auth');

class ApiExplorer extends React.Component {
Expand Down Expand Up @@ -119,7 +117,15 @@ class ApiExplorer extends React.Component {
}

render() {
const docs = this.props.docs.filter(doc => methods.includes(((doc || {}).api || {}).method));
const docs = this.props.docs.filter(doc => {
// If the HTTP method is `parameters`, then it represents common parameters and we shouldn't
// attempt to render it as a normal API operation.
if (typeof doc.api !== 'undefined' && doc.api.method === 'parameters') {
return false;
}

return true;
});

return (
<div className={`is-lang-${this.state.language}`}>
Expand Down

0 comments on commit 09b10c7

Please sign in to comment.